mirror of
https://github.com/RGBCube/serenity
synced 2025-05-29 14:15:08 +00:00
rmdir: Add -p
option to remove all directories in each given path
This commit is contained in:
parent
063efe9cf8
commit
307dc00ee0
2 changed files with 36 additions and 7 deletions
|
@ -12,6 +12,10 @@ $ rmdir `[directory...]`
|
||||||
|
|
||||||
Remove given `directory(ies)`, if they are empty
|
Remove given `directory(ies)`, if they are empty
|
||||||
|
|
||||||
|
## Options
|
||||||
|
|
||||||
|
* `-p`, `--parents`: Remove all directories in each given path
|
||||||
|
|
||||||
## Arguments
|
## Arguments
|
||||||
|
|
||||||
* `directory`: directory(ies) to remove
|
* `directory`: directory(ies) to remove
|
||||||
|
@ -31,4 +35,7 @@ $ ls -a example
|
||||||
$ rmdir example
|
$ rmdir example
|
||||||
$ ls -a example
|
$ ls -a example
|
||||||
example: No such file or directory
|
example: No such file or directory
|
||||||
|
|
||||||
|
# Removes foo/bar/baz/, foo/bar/ and foo/
|
||||||
|
$ rmdir -p foo/bar/baz/
|
||||||
```
|
```
|
||||||
|
|
|
@ -1,33 +1,55 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/LexicalPath.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
TRY(Core::System::pledge("stdio cpath"));
|
TRY(Core::System::pledge("stdio cpath"));
|
||||||
|
|
||||||
Vector<DeprecatedString> paths;
|
bool remove_parents = false;
|
||||||
|
Vector<StringView> paths;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
|
args_parser.add_option(remove_parents, "Remove all directories in each given path", "parents", 'p');
|
||||||
args_parser.add_positional_argument(paths, "Directories to remove", "paths");
|
args_parser.add_positional_argument(paths, "Directories to remove", "paths");
|
||||||
args_parser.parse(arguments);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
int status = 0;
|
int status = 0;
|
||||||
for (auto path : paths) {
|
|
||||||
int rc = rmdir(path.characters());
|
auto remove_directory = [&](StringView path) {
|
||||||
if (rc < 0) {
|
auto maybe_error = Core::System::rmdir(path);
|
||||||
perror("rmdir");
|
if (maybe_error.is_error()) {
|
||||||
|
warnln("Failed to remove '{}': {}", path, maybe_error.release_error());
|
||||||
status = 1;
|
status = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return !maybe_error.is_error();
|
||||||
|
};
|
||||||
|
|
||||||
|
for (auto path : paths) {
|
||||||
|
if (!remove_directory(path) || !remove_parents)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
LexicalPath lexical_path(path);
|
||||||
|
auto const& path_parts = lexical_path.parts_view();
|
||||||
|
if (path_parts.size() < 2)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (size_t i = path_parts.size() - 1; i > 0; --i) {
|
||||||
|
auto current_path_parts = path_parts.span().slice(0, i);
|
||||||
|
LexicalPath current_path { DeprecatedString::join('/', current_path_parts) };
|
||||||
|
if (!remove_directory(current_path.string()))
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue