summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/cpp/TARGETS3
-rw-r--r--src/utils/cpp/tmp_dir.cpp68
-rw-r--r--src/utils/cpp/tmp_dir.hpp38
3 files changed, 52 insertions, 57 deletions
diff --git a/src/utils/cpp/TARGETS b/src/utils/cpp/TARGETS
index 563e7825..2b7fa935 100644
--- a/src/utils/cpp/TARGETS
+++ b/src/utils/cpp/TARGETS
@@ -62,7 +62,8 @@
, "srcs": ["tmp_dir.cpp"]
, "stage": ["src", "utils", "cpp"]
, "private-deps":
- [ ["src/buildtool/file_system", "file_system_manager"]
+ [ ["@", "gsl", "", "gsl"]
+ , ["src/buildtool/file_system", "file_system_manager"]
, ["src/buildtool/logging", "log_level"]
, ["src/buildtool/logging", "logging"]
]
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 <unistd.h>
+#else
+#error "Non-unix is not supported yet"
+#endif
+
#include <cstdlib>
-#include <vector>
+#include <string>
+#include <string_view>
+#include <tuple>
#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<char> 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<TmpDir>();
- // 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<TmpDir const>(tmp_dir_obj);
+ return std::shared_ptr<TmpDir const>(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);
}
diff --git a/src/utils/cpp/tmp_dir.hpp b/src/utils/cpp/tmp_dir.hpp
index 92d24f52..e2e1e7a7 100644
--- a/src/utils/cpp/tmp_dir.hpp
+++ b/src/utils/cpp/tmp_dir.hpp
@@ -17,38 +17,34 @@
#include <filesystem>
#include <memory>
-#include <string>
+#include <utility>
-std::string const kDefaultTemplate{"tmp.XXXXXX"};
-
-class TmpDir;
-using TmpDirPtr = std::shared_ptr<TmpDir const>;
-
-class TmpDir {
+class TmpDir final {
public:
- // default ctor; not to be used!
- TmpDir() = default;
-
- /// \brief Destroy the TmpDir object. It tries to remove the tmp folder.
- ~TmpDir() noexcept;
+ using Ptr = std::shared_ptr<TmpDir const>;
- // no copies, no moves
TmpDir(TmpDir const&) = delete;
- TmpDir(TmpDir&& other) noexcept = delete;
- auto operator=(TmpDir const&) = delete;
- auto operator=(TmpDir&& other) noexcept -> TmpDir& = delete;
+ auto operator=(TmpDir const&) -> TmpDir& = delete;
+ TmpDir(TmpDir&& other) = delete;
+ auto operator=(TmpDir&&) -> TmpDir& = delete;
+ ~TmpDir() noexcept;
[[nodiscard]] auto GetPath() const& noexcept
- -> std::filesystem::path const&;
- [[nodiscard]] auto GetPath() && = delete;
+ -> std::filesystem::path const& {
+ return tmp_dir_;
+ }
/// \brief Creates a completely unique directory in a given prefix path.
[[nodiscard]] static auto Create(
- std::filesystem::path const& prefix,
- std::string const& dir_template = kDefaultTemplate) noexcept
- -> TmpDirPtr;
+ std::filesystem::path const& prefix) noexcept -> Ptr;
private:
+ explicit TmpDir(std::filesystem::path path) noexcept
+ : tmp_dir_{std::move(path)} {}
+
+ [[nodiscard]] static auto CreateImpl(
+ std::filesystem::path const& path) noexcept -> Ptr;
+
std::filesystem::path tmp_dir_;
};