diff --git a/include/common/pathTools.h b/include/common/pathTools.h index 5d3c065e7..c63fad275 100644 --- a/include/common/pathTools.h +++ b/include/common/pathTools.h @@ -54,6 +54,7 @@ string getFileSizeAsString(const string& path); string getFileContent(const string& path); bool fileExists(const string& path); bool makeDirectory(const string& path); +string makeTmpDirectory(); bool copyFile(const string& sourcePath, const string& destPath); string getLastPathElement(const string& path); string getExecutablePath(); diff --git a/src/common/pathTools.cpp b/src/common/pathTools.cpp index cfdc64d79..876263c04 100644 --- a/src/common/pathTools.cpp +++ b/src/common/pathTools.cpp @@ -228,6 +228,30 @@ bool makeDirectory(const string& path) 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(&dir_name)); + string dir(cbase, base_len); + dir += dir_name; + _mkdir(dir.c_str()); + RpcStringFree(reinterpret_cast(&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 */ bool copyFile(const string& sourcePath, const string& destPath) {