diff --git a/Userland/rm.cpp b/Userland/rm.cpp index 5b822095fb..2ef96f063e 100644 --- a/Userland/rm.cpp +++ b/Userland/rm.cpp @@ -35,35 +35,37 @@ #include #include -int remove(bool recursive, String path) +int remove(bool recursive, bool force, String path) { struct stat path_stat; if (lstat(path.characters(), &path_stat) < 0) { - perror("lstat"); + if (!force) + perror("lstat"); return 1; } if (S_ISDIR(path_stat.st_mode) && recursive) { auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir); if (di.has_error()) { - fprintf(stderr, "DirIterator: %s\n", di.error_string()); + if (!force) + fprintf(stderr, "DirIterator: %s\n", di.error_string()); return 1; } while (di.has_next()) { - int s = remove(true, di.next_full_path()); - if (s != 0) + int s = remove(true, force, di.next_full_path()); + if (s != 0 && !force) return s; } int s = rmdir(path.characters()); - if (s < 0) { + if (s < 0 && !force) { perror("rmdir"); return 1; } } else { int rc = unlink(path.characters()); - if (rc < 0) { + if (rc < 0 && !force) { perror("unlink"); return 1; } @@ -79,16 +81,18 @@ int main(int argc, char** argv) } bool recursive = false; + bool force = false; Vector 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_positional_argument(paths, "Path(s) to remove", "path"); args_parser.parse(argc, argv); int rc = 0; for (auto& path : paths) { - rc |= remove(recursive, path); + rc |= remove(recursive, force, path); } return rc; }