io: implement stat'ing paths, files, and directory contents

This commit is contained in:
2025-02-14 22:06:24 +00:00
parent 61848aadd7
commit bf83831cee
9 changed files with 257 additions and 46 deletions

View File

@@ -1,9 +1,12 @@
#include "status.h"
#include <blue/io/directory.h>
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
struct b_directory {
@@ -27,38 +30,38 @@ static struct b_object_type directory_type = {
.t_release = directory_release,
};
static enum b_status status_from_errno(int error, enum b_status default_value)
{
switch (error) {
case ENOENT:
return B_ERR_NO_ENTRY;
default:
return default_value;
}
}
enum b_status b_directory_open(
struct b_directory *root, const struct b_path *path,
struct b_directory **out)
{
enum b_status status = B_SUCCESS;
struct b_path *cwd = NULL;
if (!root) {
cwd = b_path_create_cwd();
}
const b_path *parts[] = {
root ? root->abs_path : NULL,
root ? root->abs_path : cwd,
path,
};
b_path *new_path = b_path_join(parts, sizeof parts / sizeof *parts);
b_path *new_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
if (!new_path) {
return B_ERR_NO_MEMORY;
}
if (cwd) {
b_path_release(cwd);
}
int root_fd = root ? root->fd : AT_FDCWD;
int fd = openat(root_fd, b_path_ptr(path), O_DIRECTORY);
if (fd == -1) {
status = status_from_errno(errno, B_ERR_IO_FAILURE);
status = b_status_from_errno(errno, B_ERR_IO_FAILURE);
b_path_release(new_path);
@@ -81,6 +84,69 @@ static int ftsent_compare(const FTSENT **one, const FTSENT **two)
return (strcmp((*one)->fts_name, (*two)->fts_name));
}
static void update_iterator_data(struct b_directory_iterator *it)
{
struct z__b_directory_iterator *it_data = it->_z;
if (it->filepath) {
b_path_release((struct b_path *)it->filepath);
it->filepath = NULL;
}
FTSENT *ent = it_data->ent;
struct b_path *path = b_path_create_from_cstr(
ent->fts_path + b_path_length(it->root->abs_path) + 1);
it->filename = ent->fts_name;
it->filepath = path;
memset(&it->info, 0x0, sizeof it->info);
it->info.length = ent->fts_statp->st_size;
if (S_ISREG(ent->fts_statp->st_flags)) {
it->info.attrib |= B_FILE_ATTRIB_NORMAL;
}
if (S_ISDIR(ent->fts_statp->st_flags)) {
it->info.attrib |= B_FILE_ATTRIB_DIRECTORY;
}
if (S_ISBLK(ent->fts_statp->st_flags)) {
it->info.attrib |= B_FILE_ATTRIB_BLOCK_DEVICE;
}
if (S_ISCHR(ent->fts_statp->st_flags)) {
it->info.attrib |= B_FILE_ATTRIB_CHAR_DEVICE;
}
if (S_ISLNK(ent->fts_statp->st_flags)) {
it->info.attrib |= B_FILE_ATTRIB_SYMLINK;
}
}
static void cleanup_iterator(struct b_directory_iterator *it)
{
if (it->filepath) {
b_path_release((struct b_path *)it->filepath);
it->filepath = NULL;
}
struct z__b_directory_iterator *it_data = it->_z;
memset(it, 0x0, sizeof *it);
if (!it_data) {
return;
}
if (it_data->fts) {
fts_close(it_data->fts);
}
free(it_data);
}
int b_directory_iterator_begin(
struct b_directory *directory, struct b_directory_iterator *it,
enum b_directory_iterator_flags flags)
@@ -89,6 +155,11 @@ int b_directory_iterator_begin(
it->root = directory;
struct z__b_directory_iterator *it_data = malloc(sizeof *it_data);
if (!it_data) {
return -1;
}
memset(it_data, 0x0, sizeof *it_data);
it->_z = it_data;
@@ -103,16 +174,13 @@ int b_directory_iterator_begin(
it_data->fts
= fts_open((char *const *)path_list, fts_flags, ftsent_compare);
if (!it_data) {
return -1;
}
bool done = false;
while (!done) {
it_data->ent = fts_read(it_data->fts);
if (!it_data->ent) {
cleanup_iterator(it);
return -1;
}
@@ -145,9 +213,7 @@ int b_directory_iterator_begin(
}
}
it->filename = it_data->ent->fts_name;
it->filepath
= it_data->ent->fts_path + b_path_length(it->root->abs_path) + 1;
update_iterator_data(it);
return 0;
}
@@ -164,6 +230,7 @@ bool b_directory_iterator_next(struct b_directory_iterator *it)
it_data->ent = fts_read(it_data->fts);
if (!it_data->ent) {
cleanup_iterator(it);
return false;
}
@@ -195,9 +262,7 @@ bool b_directory_iterator_next(struct b_directory_iterator *it)
}
}
it->filename = it_data->ent->fts_name;
it->filepath
= it_data->ent->fts_path + b_path_length(it->root->abs_path) + 1;
update_iterator_data(it);
return true;
}