summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaksim Denisov <denisov.maksim@huawei.com>2025-02-17 14:18:38 +0100
committerMaksim Denisov <denisov.maksim@huawei.com>2025-03-24 09:25:05 +0100
commit91f648dbcf9e6e5638a16dc8aee85e8236ad53d4 (patch)
treef444cd247186832d4cfcf1f662096422d5e52254 /src
parent3cd82862c1b44409dddcbe65a14bd483574e7460 (diff)
downloadjustbuild-91f648dbcf9e6e5638a16dc8aee85e8236ad53d4.tar.gz
TmpDir: Create nested directories
... and keep parent directories alive while nested directories exist.
Diffstat (limited to 'src')
-rw-r--r--src/utils/cpp/tmp_dir.cpp16
-rw-r--r--src/utils/cpp/tmp_dir.hpp11
2 files changed, 22 insertions, 5 deletions
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<TmpDir const>(new TmpDir(file_path));
+ return std::shared_ptr<TmpDir const>(
+ new TmpDir(std::move(parent), file_path));
} catch (...) {
if (not file_path.empty()) {
rmdir(file_path.c_str());
diff --git a/src/utils/cpp/tmp_dir.hpp b/src/utils/cpp/tmp_dir.hpp
index e2e1e7a7..28805e3d 100644
--- a/src/utils/cpp/tmp_dir.hpp
+++ b/src/utils/cpp/tmp_dir.hpp
@@ -38,13 +38,20 @@ class TmpDir final {
[[nodiscard]] static auto Create(
std::filesystem::path const& prefix) noexcept -> Ptr;
+ /// \brief Create a new nested temporary directory. This nested directory
+ /// remains valid even if the parent directory goes out of scope.
+ [[nodiscard]] static auto CreateNestedDirectory(
+ TmpDir::Ptr const& parent) noexcept -> Ptr;
+
private:
- explicit TmpDir(std::filesystem::path path) noexcept
- : tmp_dir_{std::move(path)} {}
+ explicit TmpDir(TmpDir::Ptr parent, std::filesystem::path path) noexcept
+ : parent_{std::move(parent)}, tmp_dir_{std::move(path)} {}
[[nodiscard]] static auto CreateImpl(
+ TmpDir::Ptr parent,
std::filesystem::path const& path) noexcept -> Ptr;
+ TmpDir::Ptr parent_;
std::filesystem::path tmp_dir_;
};