diff options
Diffstat (limited to 'src/utils/cpp/tmp_dir.cpp')
-rw-r--r-- | src/utils/cpp/tmp_dir.cpp | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/utils/cpp/tmp_dir.cpp b/src/utils/cpp/tmp_dir.cpp index 88cc0f98..8d6e0724 100644 --- a/src/utils/cpp/tmp_dir.cpp +++ b/src/utils/cpp/tmp_dir.cpp @@ -21,7 +21,6 @@ #endif #include <cstdlib> -#include <string> #include <string_view> #include <tuple> @@ -41,6 +40,28 @@ auto TmpDir::CreateNestedDirectory(TmpDir::Ptr const& parent) noexcept return CreateImpl(parent, parent->GetPath()); } +auto TmpDir::CreateFile(TmpDir::Ptr const& parent, + std::string const& file_name) noexcept -> TmpFile::Ptr { + // Create a new tmp directory to guarantee uniqueness: + auto temp_dir = CreateNestedDirectory(parent); + if (temp_dir == nullptr) { + return nullptr; + } + + try { + auto file_path = std::filesystem::weakly_canonical( + std::filesystem::absolute(temp_dir->GetPath() / file_name)); + if (not FileSystemManager::CreateFile(file_path)) { + return nullptr; + } + + return std::shared_ptr<TmpFile const>{ + new TmpFile(std::move(temp_dir), std::move(file_path))}; + } catch (...) { + return nullptr; + } +} + auto TmpDir::CreateImpl(TmpDir::Ptr parent, std::filesystem::path const& path) noexcept -> Ptr { static constexpr std::string_view kDirTemplate = "tmp.XXXXXX"; |