mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 17:44:58 +00:00
mkdir: Use FilePermissionsMask to handle mode option
This commit is contained in:
parent
005b0f7384
commit
b17fef5133
2 changed files with 17 additions and 13 deletions
|
@ -15,13 +15,15 @@ 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.
|
||||
* `-m`, `--mode`: Sets the permissions for the final directory (possibly altered by the process umask). The mode argument can be given in any of the formats
|
||||
accepted by the chmod(1) command. Addition and removal of permissions is relative to a default permission of 0777.
|
||||
|
||||
## Examples
|
||||
|
||||
```sh
|
||||
$ mkdir -p /tmp/foo/bar
|
||||
$ mkdir -m 0700 /tmp/owner-only
|
||||
$ mkdir -m a=rx /tmp/foo/bar
|
||||
```
|
||||
|
||||
## See also
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
|
||||
* Copyright (c) 2021, Xavier Defrang <xavier.defrang@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/StringUtils.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/FilePermissionsMask.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibMain/Main.h>
|
||||
#include <errno.h>
|
||||
|
@ -25,19 +26,19 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
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_option(mode_string, "Set new directory permissions", "mode", 'm', "mode");
|
||||
args_parser.add_positional_argument(directories, "Directories to create", "directories");
|
||||
args_parser.parse(arguments);
|
||||
|
||||
mode_t default_mode = 0755;
|
||||
mode_t mode = default_mode;
|
||||
mode_t const default_mode = 0755;
|
||||
mode_t const mask_reference_mode = 0777;
|
||||
|
||||
if (!mode_string.is_empty()) {
|
||||
mode = AK::StringUtils::convert_to_uint_from_octal<u16>(mode_string).value_or(01000);
|
||||
if (mode > 0777) {
|
||||
warnln("mkdir: invalid mode: {}", mode_string);
|
||||
return 1;
|
||||
}
|
||||
Core::FilePermissionsMask mask;
|
||||
|
||||
if (mode_string.is_empty()) {
|
||||
mask.assign_permissions(default_mode);
|
||||
} else {
|
||||
mask = TRY(Core::FilePermissionsMask::parse(mode_string));
|
||||
}
|
||||
|
||||
bool has_errors = false;
|
||||
|
@ -45,7 +46,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
for (auto& directory : directories) {
|
||||
LexicalPath lexical_path(directory);
|
||||
if (!create_parents) {
|
||||
if (mkdir(lexical_path.string().characters(), mode) < 0) {
|
||||
if (mkdir(lexical_path.string().characters(), mask.apply(mask_reference_mode)) < 0) {
|
||||
perror("mkdir");
|
||||
has_errors = true;
|
||||
}
|
||||
|
@ -73,8 +74,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
}
|
||||
|
||||
bool is_final = (idx == (num_parts - 1));
|
||||
mode_t mode = is_final ? mask.apply(mask_reference_mode) : default_mode;
|
||||
|
||||
if (mkdir(path.characters(), is_final ? mode : default_mode) < 0) {
|
||||
if (mkdir(path.characters(), mode) < 0) {
|
||||
perror("mkdir");
|
||||
has_errors = true;
|
||||
break;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue