diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-09-19 20:56:13 +0200 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-09-19 20:56:13 +0200 |
commit | c8900e169a1ddceec07a449f1ae7c4322ff02036 (patch) | |
tree | 5156605fb473d25786426eb6876ba2e7d3b7507b /src/filesys.cpp | |
parent | 950d2c9b3e10cbace9236e820c8119d1abb9e01f (diff) | |
parent | e0529da5c84f224c380e6d5e063392cb01f85683 (diff) | |
download | dragonfireclient-c8900e169a1ddceec07a449f1ae7c4322ff02036.tar.xz |
Merge branch 'master' of https://github.com/minetest/minetest
Diffstat (limited to 'src/filesys.cpp')
-rw-r--r-- | src/filesys.cpp | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/src/filesys.cpp b/src/filesys.cpp index 99b030624..a07370c0e 100644 --- a/src/filesys.cpp +++ b/src/filesys.cpp @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/string.h" #include <iostream> #include <cstdio> +#include <cstdlib> #include <cstring> #include <cerrno> #include <fstream> @@ -36,6 +37,7 @@ namespace fs #define _WIN32_WINNT 0x0501 #include <windows.h> #include <shlwapi.h> +#include <io.h> std::vector<DirListNode> GetDirListing(const std::string &pathstring) { @@ -176,13 +178,27 @@ std::string TempPath() errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl; return ""; } - std::vector<char> buf(bufsize); + std::string buf; + buf.resize(bufsize); DWORD len = GetTempPath(bufsize, &buf[0]); if(len == 0 || len > bufsize){ errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl; return ""; } - return std::string(buf.begin(), buf.begin() + len); + buf.resize(len); + return buf; +} + +std::string CreateTempFile() +{ + std::string path = TempPath() + DIR_DELIM "MT_XXXXXX"; + _mktemp_s(&path[0], path.size() + 1); // modifies path + HANDLE file = CreateFile(path.c_str(), GENERIC_WRITE, 0, nullptr, + CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + if (file == INVALID_HANDLE_VALUE) + return ""; + CloseHandle(file); + return path; } #else // POSIX @@ -364,6 +380,16 @@ std::string TempPath() #endif } +std::string CreateTempFile() +{ + std::string path = TempPath() + DIR_DELIM "MT_XXXXXX"; + int fd = mkstemp(&path[0]); // modifies path + if (fd == -1) + return ""; + close(fd); + return path; +} + #endif void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir) |