Files
bluelib/io/sys/darwin/directory.c

402 lines
7.2 KiB
C

#include "posix.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 {
struct b_object base;
int fd;
struct b_path *abs_path;
};
struct z__b_directory_iterator {
FTS *fts;
FTSENT *ent;
};
static void directory_release(struct b_object *obj);
static struct b_object_type directory_type = {
.t_name = "corelib::directory",
.t_flags = B_OBJECT_FUNDAMENTAL,
.t_id = B_OBJECT_TYPE_PATH,
.t_instance_size = sizeof(struct b_directory),
.t_release = directory_release,
};
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 : cwd,
path,
};
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 = b_status_from_errno(errno, B_ERR_IO_FAILURE);
b_path_release(new_path);
return status;
}
struct b_directory *dir
= (struct b_directory *)b_object_type_instantiate(&directory_type);
dir->abs_path = new_path;
dir->fd = fd;
*out = dir;
return B_SUCCESS;
}
const struct b_path *b_directory_get_path(const struct b_directory *dir)
{
return dir->abs_path;
}
bool b_directory_path_exists(
const struct b_directory *root, const struct b_path *path)
{
const struct b_path *parts[] = {
root ? root->abs_path : NULL,
path,
};
struct b_path *abs_path
= b_path_join(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return false;
}
bool result = b_path_exists(abs_path);
b_path_release(abs_path);
return result;
}
bool b_directory_path_is_file(
const struct b_directory *root, const struct b_path *path)
{
const struct b_path *parts[] = {
root ? root->abs_path : NULL,
path,
};
struct b_path *abs_path
= b_path_join(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return false;
}
bool result = b_path_is_file(abs_path);
b_path_release(abs_path);
return result;
}
bool b_directory_path_is_directory(
const struct b_directory *root, const struct b_path *path)
{
const struct b_path *parts[] = {
root ? root->abs_path : NULL,
path,
};
struct b_path *abs_path
= b_path_join(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return false;
}
bool result = b_path_is_directory(abs_path);
b_path_release(abs_path);
return result;
}
enum b_status b_directory_path_stat(
const struct b_directory *root, const struct b_path *path,
struct b_file_info *out)
{
const struct b_path *parts[] = {
root ? root->abs_path : NULL,
path,
};
struct b_path *abs_path
= b_path_join(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return B_ERR_NO_MEMORY;
}
enum b_status status = b_path_stat(abs_path, out);
b_path_release(abs_path);
return status;
}
enum b_status b_directory_path_unlink(
const struct b_directory *root, const struct b_path *path)
{
const struct b_path *parts[] = {
root ? root->abs_path : NULL,
path,
};
struct b_path *abs_path
= b_path_join(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) {
return B_ERR_NO_MEMORY;
}
enum b_status status = b_path_unlink(abs_path);
b_path_release(abs_path);
return status;
}
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_mode)) {
it->info.attrib |= B_FILE_ATTRIB_NORMAL;
}
if (S_ISDIR(ent->fts_statp->st_mode)) {
it->info.attrib |= B_FILE_ATTRIB_DIRECTORY;
}
if (S_ISBLK(ent->fts_statp->st_mode)) {
it->info.attrib |= B_FILE_ATTRIB_BLOCK_DEVICE;
}
if (S_ISCHR(ent->fts_statp->st_mode)) {
it->info.attrib |= B_FILE_ATTRIB_CHAR_DEVICE;
}
if (S_ISLNK(ent->fts_statp->st_mode)) {
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)
{
memset(it, 0x0, sizeof *it);
it->flags = flags;
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;
int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR;
const char *path_list[] = {
b_path_ptr(directory->abs_path),
NULL,
};
it_data->fts
= fts_open((char *const *)path_list, fts_flags, ftsent_compare);
bool done = false;
while (!done) {
it_data->ent = fts_read(it_data->fts);
if (!it_data->ent) {
cleanup_iterator(it);
return -1;
}
if (it_data->ent->fts_level == 0) {
continue;
}
switch (it_data->ent->fts_info) {
case FTS_DOT:
continue;
case FTS_F:
done = true;
break;
case FTS_D:
if (it->flags & B_DIRECTORY_ITERATE_PARENT_LAST) {
continue;
}
done = true;
break;
case FTS_DP:
if (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST) {
continue;
}
done = true;
break;
default:
done = true;
break;
}
}
update_iterator_data(it);
return 0;
}
bool b_directory_iterator_next(struct b_directory_iterator *it)
{
struct z__b_directory_iterator *it_data = it->_z;
if (!it_data || !it_data->fts) {
return false;
}
bool done = false;
while (!done) {
it_data->ent = fts_read(it_data->fts);
if (!it_data->ent) {
cleanup_iterator(it);
return false;
}
if (it_data->ent->fts_level == 0) {
continue;
}
switch (it_data->ent->fts_info) {
case FTS_DOT:
continue;
case FTS_F:
done = true;
break;
case FTS_D:
if (it->flags & B_DIRECTORY_ITERATE_PARENT_LAST) {
continue;
}
done = true;
break;
case FTS_DP:
if (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST) {
continue;
}
done = true;
break;
default:
done = true;
break;
}
}
update_iterator_data(it);
return true;
}
enum b_status b_directory_iterator_erase(struct b_directory_iterator *it)
{
return B_SUCCESS;
}
bool b_directory_iterator_is_valid(const struct b_directory_iterator *it)
{
if (!it->_z) {
return false;
}
if (!it->_z->ent) {
return false;
}
return true;
}
static void directory_release(struct b_object *obj)
{
struct b_directory *dir = B_DIRECTORY(obj);
close(dir->fd);
b_path_release(dir->abs_path);
}