This commit is contained in:
parent
06aa7725a1
commit
cb0d5cb6a1
30
apps/rm.cpp
30
apps/rm.cpp
@ -1,10 +1,35 @@
|
||||
#include <os/ArgumentParser.h>
|
||||
#include <os/Directory.h>
|
||||
#include <os/File.h>
|
||||
#include <os/FileSystem.h>
|
||||
|
||||
Result<void> remove_wrapper(const os::Path& path, bool verbose)
|
||||
{
|
||||
TRY(os::FileSystem::remove(path));
|
||||
if (verbose) os::println("removed '%s'", path.name().chars());
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void> 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<String> 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<int> 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<int> 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;
|
||||
}
|
||||
|
@ -68,15 +68,6 @@ namespace os
|
||||
*/
|
||||
Result<void> 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<void> Whether the operation succeeded.
|
||||
*/
|
||||
Result<void> remove_tree(const Path& path);
|
||||
|
||||
/**
|
||||
* @brief Read the target of a symbolic link.
|
||||
*
|
||||
|
@ -63,21 +63,6 @@ namespace os::FileSystem
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void> 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<String> 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<String> readlink(const Path& path)
|
||||
{
|
||||
struct stat st;
|
||||
|
Loading…
Reference in New Issue
Block a user