mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-27 14:58:46 +00:00
LibCore: Add CDirIterator, and use it in everything rather than readdir
This commit is contained in:
parent
f352a5094d
commit
9d2b08e06e
Notes:
sideshowbarker
2024-07-19 13:54:33 +09:00
Author: https://github.com/rburchell
Commit: 9d2b08e06e
Pull-request: https://github.com/SerenityOS/serenity/pull/123
Reviewed-by: https://github.com/awesomekling ✅
9 changed files with 152 additions and 55 deletions
|
@ -1,4 +1,5 @@
|
|||
#include <LibGUI/GFileSystemModel.h>
|
||||
#include <LibCore/CDirIterator.h>
|
||||
#include <AK/FileSystemPath.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <sys/stat.h>
|
||||
|
@ -33,15 +34,16 @@ struct GFileSystemModel::Node {
|
|||
has_traversed = true;
|
||||
|
||||
auto full_path = this->full_path(model);
|
||||
DIR* dirp = opendir(full_path.characters());
|
||||
if (!dirp)
|
||||
CDirIterator di(full_path, CDirIterator::SkipDots);
|
||||
if (di.has_error()) {
|
||||
fprintf(stderr, "CDirIterator: %s\n", di.error_string());
|
||||
return;
|
||||
}
|
||||
|
||||
while (auto* de = readdir(dirp)) {
|
||||
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
|
||||
continue;
|
||||
while (di.has_next()) {
|
||||
String name = di.next_path();
|
||||
struct stat st;
|
||||
int rc = lstat(String::format("%s/%s", full_path.characters(), de->d_name).characters(), &st);
|
||||
int rc = lstat(String::format("%s/%s", full_path.characters(), name.characters()).characters(), &st);
|
||||
if (rc < 0) {
|
||||
perror("lstat");
|
||||
continue;
|
||||
|
@ -49,13 +51,11 @@ struct GFileSystemModel::Node {
|
|||
if (model.m_mode == DirectoriesOnly && !S_ISDIR(st.st_mode))
|
||||
continue;
|
||||
auto* child = new Node;
|
||||
child->name = de->d_name;
|
||||
child->name = name;
|
||||
child->type = S_ISDIR(st.st_mode) ? Node::Type::Directory : Node::Type::File;
|
||||
child->parent = this;
|
||||
children.append(child);
|
||||
}
|
||||
|
||||
closedir(dirp);
|
||||
}
|
||||
|
||||
void reify_if_needed(const GFileSystemModel& model)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue