Skip to content

Commit

Permalink
Implement error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bugdea1er committed Jan 28, 2024
1 parent 125c7a6 commit 5d0973b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
10 changes: 9 additions & 1 deletion 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, mkdtemp) {}
explicit directory(std::string_view prefix = "") : path(prefix, callback) {}

/// Concatenates this directory path with a given @p source
std::filesystem::path operator/(std::string_view source) const {
Expand All @@ -58,6 +58,14 @@ class directory final : public path {
directory& operator=(directory&&) noexcept = default; ///< move-assignable
directory(const directory&) = delete; ///< not copy-constructible
auto operator=(const directory&) = delete; ///< not copy-assignable

protected:
static void callback(char* path) {
if (mkdtemp(path) == nullptr) {
auto ec = std::error_code(errno, std::system_category());
throw error("Cannot create temporary directory", ec);
}
}
};

} // namespace tmp
10 changes: 9 additions & 1 deletion include/tmp/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,22 @@ class file final : public path {
file(const file&) = delete; ///< not copy-constructible
auto operator=(const file&) = delete; ///< not copy-assignable

protected:
static void callback(char* path) {
if (mkstemp(path) == -1) {
auto ec = std::error_code(errno, std::system_category());
throw error("Cannot create temporary file", ec);
}
}

private:
bool binary; ///< This file write mode

/// Creates a unique temporary file using the system's default location
/// 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, mkstemp),
explicit file(std::string_view prefix, bool binary) : path(prefix, callback),
binary(binary) {}

/// Returns a stream for this file
Expand Down
9 changes: 5 additions & 4 deletions include/tmp/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,18 @@ class path {
}

protected:
/// Exception type that should be used by subclasses to signal errors
using error = std::filesystem::filesystem_error;

std::filesystem::path underlying; ///< This file path

/// Creates a unique temporary path using the given constructor function
template<typename C>
explicit path(std::string_view prefix, C constructor) {
explicit path(std::string_view prefix, void(*callback)(char*)) {
const auto parent = std::filesystem::temp_directory_path() / prefix;
std::filesystem::create_directories(parent);

std::string arg = parent / "XXXXXX";
constructor(arg.data());

callback(arg.data());
this->underlying = std::move(arg);
}

Expand Down

0 comments on commit 5d0973b

Please sign in to comment.