From cb0d5cb6a1a231093fb40b4d50e93df00fe9c8d6 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 2 Sep 2023 14:46:14 +0200 Subject: [PATCH] rm: Add the -v flag --- apps/rm.cpp | 30 ++++++++++++++++++++++++++++-- libos/include/os/FileSystem.h | 9 --------- libos/src/FileSystem.cpp | 15 --------------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/apps/rm.cpp b/apps/rm.cpp index c87a02ed..49f2aba3 100644 --- a/apps/rm.cpp +++ b/apps/rm.cpp @@ -1,10 +1,35 @@ #include +#include +#include #include +Result remove_wrapper(const os::Path& path, bool verbose) +{ + TRY(os::FileSystem::remove(path)); + if (verbose) os::println("removed '%s'", path.name().chars()); + return {}; +} + +Result remove_tree(const os::Path& path, bool verbose) +{ + auto rc = remove_wrapper(path, verbose); + if (!rc.has_error()) return {}; + if (rc.error() != ENOTEMPTY) return rc.release_error(); + + auto dir = TRY(os::Directory::open(path)); + + Vector entries = TRY(dir->list_names(os::Directory::Filter::ParentAndBase)); + + for (const auto& entry : entries) { TRY(remove_tree({ dir->fd(), entry.view() }, verbose)); } + + return remove_wrapper(path, verbose); +} + Result luna_main(int argc, char** argv) { StringView path; bool recursive; + bool verbose; os::ArgumentParser parser; parser.add_description("Remove a path from the file system."_sv); @@ -12,11 +37,12 @@ Result luna_main(int argc, char** argv) parser.add_positional_argument(path, "path"_sv, true); parser.add_switch_argument(recursive, 'r', "recursive"_sv, "remove a directory recursively (by default, rm removes only empty directories)"_sv); + parser.add_switch_argument(verbose, 'v', "verbose"_sv, "log every removed file and directory"_sv); parser.parse(argc, argv); - if (!recursive) TRY(os::FileSystem::remove(path)); + if (!recursive) TRY(remove_wrapper(path, verbose)); else - TRY(os::FileSystem::remove_tree(path)); + TRY(remove_tree(path, verbose)); return 0; } diff --git a/libos/include/os/FileSystem.h b/libos/include/os/FileSystem.h index 64c71965..57871389 100644 --- a/libos/include/os/FileSystem.h +++ b/libos/include/os/FileSystem.h @@ -68,15 +68,6 @@ namespace os */ Result remove(const Path& path); - /** - * @brief Remove a directory tree from the file system recursively, deleting subfiles and subdirectories as - * well. - * - * @param path The path to remove. - * @return Result Whether the operation succeeded. - */ - Result remove_tree(const Path& path); - /** * @brief Read the target of a symbolic link. * diff --git a/libos/src/FileSystem.cpp b/libos/src/FileSystem.cpp index 33964db8..2754fe34 100644 --- a/libos/src/FileSystem.cpp +++ b/libos/src/FileSystem.cpp @@ -63,21 +63,6 @@ namespace os::FileSystem return {}; } - Result remove_tree(const Path& path) - { - auto rc = remove(path); - if (!rc.has_error()) return {}; - if (rc.error() != ENOTEMPTY) return rc.release_error(); - - auto dir = TRY(os::Directory::open(path)); - - Vector entries = TRY(dir->list_names(os::Directory::Filter::ParentAndBase)); - - for (const auto& entry : entries) { TRY(remove_tree({ dir->fd(), entry.view() })); } - - return remove(path); - } - Result readlink(const Path& path) { struct stat st;