mkdir: Add -v option to print a message for each created directory

This commit is contained in:
Tim Ledbetter authored and Jelle Raaijmakers committed 2023-06-22 22:35:30 +02:00
1 parent 925ea4e854
commit c1c35f4f59
2 files changed
+20 -10

No files matched your search

+1
View File
@@ -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
+19 -10
View File
@@ -19,12 +19,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(Core::System::pledge("stdio cpath rpath"));
bool create_parents = false;
bool verbose = false;
StringView mode_string;
Vector<StringView> 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<int> 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<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;
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);