mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
mkdir: Implement --mode option
Only supports permissions given in octal format at the moment.
This commit is contained in:
parent
1255316d26
commit
6ca34f5647
Notes:
sideshowbarker
2024-07-17 22:26:56 +09:00
Author: https://github.com/xavier Commit: https://github.com/SerenityOS/serenity/commit/6ca34f56474 Pull-request: https://github.com/SerenityOS/serenity/pull/11319 Reviewed-by: https://github.com/bgianfo
2 changed files with 26 additions and 5 deletions
|
@ -15,11 +15,13 @@ Create a new empty directory for each of the given *directories*.
|
|||
## Options
|
||||
|
||||
* `-p`, `--parents`: Create parent directories if they don't exist
|
||||
* `-m`, `--mode`: Sets the permissions for the final directory (possibly altered by the process umask). The mode argument must be given in octal format.
|
||||
|
||||
## Examples
|
||||
|
||||
```sh
|
||||
$ mkdir -p /tmp/foo/bar
|
||||
$ mkdir -m 0700 /tmp/owner-only
|
||||
```
|
||||
|
||||
## See also
|
||||
|
|
|
@ -16,18 +16,27 @@
|
|||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
TRY(Core::System::pledge("stdio cpath rpath", nullptr));
|
||||
TRY(Core::System::pledge("stdio cpath rpath"));
|
||||
|
||||
bool create_parents = false;
|
||||
String mode_string;
|
||||
Vector<const char*> directories;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(create_parents, "Create parent directories if they don't exist", "parents", 'p');
|
||||
args_parser.add_option(mode_string, "Set new directory permissions", "mode", 'm', "octal-mode");
|
||||
args_parser.add_positional_argument(directories, "Directories to create", "directories");
|
||||
args_parser.parse(arguments);
|
||||
|
||||
// FIXME: Support -m/--mode option
|
||||
mode_t mode = 0755;
|
||||
mode_t default_mode = 0755;
|
||||
mode_t mode = default_mode;
|
||||
|
||||
if (!mode_string.is_empty()) {
|
||||
if (sscanf(mode_string.characters(), "%ho", &mode) != 1) {
|
||||
warnln("mkdir: invalid mode: {}", mode_string);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
bool has_errors = false;
|
||||
|
||||
|
@ -43,9 +52,16 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
StringBuilder path_builder;
|
||||
if (lexical_path.is_absolute())
|
||||
path_builder.append("/");
|
||||
for (auto& part : lexical_path.parts_view()) {
|
||||
|
||||
auto& parts = lexical_path.parts_view();
|
||||
size_t num_parts = parts.size();
|
||||
|
||||
for (size_t idx = 0; idx < num_parts; ++idx) {
|
||||
auto& part = parts[idx];
|
||||
|
||||
path_builder.append(part);
|
||||
auto path = path_builder.build();
|
||||
|
||||
struct stat st;
|
||||
if (stat(path.characters(), &st) < 0) {
|
||||
if (errno != ENOENT) {
|
||||
|
@ -53,7 +69,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
has_errors = true;
|
||||
break;
|
||||
}
|
||||
if (mkdir(path.characters(), mode) < 0) {
|
||||
|
||||
bool is_final = (idx == (num_parts - 1));
|
||||
|
||||
if (mkdir(path.characters(), is_final ? mode : default_mode) < 0) {
|
||||
perror("mkdir");
|
||||
has_errors = true;
|
||||
break;
|
||||
|
|
Loading…
Reference in a new issue