mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:04:59 +00:00
mkdir: Add -v
option to print a message for each created directory
This commit is contained in:
parent
925ea4e854
commit
c1c35f4f59
2 changed files with 20 additions and 10 deletions
|
@ -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
|
* `-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
|
* `-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.
|
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
|
## Examples
|
||||||
|
|
||||||
|
|
|
@ -19,12 +19,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
TRY(Core::System::pledge("stdio cpath rpath"));
|
TRY(Core::System::pledge("stdio cpath rpath"));
|
||||||
|
|
||||||
bool create_parents = false;
|
bool create_parents = false;
|
||||||
|
bool verbose = false;
|
||||||
StringView mode_string;
|
StringView mode_string;
|
||||||
Vector<StringView> directories;
|
Vector<StringView> directories;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.add_option(create_parents, "Create parent directories if they don't exist", "parents", 'p');
|
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(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.add_positional_argument(directories, "Directories to create", "directories");
|
||||||
args_parser.parse(arguments);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
|
@ -41,14 +43,24 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
|
||||||
bool has_errors = false;
|
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) {
|
for (auto& directory : directories) {
|
||||||
LexicalPath lexical_path(directory);
|
LexicalPath lexical_path(directory);
|
||||||
if (!create_parents) {
|
if (!create_parents) {
|
||||||
auto maybe_error = Core::System::mkdir(lexical_path.string(), mask.apply(mask_reference_mode));
|
create_directory(lexical_path.string(), mask.apply(mask_reference_mode));
|
||||||
if (maybe_error.is_error()) {
|
|
||||||
warnln("mkdir: {}", strerror(maybe_error.error().code()));
|
|
||||||
has_errors = true;
|
|
||||||
}
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
StringBuilder path_builder;
|
StringBuilder path_builder;
|
||||||
|
@ -75,12 +87,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
bool is_final = (idx == (num_parts - 1));
|
bool is_final = (idx == (num_parts - 1));
|
||||||
mode_t mode = is_final ? mask.apply(mask_reference_mode) : default_mode;
|
mode_t mode = is_final ? mask.apply(mask_reference_mode) : default_mode;
|
||||||
|
|
||||||
auto maybe_error = Core::System::mkdir(path, mode);
|
if (!create_directory(path, mode))
|
||||||
if (maybe_error.is_error()) {
|
|
||||||
warnln("mkdir: {}", strerror(maybe_error.error().code()));
|
|
||||||
has_errors = true;
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (!S_ISDIR(stat_or_error.value().st_mode)) {
|
if (!S_ISDIR(stat_or_error.value().st_mode)) {
|
||||||
warnln("mkdir: cannot create directory '{}': not a directory", path);
|
warnln("mkdir: cannot create directory '{}': not a directory", path);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue