diff options
author | Mițca Dumitru <dumitru0mitca@gmail.com> | 2021-02-21 02:55:06 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-02-21 18:14:29 +0100 |
commit | ecafee86f8f03e37029f778be63adffc0f0ff072 (patch) | |
tree | 7e3af9ffc0362c7c4fc48b1f78cc3043ebd652a4 /Userland | |
parent | f4569ecf3c968f654819d928797a0ac8f99afd51 (diff) | |
download | serenity-ecafee86f8f03e37029f778be63adffc0f0ff072.zip |
rm: Use Core::File::remove
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Utilities/rm.cpp | 60 |
1 files changed, 12 insertions, 48 deletions
diff --git a/Userland/Utilities/rm.cpp b/Userland/Utilities/rm.cpp index d563a294df..87daaecebe 100644 --- a/Userland/Utilities/rm.cpp +++ b/Userland/Utilities/rm.cpp @@ -24,55 +24,13 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include <AK/String.h> #include <AK/StringBuilder.h> #include <AK/Vector.h> #include <LibCore/ArgsParser.h> -#include <LibCore/DirIterator.h> -#include <dirent.h> +#include <LibCore/File.h> #include <stdio.h> -#include <string.h> -#include <sys/stat.h> #include <unistd.h> -static int remove(bool recursive, bool force, String path) -{ - struct stat path_stat; - if (lstat(path.characters(), &path_stat) < 0) { - if (!force) - perror("lstat"); - return force ? 0 : 1; - } - - if (S_ISDIR(path_stat.st_mode) && recursive) { - auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir); - if (di.has_error()) { - if (!force) - fprintf(stderr, "DirIterator: %s\n", di.error_string()); - return 1; - } - - while (di.has_next()) { - int s = remove(true, force, di.next_full_path()); - if (s != 0 && !force) - return s; - } - - int s = rmdir(path.characters()); - if (s < 0 && !force) { - perror("rmdir"); - return 1; - } - } else { - int rc = unlink(path.characters()); - if (rc < 0 && !force) { - perror("unlink"); - return 1; - } - } - return 0; -} - int main(int argc, char** argv) { if (pledge("stdio rpath cpath", nullptr) < 0) { @@ -92,11 +50,17 @@ int main(int argc, char** argv) args_parser.add_positional_argument(paths, "Path(s) to remove", "path"); args_parser.parse(argc, argv); - int rc = 0; + bool had_errors = false; for (auto& path : paths) { - rc |= remove(recursive, force, path); - if (verbose && rc == 0) - printf("removed '%s'\n", path); + auto result = Core::File::remove(path, recursive ? Core::File::RecursionMode::Allowed : Core::File::RecursionMode::Disallowed, force); + + if (result.is_error()) { + warnln("rm: cannot remove '{}': {}", path, result.error().error_code); + had_errors = true; + } + + if (verbose) + outln("removed '{}'", path); } - return rc; + return had_errors ? 1 : 0; } |