fx.io: convert to assembly build system

This commit is contained in:
2026-05-03 14:51:11 +01:00
parent 55ba15b1fa
commit cefb548824
18 changed files with 657 additions and 258 deletions
+9 -1
View File
@@ -11,7 +11,8 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(fx_all_assemblies
fx.runtime
fx.collections)
fx.collections
fx.io)
if (NOT DEFINED fx_assemblies)
set(fx_assemblies ${fx_all_assemblies})
@@ -51,4 +52,11 @@ if ("fx.collections" IN_LIST fx_assemblies)
DEPENDENCIES fx.runtime)
endif ()
if ("fx.io" IN_LIST fx_assemblies)
add_fx_assembly(
NAME fx.io
NAMESPACES fx.io
DEPENDENCIES fx.runtime)
endif ()
add_executable(dynamic-test test/dynamic-test.c)
+7
View File
@@ -0,0 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN()
FX_ASSEMBLY_NAME("fx.io");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END()
+1 -3
View File
@@ -1,3 +1 @@
include(../cmake/Templates.cmake)
add_fx_module(NAME io DEPENDENCIES core ds)
export_fx_namespace_details(fx.io)
+22 -11
View File
@@ -1,12 +1,12 @@
#ifndef FX_IO_DIRECTORY_H_
#define FX_IO_DIRECTORY_H_
#include <fx/core/error.h>
#include <fx/core/iterator.h>
#include <fx/core/macros.h>
#include <fx/core/misc.h>
#include <fx/error.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <fx/iterator.h>
#include <fx/macros.h>
#include <fx/misc.h>
#define FX_DIRECTORY_ROOT ((fx_directory *)NULL)
@@ -49,7 +49,9 @@ FX_API fx_type fx_directory_get_type(void);
FX_API fx_type fx_directory_iterator_get_type(void);
FX_API fx_result fx_directory_open(
fx_directory *root, const fx_path *path, fx_directory_open_flags flags,
fx_directory *root,
const fx_path *path,
fx_directory_open_flags flags,
fx_directory **out);
FX_API fx_result fx_directory_open_temp(fx_directory **out);
FX_API const fx_path *fx_directory_get_path(const fx_directory *dir);
@@ -58,17 +60,26 @@ 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 fx_result fx_directory_delete(fx_directory *dir);
FX_API bool fx_directory_path_exists(const fx_directory *root, 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_exists(
const fx_directory *root,
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(
const fx_directory *root, const fx_path *path);
const fx_directory *root,
const fx_path *path);
FX_API fx_result fx_directory_path_stat(
const fx_directory *root, const fx_path *path, struct fx_file_info *out);
const fx_directory *root,
const fx_path *path,
struct fx_file_info *out);
FX_API fx_result fx_directory_path_unlink(
const fx_directory *root, const fx_path *path);
const fx_directory *root,
const fx_path *path);
FX_API fx_iterator *fx_directory_begin(
fx_directory *dir, fx_directory_iterator_flags flags);
fx_directory *dir,
fx_directory_iterator_flags flags);
FX_DECLS_END;
+21 -11
View File
@@ -1,10 +1,10 @@
#ifndef FX_IO_FILE_H_
#define FX_IO_FILE_H_
#include <fx/core/error.h>
#include <fx/core/macros.h>
#include <fx/core/misc.h>
#include <fx/core/stream.h>
#include <fx/error.h>
#include <fx/macros.h>
#include <fx/misc.h>
#include <fx/stream.h>
FX_DECLS_BEGIN;
@@ -54,25 +54,35 @@ typedef struct fx_file_info {
FX_API fx_type fx_file_get_type(void);
FX_API fx_result fx_file_open(
FX_TYPE_FWDREF(fx_directory) * root, const FX_TYPE_FWDREF(fx_path) * path,
fx_file_mode mode, fx_file **out);
FX_TYPE_FWDREF(fx_directory) * root,
const FX_TYPE_FWDREF(fx_path) * path,
fx_file_mode mode,
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_shadow(
fx_file *original, fx_file_mode mode, fx_file **out);
FX_API fx_result
fx_file_open_shadow(fx_file *original, fx_file_mode mode, fx_file **out);
FX_API fx_status fx_file_stat(fx_file *file, fx_file_info *out);
FX_API fx_status fx_file_size(fx_file *file, size_t *out_len);
FX_API fx_status fx_file_cursor(fx_file *file, size_t *out_pos);
FX_API fx_status fx_file_resize(fx_file *file, size_t len);
FX_API fx_status fx_file_seek(fx_file *file, long long offset, fx_seek_basis basis);
FX_API fx_status
fx_file_seek(fx_file *file, long long offset, fx_seek_basis basis);
FX_API const FX_TYPE_FWDREF(fx_path) * fx_file_path(const fx_file *file);
FX_API fx_status fx_file_swap_shadow(fx_file *main_file, fx_file *shadow_file);
FX_API fx_status fx_file_read(
fx_file *file, size_t offset, size_t len, void *buf, size_t *nr_read);
fx_file *file,
size_t offset,
size_t len,
void *buf,
size_t *nr_read);
FX_API fx_status fx_file_write(
fx_file *file, size_t offset, size_t len, const void *buf,
fx_file *file,
size_t offset,
size_t len,
const void *buf,
size_t *nr_written);
FX_DECLS_END;
+9 -6
View File
@@ -1,10 +1,10 @@
#ifndef FX_IO_PATH_H_
#define FX_IO_PATH_H_
#include <fx/core/macros.h>
#include <fx/core/misc.h>
#include <fx/core/status.h>
#include <fx/ds/string.h>
#include <fx/macros.h>
#include <fx/misc.h>
#include <fx/status.h>
#include <fx/string.h>
#include <stddef.h>
FX_DECLS_BEGIN;
@@ -43,8 +43,11 @@ FX_API fx_status fx_path_stat(const fx_path *path, struct fx_file_info *out);
FX_API fx_status fx_path_unlink(const fx_path *path);
FX_API enum fx_status fx_path_get_directory(
const fx_path *path, fx_path **out_dir_path);
FX_API enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name);
const fx_path *path,
fx_path **out_dir_path);
FX_API enum fx_status fx_path_get_filename(
const fx_path *path,
fx_string *out_name);
FX_API const char *fx_path_ptr(const fx_path *path);
FX_API size_t fx_path_length(const fx_path *path);
+104 -36
View File
@@ -1,12 +1,12 @@
#include "misc.h"
#include "posix.h"
#include <fx/core/error.h>
#include <fx/ds/string.h>
#include <fx/io/directory.h>
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
#include <fx/error.h>
#include <fx/io/directory.h>
#include <fx/string.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
@@ -58,11 +58,14 @@ static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir)
return fx_path_ptr(dir->d_path_rel);
}
static fx_result directory_delete(fx_directory *dir, struct fx_directory_p *dir_p)
static fx_result directory_delete(
fx_directory *dir,
struct fx_directory_p *dir_p)
{
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);
}
@@ -81,7 +84,8 @@ static fx_result directory_delete(fx_directory *dir, struct fx_directory_p *dir_
}
static bool directory_path_exists(
const struct fx_directory_p *root, const fx_path *path)
const struct fx_directory_p *root,
const fx_path *path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
@@ -100,7 +104,8 @@ static bool directory_path_exists(
}
static bool directory_path_is_file(
const struct fx_directory_p *root, const fx_path *path)
const struct fx_directory_p *root,
const fx_path *path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
@@ -119,7 +124,8 @@ static bool directory_path_is_file(
}
static bool directory_path_is_directory(
const struct fx_directory_p *root, const fx_path *path)
const struct fx_directory_p *root,
const fx_path *path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
@@ -138,7 +144,8 @@ static bool directory_path_is_directory(
}
static fx_result directory_path_stat(
const struct fx_directory_p *root, const fx_path *path,
const struct fx_directory_p *root,
const fx_path *path,
struct fx_file_info *out)
{
const fx_path *parts[] = {
@@ -158,7 +165,8 @@ static fx_result directory_path_stat(
}
static fx_result directory_path_unlink(
const struct fx_directory_p *root, const fx_path *path)
const struct fx_directory_p *root,
const fx_path *path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
@@ -192,11 +200,15 @@ static fx_result create_directory(struct fx_directory_p *root, const char *path)
}
return fx_result_from_errno_with_subfilepath(
errno, path, directory_get_rel_path_cstr(root), FX_ERR_IO_FAILURE);
errno,
path,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE);
}
static fx_result create_directory_hierarchy(
struct fx_directory_p *root, const char *path)
struct fx_directory_p *root,
const char *path)
{
int root_fd = root ? root->d_fd : AT_FDCWD;
@@ -216,7 +228,9 @@ static fx_result create_directory_hierarchy(
int err = mkdirat(root_fd, path_buf, 0755);
if (err != 0 && errno != EEXIST) {
result = fx_result_from_errno_with_subfilepath(
errno, path_buf, directory_get_rel_path_cstr(root),
errno,
path_buf,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE);
break;
}
@@ -227,7 +241,9 @@ static fx_result create_directory_hierarchy(
int err = mkdirat(root_fd, path_buf, 0755);
if (err != 0 && errno != EEXIST) {
result = fx_result_from_errno_with_subfilepath(
errno, path_buf, directory_get_rel_path_cstr(root),
errno,
path_buf,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE);
}
@@ -236,8 +252,10 @@ static fx_result create_directory_hierarchy(
}
static fx_result directory_open(
struct fx_directory_p *root, const fx_path *path,
fx_directory_open_flags flags, fx_directory **out)
struct fx_directory_p *root,
const fx_path *path,
fx_directory_open_flags flags,
fx_directory **out)
{
enum fx_status status = FX_SUCCESS;
@@ -254,7 +272,9 @@ static fx_result directory_open(
if ((flags & FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE)
== FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE) {
result = create_directory_hierarchy(root, path_cstr);
} else if ((flags & FX_DIRECTORY_OPEN_CREATE) == FX_DIRECTORY_OPEN_CREATE) {
} else if (
(flags & FX_DIRECTORY_OPEN_CREATE)
== FX_DIRECTORY_OPEN_CREATE) {
result = create_directory(root, path_cstr);
}
@@ -271,7 +291,8 @@ 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();
@@ -307,11 +328,18 @@ static fx_result directory_open(
/*** PUBLIC FUNCTIONS *********************************************************/
fx_result fx_directory_open(
fx_directory *root, const fx_path *path, fx_directory_open_flags flags,
fx_directory *root,
const fx_path *path,
fx_directory_open_flags flags,
fx_directory **out)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_open, root, path, flags, out);
FX_TYPE_DIRECTORY,
directory_open,
root,
path,
flags,
out);
}
fx_result fx_directory_open_temp(fx_directory **out)
@@ -326,7 +354,10 @@ fx_result fx_directory_open_temp(fx_directory **out)
fx_directory *dir = NULL;
fx_result status = fx_directory_open(
FX_DIRECTORY_ROOT, rpath, FX_DIRECTORY_OPEN_CREATE, &dir);
FX_DIRECTORY_ROOT,
rpath,
FX_DIRECTORY_OPEN_CREATE,
&dir);
struct fx_directory_p *p
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
@@ -353,23 +384,32 @@ const fx_path *fx_directory_get_path(const fx_directory *dir)
const fx_path *fx_directory_get_rel_path(const fx_directory *dir)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_rel_path, dir);
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY,
directory_get_rel_path,
dir);
}
const char *fx_directory_get_path_cstr(const fx_directory *dir)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_path_cstr, dir);
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY,
directory_get_path_cstr,
dir);
}
const char *fx_directory_get_rel_path_cstr(const fx_directory *dir)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY, directory_get_rel_path_cstr, dir);
FX_TYPE_DIRECTORY,
directory_get_rel_path_cstr,
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);
@@ -378,31 +418,55 @@ fx_result fx_directory_delete(fx_directory *dir)
bool fx_directory_path_exists(const fx_directory *root, const fx_path *path)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DIRECTORY, directory_path_exists, root, path);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY,
directory_path_exists,
root,
path);
}
bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_is_file, root, path);
FX_TYPE_DIRECTORY,
directory_path_is_file,
root,
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,
const fx_path *path)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_is_directory, root, path);
FX_TYPE_DIRECTORY,
directory_path_is_directory,
root,
path);
}
fx_result fx_directory_path_stat(
const fx_directory *root, const fx_path *path, struct fx_file_info *out)
const fx_directory *root,
const fx_path *path,
struct fx_file_info *out)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_stat, root, path, out);
FX_TYPE_DIRECTORY,
directory_path_stat,
root,
path,
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,
const fx_path *path)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DIRECTORY, directory_path_unlink, root, path);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY,
directory_path_unlink,
root,
path);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
@@ -487,7 +551,8 @@ static void iterator_fini(fx_object *obj, void *priv)
}
fx_iterator *fx_directory_begin(
fx_directory *directory, enum fx_directory_iterator_flags flags)
fx_directory *directory,
enum fx_directory_iterator_flags flags)
{
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it
@@ -556,7 +621,9 @@ static fx_iterator *iterator_begin(fx_object *obj)
static const fx_iterator *iterator_cbegin(const fx_object *obj)
{
return fx_directory_begin((fx_object *)obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
return fx_directory_begin(
(fx_object *)obj,
FX_DIRECTORY_ITERATE_PARENT_FIRST);
}
static enum fx_status iterator_move_next(const fx_iterator *obj)
@@ -612,7 +679,8 @@ static enum fx_status iterator_erase(fx_iterator *obj)
{
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_result result
= fx_directory_path_unlink(it->root, it->entry.filepath);
if (fx_result_is_error(result)) {
enum fx_status status = fx_error_get_status_code(result);
fx_error_discard(result);
+100 -29
View File
@@ -1,13 +1,13 @@
#include "misc.h"
#include "posix.h"
#include <fx/core/random.h>
#include <fx/ds/string.h>
#include <errno.h>
#include <fcntl.h>
#include <fx/io/directory.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <errno.h>
#include <fcntl.h>
#include <fx/random.h>
#include <fx/string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -20,7 +20,10 @@ static enum fx_status stream_close(fx_stream *);
static enum fx_status stream_getc(fx_stream *, int *);
static enum fx_status stream_read(fx_stream *, void *, size_t, size_t *);
static enum fx_status stream_write(fx_stream *, const void *, size_t, size_t *);
static enum fx_status stream_seek(fx_stream *, long long, fx_stream_seek_origin);
static enum fx_status stream_seek(
fx_stream *,
long long,
fx_stream_seek_origin);
static enum fx_status stream_tell(const fx_stream *, size_t *);
/*** PRIVATE DATA *************************************************************/
@@ -63,7 +66,9 @@ static unsigned int fx_mode_to_unix_mode(enum fx_file_mode mode)
}
static fx_result file_open_shadow(
struct fx_file_p *original, enum fx_file_mode mode, fx_file **out)
struct fx_file_p *original,
enum fx_file_mode mode,
fx_file **out)
{
mode |= FX_FILE_SHADOW | FX_FILE_DELETE_ON_CLOSE | FX_FILE_CREATE;
@@ -75,7 +80,8 @@ static fx_result file_open_shadow(
fx_string_prepend_cstr(filename, ".~");
fx_path *shadow_filename = fx_path_create_from_cstr(fx_string_get_cstr(filename));
fx_path *shadow_filename
= fx_path_create_from_cstr(fx_string_get_cstr(filename));
fx_string_unref(filename);
const fx_path *parts[] = {
@@ -94,7 +100,10 @@ static fx_result file_open_shadow(
fx_file *shadow_file;
fx_result status = fx_file_open(
FX_DIRECTORY_ROOT, shadow_filepath, mode, &shadow_file);
FX_DIRECTORY_ROOT,
shadow_filepath,
mode,
&shadow_file);
fx_path_unref(shadow_filepath);
if (fx_result_is_error(status)) {
@@ -110,7 +119,9 @@ static const fx_path *file_path(const struct fx_file_p *file)
return file->path;
}
static enum fx_status file_stat(struct fx_file_p *file, struct fx_file_info *out)
static enum fx_status file_stat(
struct fx_file_p *file,
struct fx_file_info *out)
{
struct stat st;
int err = fstat(file->fd, &st);
@@ -167,7 +178,9 @@ static enum fx_status file_resize(struct fx_file_p *file, size_t len)
}
static enum fx_status file_seek(
struct fx_file_p *file, long long offset, enum fx_seek_basis basis)
struct fx_file_p *file,
long long offset,
enum fx_seek_basis basis)
{
int whence;
switch (basis) {
@@ -193,7 +206,8 @@ static enum fx_status file_seek(
}
static enum fx_status file_swap_shadow(
struct fx_file_p *main_file, struct fx_file_p *shadow_file)
struct fx_file_p *main_file,
struct fx_file_p *shadow_file)
{
if (main_file->mode & FX_FILE_SHADOW) {
return FX_ERR_NOT_SUPPORTED;
@@ -235,7 +249,9 @@ static enum fx_status file_swap_shadow(
int err;
err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path));
err = rename(fx_path_ptr(shadow_file->path), fx_path_ptr(main_file->path));
err = rename(
fx_path_ptr(shadow_file->path),
fx_path_ptr(main_file->path));
err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path));
fx_path_unref(tmp_path);
@@ -248,7 +264,11 @@ static enum fx_status file_swap_shadow(
}
static enum fx_status file_read(
struct fx_file_p *file, size_t offset, size_t len, void *buf, size_t *nr_read)
struct fx_file_p *file,
size_t offset,
size_t len,
void *buf,
size_t *nr_read)
{
if (offset != FX_OFFSET_CURRENT) {
lseek(file->fd, offset, SEEK_SET);
@@ -267,7 +287,10 @@ static enum fx_status file_read(
}
static enum fx_status file_write(
struct fx_file_p *file, size_t offset, size_t len, const void *buf,
struct fx_file_p *file,
size_t offset,
size_t len,
const void *buf,
size_t *nr_written)
{
if (offset != FX_OFFSET_CURRENT) {
@@ -314,7 +337,10 @@ static enum fx_status stream_getc(fx_stream *stream, int *out)
}
static enum fx_status stream_read(
fx_stream *stream, void *buf, size_t max, size_t *nr_read)
fx_stream *stream,
void *buf,
size_t max,
size_t *nr_read)
{
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
@@ -325,7 +351,10 @@ static enum fx_status stream_read(
}
static enum fx_status stream_write(
fx_stream *stream, const void *buf, size_t count, size_t *nr_written)
fx_stream *stream,
const void *buf,
size_t count,
size_t *nr_written)
{
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
@@ -336,7 +365,9 @@ static enum fx_status stream_write(
}
static enum fx_status stream_seek(
fx_stream *stream, long long offset, fx_stream_seek_origin origin)
fx_stream *stream,
long long offset,
fx_stream_seek_origin origin)
{
fx_seek_basis basis;
switch (origin) {
@@ -360,7 +391,8 @@ static enum fx_status stream_seek(
static enum fx_status stream_tell(const fx_stream *stream, size_t *pos)
{
const struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
const struct fx_file_p *file
= fx_object_get_private(stream, FX_TYPE_FILE);
off_t v = lseek(file->fd, 0, SEEK_CUR);
if (v == (off_t)-1) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
@@ -373,7 +405,10 @@ static enum fx_status stream_tell(const fx_stream *stream, size_t *pos)
/*** PUBLIC FUNCTIONS *********************************************************/
fx_result fx_file_open(
fx_directory *root, const fx_path *path, enum fx_file_mode mode, fx_file **out)
fx_directory *root,
const fx_path *path,
enum fx_file_mode mode,
fx_file **out)
{
const fx_path *file_path = path;
unsigned int flags = fx_mode_to_unix_mode(mode);
@@ -456,7 +491,10 @@ fx_result fx_file_open_temp(enum fx_file_mode mode, fx_file **out)
fx_path *rpath = fx_path_create_from_cstr(path);
fx_result status = fx_file_open(
FX_DIRECTORY_ROOT, rpath, mode | FX_FILE_CREATE_ONLY, out);
FX_DIRECTORY_ROOT,
rpath,
mode | FX_FILE_CREATE_ONLY,
out);
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
fx_path_unref(rpath);
@@ -470,9 +508,17 @@ fx_result fx_file_open_temp(enum fx_file_mode mode, fx_file **out)
}
}
fx_result fx_file_open_shadow(fx_file *original, enum fx_file_mode mode, fx_file **out)
fx_result fx_file_open_shadow(
fx_file *original,
enum fx_file_mode mode,
fx_file **out)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_open_shadow, original, mode, out);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE,
file_open_shadow,
original,
mode,
out);
}
const fx_path *fx_file_path(const fx_file *file)
@@ -500,30 +546,55 @@ enum fx_status fx_file_resize(fx_file *file, size_t len)
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_resize, file, len);
}
enum fx_status fx_file_seek(fx_file *file, long long offset, enum fx_seek_basis basis)
enum fx_status fx_file_seek(
fx_file *file,
long long offset,
enum fx_seek_basis basis)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_seek, file, offset, basis);
}
enum fx_status fx_file_swap_shadow(fx_file *main_file, fx_file *shadow_file)
{
struct fx_file_p *main_p = fx_object_get_private(main_file, FX_TYPE_FILE);
struct fx_file_p *shadow_p = fx_object_get_private(main_file, FX_TYPE_FILE);
struct fx_file_p *main_p
= fx_object_get_private(main_file, FX_TYPE_FILE);
struct fx_file_p *shadow_p
= fx_object_get_private(main_file, FX_TYPE_FILE);
return file_swap_shadow(main_p, shadow_p);
}
enum fx_status fx_file_read(
fx_file *file, size_t offset, size_t len, void *buf, size_t *nr_read)
fx_file *file,
size_t offset,
size_t len,
void *buf,
size_t *nr_read)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE, file_read, file, offset, len, buf, nr_read);
FX_TYPE_FILE,
file_read,
file,
offset,
len,
buf,
nr_read);
}
enum fx_status fx_file_write(
fx_file *file, size_t offset, size_t len, const void *buf, size_t *nr_written)
fx_file *file,
size_t offset,
size_t len,
const void *buf,
size_t *nr_written)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE, file_write, file, offset, len, buf, nr_written);
FX_TYPE_FILE,
file_write,
file,
offset,
len,
buf,
nr_written);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
+1 -1
View File
@@ -1,6 +1,6 @@
#include "misc.h"
#include <fx/core/random.h>
#include <fx/random.h>
void z__fx_io_generate_tmp_filename(char *out, size_t len)
{
+29 -12
View File
@@ -1,11 +1,11 @@
#include "posix.h"
#include <fx/ds/string.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <fx/string.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
@@ -45,7 +45,9 @@ static const char *path_ptr(const struct fx_path_p *path)
return fx_string_get_cstr(path->p_pathstr);
}
static enum fx_status path_stat(const struct fx_path_p *path, struct fx_file_info *out)
static enum fx_status path_stat(
const struct fx_path_p *path,
struct fx_file_info *out)
{
struct stat st;
int err = stat(path_ptr(path), &st);
@@ -110,11 +112,13 @@ static void append_path(struct fx_path_p *dest, const struct fx_path_p *src)
static enum fx_status path_unlink(const struct fx_path_p *path)
{
int err = remove(fx_string_get_cstr(path->p_pathstr));
return err == 0 ? FX_SUCCESS : fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
return err == 0 ? FX_SUCCESS
: fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
static enum fx_status path_get_directory(
const struct fx_path_p *path, fx_path **out_dir_path)
const struct fx_path_p *path,
fx_path **out_dir_path)
{
fx_string *path_str = path->p_pathstr;
long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL);
@@ -130,7 +134,8 @@ static enum fx_status path_get_directory(
size_t dir_path_len = (size_t)(sep - path_cstr);
fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len);
fx_path *dir_path = fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s));
fx_path *dir_path
= fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s));
fx_string_unref(dir_path_s);
*out_dir_path = dir_path;
@@ -139,7 +144,8 @@ static enum fx_status path_get_directory(
}
static enum fx_status path_get_filename(
const struct fx_path_p *path, fx_string *out_name)
const struct fx_path_p *path,
fx_string *out_name)
{
fx_string *path_str = path->p_pathstr;
long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL);
@@ -278,7 +284,8 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
return NULL;
}
struct fx_path_p *result_p = fx_object_get_private(result, FX_TYPE_PATH);
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]) {
@@ -336,14 +343,24 @@ enum fx_status fx_path_unlink(const fx_path *path)
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_unlink, path);
}
enum fx_status fx_path_get_directory(const fx_path *path, fx_path **out_dir_path)
enum fx_status fx_path_get_directory(
const fx_path *path,
fx_path **out_dir_path)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_get_directory, path, out_dir_path);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_PATH,
path_get_directory,
path,
out_dir_path);
}
enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_get_filename, path, out_name);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_PATH,
path_get_filename,
path,
out_name);
}
const char *fx_path_ptr(const fx_path *path)
+37 -17
View File
@@ -1,7 +1,7 @@
#include <fx/core/error.h>
#include <fx/core/status.h>
#include <fx/io/file.h>
#include <errno.h>
#include <fx/error.h>
#include <fx/io/file.h>
#include <fx/status.h>
#include <sys/stat.h>
enum fx_status fx_status_from_errno(int error, enum fx_status default_value)
@@ -35,40 +35,53 @@ enum fx_status fx_status_from_errno(int error, enum fx_status default_value)
}
fx_result fx_result_from_errno_with_filepath(
int error, const char *path, enum fx_status default_value)
int error,
const char *path,
enum fx_status default_value)
{
switch (error) {
case 0:
return FX_RESULT_SUCCESS;
case ENOENT:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NO_ENTRY, "Path @i{%s} does not exist", path);
FX_ERR_NO_ENTRY,
"Path @i{%s} does not exist",
path);
case ENOTDIR:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NOT_DIRECTORY, "Path @i{%s} is not a directory",
FX_ERR_NOT_DIRECTORY,
"Path @i{%s} is not a directory",
path);
case EISDIR:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_IS_DIRECTORY, "Path @i{%s} is a directory", path);
FX_ERR_IS_DIRECTORY,
"Path @i{%s} is a directory",
path);
case EPERM:
case EACCES:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_PERMISSION_DENIED,
"Permission denied while accessing path @i{%s}", path);
"Permission denied while accessing path @i{%s}",
path);
default:
return FX_RESULT_STATUS(fx_status_from_errno(error, default_value));
return FX_RESULT_STATUS(
fx_status_from_errno(error, default_value));
}
return FX_RESULT_SUCCESS;
}
fx_result fx_result_from_errno_with_subfilepath(
int error, const char *path, const char *dir_path,
int error,
const char *path,
const char *dir_path,
enum fx_status default_value)
{
if (!dir_path) {
return fx_result_propagate(fx_result_from_errno_with_filepath(
error, path, default_value));
error,
path,
default_value));
}
switch (error) {
@@ -77,17 +90,20 @@ fx_result fx_result_from_errno_with_subfilepath(
case ENOENT:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NO_ENTRY,
"Path @i{%s} in directory @i{%s} does not exist", path,
"Path @i{%s} in directory @i{%s} does not exist",
path,
dir_path);
case ENOTDIR:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NOT_DIRECTORY,
"Path @i{%s} in directory @i{%s} is not a directory",
path, dir_path);
path,
dir_path);
case EISDIR:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_IS_DIRECTORY,
"Path @i{%s} in directory @i{%s} is a directory", path,
"Path @i{%s} in directory @i{%s} is a directory",
path,
dir_path);
case EPERM:
case EACCES:
@@ -95,15 +111,19 @@ fx_result fx_result_from_errno_with_subfilepath(
FX_ERR_PERMISSION_DENIED,
"Permission denied while accessing path @i{%s} in "
"directory @i{%s}",
path, dir_path);
path,
dir_path);
default:
return FX_RESULT_STATUS(fx_status_from_errno(error, default_value));
return FX_RESULT_STATUS(
fx_status_from_errno(error, default_value));
}
return FX_RESULT_SUCCESS;
}
enum fx_status fx_file_info_from_stat(const struct stat *st, struct fx_file_info *out)
enum fx_status fx_file_info_from_stat(
const struct stat *st,
struct fx_file_info *out)
{
out->length = st->st_size;
+13 -6
View File
@@ -1,19 +1,26 @@
#ifndef _IO_DARWIN_POSIX_H_
#define _IO_DARWIN_POSIX_H_
#include <fx/core/error.h>
#include <fx/core/status.h>
#include <fx/error.h>
#include <fx/status.h>
struct stat;
struct fx_file_info;
extern enum fx_status fx_status_from_errno(int error, enum fx_status default_value);
extern enum fx_status fx_status_from_errno(
int error,
enum fx_status default_value);
extern fx_result fx_result_from_errno_with_filepath(
int error, const char *path, enum fx_status default_value);
int error,
const char *path,
enum fx_status default_value);
extern fx_result fx_result_from_errno_with_subfilepath(
int error, const char *path, const char *dir_path,
int error,
const char *path,
const char *dir_path,
enum fx_status default_value);
extern enum fx_status fx_file_info_from_stat(
const struct stat *in, struct fx_file_info *out);
const struct stat *in,
struct fx_file_info *out);
#endif
+98 -34
View File
@@ -6,9 +6,9 @@
#include <errno.h>
#include <fcntl.h>
#include <fts.h>
#include <fx/core/error.h>
#include <fx/ds/string.h>
#include <fx/error.h>
#include <fx/io/directory.h>
#include <fx/string.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
@@ -60,7 +60,9 @@ static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir)
return fx_path_ptr(dir->d_path_rel);
}
static fx_result directory_delete(fx_directory *dir, struct fx_directory_p *dir_p)
static fx_result directory_delete(
fx_directory *dir,
struct fx_directory_p *dir_p)
{
enum fx_status status = FX_SUCCESS;
@@ -84,7 +86,8 @@ static fx_result directory_delete(fx_directory *dir, struct fx_directory_p *dir_
}
static bool directory_path_exists(
const struct fx_directory_p *root, const fx_path *path)
const struct fx_directory_p *root,
const fx_path *path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
@@ -103,7 +106,8 @@ static bool directory_path_exists(
}
static bool directory_path_is_file(
const struct fx_directory_p *root, const fx_path *path)
const struct fx_directory_p *root,
const fx_path *path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
@@ -122,7 +126,8 @@ static bool directory_path_is_file(
}
static bool directory_path_is_directory(
const struct fx_directory_p *root, const fx_path *path)
const struct fx_directory_p *root,
const fx_path *path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
@@ -141,7 +146,8 @@ static bool directory_path_is_directory(
}
static fx_result directory_path_stat(
const struct fx_directory_p *root, const fx_path *path,
const struct fx_directory_p *root,
const fx_path *path,
struct fx_file_info *out)
{
const fx_path *parts[] = {
@@ -161,7 +167,8 @@ static fx_result directory_path_stat(
}
static fx_result directory_path_unlink(
const struct fx_directory_p *root, const fx_path *path)
const struct fx_directory_p *root,
const fx_path *path)
{
const fx_path *parts[] = {
root ? root->d_path_abs : NULL,
@@ -195,11 +202,15 @@ static fx_result create_directory(struct fx_directory_p *root, const char *path)
}
return fx_result_from_errno_with_subfilepath(
errno, path, directory_get_rel_path_cstr(root), FX_ERR_IO_FAILURE);
errno,
path,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE);
}
static fx_result create_directory_hierarchy(
struct fx_directory_p *root, const char *path)
struct fx_directory_p *root,
const char *path)
{
int root_fd = root ? root->d_fd : AT_FDCWD;
@@ -219,7 +230,9 @@ static fx_result create_directory_hierarchy(
int err = mkdirat(root_fd, path_buf, 0755);
if (err != 0 && errno != EEXIST) {
result = fx_result_from_errno_with_subfilepath(
errno, path_buf, directory_get_rel_path_cstr(root),
errno,
path_buf,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE);
break;
}
@@ -230,7 +243,9 @@ static fx_result create_directory_hierarchy(
int err = mkdirat(root_fd, path_buf, 0755);
if (err != 0 && errno != EEXIST) {
result = fx_result_from_errno_with_subfilepath(
errno, path_buf, directory_get_rel_path_cstr(root),
errno,
path_buf,
directory_get_rel_path_cstr(root),
FX_ERR_IO_FAILURE);
}
@@ -239,8 +254,10 @@ static fx_result create_directory_hierarchy(
}
static fx_result directory_open(
struct fx_directory_p *root, const fx_path *path,
fx_directory_open_flags flags, fx_directory **out)
struct fx_directory_p *root,
const fx_path *path,
fx_directory_open_flags flags,
fx_directory **out)
{
enum fx_status status = FX_SUCCESS;
@@ -257,7 +274,9 @@ static fx_result directory_open(
if ((flags & FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE)
== FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE) {
result = create_directory_hierarchy(root, path_cstr);
} else if ((flags & FX_DIRECTORY_OPEN_CREATE) == FX_DIRECTORY_OPEN_CREATE) {
} else if (
(flags & FX_DIRECTORY_OPEN_CREATE)
== FX_DIRECTORY_OPEN_CREATE) {
result = create_directory(root, path_cstr);
}
@@ -274,7 +293,8 @@ 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();
@@ -310,11 +330,18 @@ static fx_result directory_open(
/*** PUBLIC FUNCTIONS *********************************************************/
fx_result fx_directory_open(
fx_directory *root, const fx_path *path, fx_directory_open_flags flags,
fx_directory *root,
const fx_path *path,
fx_directory_open_flags flags,
fx_directory **out)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_open, root, path, flags, out);
FX_TYPE_DIRECTORY,
directory_open,
root,
path,
flags,
out);
}
fx_result fx_directory_open_temp(fx_directory **out)
@@ -329,7 +356,10 @@ fx_result fx_directory_open_temp(fx_directory **out)
fx_directory *dir = NULL;
fx_result status = fx_directory_open(
FX_DIRECTORY_ROOT, rpath, FX_DIRECTORY_OPEN_CREATE, &dir);
FX_DIRECTORY_ROOT,
rpath,
FX_DIRECTORY_OPEN_CREATE,
&dir);
struct fx_directory_p *p
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
@@ -356,23 +386,32 @@ const fx_path *fx_directory_get_path(const fx_directory *dir)
const fx_path *fx_directory_get_rel_path(const fx_directory *dir)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_rel_path, dir);
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY,
directory_get_rel_path,
dir);
}
const char *fx_directory_get_path_cstr(const fx_directory *dir)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_path_cstr, dir);
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY,
directory_get_path_cstr,
dir);
}
const char *fx_directory_get_rel_path_cstr(const fx_directory *dir)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_DIRECTORY, directory_get_rel_path_cstr, dir);
FX_TYPE_DIRECTORY,
directory_get_rel_path_cstr,
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);
@@ -382,32 +421,54 @@ fx_result fx_directory_delete(fx_directory *dir)
bool fx_directory_path_exists(const fx_directory *root, const fx_path *path)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_exists, root, path);
FX_TYPE_DIRECTORY,
directory_path_exists,
root,
path);
}
bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_is_file, root, path);
FX_TYPE_DIRECTORY,
directory_path_is_file,
root,
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,
const fx_path *path)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_is_directory, root, path);
FX_TYPE_DIRECTORY,
directory_path_is_directory,
root,
path);
}
fx_result fx_directory_path_stat(
const fx_directory *root, const fx_path *path, struct fx_file_info *out)
const fx_directory *root,
const fx_path *path,
struct fx_file_info *out)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_stat, root, path, out);
FX_TYPE_DIRECTORY,
directory_path_stat,
root,
path,
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,
const fx_path *path)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_DIRECTORY, directory_path_unlink, root, path);
FX_TYPE_DIRECTORY,
directory_path_unlink,
root,
path);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
@@ -492,7 +553,8 @@ static void iterator_fini(fx_object *obj, void *priv)
}
fx_iterator *fx_directory_begin(
fx_directory *directory, enum fx_directory_iterator_flags flags)
fx_directory *directory,
enum fx_directory_iterator_flags flags)
{
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it
@@ -562,7 +624,8 @@ static fx_iterator *iterator_begin(fx_object *obj)
static const fx_iterator *iterator_cbegin(const fx_object *obj)
{
return fx_directory_begin(
(fx_object *)obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
(fx_object *)obj,
FX_DIRECTORY_ITERATE_PARENT_FIRST);
}
static enum fx_status iterator_move_next(const fx_iterator *obj)
@@ -618,7 +681,8 @@ static enum fx_status iterator_erase(fx_iterator *obj)
{
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_result result
= fx_directory_path_unlink(it->root, it->entry.filepath);
if (fx_result_is_error(result)) {
enum fx_status status = fx_error_get_status_code(result);
fx_error_discard(result);
+100 -29
View File
@@ -3,13 +3,13 @@
#include "misc.h"
#include "posix.h"
#include <fx/core/random.h>
#include <fx/ds/string.h>
#include <errno.h>
#include <fcntl.h>
#include <fx/io/directory.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <errno.h>
#include <fcntl.h>
#include <fx/random.h>
#include <fx/string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -22,7 +22,10 @@ static enum fx_status stream_close(fx_stream *);
static enum fx_status stream_getc(fx_stream *, int *);
static enum fx_status stream_read(fx_stream *, void *, size_t, size_t *);
static enum fx_status stream_write(fx_stream *, const void *, size_t, size_t *);
static enum fx_status stream_seek(fx_stream *, long long, fx_stream_seek_origin);
static enum fx_status stream_seek(
fx_stream *,
long long,
fx_stream_seek_origin);
static enum fx_status stream_tell(const fx_stream *, size_t *);
/*** PRIVATE DATA *************************************************************/
@@ -65,7 +68,9 @@ static unsigned int fx_mode_to_unix_mode(enum fx_file_mode mode)
}
static fx_result file_open_shadow(
struct fx_file_p *original, enum fx_file_mode mode, fx_file **out)
struct fx_file_p *original,
enum fx_file_mode mode,
fx_file **out)
{
mode |= FX_FILE_SHADOW | FX_FILE_DELETE_ON_CLOSE | FX_FILE_CREATE;
@@ -77,7 +82,8 @@ static fx_result file_open_shadow(
fx_string_prepend_cstr(filename, ".~");
fx_path *shadow_filename = fx_path_create_from_cstr(fx_string_get_cstr(filename));
fx_path *shadow_filename
= fx_path_create_from_cstr(fx_string_get_cstr(filename));
fx_string_unref(filename);
const fx_path *parts[] = {
@@ -96,7 +102,10 @@ static fx_result file_open_shadow(
fx_file *shadow_file;
fx_result status = fx_file_open(
FX_DIRECTORY_ROOT, shadow_filepath, mode, &shadow_file);
FX_DIRECTORY_ROOT,
shadow_filepath,
mode,
&shadow_file);
fx_path_unref(shadow_filepath);
if (fx_result_is_error(status)) {
@@ -112,7 +121,9 @@ static const fx_path *file_path(const struct fx_file_p *file)
return file->path;
}
static enum fx_status file_stat(struct fx_file_p *file, struct fx_file_info *out)
static enum fx_status file_stat(
struct fx_file_p *file,
struct fx_file_info *out)
{
struct stat st;
int err = fstat(file->fd, &st);
@@ -169,7 +180,9 @@ static enum fx_status file_resize(struct fx_file_p *file, size_t len)
}
static enum fx_status file_seek(
struct fx_file_p *file, long long offset, enum fx_seek_basis basis)
struct fx_file_p *file,
long long offset,
enum fx_seek_basis basis)
{
int whence;
switch (basis) {
@@ -195,7 +208,8 @@ static enum fx_status file_seek(
}
static enum fx_status file_swap_shadow(
struct fx_file_p *main_file, struct fx_file_p *shadow_file)
struct fx_file_p *main_file,
struct fx_file_p *shadow_file)
{
if (main_file->mode & FX_FILE_SHADOW) {
return FX_ERR_NOT_SUPPORTED;
@@ -237,7 +251,9 @@ static enum fx_status file_swap_shadow(
int err;
err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path));
err = rename(fx_path_ptr(shadow_file->path), fx_path_ptr(main_file->path));
err = rename(
fx_path_ptr(shadow_file->path),
fx_path_ptr(main_file->path));
err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path));
fx_path_unref(tmp_path);
@@ -250,7 +266,11 @@ static enum fx_status file_swap_shadow(
}
static enum fx_status file_read(
struct fx_file_p *file, size_t offset, size_t len, void *buf, size_t *nr_read)
struct fx_file_p *file,
size_t offset,
size_t len,
void *buf,
size_t *nr_read)
{
if (offset != FX_OFFSET_CURRENT) {
lseek(file->fd, offset, SEEK_SET);
@@ -269,7 +289,10 @@ static enum fx_status file_read(
}
static enum fx_status file_write(
struct fx_file_p *file, size_t offset, size_t len, const void *buf,
struct fx_file_p *file,
size_t offset,
size_t len,
const void *buf,
size_t *nr_written)
{
if (offset != FX_OFFSET_CURRENT) {
@@ -316,7 +339,10 @@ static enum fx_status stream_getc(fx_stream *stream, int *out)
}
static enum fx_status stream_read(
fx_stream *stream, void *buf, size_t max, size_t *nr_read)
fx_stream *stream,
void *buf,
size_t max,
size_t *nr_read)
{
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
@@ -327,7 +353,10 @@ static enum fx_status stream_read(
}
static enum fx_status stream_write(
fx_stream *stream, const void *buf, size_t count, size_t *nr_written)
fx_stream *stream,
const void *buf,
size_t count,
size_t *nr_written)
{
struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
@@ -338,7 +367,9 @@ static enum fx_status stream_write(
}
static enum fx_status stream_seek(
fx_stream *stream, long long offset, fx_stream_seek_origin origin)
fx_stream *stream,
long long offset,
fx_stream_seek_origin origin)
{
fx_seek_basis basis;
switch (origin) {
@@ -362,7 +393,8 @@ static enum fx_status stream_seek(
static enum fx_status stream_tell(const fx_stream *stream, size_t *pos)
{
const struct fx_file_p *file = fx_object_get_private(stream, FX_TYPE_FILE);
const struct fx_file_p *file
= fx_object_get_private(stream, FX_TYPE_FILE);
off_t v = lseek(file->fd, 0, SEEK_CUR);
if (v == (off_t)-1) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
@@ -375,7 +407,10 @@ static enum fx_status stream_tell(const fx_stream *stream, size_t *pos)
/*** PUBLIC FUNCTIONS *********************************************************/
fx_result fx_file_open(
fx_directory *root, const fx_path *path, enum fx_file_mode mode, fx_file **out)
fx_directory *root,
const fx_path *path,
enum fx_file_mode mode,
fx_file **out)
{
const fx_path *file_path = path;
unsigned int flags = fx_mode_to_unix_mode(mode);
@@ -458,7 +493,10 @@ fx_result fx_file_open_temp(enum fx_file_mode mode, fx_file **out)
fx_path *rpath = fx_path_create_from_cstr(path);
fx_result status = fx_file_open(
FX_DIRECTORY_ROOT, rpath, mode | FX_FILE_CREATE_ONLY, out);
FX_DIRECTORY_ROOT,
rpath,
mode | FX_FILE_CREATE_ONLY,
out);
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
fx_path_unref(rpath);
@@ -472,9 +510,17 @@ fx_result fx_file_open_temp(enum fx_file_mode mode, fx_file **out)
}
}
fx_result fx_file_open_shadow(fx_file *original, enum fx_file_mode mode, fx_file **out)
fx_result fx_file_open_shadow(
fx_file *original,
enum fx_file_mode mode,
fx_file **out)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_open_shadow, original, mode, out);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE,
file_open_shadow,
original,
mode,
out);
}
const fx_path *fx_file_path(const fx_file *file)
@@ -502,30 +548,55 @@ enum fx_status fx_file_resize(fx_file *file, size_t len)
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_resize, file, len);
}
enum fx_status fx_file_seek(fx_file *file, long long offset, enum fx_seek_basis basis)
enum fx_status fx_file_seek(
fx_file *file,
long long offset,
enum fx_seek_basis basis)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_FILE, file_seek, file, offset, basis);
}
enum fx_status fx_file_swap_shadow(fx_file *main_file, fx_file *shadow_file)
{
struct fx_file_p *main_p = fx_object_get_private(main_file, FX_TYPE_FILE);
struct fx_file_p *shadow_p = fx_object_get_private(main_file, FX_TYPE_FILE);
struct fx_file_p *main_p
= fx_object_get_private(main_file, FX_TYPE_FILE);
struct fx_file_p *shadow_p
= fx_object_get_private(main_file, FX_TYPE_FILE);
return file_swap_shadow(main_p, shadow_p);
}
enum fx_status fx_file_read(
fx_file *file, size_t offset, size_t len, void *buf, size_t *nr_read)
fx_file *file,
size_t offset,
size_t len,
void *buf,
size_t *nr_read)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE, file_read, file, offset, len, buf, nr_read);
FX_TYPE_FILE,
file_read,
file,
offset,
len,
buf,
nr_read);
}
enum fx_status fx_file_write(
fx_file *file, size_t offset, size_t len, const void *buf, size_t *nr_written)
fx_file *file,
size_t offset,
size_t len,
const void *buf,
size_t *nr_written)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_FILE, file_write, file, offset, len, buf, nr_written);
FX_TYPE_FILE,
file_write,
file,
offset,
len,
buf,
nr_written);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
+1 -1
View File
@@ -1,6 +1,6 @@
#include "misc.h"
#include <fx/core/random.h>
#include <fx/random.h>
void z__fx_io_generate_tmp_filename(char *out, size_t len)
{
+29 -12
View File
@@ -1,11 +1,11 @@
#include "posix.h"
#include <fx/ds/string.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <fx/string.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
@@ -45,7 +45,9 @@ static const char *path_ptr(const struct fx_path_p *path)
return fx_string_get_cstr(path->p_pathstr);
}
static enum fx_status path_stat(const struct fx_path_p *path, struct fx_file_info *out)
static enum fx_status path_stat(
const struct fx_path_p *path,
struct fx_file_info *out)
{
struct stat st;
int err = stat(path_ptr(path), &st);
@@ -110,11 +112,13 @@ static void append_path(struct fx_path_p *dest, const struct fx_path_p *src)
static enum fx_status path_unlink(const struct fx_path_p *path)
{
int err = remove(fx_string_get_cstr(path->p_pathstr));
return err == 0 ? FX_SUCCESS : fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
return err == 0 ? FX_SUCCESS
: fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
static enum fx_status path_get_directory(
const struct fx_path_p *path, fx_path **out_dir_path)
const struct fx_path_p *path,
fx_path **out_dir_path)
{
fx_string *path_str = path->p_pathstr;
long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL);
@@ -130,7 +134,8 @@ static enum fx_status path_get_directory(
size_t dir_path_len = (size_t)(sep - path_cstr);
fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len);
fx_path *dir_path = fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s));
fx_path *dir_path
= fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s));
fx_string_unref(dir_path_s);
*out_dir_path = dir_path;
@@ -139,7 +144,8 @@ static enum fx_status path_get_directory(
}
static enum fx_status path_get_filename(
const struct fx_path_p *path, fx_string *out_name)
const struct fx_path_p *path,
fx_string *out_name)
{
fx_string *path_str = path->p_pathstr;
long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL);
@@ -278,7 +284,8 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
return NULL;
}
struct fx_path_p *result_p = fx_object_get_private(result, FX_TYPE_PATH);
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]) {
@@ -336,14 +343,24 @@ enum fx_status fx_path_unlink(const fx_path *path)
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_unlink, path);
}
enum fx_status fx_path_get_directory(const fx_path *path, fx_path **out_dir_path)
enum fx_status fx_path_get_directory(
const fx_path *path,
fx_path **out_dir_path)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_get_directory, path, out_dir_path);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_PATH,
path_get_directory,
path,
out_dir_path);
}
enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_get_filename, path, out_name);
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_PATH,
path_get_filename,
path,
out_name);
}
const char *fx_path_ptr(const fx_path *path)
+37 -17
View File
@@ -1,7 +1,7 @@
#include <fx/core/error.h>
#include <fx/core/status.h>
#include <fx/io/file.h>
#include <errno.h>
#include <fx/error.h>
#include <fx/io/file.h>
#include <fx/status.h>
#include <sys/stat.h>
enum fx_status fx_status_from_errno(int error, enum fx_status default_value)
@@ -35,40 +35,53 @@ enum fx_status fx_status_from_errno(int error, enum fx_status default_value)
}
fx_result fx_result_from_errno_with_filepath(
int error, const char *path, enum fx_status default_value)
int error,
const char *path,
enum fx_status default_value)
{
switch (error) {
case 0:
return FX_RESULT_SUCCESS;
case ENOENT:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NO_ENTRY, "Path @i{%s} does not exist", path);
FX_ERR_NO_ENTRY,
"Path @i{%s} does not exist",
path);
case ENOTDIR:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NOT_DIRECTORY, "Path @i{%s} is not a directory",
FX_ERR_NOT_DIRECTORY,
"Path @i{%s} is not a directory",
path);
case EISDIR:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_IS_DIRECTORY, "Path @i{%s} is a directory", path);
FX_ERR_IS_DIRECTORY,
"Path @i{%s} is a directory",
path);
case EPERM:
case EACCES:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_PERMISSION_DENIED,
"Permission denied while accessing path @i{%s}", path);
"Permission denied while accessing path @i{%s}",
path);
default:
return FX_RESULT_STATUS(fx_status_from_errno(error, default_value));
return FX_RESULT_STATUS(
fx_status_from_errno(error, default_value));
}
return FX_RESULT_SUCCESS;
}
fx_result fx_result_from_errno_with_subfilepath(
int error, const char *path, const char *dir_path,
int error,
const char *path,
const char *dir_path,
enum fx_status default_value)
{
if (!dir_path) {
return fx_result_propagate(fx_result_from_errno_with_filepath(
error, path, default_value));
error,
path,
default_value));
}
switch (error) {
@@ -77,17 +90,20 @@ fx_result fx_result_from_errno_with_subfilepath(
case ENOENT:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NO_ENTRY,
"Path @i{%s} in directory @i{%s} does not exist", path,
"Path @i{%s} in directory @i{%s} does not exist",
path,
dir_path);
case ENOTDIR:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_NOT_DIRECTORY,
"Path @i{%s} in directory @i{%s} is not a directory",
path, dir_path);
path,
dir_path);
case EISDIR:
return FX_RESULT_STATUS_WITH_STRING(
FX_ERR_IS_DIRECTORY,
"Path @i{%s} in directory @i{%s} is a directory", path,
"Path @i{%s} in directory @i{%s} is a directory",
path,
dir_path);
case EPERM:
case EACCES:
@@ -95,15 +111,19 @@ fx_result fx_result_from_errno_with_subfilepath(
FX_ERR_PERMISSION_DENIED,
"Permission denied while accessing path @i{%s} in "
"directory @i{%s}",
path, dir_path);
path,
dir_path);
default:
return FX_RESULT_STATUS(fx_status_from_errno(error, default_value));
return FX_RESULT_STATUS(
fx_status_from_errno(error, default_value));
}
return FX_RESULT_SUCCESS;
}
enum fx_status fx_file_info_from_stat(const struct stat *st, struct fx_file_info *out)
enum fx_status fx_file_info_from_stat(
const struct stat *st,
struct fx_file_info *out)
{
out->length = st->st_size;
+13 -6
View File
@@ -1,19 +1,26 @@
#ifndef _IO_DARWIN_POSIX_H_
#define _IO_DARWIN_POSIX_H_
#include <fx/core/error.h>
#include <fx/core/status.h>
#include <fx/error.h>
#include <fx/status.h>
struct stat;
struct fx_file_info;
extern enum fx_status fx_status_from_errno(int error, enum fx_status default_value);
extern enum fx_status fx_status_from_errno(
int error,
enum fx_status default_value);
extern fx_result fx_result_from_errno_with_filepath(
int error, const char *path, enum fx_status default_value);
int error,
const char *path,
enum fx_status default_value);
extern fx_result fx_result_from_errno_with_subfilepath(
int error, const char *path, const char *dir_path,
int error,
const char *path,
const char *dir_path,
enum fx_status default_value);
extern enum fx_status fx_file_info_from_stat(
const struct stat *in, struct fx_file_info *out);
const struct stat *in,
struct fx_file_info *out);
#endif