2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-06-03 02:04:18 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <AK/Vector.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-02-21 03:55:06 +03:00
|
|
|
#include <LibCore/File.h>
|
2019-01-22 09:03:44 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-06-03 02:04:18 +03:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2020-02-18 15:23:32 +03:00
|
|
|
if (pledge("stdio rpath cpath", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-01-27 20:25:36 +03:00
|
|
|
bool recursive = false;
|
2020-08-11 14:15:15 +03:00
|
|
|
bool force = false;
|
2020-11-17 04:39:30 +03:00
|
|
|
bool verbose = false;
|
2022-01-02 17:10:02 +03:00
|
|
|
bool no_preserve_root = false;
|
|
|
|
Vector<StringView> paths;
|
2019-06-03 02:04:18 +03:00
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
Core::ArgsParser args_parser;
|
2020-01-27 20:25:36 +03:00
|
|
|
args_parser.add_option(recursive, "Delete directories recursively", "recursive", 'r');
|
2020-08-11 14:15:15 +03:00
|
|
|
args_parser.add_option(force, "Force", "force", 'f');
|
2020-11-17 04:39:30 +03:00
|
|
|
args_parser.add_option(verbose, "Verbose", "verbose", 'v');
|
2022-01-02 17:10:02 +03:00
|
|
|
args_parser.add_option(no_preserve_root, "Do not consider '/' specially", "no-preserve-root", 0);
|
2021-06-05 04:08:02 +03:00
|
|
|
args_parser.add_positional_argument(paths, "Path(s) to remove", "path", Core::ArgsParser::Required::No);
|
2020-01-27 20:25:36 +03:00
|
|
|
args_parser.parse(argc, argv);
|
2019-06-03 02:04:18 +03:00
|
|
|
|
2021-06-05 04:08:02 +03:00
|
|
|
if (!force && paths.is_empty()) {
|
|
|
|
args_parser.print_usage(stderr, argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-02-21 03:55:06 +03:00
|
|
|
bool had_errors = false;
|
2020-03-01 14:08:44 +03:00
|
|
|
for (auto& path : paths) {
|
2022-01-02 17:10:02 +03:00
|
|
|
if (!no_preserve_root && path == "/") {
|
|
|
|
warnln("rm: '/' is protected, try with --no-preserve-root to override this behavior");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-02-21 03:55:06 +03:00
|
|
|
auto result = Core::File::remove(path, recursive ? Core::File::RecursionMode::Allowed : Core::File::RecursionMode::Disallowed, force);
|
|
|
|
|
|
|
|
if (result.is_error()) {
|
2021-11-07 03:31:00 +03:00
|
|
|
warnln("rm: cannot remove '{}': {}", path, static_cast<Error const&>(result.error()));
|
2021-02-21 03:55:06 +03:00
|
|
|
had_errors = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
outln("removed '{}'", path);
|
2020-03-01 14:08:44 +03:00
|
|
|
}
|
2021-02-21 03:55:06 +03:00
|
|
|
return had_errors ? 1 : 0;
|
2019-06-03 02:04:18 +03:00
|
|
|
}
|