Skip to content

Commit

Permalink
Better handling of temporary folders
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Apr 6, 2024
1 parent f87994e commit 7e4462e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
5 changes: 1 addition & 4 deletions builtin/mainmenu/content/dlg_contentstore.lua
Expand Up @@ -98,15 +98,12 @@ local function download_and_extract(param)

local tempfolder = core.get_temp_path()
if tempfolder ~= "" then
tempfolder = tempfolder .. DIR_DELIM .. "MT_" .. math.random(1, 1024000)
if not core.extract_zip(filename, tempfolder) then
tempfolder = nil
end
else
tempfolder = nil
end
os.remove(filename)
if not tempfolder then
if not tempfolder or tempfolder == "" then
return {
msg = fgettext_ne("Failed to extract \"$1\" (unsupported file type or broken archive)", package.title),
}
Expand Down
4 changes: 2 additions & 2 deletions doc/menu_lua_api.md
Expand Up @@ -93,8 +93,8 @@ Filesystem
registered in the core (possible in async calls)
* `core.get_cache_path()` -> path of cache
* `core.get_temp_path([param])` (possible in async calls)
* `param`=true: returns path to a temporary file
otherwise: returns path to the temporary folder
* `param`=true: returns path to a newly created temporary file
* otherwise: returns path to a newly created temporary folder


HTTP Requests
Expand Down
19 changes: 19 additions & 0 deletions src/filesys.cpp
Expand Up @@ -223,6 +223,16 @@ std::string CreateTempFile()
return path;
}

std::string CreateTempDir()
{
std::string path = TempPath() + DIR_DELIM "MT_XXXXXX";
_mktemp_s(&path[0], path.size() + 1); // modifies path
// will error if it already exists
if (!CreateDirectory(path.c_str(), nullptr))
return "";
return path;
}

bool CopyFileContents(const std::string &source, const std::string &target)
{
BOOL ok = CopyFileEx(source.c_str(), target.c_str(), nullptr, nullptr,
Expand Down Expand Up @@ -446,6 +456,15 @@ std::string CreateTempFile()
return path;
}

std::string CreateTempDir()
{
std::string path = TempPath() + DIR_DELIM "MT_XXXXXX";
auto r = mkdtemp(&path[0]); // modifies path
if (!r)
return "";
return path;
}

namespace {
struct FileDeleter {
void operator()(FILE *stream) {
Expand Down
12 changes: 9 additions & 3 deletions src/filesys.h
Expand Up @@ -75,13 +75,19 @@ bool RecursiveDelete(const std::string &path);

bool DeleteSingleFileOrEmptyDirectory(const std::string &path);

// Returns path to temp directory, can return "" on error
/// Returns path to temp directory.
/// You probably don't want to use this directly, see `CreateTempFile` or `CreateTempDir`.
/// @return path or "" on error
std::string TempPath();

// Returns path to securely-created temporary file (will already exist when this function returns)
// can return "" on error
/// Returns path to securely-created temporary file (will already exist when this function returns).
/// @return path or "" on error
std::string CreateTempFile();

/// Returns path to securely-created temporary directory (will already exist when this function returns).
/// @return path or "" on error
std::string CreateTempDir();

/* Returns a list of subdirectories, including the path itself, but excluding
hidden directories (whose names start with . or _)
*/
Expand Down
2 changes: 1 addition & 1 deletion src/script/lua_api/l_mainmenu.cpp
Expand Up @@ -729,7 +729,7 @@ int ModApiMainMenu::l_get_cache_path(lua_State *L)
int ModApiMainMenu::l_get_temp_path(lua_State *L)
{
if (lua_isnoneornil(L, 1) || !lua_toboolean(L, 1))
lua_pushstring(L, fs::TempPath().c_str());
lua_pushstring(L, fs::CreateTempDir().c_str());
else
lua_pushstring(L, fs::CreateTempFile().c_str());
return 1;
Expand Down
9 changes: 2 additions & 7 deletions src/unittest/test.cpp
Expand Up @@ -324,13 +324,8 @@ std::string TestBase::getTestTempDirectory()
if (!m_test_dir.empty())
return m_test_dir;

char buf[32];
porting::mt_snprintf(buf, sizeof(buf), "%08X", myrand());

m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
if (!fs::CreateDir(m_test_dir))
UASSERT(false);

m_test_dir = fs::CreateTempDir();
UASSERT(!m_test_dir.empty());
return m_test_dir;
}

Expand Down

0 comments on commit 7e4462e

Please sign in to comment.