aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--nihil/CMakeLists.txt2
-rw-r--r--nihil/ensure_dir.cc27
-rw-r--r--nihil/ensure_dir.ccm20
-rw-r--r--nihil/nihil.ccm1
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;