aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-11-27 12:05:49 -0500
committerBruce Hill <bruce@bruce-hill.com>2025-11-27 12:07:34 -0500
commit437be558a893ac70c030794df99a866e8ed01879 (patch)
tree63126858e06f14db9e23306d7d9d4c7e25f3f421 /src/stdlib
parent35053e65b946264715aca2b348ee25313b55d2f6 (diff)
Add `recursive` arg to Path.create_directory()
Diffstat (limited to 'src/stdlib')
-rw-r--r--src/stdlib/paths.c12
-rw-r--r--src/stdlib/paths.h2
2 files changed, 11 insertions, 3 deletions
diff --git a/src/stdlib/paths.c b/src/stdlib/paths.c
index 22effad7..6c99cf0b 100644
--- a/src/stdlib/paths.c
+++ b/src/stdlib/paths.c
@@ -468,11 +468,19 @@ void Path$remove(Path_t path, bool ignore_missing) {
}
public
-void Path$create_directory(Path_t path, int permissions) {
+void Path$create_directory(Path_t path, int permissions, bool recursive) {
+retry:
path = Path$expand_home(path);
const char *c_path = Path$as_c_string(path);
int status = mkdir(c_path, (mode_t)permissions);
- if (status != 0 && errno != EEXIST) fail("Could not create directory: ", c_path, " (", strerror(errno), ")");
+ if (status != 0) {
+ if (recursive && errno == ENOENT) {
+ Path$create_directory(Path$parent(path), permissions, recursive);
+ goto retry;
+ } else if (errno != EEXIST) {
+ fail("Could not create directory: ", c_path, " (", strerror(errno), ")");
+ }
+ }
}
static List_t _filtered_children(Path_t path, bool include_hidden, mode_t filter) {
diff --git a/src/stdlib/paths.h b/src/stdlib/paths.h
index 3b1f3ce6..677631b2 100644
--- a/src/stdlib/paths.h
+++ b/src/stdlib/paths.h
@@ -41,7 +41,7 @@ void Path$set_owner(Path_t path, OptionalText_t owner, OptionalText_t group, boo
OptionalText_t Path$owner(Path_t path, bool follow_symlinks);
OptionalText_t Path$group(Path_t path, bool follow_symlinks);
void Path$remove(Path_t path, bool ignore_missing);
-void Path$create_directory(Path_t path, int permissions);
+void Path$create_directory(Path_t path, int permissions, bool recursive);
List_t Path$children(Path_t path, bool include_hidden);
List_t Path$files(Path_t path, bool include_hidden);
List_t Path$subdirectories(Path_t path, bool include_hidden);