Skip to content

Commit

Permalink
Create file or directory before calling tmp::path constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
bugdea1er committed Jan 28, 2024
1 parent ab67ab1 commit 56e39cb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
11 changes: 7 additions & 4 deletions include/tmp/directory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class directory final : public path {
/// for temporary files. If a prefix is provided to the constructor, the
/// directory is created in the path <temp dir>/prefix/. The prefix can be
/// a path consisting of multiple segments.
explicit directory(std::string_view prefix = "") : path(prefix, creator) {}
explicit directory(std::string_view prefix = "") : path(create(prefix)) {}

/// Concatenates this directory path with a given @p source
std::filesystem::path operator/(std::string_view source) const {
Expand All @@ -60,9 +60,12 @@ class directory final : public path {
auto operator=(const directory&) = delete; ///< not copy-assignable

private:
/// Creates a unique temporary directory based on the given @p pattern path.
/// The parent path of the given argument must exist.
static std::string creator(std::string pattern) {
/// Creates a unique temporary directory based on the given @p prefix
static std::filesystem::path create(std::string_view prefix) {
const auto parent = std::filesystem::temp_directory_path() / prefix;
std::filesystem::create_directories(parent);

auto pattern = std::string(parent / "XXXXXX");
if (mkdtemp(pattern.data()) == nullptr) {
auto ec = std::error_code(errno, std::system_category());
throw error("Cannot create temporary directory", ec);
Expand Down
15 changes: 9 additions & 6 deletions include/tmp/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,21 @@ class file final : public path {
/// for temporary files. If a prefix is provided to the constructor, the
/// directory is created in the path <temp dir>/prefix/. The prefix can be
/// a path consisting of multiple segments.
explicit file(std::string_view prefix, bool binary) : path(prefix, creator),
explicit file(std::string_view prefix, bool binary) : path(create(prefix)),
binary(binary) {}

/// Creates a unique temporary file based on the given @p pattern path.
/// The parent path of the given argument must exist.
static std::string creator(std::string path) {
if (mkstemp(path.data()) == -1) {
/// Creates a unique temporary file based on the given @p prefix
static std::filesystem::path create(std::string_view prefix) {
const auto parent = std::filesystem::temp_directory_path() / prefix;
std::filesystem::create_directories(parent);

auto pattern = std::string(parent / "XXXXXX");
if (mkstemp(pattern.data()) == -1) {
auto ec = std::error_code(errno, std::system_category());
throw error("Cannot create temporary file", ec);
}

return path;
return pattern;
}

/// Returns a stream for this file
Expand Down
7 changes: 1 addition & 6 deletions include/tmp/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ class path {
/// Creates a unique temporary path using the given constructor function.
/// @param prefix the path between system temp
/// @param creator wrapped mktemp-like function that returns resulting path
explicit path(std::string_view prefix, std::string(*creator)(std::string)) {
const auto parent = std::filesystem::temp_directory_path() / prefix;
std::filesystem::create_directories(parent);

this->underlying = creator(parent / "XXXXXX");
}
explicit path(std::filesystem::path path) : underlying(path){}

private:
/// Deletes this path recursively, ignoring any errors
Expand Down

0 comments on commit 56e39cb

Please sign in to comment.