fx.io: update all path parameters to be fx_values
This commit is contained in:
@@ -50,7 +50,7 @@ FX_API fx_type_id fx_directory_iterator_get_type(void);
|
|||||||
|
|
||||||
FX_API fx_result fx_directory_open(
|
FX_API fx_result fx_directory_open(
|
||||||
fx_directory *root,
|
fx_directory *root,
|
||||||
const fx_path *path,
|
fx_value path,
|
||||||
fx_directory_open_flags flags,
|
fx_directory_open_flags flags,
|
||||||
fx_directory **out);
|
fx_directory **out);
|
||||||
FX_API fx_result fx_directory_open_temp(fx_directory **out);
|
FX_API fx_result fx_directory_open_temp(fx_directory **out);
|
||||||
@@ -60,22 +60,17 @@ FX_API const char *fx_directory_get_path_cstr(const fx_directory *dir);
|
|||||||
FX_API const char *fx_directory_get_rel_path_cstr(const fx_directory *dir);
|
FX_API const char *fx_directory_get_rel_path_cstr(const fx_directory *dir);
|
||||||
FX_API fx_result fx_directory_delete(fx_directory *dir);
|
FX_API fx_result fx_directory_delete(fx_directory *dir);
|
||||||
|
|
||||||
FX_API bool fx_directory_path_exists(
|
FX_API bool fx_directory_path_exists(const fx_directory *root, fx_value path);
|
||||||
const fx_directory *root,
|
FX_API bool fx_directory_path_is_file(const fx_directory *root, fx_value path);
|
||||||
const fx_path *path);
|
|
||||||
FX_API bool fx_directory_path_is_file(
|
|
||||||
const fx_directory *root,
|
|
||||||
const fx_path *path);
|
|
||||||
FX_API bool fx_directory_path_is_directory(
|
FX_API bool fx_directory_path_is_directory(
|
||||||
const fx_directory *root,
|
const fx_directory *root,
|
||||||
const fx_path *path);
|
fx_value path);
|
||||||
FX_API fx_result fx_directory_path_stat(
|
FX_API fx_result fx_directory_path_stat(
|
||||||
const fx_directory *root,
|
const fx_directory *root,
|
||||||
const fx_path *path,
|
fx_value path,
|
||||||
struct fx_file_info *out);
|
struct fx_file_info *out);
|
||||||
FX_API fx_result fx_directory_path_unlink(
|
FX_API fx_result
|
||||||
const fx_directory *root,
|
fx_directory_path_unlink(const fx_directory *root, fx_value path);
|
||||||
const fx_path *path);
|
|
||||||
|
|
||||||
FX_API fx_iterator *fx_directory_begin(
|
FX_API fx_iterator *fx_directory_begin(
|
||||||
fx_directory *dir,
|
fx_directory *dir,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <fx/macros.h>
|
#include <fx/macros.h>
|
||||||
#include <fx/misc.h>
|
#include <fx/misc.h>
|
||||||
#include <fx/stream.h>
|
#include <fx/stream.h>
|
||||||
|
#include <fx/value.h>
|
||||||
|
|
||||||
FX_DECLS_BEGIN;
|
FX_DECLS_BEGIN;
|
||||||
|
|
||||||
@@ -55,7 +56,7 @@ FX_API fx_type_id fx_file_get_type(void);
|
|||||||
|
|
||||||
FX_API fx_result fx_file_open(
|
FX_API fx_result fx_file_open(
|
||||||
FX_TYPE_FWDREF(fx_directory) * root,
|
FX_TYPE_FWDREF(fx_directory) * root,
|
||||||
const FX_TYPE_FWDREF(fx_path) * path,
|
fx_value path,
|
||||||
fx_file_mode mode,
|
fx_file_mode mode,
|
||||||
fx_file **out);
|
fx_file **out);
|
||||||
FX_API fx_result fx_file_open_temp(fx_file_mode mode, fx_file **out);
|
FX_API fx_result fx_file_open_temp(fx_file_mode mode, fx_file **out);
|
||||||
|
|||||||
+71
-79
@@ -1,3 +1,5 @@
|
|||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "posix.h"
|
#include "posix.h"
|
||||||
|
|
||||||
@@ -65,9 +67,8 @@ static fx_result directory_delete(
|
|||||||
{
|
{
|
||||||
enum fx_status status = FX_SUCCESS;
|
enum fx_status status = FX_SUCCESS;
|
||||||
|
|
||||||
fx_iterator *it = fx_directory_begin(
|
fx_iterator *it
|
||||||
dir,
|
= fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_LAST);
|
||||||
FX_DIRECTORY_ITERATE_PARENT_LAST);
|
|
||||||
while (FX_OK(fx_iterator_get_status(it))) {
|
while (FX_OK(fx_iterator_get_status(it))) {
|
||||||
fx_iterator_erase(it);
|
fx_iterator_erase(it);
|
||||||
}
|
}
|
||||||
@@ -87,14 +88,12 @@ static fx_result directory_delete(
|
|||||||
|
|
||||||
static bool directory_path_exists(
|
static bool directory_path_exists(
|
||||||
const struct fx_directory_p *root,
|
const struct fx_directory_p *root,
|
||||||
const fx_path *path)
|
fx_value path)
|
||||||
{
|
{
|
||||||
const fx_path *parts[] = {
|
fx_path *abs_path = fx_path_join_list(
|
||||||
root ? root->d_path_abs : NULL,
|
2,
|
||||||
path,
|
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL),
|
||||||
};
|
&path);
|
||||||
|
|
||||||
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
|
||||||
if (!abs_path) {
|
if (!abs_path) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -107,14 +106,14 @@ static bool directory_path_exists(
|
|||||||
|
|
||||||
static bool directory_path_is_file(
|
static bool directory_path_is_file(
|
||||||
const struct fx_directory_p *root,
|
const struct fx_directory_p *root,
|
||||||
const fx_path *path)
|
fx_value path)
|
||||||
{
|
{
|
||||||
const fx_path *parts[] = {
|
const fx_value *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) {
|
if (!abs_path) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -127,14 +126,14 @@ static bool directory_path_is_file(
|
|||||||
|
|
||||||
static bool directory_path_is_directory(
|
static bool directory_path_is_directory(
|
||||||
const struct fx_directory_p *root,
|
const struct fx_directory_p *root,
|
||||||
const fx_path *path)
|
fx_value path)
|
||||||
{
|
{
|
||||||
const fx_path *parts[] = {
|
const fx_value *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) {
|
if (!abs_path) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -147,15 +146,15 @@ static bool directory_path_is_directory(
|
|||||||
|
|
||||||
static fx_result directory_path_stat(
|
static fx_result directory_path_stat(
|
||||||
const struct fx_directory_p *root,
|
const struct fx_directory_p *root,
|
||||||
const fx_path *path,
|
fx_value path,
|
||||||
struct fx_file_info *out)
|
struct fx_file_info *out)
|
||||||
{
|
{
|
||||||
const fx_path *parts[] = {
|
const fx_value *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) {
|
if (!abs_path) {
|
||||||
return FX_RESULT_ERR(NO_MEMORY);
|
return FX_RESULT_ERR(NO_MEMORY);
|
||||||
}
|
}
|
||||||
@@ -168,14 +167,14 @@ static fx_result directory_path_stat(
|
|||||||
|
|
||||||
static fx_result directory_path_unlink(
|
static fx_result directory_path_unlink(
|
||||||
const struct fx_directory_p *root,
|
const struct fx_directory_p *root,
|
||||||
const fx_path *path)
|
fx_value path)
|
||||||
{
|
{
|
||||||
const fx_path *parts[] = {
|
const fx_value *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) {
|
if (!abs_path) {
|
||||||
return FX_RESULT_ERR(NO_MEMORY);
|
return FX_RESULT_ERR(NO_MEMORY);
|
||||||
}
|
}
|
||||||
@@ -255,7 +254,7 @@ static fx_result create_directory_hierarchy(
|
|||||||
|
|
||||||
static fx_result directory_open(
|
static fx_result directory_open(
|
||||||
struct fx_directory_p *root,
|
struct fx_directory_p *root,
|
||||||
const fx_path *path,
|
fx_value path,
|
||||||
fx_directory_open_flags flags,
|
fx_directory_open_flags flags,
|
||||||
fx_directory **out)
|
fx_directory **out)
|
||||||
{
|
{
|
||||||
@@ -263,7 +262,10 @@ static fx_result directory_open(
|
|||||||
|
|
||||||
int root_fd = root ? root->d_fd : AT_FDCWD;
|
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) {
|
if (root) {
|
||||||
while (*path_cstr == '/') {
|
while (*path_cstr == '/') {
|
||||||
path_cstr++;
|
path_cstr++;
|
||||||
@@ -293,21 +295,22 @@ static fx_result directory_open(
|
|||||||
|
|
||||||
fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY);
|
fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY);
|
||||||
fx_path *cwd = NULL;
|
fx_path *cwd = NULL;
|
||||||
struct fx_directory_p *p = fx_object_get_private(
|
struct fx_directory_p *p
|
||||||
dir,
|
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
||||||
FX_TYPE_DIRECTORY);
|
|
||||||
|
|
||||||
if (!root) {
|
if (!root) {
|
||||||
cwd = fx_path_create_cwd();
|
cwd = fx_path_get_system(FX_PATH_CWD);
|
||||||
}
|
}
|
||||||
|
|
||||||
const fx_path *parts[] = {
|
const fx_value *parts[] = {
|
||||||
root ? root->d_path_abs : cwd,
|
&FX_VALUE_OBJECT(root ? root->d_path_abs : cwd),
|
||||||
path,
|
&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) {
|
if (!new_path) {
|
||||||
|
fx_stringstream_unref(path_str);
|
||||||
return FX_RESULT_ERR(NO_MEMORY);
|
return FX_RESULT_ERR(NO_MEMORY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,7 +319,7 @@ static fx_result directory_open(
|
|||||||
}
|
}
|
||||||
|
|
||||||
p->d_path_abs = new_path;
|
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;
|
p->d_fd = fd;
|
||||||
|
|
||||||
if (flags & FX_DIRECTORY_OPEN_DELETE_ON_CLOSE) {
|
if (flags & FX_DIRECTORY_OPEN_DELETE_ON_CLOSE) {
|
||||||
@@ -324,6 +327,7 @@ static fx_result directory_open(
|
|||||||
}
|
}
|
||||||
|
|
||||||
*out = dir;
|
*out = dir;
|
||||||
|
fx_stringstream_unref(path_str);
|
||||||
|
|
||||||
return FX_RESULT_SUCCESS;
|
return FX_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
@@ -332,7 +336,7 @@ static fx_result directory_open(
|
|||||||
|
|
||||||
fx_result fx_directory_open(
|
fx_result fx_directory_open(
|
||||||
fx_directory *root,
|
fx_directory *root,
|
||||||
const fx_path *path,
|
fx_value path,
|
||||||
fx_directory_open_flags flags,
|
fx_directory_open_flags flags,
|
||||||
fx_directory **out)
|
fx_directory **out)
|
||||||
{
|
{
|
||||||
@@ -353,20 +357,17 @@ fx_result fx_directory_open_temp(fx_directory **out)
|
|||||||
while (1) {
|
while (1) {
|
||||||
z__fx_io_generate_tmp_filename(name, sizeof name);
|
z__fx_io_generate_tmp_filename(name, sizeof name);
|
||||||
snprintf(path, sizeof path, "/tmp/%s", name);
|
snprintf(path, sizeof path, "/tmp/%s", name);
|
||||||
fx_path *rpath = fx_path_create_from_cstr(path);
|
|
||||||
|
|
||||||
fx_directory *dir = NULL;
|
fx_directory *dir = NULL;
|
||||||
fx_result status = fx_directory_open(
|
fx_result status = fx_directory_open(
|
||||||
FX_DIRECTORY_ROOT,
|
FX_DIRECTORY_ROOT,
|
||||||
rpath,
|
FX_CSTR(path),
|
||||||
FX_DIRECTORY_OPEN_CREATE,
|
FX_DIRECTORY_OPEN_CREATE,
|
||||||
&dir);
|
&dir);
|
||||||
struct fx_directory_p *p = fx_object_get_private(
|
struct fx_directory_p *p
|
||||||
dir,
|
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
||||||
FX_TYPE_DIRECTORY);
|
|
||||||
|
|
||||||
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
|
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
|
||||||
fx_path_unref(rpath);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -374,8 +375,8 @@ fx_result fx_directory_open_temp(fx_directory **out)
|
|||||||
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
|
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_path_unlink(rpath);
|
*out = dir;
|
||||||
fx_path_unref(rpath);
|
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)
|
fx_result fx_directory_delete(fx_directory *dir)
|
||||||
{
|
{
|
||||||
struct fx_directory_p *p = fx_object_get_private(
|
struct fx_directory_p *p
|
||||||
dir,
|
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
||||||
FX_TYPE_DIRECTORY);
|
|
||||||
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
|
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
|
||||||
/* TODO allow object release functions to return a fx_result value */
|
/* TODO allow object release functions to return a fx_result value */
|
||||||
fx_directory_unref(dir);
|
fx_directory_unref(dir);
|
||||||
return FX_RESULT_SUCCESS;
|
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_CLASS_DISPATCH_STATIC(
|
||||||
FX_TYPE_DIRECTORY,
|
FX_TYPE_DIRECTORY,
|
||||||
@@ -428,7 +428,7 @@ bool fx_directory_path_exists(const fx_directory *root, const fx_path *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_CLASS_DISPATCH_STATIC(
|
||||||
FX_TYPE_DIRECTORY,
|
FX_TYPE_DIRECTORY,
|
||||||
@@ -437,9 +437,7 @@ bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path)
|
|||||||
path);
|
path);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool fx_directory_path_is_directory(
|
bool fx_directory_path_is_directory(const fx_directory *root, fx_value path)
|
||||||
const fx_directory *root,
|
|
||||||
const fx_path *path)
|
|
||||||
{
|
{
|
||||||
FX_CLASS_DISPATCH_STATIC(
|
FX_CLASS_DISPATCH_STATIC(
|
||||||
FX_TYPE_DIRECTORY,
|
FX_TYPE_DIRECTORY,
|
||||||
@@ -450,7 +448,7 @@ bool fx_directory_path_is_directory(
|
|||||||
|
|
||||||
fx_result fx_directory_path_stat(
|
fx_result fx_directory_path_stat(
|
||||||
const fx_directory *root,
|
const fx_directory *root,
|
||||||
const fx_path *path,
|
fx_value path,
|
||||||
struct fx_file_info *out)
|
struct fx_file_info *out)
|
||||||
{
|
{
|
||||||
FX_CLASS_DISPATCH_STATIC(
|
FX_CLASS_DISPATCH_STATIC(
|
||||||
@@ -461,9 +459,7 @@ fx_result fx_directory_path_stat(
|
|||||||
out);
|
out);
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_result fx_directory_path_unlink(
|
fx_result fx_directory_path_unlink(const fx_directory *root, fx_value path)
|
||||||
const fx_directory *root,
|
|
||||||
const fx_path *path)
|
|
||||||
{
|
{
|
||||||
FX_CLASS_DISPATCH_STATIC(
|
FX_CLASS_DISPATCH_STATIC(
|
||||||
FX_TYPE_DIRECTORY,
|
FX_TYPE_DIRECTORY,
|
||||||
@@ -560,9 +556,8 @@ fx_iterator *fx_directory_begin(
|
|||||||
enum fx_directory_iterator_flags flags)
|
enum fx_directory_iterator_flags flags)
|
||||||
{
|
{
|
||||||
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
|
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
struct fx_directory_iterator_p *it = fx_object_get_private(
|
struct fx_directory_iterator_p *it
|
||||||
it_obj,
|
= fx_object_get_private(it_obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
FX_TYPE_DIRECTORY_ITERATOR);
|
|
||||||
|
|
||||||
it->flags = flags;
|
it->flags = flags;
|
||||||
it->root = directory;
|
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)
|
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_directory_iterator_p *it = fx_object_get_private(
|
struct fx_directory_iterator_p *it
|
||||||
obj,
|
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
FX_TYPE_DIRECTORY_ITERATOR);
|
|
||||||
if (!it || !it->fts) {
|
if (!it || !it->fts) {
|
||||||
return FX_ERR_NO_DATA;
|
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)
|
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_directory_iterator_p *it = fx_object_get_private(
|
struct fx_directory_iterator_p *it
|
||||||
obj,
|
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
FX_TYPE_DIRECTORY_ITERATOR);
|
|
||||||
fx_result result = fx_directory_path_unlink(
|
fx_result result = fx_directory_path_unlink(
|
||||||
it->root,
|
it->root,
|
||||||
it->entry.filepath);
|
FX_VALUE_OBJECT(it->entry.filepath));
|
||||||
if (fx_result_is_error(result)) {
|
if (fx_result_is_error(result)) {
|
||||||
enum fx_status status = fx_error_get_status_code(result);
|
enum fx_status status = fx_error_get_status_code(result);
|
||||||
fx_error_discard(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)
|
static const fx_value *iterator_get_value(const fx_iterator *obj)
|
||||||
{
|
{
|
||||||
struct fx_directory_iterator_p *it = fx_object_get_private(
|
struct fx_directory_iterator_p *it
|
||||||
obj,
|
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||||
FX_TYPE_DIRECTORY_ITERATOR);
|
|
||||||
|
|
||||||
return &it->entry_value;
|
return &it->entry_value;
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-20
@@ -1,3 +1,5 @@
|
|||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "posix.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_path_create_from_cstr(fx_string_get_cstr(filename));
|
||||||
fx_string_unref(filename);
|
fx_string_unref(filename);
|
||||||
|
|
||||||
const fx_path *parts[] = {
|
const fx_value *parts[] = {
|
||||||
dir,
|
&FX_VALUE_OBJECT(dir),
|
||||||
shadow_filename,
|
&FX_VALUE_OBJECT(shadow_filename),
|
||||||
};
|
};
|
||||||
|
|
||||||
fx_path *shadow_filepath
|
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(dir);
|
||||||
fx_path_unref(shadow_filename);
|
fx_path_unref(shadow_filename);
|
||||||
|
|
||||||
@@ -101,7 +103,7 @@ static fx_result file_open_shadow(
|
|||||||
fx_file *shadow_file;
|
fx_file *shadow_file;
|
||||||
fx_result status = fx_file_open(
|
fx_result status = fx_file_open(
|
||||||
FX_DIRECTORY_ROOT,
|
FX_DIRECTORY_ROOT,
|
||||||
shadow_filepath,
|
FX_VALUE_OBJECT(shadow_filepath),
|
||||||
mode,
|
mode,
|
||||||
&shadow_file);
|
&shadow_file);
|
||||||
fx_path_unref(shadow_filepath);
|
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);
|
z__fx_io_generate_tmp_filename(tmp_name, sizeof tmp_name);
|
||||||
fx_path *tmp_name_p = fx_path_create_from_cstr(tmp_name);
|
fx_path *tmp_name_p = fx_path_create_from_cstr(tmp_name);
|
||||||
|
|
||||||
const fx_path *parts[] = {
|
const fx_value *parts[] = {
|
||||||
dir_path,
|
&FX_VALUE_OBJECT(dir_path),
|
||||||
tmp_name_p,
|
&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);
|
fx_path_unref(tmp_name_p);
|
||||||
|
|
||||||
@@ -248,11 +252,15 @@ static enum fx_status file_swap_shadow(
|
|||||||
|
|
||||||
int err;
|
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(
|
err = rename(
|
||||||
fx_path_get_cstr(shadow_file->path),
|
fx_path_get_cstr(shadow_file->path),
|
||||||
fx_path_get_cstr(main_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);
|
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_result fx_file_open(
|
||||||
fx_directory *root,
|
fx_directory *root,
|
||||||
const fx_path *path,
|
fx_value path,
|
||||||
enum fx_file_mode mode,
|
enum fx_file_mode mode,
|
||||||
fx_file **out)
|
fx_file **out)
|
||||||
{
|
{
|
||||||
const fx_path *file_path = path;
|
|
||||||
unsigned int flags = fx_mode_to_unix_mode(mode);
|
unsigned int flags = fx_mode_to_unix_mode(mode);
|
||||||
|
|
||||||
if (flags == (unsigned int)-1) {
|
if (flags == (unsigned int)-1) {
|
||||||
@@ -422,16 +429,14 @@ fx_result fx_file_open(
|
|||||||
if (root) {
|
if (root) {
|
||||||
root_path = fx_directory_get_path(root);
|
root_path = fx_directory_get_path(root);
|
||||||
} else {
|
} else {
|
||||||
root_path = fx_path_create_cwd();
|
root_path = fx_path_get_system(FX_PATH_CWD);
|
||||||
free_root_path = true;
|
free_root_path = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fx_path *parts[] = {
|
const fx_value *parts[] = {};
|
||||||
root_path,
|
|
||||||
file_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_path), &path);
|
||||||
|
|
||||||
if (free_root_path) {
|
if (free_root_path) {
|
||||||
fx_path_unref((fx_path *)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_result status = fx_file_open(
|
||||||
FX_DIRECTORY_ROOT,
|
FX_DIRECTORY_ROOT,
|
||||||
rpath,
|
FX_VALUE_OBJECT(rpath),
|
||||||
mode | FX_FILE_CREATE_ONLY,
|
mode | FX_FILE_CREATE_ONLY,
|
||||||
out);
|
out);
|
||||||
|
|
||||||
|
|||||||
+61
-21
@@ -6,7 +6,6 @@
|
|||||||
#include <fx/io/file.h>
|
#include <fx/io/file.h>
|
||||||
#include <fx/io/path.h>
|
#include <fx/io/path.h>
|
||||||
#include <fx/string.h>
|
#include <fx/string.h>
|
||||||
#include <fx/value.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
@@ -41,7 +40,7 @@ static bool path_is_absolute(const struct fx_path_p *path)
|
|||||||
return s[0] == '/';
|
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);
|
return fx_string_get_cstr(path->p_pathstr);
|
||||||
}
|
}
|
||||||
@@ -51,7 +50,7 @@ static enum fx_status path_stat(
|
|||||||
struct fx_file_info *out)
|
struct fx_file_info *out)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
int err = stat(path_get_cstr(path), &st);
|
int err = stat(path_ptr(path), &st);
|
||||||
|
|
||||||
if (err != 0) {
|
if (err != 0) {
|
||||||
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
|
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;
|
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_clear(dest->p_pathstr);
|
||||||
fx_string_append_s(dest->p_pathstr, src->p_pathstr);
|
fx_string_append_s(dest->p_pathstr, src);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0
|
if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0
|
||||||
&& fx_string_get_last_char(dest->p_pathstr) != '/'
|
&& fx_string_get_last_char(dest->p_pathstr) != '/'
|
||||||
&& fx_string_get_first_char(src->p_pathstr) != '/') {
|
&& fx_string_get_first_char(src) != '/') {
|
||||||
char s[] = {'/', 0};
|
fx_string_append_c(dest->p_pathstr, '/');
|
||||||
fx_string_append_cstr(dest->p_pathstr, s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
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;
|
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();
|
fx_path *result = fx_path_create();
|
||||||
if (!result) {
|
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
|
struct fx_path_p *result_p
|
||||||
= fx_object_get_private(result, FX_TYPE_PATH);
|
= fx_object_get_private(result, FX_TYPE_PATH);
|
||||||
|
|
||||||
for (size_t i = 0; i < nr_paths; i++) {
|
fx_stringstream *tmp = fx_stringstream_create();
|
||||||
if (paths[i]) {
|
fx_string *value_s = fx_string_create();
|
||||||
struct fx_path_p *path_p
|
for (size_t i = 0; i < nr_values; i++) {
|
||||||
= fx_object_get_private(paths[i], FX_TYPE_PATH);
|
if (!values[i]) {
|
||||||
append_path(result_p, path_p);
|
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;
|
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)
|
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)
|
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;
|
struct fx_path_p *path = priv;
|
||||||
|
|
||||||
@@ -417,8 +455,10 @@ static fx_status path_to_string(
|
|||||||
struct fx_path_p *path
|
struct fx_path_p *path
|
||||||
= fx_object_get_private(obj->v_object, FX_TYPE_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_stream_write_cstr(
|
||||||
return FX_SUCCESS;
|
out,
|
||||||
|
fx_string_get_cstr(path->p_pathstr),
|
||||||
|
NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** CLASS DEFINITION *********************************************************/
|
/*** CLASS DEFINITION *********************************************************/
|
||||||
|
|||||||
+4
-1
@@ -11,7 +11,10 @@ int main(int argc, const char **argv)
|
|||||||
|
|
||||||
fx_directory *dir;
|
fx_directory *dir;
|
||||||
fx_result result = fx_directory_open(
|
fx_result result = fx_directory_open(
|
||||||
NULL, FX_RV_PATH(path), FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE, &dir);
|
NULL,
|
||||||
|
FX_CSTR(path),
|
||||||
|
FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE,
|
||||||
|
&dir);
|
||||||
if (fx_result_is_error(result)) {
|
if (fx_result_is_error(result)) {
|
||||||
fx_throw(result);
|
fx_throw(result);
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
+1
-1
@@ -9,7 +9,7 @@ int main(int argc, const char **argv)
|
|||||||
const char *path = argv[1];
|
const char *path = argv[1];
|
||||||
|
|
||||||
fx_directory *dir;
|
fx_directory *dir;
|
||||||
fx_result result = fx_directory_open(NULL, FX_RV_PATH(path), 0, &dir);
|
fx_result result = fx_directory_open(NULL, FX_CSTR(path), 0, &dir);
|
||||||
if (fx_result_is_error(result)) {
|
if (fx_result_is_error(result)) {
|
||||||
fx_throw(result);
|
fx_throw(result);
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@@ -6,10 +6,9 @@
|
|||||||
int main(int argc, const char **argv)
|
int main(int argc, const char **argv)
|
||||||
{
|
{
|
||||||
fx_file *dest;
|
fx_file *dest;
|
||||||
fx_path *path = fx_path_create_from_cstr("data.txt");
|
|
||||||
fx_result result = fx_file_open(
|
fx_result result = fx_file_open(
|
||||||
NULL,
|
NULL,
|
||||||
path,
|
FX_CSTR("data.txt"),
|
||||||
FX_FILE_WRITE_ONLY | FX_FILE_CREATE,
|
FX_FILE_WRITE_ONLY | FX_FILE_CREATE,
|
||||||
&dest);
|
&dest);
|
||||||
if (fx_result_is_error(result)) {
|
if (fx_result_is_error(result)) {
|
||||||
@@ -23,7 +22,6 @@ int main(int argc, const char **argv)
|
|||||||
|
|
||||||
printf("done. read %zu bytes total.\n", nr_read);
|
printf("done. read %zu bytes total.\n", nr_read);
|
||||||
|
|
||||||
fx_path_unref(path);
|
|
||||||
fx_file_unref(dest);
|
fx_file_unref(dest);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-5
@@ -12,17 +12,19 @@ int main(int argc, const char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fx_directory *dir = NULL;
|
fx_directory *dir = NULL;
|
||||||
fx_path *path = fx_path_create_from_cstr(argv[1]);
|
fx_result result = fx_directory_open(
|
||||||
fx_result result = fx_directory_open(FX_DIRECTORY_ROOT, path, 0, &dir);
|
FX_DIRECTORY_ROOT,
|
||||||
|
FX_CSTR(argv[1]),
|
||||||
|
0,
|
||||||
|
&dir);
|
||||||
|
|
||||||
if (fx_result_is_error(result)) {
|
if (fx_result_is_error(result)) {
|
||||||
fx_throw(result);
|
fx_throw(result);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_iterator *it = fx_directory_begin(
|
fx_iterator *it
|
||||||
dir,
|
= fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
||||||
FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
|
||||||
fx_foreach(val, it)
|
fx_foreach(val, it)
|
||||||
{
|
{
|
||||||
fx_directory_entry *entry = NULL;
|
fx_directory_entry *entry = NULL;
|
||||||
|
|||||||
Reference in New Issue
Block a user