summaryrefslogtreecommitdiff
path: root/Userland/Utilities/rm.cpp
diff options
context:
space:
mode:
authorLucas CHOLLET <lucas.chollet@free.fr>2022-01-02 15:10:02 +0100
committerAndreas Kling <kling@serenityos.org>2022-01-02 17:37:35 +0100
commit4d557a4b81f2d05e757b0b4c316d992fb205529e (patch)
tree33bd6c5d60bfe391dae41fd2f6e19962adc31fb4 /Userland/Utilities/rm.cpp
parent416033a66086478a3e71d0f6318797ac432c73c8 (diff)
downloadserenity-4d557a4b81f2d05e757b0b4c316d992fb205529e.zip
rm: Check before removing '/'
A simple check to not erase '/' by mistake.
Diffstat (limited to 'Userland/Utilities/rm.cpp')
-rw-r--r--Userland/Utilities/rm.cpp9
1 files changed, 8 insertions, 1 deletions
diff --git a/Userland/Utilities/rm.cpp b/Userland/Utilities/rm.cpp
index a6e08b87f6..8b6cb0a8e1 100644
--- a/Userland/Utilities/rm.cpp
+++ b/Userland/Utilities/rm.cpp
@@ -21,12 +21,14 @@ int main(int argc, char** argv)
bool recursive = false;
bool force = false;
bool verbose = false;
- Vector<const char*> paths;
+ bool no_preserve_root = false;
+ Vector<StringView> paths;
Core::ArgsParser args_parser;
args_parser.add_option(recursive, "Delete directories recursively", "recursive", 'r');
args_parser.add_option(force, "Force", "force", 'f');
args_parser.add_option(verbose, "Verbose", "verbose", 'v');
+ args_parser.add_option(no_preserve_root, "Do not consider '/' specially", "no-preserve-root", 0);
args_parser.add_positional_argument(paths, "Path(s) to remove", "path", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
@@ -37,6 +39,11 @@ int main(int argc, char** argv)
bool had_errors = false;
for (auto& path : paths) {
+ if (!no_preserve_root && path == "/") {
+ warnln("rm: '/' is protected, try with --no-preserve-root to override this behavior");
+ continue;
+ }
+
auto result = Core::File::remove(path, recursive ? Core::File::RecursionMode::Allowed : Core::File::RecursionMode::Disallowed, force);
if (result.is_error()) {