From 6c009825cb320cc00531494985aa5fe7e0989971 Mon Sep 17 00:00:00 2001 From: Maksim Denisov Date: Fri, 14 Feb 2025 16:15:05 +0100 Subject: TmpDir: minor refactoring --- src/utils/cpp/tmp_dir.cpp | 68 +++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 35 deletions(-) (limited to 'src/utils/cpp/tmp_dir.cpp') diff --git a/src/utils/cpp/tmp_dir.cpp b/src/utils/cpp/tmp_dir.cpp index 7382ce5c..c46586ef 100644 --- a/src/utils/cpp/tmp_dir.cpp +++ b/src/utils/cpp/tmp_dir.cpp @@ -14,55 +14,53 @@ #include "src/utils/cpp/tmp_dir.hpp" +#ifdef __unix__ +#include +#else +#error "Non-unix is not supported yet" +#endif + #include -#include +#include +#include +#include #include "src/buildtool/file_system/file_system_manager.hpp" #include "src/buildtool/logging/log_level.hpp" #include "src/buildtool/logging/logger.hpp" -auto TmpDir::Create(std::filesystem::path const& prefix, - std::string const& dir_template) noexcept -> TmpDirPtr { - try { - // make sure prefix folder exists - if (not FileSystemManager::CreateDirectory(prefix)) { - Logger::Log(LogLevel::Error, - "TmpDir: could not create prefix directory {}", - prefix.string()); - return nullptr; - } - - auto full_template_str = - std::filesystem::weakly_canonical( - std::filesystem::absolute(prefix / dir_template)) - .string(); +auto TmpDir::Create(std::filesystem::path const& prefix) noexcept -> Ptr { + return CreateImpl(prefix); +} - std::vector c_tmpl(full_template_str.begin(), - full_template_str.end()); - c_tmpl.emplace_back('\0'); // get as c-string +auto TmpDir::CreateImpl(std::filesystem::path const& path) noexcept -> Ptr { + static constexpr std::string_view kDirTemplate = "tmp.XXXXXX"; + // make sure prefix folder exists + if (not FileSystemManager::CreateDirectory(path)) { + Logger::Log(LogLevel::Error, + "TmpDir: could not create prefix directory {}", + path.string()); + return nullptr; + } - // attempt to make the tmp dir - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - char* tmp_dir = mkdtemp(c_tmpl.data()); - if (tmp_dir == nullptr) { + std::string file_path; + try { + file_path = std::filesystem::weakly_canonical( + std::filesystem::absolute(path / kDirTemplate)); + // Create a temporary directory: + if (mkdtemp(file_path.data()) == nullptr) { return nullptr; } - auto tmp_dir_obj = std::make_shared(); - // need to explicitly transform to std::string first - tmp_dir_obj->tmp_dir_ = std::filesystem::path(std::string(tmp_dir)); - - return std::static_pointer_cast(tmp_dir_obj); + return std::shared_ptr(new TmpDir(file_path)); } catch (...) { - return nullptr; + if (not file_path.empty()) { + rmdir(file_path.c_str()); + } } -} - -auto TmpDir::GetPath() const& noexcept -> std::filesystem::path const& { - return tmp_dir_; + return nullptr; } TmpDir::~TmpDir() noexcept { // try to remove the tmp dir and all its content - [[maybe_unused]] auto res = - FileSystemManager::RemoveDirectory(tmp_dir_, true); + std::ignore = FileSystemManager::RemoveDirectory(tmp_dir_, true); } -- cgit v1.2.3