Support creating parent directories as needed (mkdir -p) and also set a

better default permission for new dirs
This commit is contained in:
Bruce Hill 2024-09-23 14:53:44 -04:00
parent b432fc82c7
commit 89c427172a
3 changed files with 25 additions and 5 deletions

View File

@ -180,11 +180,12 @@ A list of paths for the children.
### `create_directory`
**Description:**
Creates a new directory at the specified path with the given permissions.
Creates a new directory at the specified path with the given permissions. If
any of the parent directories do not exist, they will be created as needed.
**Usage:**
```markdown
create_directory(path: Path, permissions=0o644[32]) -> Void
create_directory(path: Path, permissions=0o755[32]) -> Void
```
**Parameters:**

View File

@ -267,7 +267,7 @@ env_t *new_compilation_unit(CORD *libname)
{"base_name", "Path$base_name", "func(path:Path)->Text"},
{"by_line", "Path$by_line", "func(path:Path)->(func()->Text?)?"},
{"children", "Path$children", "func(path:Path, include_hidden=no)->[Path]"},
{"create_directory", "Path$create_directory", "func(path:Path, permissions=0o644[32])"},
{"create_directory", "Path$create_directory", "func(path:Path, permissions=0o755[32])"},
{"escape_int", "Int$value_as_text", "func(i:Int)->Path"},
{"escape_path", "Path$escape_path", "func(path:Path)->Path"},
{"escape_text", "Path$escape_text", "func(text:Text)->Path"},

View File

@ -322,8 +322,27 @@ public void Path$remove(Path_t path, bool ignore_missing)
public void Path$create_directory(Path_t path, int permissions)
{
path = Path$_expand_home(path);
if (mkdir(Text$as_c_string(path), (mode_t)permissions) != 0)
fail("Could not create directory: %k (%s)", &path, strerror(errno));
char *c_path = Text$as_c_string(path);
char *end = c_path + strlen(c_path);
if (*end == '/' && end > c_path) {
*end = '\0';
--end;
}
char *end_of_component = strchrnul(c_path + 1, '/');
for (;;) {
if (end_of_component < end)
*end_of_component = '\0';
int status = mkdir(c_path, (mode_t)permissions);
if (status != 0 && errno != EEXIST)
fail("Could not create directory: %s (%s)", c_path, strerror(errno));
if (end_of_component >= end)
break;
*end_of_component = '/';
end_of_component = strchrnul(end_of_component + 1, '/');
}
}
static Array_t _filtered_children(Path_t path, bool include_hidden, mode_t filter)