io: add lots of directory and path manipulation functions

This commit is contained in:
2025-04-11 13:54:53 +01:00
parent f3e48f3e47
commit a627116264
10 changed files with 689 additions and 42 deletions

View File

@@ -1,4 +1,4 @@
#include "status.h"
#include "posix.h"
#include <blue/io/directory.h>
#include <errno.h>
@@ -79,6 +79,112 @@ enum b_status b_directory_open(
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));
@@ -151,6 +257,8 @@ 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;
@@ -286,4 +394,8 @@ bool b_directory_iterator_is_valid(const struct b_directory_iterator *it)
static void directory_release(struct b_object *obj)
{
struct b_directory *dir = B_DIRECTORY(obj);
close(dir->fd);
b_path_release(dir->abs_path);
}