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;
}
+25 -20
View File
@@ -1,3 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include "misc.h"
#include "posix.h"
@@ -84,13 +86,13 @@ static fx_result file_open_shadow(
= fx_path_create_from_cstr(fx_string_get_cstr(filename));
fx_string_unref(filename);
const fx_path *parts[] = {
dir,
shadow_filename,
const fx_value *parts[] = {
&FX_VALUE_OBJECT(dir),
&FX_VALUE_OBJECT(shadow_filename),
};
fx_path *shadow_filepath
= fx_path_join(parts, sizeof parts / sizeof parts[0]);
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
fx_path_unref(dir);
fx_path_unref(shadow_filename);
@@ -101,7 +103,7 @@ static fx_result file_open_shadow(
fx_file *shadow_file;
fx_result status = fx_file_open(
FX_DIRECTORY_ROOT,
shadow_filepath,
FX_VALUE_OBJECT(shadow_filepath),
mode,
&shadow_file);
fx_path_unref(shadow_filepath);
@@ -227,12 +229,14 @@ static enum fx_status file_swap_shadow(
z__fx_io_generate_tmp_filename(tmp_name, sizeof tmp_name);
fx_path *tmp_name_p = fx_path_create_from_cstr(tmp_name);
const fx_path *parts[] = {
dir_path,
tmp_name_p,
const fx_value *parts[] = {
&FX_VALUE_OBJECT(dir_path),
&FX_VALUE_OBJECT(tmp_name_p),
};
tmp_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
tmp_path = fx_path_join_array(
parts,
sizeof parts / sizeof parts[0]);
fx_path_unref(tmp_name_p);
@@ -248,11 +252,15 @@ static enum fx_status file_swap_shadow(
int err;
err = rename(fx_path_get_cstr(main_file->path), fx_path_get_cstr(tmp_path));
err = rename(
fx_path_get_cstr(main_file->path),
fx_path_get_cstr(tmp_path));
err = rename(
fx_path_get_cstr(shadow_file->path),
fx_path_get_cstr(main_file->path));
err = rename(fx_path_get_cstr(tmp_path), fx_path_get_cstr(shadow_file->path));
err = rename(
fx_path_get_cstr(tmp_path),
fx_path_get_cstr(shadow_file->path));
fx_path_unref(tmp_path);
@@ -406,11 +414,10 @@ static enum fx_status stream_tell(const fx_stream *stream, size_t *pos)
fx_result fx_file_open(
fx_directory *root,
const fx_path *path,
fx_value path,
enum fx_file_mode mode,
fx_file **out)
{
const fx_path *file_path = path;
unsigned int flags = fx_mode_to_unix_mode(mode);
if (flags == (unsigned int)-1) {
@@ -422,16 +429,14 @@ fx_result fx_file_open(
if (root) {
root_path = fx_directory_get_path(root);
} else {
root_path = fx_path_create_cwd();
root_path = fx_path_get_system(FX_PATH_CWD);
free_root_path = true;
}
const fx_path *parts[] = {
root_path,
file_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_path), &path);
if (free_root_path) {
fx_path_unref((fx_path *)root_path);
@@ -492,7 +497,7 @@ fx_result fx_file_open_temp(enum fx_file_mode mode, fx_file **out)
fx_result status = fx_file_open(
FX_DIRECTORY_ROOT,
rpath,
FX_VALUE_OBJECT(rpath),
mode | FX_FILE_CREATE_ONLY,
out);
+60 -20
View File
@@ -6,7 +6,6 @@
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <fx/string.h>
#include <fx/value.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
@@ -41,7 +40,7 @@ static bool path_is_absolute(const struct fx_path_p *path)
return s[0] == '/';
}
static const char *path_get_cstr(const struct fx_path_p *path)
static const char *path_ptr(const struct fx_path_p *path)
{
return fx_string_get_cstr(path->p_pathstr);
}
@@ -51,7 +50,7 @@ static enum fx_status path_stat(
struct fx_file_info *out)
{
struct stat st;
int err = stat(path_get_cstr(path), &st);
int err = stat(path_ptr(path), &st);
if (err != 0) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
@@ -92,22 +91,22 @@ static bool path_is_directory(const struct fx_path_p *path)
return (info.attrib & FX_FILE_ATTRIB_DIRECTORY) != 0;
}
static void append_path(struct fx_path_p *dest, const struct fx_path_p *src)
static void append_path(struct fx_path_p *dest, const fx_string *src)
{
if (path_is_absolute(src)) {
const char *src_cstr = fx_string_get_cstr(src);
if (src_cstr[0] == '/') {
fx_string_clear(dest->p_pathstr);
fx_string_append_s(dest->p_pathstr, src->p_pathstr);
fx_string_append_s(dest->p_pathstr, src);
return;
}
if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0
&& fx_string_get_last_char(dest->p_pathstr) != '/'
&& fx_string_get_first_char(src->p_pathstr) != '/') {
char s[] = {'/', 0};
fx_string_append_cstr(dest->p_pathstr, s);
&& fx_string_get_first_char(src) != '/') {
fx_string_append_c(dest->p_pathstr, '/');
}
fx_string_append_s(dest->p_pathstr, src->p_pathstr);
fx_string_append_s(dest->p_pathstr, src);
}
static enum fx_status path_unlink(const struct fx_path_p *path)
@@ -294,7 +293,7 @@ fx_path *fx_path_duplicate(const fx_path *path)
return new_path;
}
fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
fx_path *fx_path_join_array(const fx_value *values[], size_t nr_values)
{
fx_path *result = fx_path_create();
if (!result) {
@@ -304,14 +303,53 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
struct fx_path_p *result_p
= fx_object_get_private(result, FX_TYPE_PATH);
for (size_t i = 0; i < nr_paths; i++) {
if (paths[i]) {
struct fx_path_p *path_p
= fx_object_get_private(paths[i], FX_TYPE_PATH);
append_path(result_p, path_p);
fx_stringstream *tmp = fx_stringstream_create();
fx_string *value_s = fx_string_create();
for (size_t i = 0; i < nr_values; i++) {
if (!values[i]) {
continue;
}
fx_stringstream_reset(tmp);
fx_value_to_string(values[i], tmp, NULL);
fx_string_clear(value_s);
fx_string_append_cstr(value_s, fx_stringstream_get_cstr(tmp));
append_path(result_p, value_s);
}
fx_string_unref(value_s);
fx_stringstream_unref(tmp);
return result;
}
fx_path *fx_path_join_list(size_t count, ...)
{
fx_path *result = fx_path_create();
if (!result) {
return NULL;
}
struct fx_path_p *result_p
= fx_object_get_private(result, FX_TYPE_PATH);
va_list arg;
va_start(arg, count);
fx_stringstream *tmp = fx_stringstream_create();
fx_string *value_s = fx_string_create();
for (size_t i = 0; i < count; i++) {
const fx_value *value = va_arg(arg, const fx_value *);
fx_stringstream_reset(tmp);
fx_value_to_string(value, tmp, NULL);
fx_string_clear(value_s);
fx_string_append_cstr(value_s, fx_stringstream_get_cstr(tmp));
append_path(result_p, value_s);
}
fx_string_unref(value_s);
fx_stringstream_unref(tmp);
return result;
}
@@ -382,7 +420,7 @@ enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
const char *fx_path_get_cstr(const fx_path *path)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_get_cstr, path);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path);
}
size_t fx_path_length(const fx_path *path)
@@ -402,7 +440,7 @@ static void path_init(fx_object *obj, void *priv)
}
}
static void path_fini(fx_object *obj, void *priv)
void path_fini(fx_object *obj, void *priv)
{
struct fx_path_p *path = priv;
@@ -417,8 +455,10 @@ static fx_status path_to_string(
struct fx_path_p *path
= fx_object_get_private(obj->v_object, FX_TYPE_PATH);
fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL);
return FX_SUCCESS;
return fx_stream_write_cstr(
out,
fx_string_get_cstr(path->p_pathstr),
NULL);
}
/*** CLASS DEFINITION *********************************************************/