1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:17:35 +00:00

LibCore: Add CDirIterator, and use it in everything rather than readdir

This commit is contained in:
Robin Burchell 2019-05-27 09:26:54 +02:00 committed by Andreas Kling
parent f352a5094d
commit 9d2b08e06e
9 changed files with 152 additions and 55 deletions

View file

@ -9,6 +9,7 @@
#include <SharedGraphics/GraphicsBitmap.h>
#include <LibGUI/GPainter.h>
#include <LibCore/CLock.h>
#include <LibCore/CDirIterator.h>
static CLockable<HashMap<String, RetainPtr<GraphicsBitmap>>>& thumbnail_cache()
{
@ -235,22 +236,23 @@ GVariant GDirectoryModel::data(const GModelIndex& index, Role role) const
void GDirectoryModel::update()
{
DIR* dirp = opendir(m_path.characters());
if (!dirp) {
perror("opendir");
CDirIterator di(m_path, CDirIterator::SkipDots);
if (di.has_error()) {
fprintf(stderr, "CDirIterator: %s\n", di.error_string());
exit(1);
}
m_directories.clear();
m_files.clear();
m_bytes_in_files = 0;
while (auto* de = readdir(dirp)) {
while (di.has_next()) {
String name = di.next_path();
Entry entry;
entry.name = de->d_name;
if (entry.name == "." || entry.name == "..")
continue;
entry.name = name;
struct stat st;
int rc = lstat(String::format("%s/%s", m_path.characters(), de->d_name).characters(), &st);
int rc = lstat(String::format("%s/%s", m_path.characters(), name.characters()).characters(), &st);
if (rc < 0) {
perror("lstat");
continue;
@ -266,7 +268,6 @@ void GDirectoryModel::update()
if (S_ISREG(entry.mode))
m_bytes_in_files += st.st_size;
}
closedir(dirp);
did_update();
}

View file

@ -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)

View file

@ -1,4 +1,5 @@
#include <LibGUI/GFontDatabase.h>
#include <LibCore/CDirIterator.h>
#include <SharedGraphics/Font.h>
#include <dirent.h>
#include <stdio.h>
@ -15,15 +16,14 @@ GFontDatabase& GFontDatabase::the()
GFontDatabase::GFontDatabase()
{
DIR* dirp = opendir("/res/fonts");
if (!dirp) {
perror("opendir");
CDirIterator di("/res/fonts", CDirIterator::SkipDots);
if (di.has_error()) {
fprintf(stderr, "CDirIterator: %s\n", di.error_string());
exit(1);
}
while (auto* de = readdir(dirp)) {
if (de->d_name[0] == '.')
continue;
auto path = String::format("/res/fonts/%s", de->d_name);
while (di.has_next()) {
String name = di.next_path();
auto path = String::format("/res/fonts/%s", name.characters());
if (auto font = Font::load_from_file(path)) {
Metadata metadata;
metadata.path = path;
@ -32,7 +32,6 @@ GFontDatabase::GFontDatabase()
m_name_to_metadata.set(font->name(), move(metadata));
}
}
closedir(dirp);
}
GFontDatabase::~GFontDatabase()