From 91f648dbcf9e6e5638a16dc8aee85e8236ad53d4 Mon Sep 17 00:00:00 2001 From: Maksim Denisov Date: Mon, 17 Feb 2025 14:18:38 +0100 Subject: TmpDir: Create nested directories ... and keep parent directories alive while nested directories exist. --- src/utils/cpp/tmp_dir.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 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 c46586ef..88cc0f98 100644 --- a/src/utils/cpp/tmp_dir.cpp +++ b/src/utils/cpp/tmp_dir.cpp @@ -30,10 +30,19 @@ #include "src/buildtool/logging/logger.hpp" auto TmpDir::Create(std::filesystem::path const& prefix) noexcept -> Ptr { - return CreateImpl(prefix); + return CreateImpl(/*parent=*/nullptr, prefix); } -auto TmpDir::CreateImpl(std::filesystem::path const& path) noexcept -> Ptr { +auto TmpDir::CreateNestedDirectory(TmpDir::Ptr const& parent) noexcept + -> TmpDir::Ptr { + if (parent == nullptr) { + return nullptr; + } + return CreateImpl(parent, parent->GetPath()); +} + +auto TmpDir::CreateImpl(TmpDir::Ptr parent, + 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)) { @@ -51,7 +60,8 @@ auto TmpDir::CreateImpl(std::filesystem::path const& path) noexcept -> Ptr { if (mkdtemp(file_path.data()) == nullptr) { return nullptr; } - return std::shared_ptr(new TmpDir(file_path)); + return std::shared_ptr( + new TmpDir(std::move(parent), file_path)); } catch (...) { if (not file_path.empty()) { rmdir(file_path.c_str()); -- cgit v1.2.3