diff options
| author | Lexi Winter <lexi@le-fay.org> | 2025-06-23 19:38:13 +0100 |
|---|---|---|
| committer | Lexi Winter <lexi@le-fay.org> | 2025-06-23 19:38:13 +0100 |
| commit | 504c02086832dcaab5cfa042ee7a488470284424 (patch) | |
| tree | 176545128360aa854b94aa27644e46fa248baea6 | |
| parent | 936c7d1293ba2ed01719c1656dd30842c6db5582 (diff) | |
| download | nihil-504c02086832dcaab5cfa042ee7a488470284424.tar.gz nihil-504c02086832dcaab5cfa042ee7a488470284424.tar.bz2 | |
nihil: add ensure_dir
| -rw-r--r-- | nihil/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | nihil/ensure_dir.cc | 27 | ||||
| -rw-r--r-- | nihil/ensure_dir.ccm | 20 | ||||
| -rw-r--r-- | nihil/nihil.ccm | 1 |
4 files changed, 50 insertions, 0 deletions
diff --git a/nihil/CMakeLists.txt b/nihil/CMakeLists.txt index 6dcec2c..ae2f49d 100644 --- a/nihil/CMakeLists.txt +++ b/nihil/CMakeLists.txt @@ -7,6 +7,7 @@ target_sources(nihil argv.ccm command_map.ccm ctype.ccm + ensure_dir.ccm exec.ccm fd.ccm find_in_path.ccm @@ -27,6 +28,7 @@ target_sources(nihil write_file.ccm PRIVATE + ensure_dir.cc exec.cc fd.cc find_in_path.cc diff --git a/nihil/ensure_dir.cc b/nihil/ensure_dir.cc new file mode 100644 index 0000000..019d268 --- /dev/null +++ b/nihil/ensure_dir.cc @@ -0,0 +1,27 @@ +/* + * This source code is released into the public domain. + */ + +module; + +#include <filesystem> +#include <format> +#include <system_error> + +module nihil; + +namespace nihil { + +auto ensure_dir(std::filesystem::path const &dir) -> void +{ + std::error_code err; + + if (std::filesystem::create_directories(dir, err)) + return; + + if (err) + throw generic_error(std::format("{}: mkdir: {}", + dir.string(), err.message())); +} + +} // namespace nihil diff --git a/nihil/ensure_dir.ccm b/nihil/ensure_dir.ccm new file mode 100644 index 0000000..6ea1b32 --- /dev/null +++ b/nihil/ensure_dir.ccm @@ -0,0 +1,20 @@ +/* + * This source code is released into the public domain. + */ + +module; + +#include <filesystem> + +export module nihil:ensure_dir; + +namespace nihil { + +/* + * Create the given directory and any parent directories; throws + * generic_error on failure. + */ +export auto ensure_dir(std::filesystem::path const &dir) -> void; + +} // namespace nihil + diff --git a/nihil/nihil.ccm b/nihil/nihil.ccm index 718bc08..85050e1 100644 --- a/nihil/nihil.ccm +++ b/nihil/nihil.ccm @@ -9,6 +9,7 @@ export module nihil; export import :argv; export import :command_map; export import :ctype; +export import :ensure_dir; export import :exec; export import :fd; export import :find_in_path; |
