fx.io: update all path parameters to be fx_values

This commit is contained in:
2026-07-12 22:15:14 +01:00
parent e9a1bfbde2
commit 24bcb18543
9 changed files with 178 additions and 142 deletions
+71 -79
View File
@@ -1,3 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include "misc.h"
#include "posix.h"
@@ -65,9 +67,8 @@ static fx_result directory_delete(
{
enum fx_status status = FX_SUCCESS;
fx_iterator *it = fx_directory_begin(
dir,
FX_DIRECTORY_ITERATE_PARENT_LAST);
fx_iterator *it
= fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_LAST);
while (FX_OK(fx_iterator_get_status(it))) {
fx_iterator_erase(it);
}
@@ -87,14 +88,12 @@ static fx_result directory_delete(
static bool directory_path_exists(
const struct fx_directory_p *root,
const fx_path *path)
fx_value path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path = fx_path_join_list(
2,
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&path);
if (!abs_path) {
return false;
}
@@ -107,14 +106,14 @@ static bool directory_path_exists(
static bool directory_path_is_file(
const struct fx_directory_p *root,
const fx_path *path)
fx_value path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
};
const fx_value *parts[] = {};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path = fx_path_join_list(
2,
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&path);
if (!abs_path) {
return false;
}
@@ -127,14 +126,14 @@ static bool directory_path_is_file(
static bool directory_path_is_directory(
const struct fx_directory_p *root,
const fx_path *path)
fx_value path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
};
const fx_value *parts[] = {};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path = fx_path_join_list(
2,
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&path);
if (!abs_path) {
return false;
}
@@ -147,15 +146,15 @@ static bool directory_path_is_directory(
static fx_result directory_path_stat(
const struct fx_directory_p *root,
const fx_path *path,
fx_value path,
struct fx_file_info *out)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
};
const fx_value *parts[] = {};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path = fx_path_join_list(
2,
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&path);
if (!abs_path) {
return FX_RESULT_ERR(NO_MEMORY);
}
@@ -168,14 +167,14 @@ static fx_result directory_path_stat(
static fx_result directory_path_unlink(
const struct fx_directory_p *root,
const fx_path *path)
fx_value path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
path,
};
const fx_value *parts[] = {};
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *abs_path = fx_path_join_list(
2,
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
&path);
if (!abs_path) {
return FX_RESULT_ERR(NO_MEMORY);
}
@@ -255,7 +254,7 @@ static fx_result create_directory_hierarchy(
static fx_result directory_open(
struct fx_directory_p *root,
const fx_path *path,
fx_value path,
fx_directory_open_flags flags,
fx_directory **out)
{
@@ -263,7 +262,10 @@ static fx_result directory_open(
int root_fd = root ? root->d_fd : AT_FDCWD;
const char *path_cstr = fx_path_get_cstr(path);
fx_stringstream *path_str = fx_stringstream_create();
fx_value_to_string(&path, path_str, NULL);
const char *path_cstr = fx_stringstream_get_cstr(path_str);
if (root) {
while (*path_cstr == '/') {
path_cstr++;
@@ -293,21 +295,22 @@ static fx_result directory_open(
fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY);
fx_path *cwd = NULL;
struct fx_directory_p *p = fx_object_get_private(
dir,
FX_TYPE_DIRECTORY);
struct fx_directory_p *p
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
if (!root) {
cwd = fx_path_create_cwd();
cwd = fx_path_get_system(FX_PATH_CWD);
}
const fx_path *parts[] = {
root ? root->d_path_abs : cwd,
path,
const fx_value *parts[] = {
&FX_VALUE_OBJECT(root ? root->d_path_abs : cwd),
&FX_CSTR(path_cstr),
};
fx_path *new_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path *new_path
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!new_path) {
fx_stringstream_unref(path_str);
return FX_RESULT_ERR(NO_MEMORY);
}
@@ -316,7 +319,7 @@ static fx_result directory_open(
}
p->d_path_abs = new_path;
p->d_path_rel = fx_path_duplicate(path);
p->d_path_rel = fx_path_create_from_cstr(path_cstr);
p->d_fd = fd;
if (flags & FX_DIRECTORY_OPEN_DELETE_ON_CLOSE) {
@@ -324,6 +327,7 @@ static fx_result directory_open(
}
*out = dir;
fx_stringstream_unref(path_str);
return FX_RESULT_SUCCESS;
}
@@ -332,7 +336,7 @@ static fx_result directory_open(
fx_result fx_directory_open(
fx_directory *root,
const fx_path *path,
fx_value path,
fx_directory_open_flags flags,
fx_directory **out)
{
@@ -353,20 +357,17 @@ fx_result fx_directory_open_temp(fx_directory **out)
while (1) {
z__fx_io_generate_tmp_filename(name, sizeof name);
snprintf(path, sizeof path, "/tmp/%s", name);
fx_path *rpath = fx_path_create_from_cstr(path);
fx_directory *dir = NULL;
fx_result status = fx_directory_open(
FX_DIRECTORY_ROOT,
rpath,
FX_CSTR(path),
FX_DIRECTORY_OPEN_CREATE,
&dir);
struct fx_directory_p *p = fx_object_get_private(
dir,
FX_TYPE_DIRECTORY);
struct fx_directory_p *p
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
fx_path_unref(rpath);
continue;
}
@@ -374,8 +375,8 @@ fx_result fx_directory_open_temp(fx_directory **out)
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
}
fx_path_unlink(rpath);
fx_path_unref(rpath);
*out = dir;
return status;
}
}
@@ -410,16 +411,15 @@ const char *fx_directory_get_rel_path_cstr(const fx_directory *dir)
fx_result fx_directory_delete(fx_directory *dir)
{
struct fx_directory_p *p = fx_object_get_private(
dir,
FX_TYPE_DIRECTORY);
struct fx_directory_p *p
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
/* TODO allow object release functions to return a fx_result value */
fx_directory_unref(dir);
return FX_RESULT_SUCCESS;
}
bool fx_directory_path_exists(const fx_directory *root, const fx_path *path)
bool fx_directory_path_exists(const fx_directory *root, fx_value path)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY,
@@ -428,7 +428,7 @@ bool fx_directory_path_exists(const fx_directory *root, const fx_path *path)
path);
}
bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path)
bool fx_directory_path_is_file(const fx_directory *root, fx_value path)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY,
@@ -437,9 +437,7 @@ bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path)
path);
}
bool fx_directory_path_is_directory(
const fx_directory *root,
const fx_path *path)
bool fx_directory_path_is_directory(const fx_directory *root, fx_value path)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY,
@@ -450,7 +448,7 @@ bool fx_directory_path_is_directory(
fx_result fx_directory_path_stat(
const fx_directory *root,
const fx_path *path,
fx_value path,
struct fx_file_info *out)
{
FX_CLASS_DISPATCH_STATIC(
@@ -461,9 +459,7 @@ fx_result fx_directory_path_stat(
out);
}
fx_result fx_directory_path_unlink(
const fx_directory *root,
const fx_path *path)
fx_result fx_directory_path_unlink(const fx_directory *root, fx_value path)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY,
@@ -560,9 +556,8 @@ fx_iterator *fx_directory_begin(
enum fx_directory_iterator_flags flags)
{
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_DIRECTORY_ITERATOR);
it->flags = flags;
it->root = directory;
@@ -629,9 +624,8 @@ static const fx_iterator *iterator_begin(const fx_object *obj)
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_directory_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
if (!it || !it->fts) {
return FX_ERR_NO_DATA;
}
@@ -679,12 +673,11 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_directory_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
fx_result result = fx_directory_path_unlink(
it->root,
it->entry.filepath);
FX_VALUE_OBJECT(it->entry.filepath));
if (fx_result_is_error(result)) {
enum fx_status status = fx_error_get_status_code(result);
fx_error_discard(result);
@@ -696,9 +689,8 @@ static enum fx_status iterator_erase(fx_iterator *obj)
static const fx_value *iterator_get_value(const fx_iterator *obj)
{
struct fx_directory_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
return &it->entry_value;
}