Skip to content

Commit 3a8c371

Browse files
authoredFeb 7, 2021
Use consistent temp folder path (#10892)
1 parent 4caf156 commit 3a8c371

File tree

6 files changed

+24
-33
lines changed

6 files changed

+24
-33
lines changed
 

Diff for: ‎builtin/mainmenu/common.lua

+8-28
Original file line numberDiff line numberDiff line change
@@ -146,35 +146,15 @@ end
146146

147147
--------------------------------------------------------------------------------
148148
os.tempfolder = function()
149-
if core.settings:get("TMPFolder") then
150-
return core.settings:get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
151-
end
152-
153-
local filetocheck = os.tmpname()
154-
os.remove(filetocheck)
155-
156-
-- luacheck: ignore
157-
-- https://blogs.msdn.microsoft.com/vcblog/2014/06/18/c-runtime-crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1/
158-
-- The C runtime (CRT) function called by os.tmpname is tmpnam.
159-
-- Microsofts tmpnam implementation in older CRT / MSVC releases is defective.
160-
-- tmpnam return values starting with a backslash characterize this behavior.
161-
-- https://sourceforge.net/p/mingw-w64/bugs/555/
162-
-- MinGW tmpnam implementation is forwarded to the CRT directly.
163-
-- https://sourceforge.net/p/mingw-w64/discussion/723797/thread/55520785/
164-
-- MinGW links to an older CRT release (msvcrt.dll).
165-
-- Due to legal concerns MinGW will never use a newer CRT.
166-
--
167-
-- Make use of TEMP to compose the temporary filename if an old
168-
-- style tmpnam return value is detected.
169-
if filetocheck:sub(1, 1) == "\\" then
170-
local tempfolder = os.getenv("TEMP")
171-
return tempfolder .. filetocheck
172-
end
149+
local temp = core.get_temp_path()
150+
return temp .. DIR_DELIM .. "MT_" .. math.random(0, 10000)
151+
end
173152

174-
local randname = "MTTempModFolder_" .. math.random(0,10000)
175-
local backstring = filetocheck:reverse()
176-
return filetocheck:sub(0, filetocheck:len() - backstring:find(DIR_DELIM) + 1) ..
177-
randname
153+
--------------------------------------------------------------------------------
154+
os.tmpname = function()
155+
local path = os.tempfolder()
156+
io.open(path, "w"):close()
157+
return path
178158
end
179159

180160
--------------------------------------------------------------------------------

Diff for: ‎doc/menu_lua_api.txt

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ core.get_video_drivers()
8585
core.get_mapgen_names([include_hidden=false]) -> table of map generator algorithms
8686
registered in the core (possible in async calls)
8787
core.get_cache_path() -> path of cache
88+
core.get_temp_path() -> path of temp folder
8889

8990

9091
HTTP Requests

Diff for: ‎src/defaultsettings.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,6 @@ void set_default_settings()
461461
settings->setDefault("screen_h", "0");
462462
settings->setDefault("fullscreen", "true");
463463
settings->setDefault("touchtarget", "true");
464-
settings->setDefault("TMPFolder", porting::path_cache);
465464
settings->setDefault("touchscreen_threshold","20");
466465
settings->setDefault("fixed_virtual_joystick", "false");
467466
settings->setDefault("virtual_joystick_triggers_aux", "false");

Diff for: ‎src/filesys.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2727
#include "log.h"
2828
#include "config.h"
2929
#include "porting.h"
30-
#ifdef __ANDROID__
31-
#include "settings.h" // For g_settings
32-
#endif
3330

3431
namespace fs
3532
{
@@ -359,8 +356,9 @@ std::string TempPath()
359356
compatible with lua's os.tmpname which under the default
360357
configuration hardcodes mkstemp("/tmp/lua_XXXXXX").
361358
*/
359+
362360
#ifdef __ANDROID__
363-
return g_settings->get("TMPFolder");
361+
return porting::path_cache;
364362
#else
365363
return DIR_DELIM "tmp";
366364
#endif

Diff for: ‎src/script/lua_api/l_mainmenu.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ int ModApiMainMenu::l_get_texturepath(lua_State *L)
529529
return 1;
530530
}
531531

532+
/******************************************************************************/
532533
int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
533534
{
534535
std::string gamepath = fs::RemoveRelativePathComponents(
@@ -537,12 +538,20 @@ int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
537538
return 1;
538539
}
539540

541+
/******************************************************************************/
540542
int ModApiMainMenu::l_get_cache_path(lua_State *L)
541543
{
542544
lua_pushstring(L, fs::RemoveRelativePathComponents(porting::path_cache).c_str());
543545
return 1;
544546
}
545547

548+
/******************************************************************************/
549+
int ModApiMainMenu::l_get_temp_path(lua_State *L)
550+
{
551+
lua_pushstring(L, fs::TempPath().c_str());
552+
return 1;
553+
}
554+
546555
/******************************************************************************/
547556
int ModApiMainMenu::l_create_dir(lua_State *L) {
548557
const char *path = luaL_checkstring(L, 1);
@@ -942,6 +951,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
942951
API_FCT(get_texturepath);
943952
API_FCT(get_texturepath_share);
944953
API_FCT(get_cache_path);
954+
API_FCT(get_temp_path);
945955
API_FCT(create_dir);
946956
API_FCT(delete_dir);
947957
API_FCT(copy_dir);
@@ -975,6 +985,7 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
975985
API_FCT(get_texturepath);
976986
API_FCT(get_texturepath_share);
977987
API_FCT(get_cache_path);
988+
API_FCT(get_temp_path);
978989
API_FCT(create_dir);
979990
API_FCT(delete_dir);
980991
API_FCT(copy_dir);

Diff for: ‎src/script/lua_api/l_mainmenu.h

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class ModApiMainMenu: public ModApiBase
122122

123123
static int l_get_cache_path(lua_State *L);
124124

125+
static int l_get_temp_path(lua_State *L);
126+
125127
static int l_create_dir(lua_State *L);
126128

127129
static int l_delete_dir(lua_State *L);

0 commit comments

Comments
 (0)