diff --git a/Base/usr/share/man/man1/mkdir.md b/Base/usr/share/man/man1/mkdir.md index 3cf28d7768a..fbb31e16fcb 100644 --- a/Base/usr/share/man/man1/mkdir.md +++ b/Base/usr/share/man/man1/mkdir.md @@ -17,6 +17,7 @@ Create a new empty directory for each of the given *directories*. * `-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 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. +* `-v`, `--verbose`: Print a message for each created directory ## Examples diff --git a/Userland/Utilities/mkdir.cpp b/Userland/Utilities/mkdir.cpp index 82dd016c92d..55caf5cf0a8 100644 --- a/Userland/Utilities/mkdir.cpp +++ b/Userland/Utilities/mkdir.cpp @@ -19,12 +19,14 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(Core::System::pledge("stdio cpath rpath")); bool create_parents = false; + bool verbose = false; StringView mode_string; Vector 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', "mode"); + args_parser.add_option(verbose, "Print a message for each created directory", "verbose", 'v'); args_parser.add_positional_argument(directories, "Directories to create", "directories"); args_parser.parse(arguments); @@ -41,14 +43,24 @@ ErrorOr serenity_main(Main::Arguments arguments) bool has_errors = false; + auto create_directory = [&](StringView path, mode_t mode) { + auto maybe_error = Core::System::mkdir(path, mode); + if (maybe_error.is_error()) { + warnln("mkdir: {}", strerror(maybe_error.error().code())); + has_errors = true; + return false; + } + + if (verbose) + outln("mkdir: Created directory '{}'", path); + + return true; + }; + for (auto& directory : directories) { LexicalPath lexical_path(directory); if (!create_parents) { - auto maybe_error = Core::System::mkdir(lexical_path.string(), mask.apply(mask_reference_mode)); - if (maybe_error.is_error()) { - warnln("mkdir: {}", strerror(maybe_error.error().code())); - has_errors = true; - } + create_directory(lexical_path.string(), mask.apply(mask_reference_mode)); continue; } StringBuilder path_builder; @@ -75,12 +87,9 @@ ErrorOr serenity_main(Main::Arguments arguments) bool is_final = (idx == (num_parts - 1)); mode_t mode = is_final ? mask.apply(mask_reference_mode) : default_mode; - auto maybe_error = Core::System::mkdir(path, mode); - if (maybe_error.is_error()) { - warnln("mkdir: {}", strerror(maybe_error.error().code())); - has_errors = true; + if (!create_directory(path, mode)) break; - } + } else { if (!S_ISDIR(stat_or_error.value().st_mode)) { warnln("mkdir: cannot create directory '{}': not a directory", path);