1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:47:45 +00:00

Userland: Use DirIterator in rm

This commit is contained in:
Shannon Booth 2020-02-16 14:11:58 +13:00 committed by Andreas Kling
parent 42399167b3
commit 4d8547d112

View file

@ -28,6 +28,7 @@
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/DirIterator.h>
#include <dirent.h> #include <dirent.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -43,22 +44,18 @@ int remove(bool recursive, String path)
} }
if (S_ISDIR(path_stat.st_mode) && recursive) { if (S_ISDIR(path_stat.st_mode) && recursive) {
DIR* derp = opendir(path.characters()); auto di = Core::DirIterator(path, Core::DirIterator::SkipParentAndBaseDir);
if (!derp) { if (di.has_error()) {
fprintf(stderr, "DirIterator: %s\n", di.error_string());
return 1; return 1;
} }
while (auto* de = readdir(derp)) { while (di.has_next()) {
if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) { int s = remove(true, di.next_full_path());
StringBuilder builder;
builder.append(path);
builder.append('/');
builder.append(de->d_name);
int s = remove(true, builder.to_string());
if (s != 0) if (s != 0)
return s; return s;
} }
}
int s = rmdir(path.characters()); int s = rmdir(path.characters());
if (s < 0) { if (s < 0) {
perror("rmdir"); perror("rmdir");