Add a function to create a temporary directory.

This commit is contained in:
Matthieu Gautier 2018-03-27 16:37:17 +02:00
parent 9f86b59d1d
commit d4fefd1a57
2 changed files with 25 additions and 0 deletions

View File

@ -54,6 +54,7 @@ string getFileSizeAsString(const string& path);
string getFileContent(const string& path); string getFileContent(const string& path);
bool fileExists(const string& path); bool fileExists(const string& path);
bool makeDirectory(const string& path); bool makeDirectory(const string& path);
string makeTmpDirectory();
bool copyFile(const string& sourcePath, const string& destPath); bool copyFile(const string& sourcePath, const string& destPath);
string getLastPathElement(const string& path); string getLastPathElement(const string& path);
string getExecutablePath(); string getExecutablePath();

View File

@ -228,6 +228,30 @@ bool makeDirectory(const string& path)
return status == 0; return status == 0;
} }
string makeTmpDirectory()
{
#ifdef _WIN32
char cbase[MAX_PATH+1];
int base_len = GetTempPath(MAX_PATH+1, cbase);
UUID uuid;
UuidCreate(&uuid);
char* dir_name;
UuidToString(&uuid, reinterpret_cast<unsigned char**>(&dir_name));
string dir(cbase, base_len);
dir += dir_name;
_mkdir(dir.c_str());
RpcStringFree(reinterpret_cast<unsigned char**>(&dir_name));
#else
string base = "/tmp";
auto _template = base + "/kiwix-lib_XXXXXX";
char* _template_array = new char[_template.size()+1];
memcpy(_template_array, _template.c_str(), _template.size());
string dir = mkdtemp(_template_array);
delete[] _template_array;
#endif
return dir;
}
/* Try to create a link and if does not work then make a copy */ /* Try to create a link and if does not work then make a copy */
bool copyFile(const string& sourcePath, const string& destPath) bool copyFile(const string& sourcePath, const string& destPath)
{ {