summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorXavier Defrang <xavier.defrang@gmail.com>2021-12-25 11:40:38 +0100
committerBrian Gianforcaro <b.gianfo@gmail.com>2022-01-01 17:33:43 +0000
commitb17fef513383b595ff37447aa43dc0b8449e751c (patch)
tree0691216711f58f78c3d680919f059dac1469938f /Userland
parent005b0f73849e3f9b4d0a0c069bf70caaea102e84 (diff)
downloadserenity-b17fef513383b595ff37447aa43dc0b8449e751c.zip
mkdir: Use FilePermissionsMask to handle mode option
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Utilities/mkdir.cpp26
1 files changed, 14 insertions, 12 deletions
diff --git a/Userland/Utilities/mkdir.cpp b/Userland/Utilities/mkdir.cpp
index 72d24b499a..156fd1b2ca 100644
--- a/Userland/Utilities/mkdir.cpp
+++ b/Userland/Utilities/mkdir.cpp
@@ -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;