diff options
author | Shannon Booth <shannon.ml.booth@gmail.com> | 2020-02-16 14:11:58 +1300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-02-16 02:19:22 +0100 |
commit | 4d8547d112dfd247bab13df4a6482e1d89cb5053 (patch) | |
tree | 75e08d26a66238bca3ed351e60c64b0cab98a0f3 /Userland/rm.cpp | |
parent | 42399167b3d2284ba09e57a845264d35c668b01c (diff) | |
download | serenity-4d8547d112dfd247bab13df4a6482e1d89cb5053.zip |
Userland: Use DirIterator in rm
Diffstat (limited to 'Userland/rm.cpp')
-rw-r--r-- | Userland/rm.cpp | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/Userland/rm.cpp b/Userland/rm.cpp index 2b33d3d7d9..8c2a7c325f 100644 --- a/Userland/rm.cpp +++ b/Userland/rm.cpp @@ -28,6 +28,7 @@ #include <AK/StringBuilder.h> #include <AK/Vector.h> #include <LibCore/ArgsParser.h> +#include <LibCore/DirIterator.h> #include <dirent.h> #include <stdio.h> #include <string.h> @@ -43,22 +44,18 @@ int remove(bool recursive, String path) } if (S_ISDIR(path_stat.st_mode) && recursive) { - DIR* derp = opendir(path.characters()); - if (!derp) { + auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir); + if (di.has_error()) { + fprintf(stderr, "DirIterator: %s\n", di.error_string()); return 1; } - while (auto* de = readdir(derp)) { - if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) { - StringBuilder builder; - builder.append(path); - builder.append('/'); - builder.append(de->d_name); - int s = remove(true, builder.to_string()); - if (s != 0) - return s; - } + while (di.has_next()) { + int s = remove(true, di.next_full_path()); + if (s != 0) + return s; } + int s = rmdir(path.characters()); if (s < 0) { perror("rmdir"); |