Skip to content

Commit

Permalink
Use consistent temp folder path (#10892)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenwardy committed Feb 7, 2021
1 parent 4caf156 commit 3a8c371
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 33 deletions.
36 changes: 8 additions & 28 deletions builtin/mainmenu/common.lua
Expand Up @@ -146,35 +146,15 @@ end

--------------------------------------------------------------------------------
os.tempfolder = function()
if core.settings:get("TMPFolder") then
return core.settings:get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
end

local filetocheck = os.tmpname()
os.remove(filetocheck)

-- luacheck: ignore
-- https://blogs.msdn.microsoft.com/vcblog/2014/06/18/c-runtime-crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1/
-- The C runtime (CRT) function called by os.tmpname is tmpnam.
-- Microsofts tmpnam implementation in older CRT / MSVC releases is defective.
-- tmpnam return values starting with a backslash characterize this behavior.
-- https://sourceforge.net/p/mingw-w64/bugs/555/
-- MinGW tmpnam implementation is forwarded to the CRT directly.
-- https://sourceforge.net/p/mingw-w64/discussion/723797/thread/55520785/
-- MinGW links to an older CRT release (msvcrt.dll).
-- Due to legal concerns MinGW will never use a newer CRT.
--
-- Make use of TEMP to compose the temporary filename if an old
-- style tmpnam return value is detected.
if filetocheck:sub(1, 1) == "\\" then
local tempfolder = os.getenv("TEMP")
return tempfolder .. filetocheck
end
local temp = core.get_temp_path()
return temp .. DIR_DELIM .. "MT_" .. math.random(0, 10000)
end

local randname = "MTTempModFolder_" .. math.random(0,10000)
local backstring = filetocheck:reverse()
return filetocheck:sub(0, filetocheck:len() - backstring:find(DIR_DELIM) + 1) ..
randname
--------------------------------------------------------------------------------
os.tmpname = function()
local path = os.tempfolder()
io.open(path, "w"):close()
return path
end

--------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions doc/menu_lua_api.txt
Expand Up @@ -85,6 +85,7 @@ core.get_video_drivers()
core.get_mapgen_names([include_hidden=false]) -> table of map generator algorithms
registered in the core (possible in async calls)
core.get_cache_path() -> path of cache
core.get_temp_path() -> path of temp folder


HTTP Requests
Expand Down
1 change: 0 additions & 1 deletion src/defaultsettings.cpp
Expand Up @@ -461,7 +461,6 @@ void set_default_settings()
settings->setDefault("screen_h", "0");
settings->setDefault("fullscreen", "true");
settings->setDefault("touchtarget", "true");
settings->setDefault("TMPFolder", porting::path_cache);
settings->setDefault("touchscreen_threshold","20");
settings->setDefault("fixed_virtual_joystick", "false");
settings->setDefault("virtual_joystick_triggers_aux", "false");
Expand Down
6 changes: 2 additions & 4 deletions src/filesys.cpp
Expand Up @@ -27,9 +27,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "log.h"
#include "config.h"
#include "porting.h"
#ifdef __ANDROID__
#include "settings.h" // For g_settings
#endif

namespace fs
{
Expand Down Expand Up @@ -359,8 +356,9 @@ std::string TempPath()
compatible with lua's os.tmpname which under the default
configuration hardcodes mkstemp("/tmp/lua_XXXXXX").
*/

#ifdef __ANDROID__
return g_settings->get("TMPFolder");
return porting::path_cache;
#else
return DIR_DELIM "tmp";
#endif
Expand Down
11 changes: 11 additions & 0 deletions src/script/lua_api/l_mainmenu.cpp
Expand Up @@ -529,6 +529,7 @@ int ModApiMainMenu::l_get_texturepath(lua_State *L)
return 1;
}

/******************************************************************************/
int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
{
std::string gamepath = fs::RemoveRelativePathComponents(
Expand All @@ -537,12 +538,20 @@ int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
return 1;
}

/******************************************************************************/
int ModApiMainMenu::l_get_cache_path(lua_State *L)
{
lua_pushstring(L, fs::RemoveRelativePathComponents(porting::path_cache).c_str());
return 1;
}

/******************************************************************************/
int ModApiMainMenu::l_get_temp_path(lua_State *L)
{
lua_pushstring(L, fs::TempPath().c_str());
return 1;
}

/******************************************************************************/
int ModApiMainMenu::l_create_dir(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
Expand Down Expand Up @@ -942,6 +951,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
API_FCT(get_texturepath);
API_FCT(get_texturepath_share);
API_FCT(get_cache_path);
API_FCT(get_temp_path);
API_FCT(create_dir);
API_FCT(delete_dir);
API_FCT(copy_dir);
Expand Down Expand Up @@ -975,6 +985,7 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
API_FCT(get_texturepath);
API_FCT(get_texturepath_share);
API_FCT(get_cache_path);
API_FCT(get_temp_path);
API_FCT(create_dir);
API_FCT(delete_dir);
API_FCT(copy_dir);
Expand Down
2 changes: 2 additions & 0 deletions src/script/lua_api/l_mainmenu.h
Expand Up @@ -122,6 +122,8 @@ class ModApiMainMenu: public ModApiBase

static int l_get_cache_path(lua_State *L);

static int l_get_temp_path(lua_State *L);

static int l_create_dir(lua_State *L);

static int l_delete_dir(lua_State *L);
Expand Down

0 comments on commit 3a8c371

Please sign in to comment.