From fbab2f2b4db4e2d812233a59f0827bd560746393 Mon Sep 17 00:00:00 2001 From: "Xu, Kunda" Date: Wed, 27 Nov 2024 10:44:08 +0800 Subject: [PATCH] CVS-154703, Implement createTempPath(std::string* local_path) for windows --- src/filesystem.hpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/filesystem.hpp b/src/filesystem.hpp index e77ccca926..874c1db89b 100644 --- a/src/filesystem.hpp +++ b/src/filesystem.hpp @@ -146,6 +146,36 @@ class FileSystem { *local_path = std::string(tmp_folder); + return StatusCode::OK; + } +#elif _WIN32 + static StatusCode createTempPath(std::string* local_path) { + char temp_path[MAX_PATH] = {0}; + if (GetTempPath(MAX_PATH, temp_path) == 0) { + SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to get temporary directory : {}", GetLastError()); + return StatusCode::FILESYSTEM_ERROR; + } + + std::string tmp_folder = temp_path; + tmp_folder += "fileXXXXXX"; + char tmp_folder_ctr[MAX_PATH]; + strncpy(tmp_folder_ctr, tmp_folder.c_str(), MAX_PATH); + for (int i = strlen(tmp_folder_ctr) - 6; i < strlen(tmp_folder_ctr); ++i) { + tmp_folder_ctr[i] = "A" + (rand_r() % 26); + } + + if (!CreateDirectoryA(tmp_folder_ctr, NULL)) { + DWORD error = GetLastError(); + SPDLOG_LOGGER_ERROR(modelmanager_logger, "Failed to create local temp folder: {} {}", tmp_folder_ctr, strerror(errno)); + return StatusCode::FILESYSTEM_ERROR; + } + + fs::permission(tmp_folder_ctr, + fs::perms::others_all | fs::perms::group_all, + fs::perm_options::remove); + + *local_path = std::string(tmp_folder_ctr); + return StatusCode::OK; } #endif