Skip to content

Commit

Permalink
Move parent creation to the separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
bugdea1er committed Jan 28, 2024
1 parent 56e39cb commit 444c277
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
5 changes: 1 addition & 4 deletions include/tmp/directory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ class directory final : public path {
private:
/// 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");
auto pattern = std::string(make_parent(prefix) / "XXXXXX");
if (mkdtemp(pattern.data()) == nullptr) {
auto ec = std::error_code(errno, std::system_category());
throw error("Cannot create temporary directory", ec);
Expand Down
5 changes: 1 addition & 4 deletions include/tmp/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ class file final : public path {

/// 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");
auto pattern = std::string(make_parent(prefix) / "XXXXXX");
if (mkstemp(pattern.data()) == -1) {
auto ec = std::error_code(errno, std::system_category());
throw error("Cannot create temporary file", ec);
Expand Down
9 changes: 8 additions & 1 deletion include/tmp/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ 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::filesystem::path path) : underlying(path){}
explicit path(std::filesystem::path path) : underlying(path) {}

static std::filesystem::path make_parent(std::string_view prefix) {
const auto parent = std::filesystem::temp_directory_path() / prefix;
std::filesystem::create_directories(parent);

return parent;
}

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

0 comments on commit 444c277

Please sign in to comment.