meta: rename to fx
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
#include "misc.h"
|
||||
#include "posix.h"
|
||||
|
||||
#include <blue/core/error.h>
|
||||
#include <blue/ds/string.h>
|
||||
#include <blue/io/directory.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>
|
||||
@@ -18,165 +18,165 @@ enum directory_flags {
|
||||
DIRECTORY_DELETE_ON_CLOSE = 0x01u,
|
||||
};
|
||||
|
||||
struct b_directory_p {
|
||||
struct fx_directory_p {
|
||||
enum directory_flags d_flags;
|
||||
int d_fd;
|
||||
b_path *d_path_rel;
|
||||
b_path *d_path_abs;
|
||||
fx_path *d_path_rel;
|
||||
fx_path *d_path_abs;
|
||||
};
|
||||
|
||||
struct b_directory_iterator_p {
|
||||
struct b_directory_p *_p;
|
||||
struct fx_directory_iterator_p {
|
||||
struct fx_directory_p *_p;
|
||||
|
||||
FTS *fts;
|
||||
FTSENT *ent;
|
||||
|
||||
b_directory_iterator_flags flags;
|
||||
b_directory *root;
|
||||
fx_directory_iterator_flags flags;
|
||||
fx_directory *root;
|
||||
|
||||
b_directory_entry entry;
|
||||
fx_directory_entry entry;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
static const b_path *directory_get_path(const struct b_directory_p *dir)
|
||||
static const fx_path *directory_get_path(const struct fx_directory_p *dir)
|
||||
{
|
||||
return dir->d_path_abs;
|
||||
}
|
||||
|
||||
static const b_path *directory_get_rel_path(const struct b_directory_p *dir)
|
||||
static const fx_path *directory_get_rel_path(const struct fx_directory_p *dir)
|
||||
{
|
||||
return dir->d_path_rel;
|
||||
}
|
||||
|
||||
static const char *directory_get_path_cstr(const struct b_directory_p *dir)
|
||||
static const char *directory_get_path_cstr(const struct fx_directory_p *dir)
|
||||
{
|
||||
return b_path_ptr(dir->d_path_abs);
|
||||
return fx_path_ptr(dir->d_path_abs);
|
||||
}
|
||||
|
||||
static const char *directory_get_rel_path_cstr(const struct b_directory_p *dir)
|
||||
static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir)
|
||||
{
|
||||
return b_path_ptr(dir->d_path_rel);
|
||||
return fx_path_ptr(dir->d_path_rel);
|
||||
}
|
||||
|
||||
static b_result directory_delete(b_directory *dir, struct b_directory_p *dir_p)
|
||||
static fx_result directory_delete(fx_directory *dir, struct fx_directory_p *dir_p)
|
||||
{
|
||||
enum b_status status = B_SUCCESS;
|
||||
enum fx_status status = FX_SUCCESS;
|
||||
|
||||
b_iterator *it = b_directory_begin(dir, B_DIRECTORY_ITERATE_PARENT_LAST);
|
||||
while (B_OK(b_iterator_get_status(it))) {
|
||||
b_iterator_erase(it);
|
||||
fx_iterator *it = fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_LAST);
|
||||
while (FX_OK(fx_iterator_get_status(it))) {
|
||||
fx_iterator_erase(it);
|
||||
}
|
||||
|
||||
status = b_iterator_get_status(it);
|
||||
if (!B_OK(status) && status != B_ERR_NO_DATA) {
|
||||
return B_RESULT_STATUS(status);
|
||||
status = fx_iterator_get_status(it);
|
||||
if (!FX_OK(status) && status != FX_ERR_NO_DATA) {
|
||||
return FX_RESULT_STATUS(status);
|
||||
}
|
||||
|
||||
status = b_path_unlink(dir_p->d_path_abs);
|
||||
if (!B_OK(status)) {
|
||||
return B_RESULT_STATUS(status);
|
||||
status = fx_path_unlink(dir_p->d_path_abs);
|
||||
if (!FX_OK(status)) {
|
||||
return FX_RESULT_STATUS(status);
|
||||
}
|
||||
|
||||
return B_RESULT_SUCCESS;
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
static bool directory_path_exists(
|
||||
const struct b_directory_p *root, const b_path *path)
|
||||
const struct fx_directory_p *root, const fx_path *path)
|
||||
{
|
||||
const b_path *parts[] = {
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
};
|
||||
|
||||
b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool result = b_path_exists(abs_path);
|
||||
b_path_unref(abs_path);
|
||||
bool result = fx_path_exists(abs_path);
|
||||
fx_path_unref(abs_path);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool directory_path_is_file(
|
||||
const struct b_directory_p *root, const b_path *path)
|
||||
const struct fx_directory_p *root, const fx_path *path)
|
||||
{
|
||||
const b_path *parts[] = {
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
};
|
||||
|
||||
b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool result = b_path_is_file(abs_path);
|
||||
b_path_unref(abs_path);
|
||||
bool result = fx_path_is_file(abs_path);
|
||||
fx_path_unref(abs_path);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool directory_path_is_directory(
|
||||
const struct b_directory_p *root, const b_path *path)
|
||||
const struct fx_directory_p *root, const fx_path *path)
|
||||
{
|
||||
const b_path *parts[] = {
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
};
|
||||
|
||||
b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool result = b_path_is_directory(abs_path);
|
||||
b_path_unref(abs_path);
|
||||
bool result = fx_path_is_directory(abs_path);
|
||||
fx_path_unref(abs_path);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static b_result directory_path_stat(
|
||||
const struct b_directory_p *root, const b_path *path,
|
||||
struct b_file_info *out)
|
||||
static fx_result directory_path_stat(
|
||||
const struct fx_directory_p *root, const fx_path *path,
|
||||
struct fx_file_info *out)
|
||||
{
|
||||
const b_path *parts[] = {
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
};
|
||||
|
||||
b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return B_RESULT_ERR(NO_MEMORY);
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
enum b_status status = b_path_stat(abs_path, out);
|
||||
b_path_unref(abs_path);
|
||||
enum fx_status status = fx_path_stat(abs_path, out);
|
||||
fx_path_unref(abs_path);
|
||||
|
||||
return B_RESULT_STATUS(status);
|
||||
return FX_RESULT_STATUS(status);
|
||||
}
|
||||
|
||||
static b_result directory_path_unlink(
|
||||
const struct b_directory_p *root, const b_path *path)
|
||||
static fx_result directory_path_unlink(
|
||||
const struct fx_directory_p *root, const fx_path *path)
|
||||
{
|
||||
const b_path *parts[] = {
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : NULL,
|
||||
path,
|
||||
};
|
||||
|
||||
b_path *abs_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return B_RESULT_ERR(NO_MEMORY);
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
enum b_status status = b_path_unlink(abs_path);
|
||||
b_path_unref(abs_path);
|
||||
enum fx_status status = fx_path_unlink(abs_path);
|
||||
fx_path_unref(abs_path);
|
||||
|
||||
return B_RESULT_STATUS(status);
|
||||
return FX_RESULT_STATUS(status);
|
||||
}
|
||||
|
||||
static b_result create_directory(struct b_directory_p *root, const char *path)
|
||||
static fx_result create_directory(struct fx_directory_p *root, const char *path)
|
||||
{
|
||||
int root_fd = root ? root->d_fd : -1;
|
||||
int err;
|
||||
@@ -188,24 +188,24 @@ static b_result create_directory(struct b_directory_p *root, const char *path)
|
||||
}
|
||||
|
||||
if (err == 0 || errno == EEXIST) {
|
||||
return B_RESULT_SUCCESS;
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
return b_result_from_errno_with_subfilepath(
|
||||
errno, path, directory_get_rel_path_cstr(root), B_ERR_IO_FAILURE);
|
||||
return fx_result_from_errno_with_subfilepath(
|
||||
errno, path, directory_get_rel_path_cstr(root), FX_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
static b_result create_directory_hierarchy(
|
||||
struct b_directory_p *root, const char *path)
|
||||
static fx_result create_directory_hierarchy(
|
||||
struct fx_directory_p *root, const char *path)
|
||||
{
|
||||
int root_fd = root ? root->d_fd : AT_FDCWD;
|
||||
|
||||
char *path_buf = b_strdup(path);
|
||||
char *path_buf = fx_strdup(path);
|
||||
if (!path_buf) {
|
||||
return B_RESULT_ERR(NO_MEMORY);
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
b_result result = B_RESULT_SUCCESS;
|
||||
fx_result result = FX_RESULT_SUCCESS;
|
||||
for (size_t i = 0; path_buf[i]; i++) {
|
||||
if (path_buf[i] != '/') {
|
||||
continue;
|
||||
@@ -215,9 +215,9 @@ static b_result create_directory_hierarchy(
|
||||
|
||||
int err = mkdirat(root_fd, path_buf, 0755);
|
||||
if (err != 0 && errno != EEXIST) {
|
||||
result = b_result_from_errno_with_subfilepath(
|
||||
result = fx_result_from_errno_with_subfilepath(
|
||||
errno, path_buf, directory_get_rel_path_cstr(root),
|
||||
B_ERR_IO_FAILURE);
|
||||
FX_ERR_IO_FAILURE);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -226,112 +226,112 @@ static b_result create_directory_hierarchy(
|
||||
|
||||
int err = mkdirat(root_fd, path_buf, 0755);
|
||||
if (err != 0 && errno != EEXIST) {
|
||||
result = b_result_from_errno_with_subfilepath(
|
||||
result = fx_result_from_errno_with_subfilepath(
|
||||
errno, path_buf, directory_get_rel_path_cstr(root),
|
||||
B_ERR_IO_FAILURE);
|
||||
FX_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
free(path_buf);
|
||||
return result;
|
||||
}
|
||||
|
||||
static b_result directory_open(
|
||||
struct b_directory_p *root, const b_path *path,
|
||||
b_directory_open_flags flags, b_directory **out)
|
||||
static fx_result directory_open(
|
||||
struct fx_directory_p *root, const fx_path *path,
|
||||
fx_directory_open_flags flags, fx_directory **out)
|
||||
{
|
||||
enum b_status status = B_SUCCESS;
|
||||
enum fx_status status = FX_SUCCESS;
|
||||
|
||||
int root_fd = root ? root->d_fd : AT_FDCWD;
|
||||
|
||||
const char *path_cstr = b_path_ptr(path);
|
||||
const char *path_cstr = fx_path_ptr(path);
|
||||
if (root) {
|
||||
while (*path_cstr == '/') {
|
||||
path_cstr++;
|
||||
}
|
||||
}
|
||||
|
||||
b_result result = B_RESULT_SUCCESS;
|
||||
if ((flags & B_DIRECTORY_OPEN_CREATE_INTERMEDIATE)
|
||||
== B_DIRECTORY_OPEN_CREATE_INTERMEDIATE) {
|
||||
fx_result result = FX_RESULT_SUCCESS;
|
||||
if ((flags & FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE)
|
||||
== FX_DIRECTORY_OPEN_CREATE_INTERMEDIATE) {
|
||||
result = create_directory_hierarchy(root, path_cstr);
|
||||
} else if ((flags & B_DIRECTORY_OPEN_CREATE) == B_DIRECTORY_OPEN_CREATE) {
|
||||
} else if ((flags & FX_DIRECTORY_OPEN_CREATE) == FX_DIRECTORY_OPEN_CREATE) {
|
||||
result = create_directory(root, path_cstr);
|
||||
}
|
||||
|
||||
if (b_result_is_error(result)) {
|
||||
return b_result_propagate(result);
|
||||
if (fx_result_is_error(result)) {
|
||||
return fx_result_propagate(result);
|
||||
}
|
||||
|
||||
int fd = openat(root_fd, path_cstr, O_DIRECTORY);
|
||||
|
||||
if (fd == -1) {
|
||||
status = b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
||||
return B_RESULT_STATUS(status);
|
||||
status = fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
|
||||
return FX_RESULT_STATUS(status);
|
||||
}
|
||||
|
||||
b_directory *dir = b_object_create(B_TYPE_DIRECTORY);
|
||||
b_path *cwd = NULL;
|
||||
struct b_directory_p *p = b_object_get_private(dir, B_TYPE_DIRECTORY);
|
||||
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);
|
||||
|
||||
if (!root) {
|
||||
cwd = b_path_create_cwd();
|
||||
cwd = fx_path_create_cwd();
|
||||
}
|
||||
|
||||
const b_path *parts[] = {
|
||||
const fx_path *parts[] = {
|
||||
root ? root->d_path_abs : cwd,
|
||||
path,
|
||||
};
|
||||
|
||||
b_path *new_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
fx_path *new_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!new_path) {
|
||||
return B_RESULT_ERR(NO_MEMORY);
|
||||
return FX_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
if (cwd) {
|
||||
b_path_unref(cwd);
|
||||
fx_path_unref(cwd);
|
||||
}
|
||||
|
||||
p->d_path_abs = new_path;
|
||||
p->d_path_rel = b_path_duplicate(path);
|
||||
p->d_path_rel = fx_path_duplicate(path);
|
||||
p->d_fd = fd;
|
||||
|
||||
if (flags & B_DIRECTORY_OPEN_DELETE_ON_CLOSE) {
|
||||
if (flags & FX_DIRECTORY_OPEN_DELETE_ON_CLOSE) {
|
||||
p->d_flags = DIRECTORY_DELETE_ON_CLOSE;
|
||||
}
|
||||
|
||||
*out = dir;
|
||||
|
||||
return B_RESULT_SUCCESS;
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
b_result b_directory_open(
|
||||
b_directory *root, const b_path *path, b_directory_open_flags flags,
|
||||
b_directory **out)
|
||||
fx_result fx_directory_open(
|
||||
fx_directory *root, const fx_path *path, fx_directory_open_flags flags,
|
||||
fx_directory **out)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC(
|
||||
B_TYPE_DIRECTORY, directory_open, root, path, flags, out);
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_DIRECTORY, directory_open, root, path, flags, out);
|
||||
}
|
||||
|
||||
b_result b_directory_open_temp(b_directory **out)
|
||||
fx_result fx_directory_open_temp(fx_directory **out)
|
||||
{
|
||||
char name[16];
|
||||
char path[128];
|
||||
|
||||
while (1) {
|
||||
z__b_io_generate_tmp_filename(name, sizeof name);
|
||||
z__fx_io_generate_tmp_filename(name, sizeof name);
|
||||
snprintf(path, sizeof path, "/tmp/%s", name);
|
||||
b_path *rpath = b_path_create_from_cstr(path);
|
||||
fx_path *rpath = fx_path_create_from_cstr(path);
|
||||
|
||||
b_directory *dir = NULL;
|
||||
b_result status = b_directory_open(
|
||||
B_DIRECTORY_ROOT, rpath, B_DIRECTORY_OPEN_CREATE, &dir);
|
||||
struct b_directory_p *p
|
||||
= b_object_get_private(dir, B_TYPE_DIRECTORY);
|
||||
fx_directory *dir = NULL;
|
||||
fx_result status = fx_directory_open(
|
||||
FX_DIRECTORY_ROOT, rpath, FX_DIRECTORY_OPEN_CREATE, &dir);
|
||||
struct fx_directory_p *p
|
||||
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
|
||||
|
||||
if (b_error_get_status_code(status) == B_ERR_NAME_EXISTS) {
|
||||
b_path_unref(rpath);
|
||||
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
|
||||
fx_path_unref(rpath);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -339,82 +339,82 @@ b_result b_directory_open_temp(b_directory **out)
|
||||
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
|
||||
}
|
||||
|
||||
b_path_unlink(rpath);
|
||||
b_path_unref(rpath);
|
||||
fx_path_unlink(rpath);
|
||||
fx_path_unref(rpath);
|
||||
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
const b_path *b_directory_get_path(const b_directory *dir)
|
||||
const fx_path *fx_directory_get_path(const fx_directory *dir)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_DIRECTORY, directory_get_path, dir);
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_path, dir);
|
||||
}
|
||||
|
||||
const b_path *b_directory_get_rel_path(const b_directory *dir)
|
||||
const fx_path *fx_directory_get_rel_path(const fx_directory *dir)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_DIRECTORY, directory_get_rel_path, dir);
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_rel_path, dir);
|
||||
}
|
||||
|
||||
const char *b_directory_get_path_cstr(const b_directory *dir)
|
||||
const char *fx_directory_get_path_cstr(const fx_directory *dir)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_DIRECTORY, directory_get_path_cstr, dir);
|
||||
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DIRECTORY, directory_get_path_cstr, dir);
|
||||
}
|
||||
|
||||
const char *b_directory_get_rel_path_cstr(const b_directory *dir)
|
||||
const char *fx_directory_get_rel_path_cstr(const fx_directory *dir)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC_0(
|
||||
B_TYPE_DIRECTORY, directory_get_rel_path_cstr, dir);
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_TYPE_DIRECTORY, directory_get_rel_path_cstr, dir);
|
||||
}
|
||||
|
||||
b_result b_directory_delete(b_directory *dir)
|
||||
fx_result fx_directory_delete(fx_directory *dir)
|
||||
{
|
||||
struct b_directory_p *p = b_object_get_private(dir, B_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 b_result value */
|
||||
b_directory_unref(dir);
|
||||
return B_RESULT_SUCCESS;
|
||||
/* TODO allow object release functions to return a fx_result value */
|
||||
fx_directory_unref(dir);
|
||||
return FX_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
bool b_directory_path_exists(const b_directory *root, const b_path *path)
|
||||
bool fx_directory_path_exists(const fx_directory *root, const fx_path *path)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_DIRECTORY, directory_path_exists, root, path);
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DIRECTORY, directory_path_exists, root, path);
|
||||
}
|
||||
|
||||
bool b_directory_path_is_file(const b_directory *root, const b_path *path)
|
||||
bool fx_directory_path_is_file(const fx_directory *root, const fx_path *path)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC(
|
||||
B_TYPE_DIRECTORY, directory_path_is_file, root, path);
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_DIRECTORY, directory_path_is_file, root, path);
|
||||
}
|
||||
|
||||
bool b_directory_path_is_directory(const b_directory *root, const b_path *path)
|
||||
bool fx_directory_path_is_directory(const fx_directory *root, const fx_path *path)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC(
|
||||
B_TYPE_DIRECTORY, directory_path_is_directory, root, path);
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_DIRECTORY, directory_path_is_directory, root, path);
|
||||
}
|
||||
|
||||
b_result b_directory_path_stat(
|
||||
const b_directory *root, const b_path *path, struct b_file_info *out)
|
||||
fx_result fx_directory_path_stat(
|
||||
const fx_directory *root, const fx_path *path, struct fx_file_info *out)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC(
|
||||
B_TYPE_DIRECTORY, directory_path_stat, root, path, out);
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_DIRECTORY, directory_path_stat, root, path, out);
|
||||
}
|
||||
|
||||
b_result b_directory_path_unlink(const b_directory *root, const b_path *path)
|
||||
fx_result fx_directory_path_unlink(const fx_directory *root, const fx_path *path)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_DIRECTORY, directory_path_unlink, root, path);
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DIRECTORY, directory_path_unlink, root, path);
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void directory_init(b_object *obj, void *priv)
|
||||
static void directory_init(fx_object *obj, void *priv)
|
||||
{
|
||||
struct b_directory_p *dir = priv;
|
||||
struct fx_directory_p *dir = priv;
|
||||
}
|
||||
|
||||
static void directory_fini(b_object *obj, void *priv)
|
||||
static void directory_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
struct b_directory_p *dir = priv;
|
||||
struct fx_directory_p *dir = priv;
|
||||
|
||||
close(dir->d_fd);
|
||||
|
||||
@@ -422,7 +422,7 @@ static void directory_fini(b_object *obj, void *priv)
|
||||
directory_delete(obj, dir);
|
||||
}
|
||||
|
||||
b_path_unref(dir->d_path_abs);
|
||||
fx_path_unref(dir->d_path_abs);
|
||||
}
|
||||
|
||||
/*** ITERATOR FUNCTIONS *******************************************************/
|
||||
@@ -432,17 +432,17 @@ static int ftsent_compare(const FTSENT **one, const FTSENT **two)
|
||||
return (strcmp((*one)->fts_name, (*two)->fts_name));
|
||||
}
|
||||
|
||||
static void update_iterator_data(struct b_directory_iterator_p *it)
|
||||
static void update_iterator_data(struct fx_directory_iterator_p *it)
|
||||
{
|
||||
if (it->entry.filepath) {
|
||||
b_path_unref((b_path *)it->entry.filepath);
|
||||
fx_path_unref((fx_path *)it->entry.filepath);
|
||||
it->entry.filepath = NULL;
|
||||
}
|
||||
|
||||
FTSENT *ent = it->ent;
|
||||
|
||||
b_path *path = b_path_create_from_cstr(
|
||||
ent->fts_path + b_path_length(it->_p->d_path_abs) + 1);
|
||||
fx_path *path = fx_path_create_from_cstr(
|
||||
ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1);
|
||||
|
||||
it->entry.filename = ent->fts_name;
|
||||
it->entry.filepath = path;
|
||||
@@ -452,32 +452,32 @@ static void update_iterator_data(struct b_directory_iterator_p *it)
|
||||
it->entry.info.length = ent->fts_statp->st_size;
|
||||
|
||||
if (S_ISREG(ent->fts_statp->st_mode)) {
|
||||
it->entry.info.attrib |= B_FILE_ATTRIB_NORMAL;
|
||||
it->entry.info.attrib |= FX_FILE_ATTRIB_NORMAL;
|
||||
}
|
||||
|
||||
if (S_ISDIR(ent->fts_statp->st_mode)) {
|
||||
it->entry.info.attrib |= B_FILE_ATTRIB_DIRECTORY;
|
||||
it->entry.info.attrib |= FX_FILE_ATTRIB_DIRECTORY;
|
||||
}
|
||||
|
||||
if (S_ISBLK(ent->fts_statp->st_mode)) {
|
||||
it->entry.info.attrib |= B_FILE_ATTRIB_BLOCK_DEVICE;
|
||||
it->entry.info.attrib |= FX_FILE_ATTRIB_BLOCK_DEVICE;
|
||||
}
|
||||
|
||||
if (S_ISCHR(ent->fts_statp->st_mode)) {
|
||||
it->entry.info.attrib |= B_FILE_ATTRIB_CHAR_DEVICE;
|
||||
it->entry.info.attrib |= FX_FILE_ATTRIB_CHAR_DEVICE;
|
||||
}
|
||||
|
||||
if (S_ISLNK(ent->fts_statp->st_mode)) {
|
||||
it->entry.info.attrib |= B_FILE_ATTRIB_SYMLINK;
|
||||
it->entry.info.attrib |= FX_FILE_ATTRIB_SYMLINK;
|
||||
}
|
||||
}
|
||||
|
||||
static void iterator_fini(b_object *obj, void *priv)
|
||||
static void iterator_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
struct b_directory_iterator_p *it = priv;
|
||||
struct fx_directory_iterator_p *it = priv;
|
||||
|
||||
if (it->entry.filepath) {
|
||||
b_path_unref((b_path *)it->entry.filepath);
|
||||
fx_path_unref((fx_path *)it->entry.filepath);
|
||||
it->entry.filepath = NULL;
|
||||
}
|
||||
|
||||
@@ -486,21 +486,21 @@ static void iterator_fini(b_object *obj, void *priv)
|
||||
}
|
||||
}
|
||||
|
||||
b_iterator *b_directory_begin(
|
||||
b_directory *directory, enum b_directory_iterator_flags flags)
|
||||
fx_iterator *fx_directory_begin(
|
||||
fx_directory *directory, enum fx_directory_iterator_flags flags)
|
||||
{
|
||||
b_iterator *it_obj = b_object_create(B_TYPE_DIRECTORY_ITERATOR);
|
||||
struct b_directory_iterator_p *it
|
||||
= b_object_get_private(it_obj, B_TYPE_DIRECTORY_ITERATOR);
|
||||
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);
|
||||
|
||||
it->flags = flags;
|
||||
it->root = directory;
|
||||
it->_p = b_object_get_private(directory, B_TYPE_DIRECTORY);
|
||||
it->_p = fx_object_get_private(directory, FX_TYPE_DIRECTORY);
|
||||
|
||||
int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR;
|
||||
|
||||
const char *path_list[] = {
|
||||
b_path_ptr(it->_p->d_path_abs),
|
||||
fx_path_ptr(it->_p->d_path_abs),
|
||||
NULL,
|
||||
};
|
||||
|
||||
@@ -512,7 +512,7 @@ b_iterator *b_directory_begin(
|
||||
it->ent = fts_read(it->fts);
|
||||
|
||||
if (!it->ent) {
|
||||
b_iterator_set_status(it_obj, B_ERR_NO_DATA);
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
@@ -527,14 +527,14 @@ b_iterator *b_directory_begin(
|
||||
done = true;
|
||||
break;
|
||||
case FTS_D:
|
||||
if (it->flags & B_DIRECTORY_ITERATE_PARENT_LAST) {
|
||||
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_LAST) {
|
||||
continue;
|
||||
}
|
||||
|
||||
done = true;
|
||||
break;
|
||||
case FTS_DP:
|
||||
if (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST) {
|
||||
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST) {
|
||||
continue;
|
||||
}
|
||||
done = true;
|
||||
@@ -549,22 +549,22 @@ b_iterator *b_directory_begin(
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
static b_iterator *iterator_begin(b_object *obj)
|
||||
static fx_iterator *iterator_begin(fx_object *obj)
|
||||
{
|
||||
return b_directory_begin(obj, B_DIRECTORY_ITERATE_PARENT_FIRST);
|
||||
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
||||
}
|
||||
|
||||
static const b_iterator *iterator_cbegin(const b_object *obj)
|
||||
static const fx_iterator *iterator_cbegin(const fx_object *obj)
|
||||
{
|
||||
return b_directory_begin((b_object *)obj, B_DIRECTORY_ITERATE_PARENT_FIRST);
|
||||
return fx_directory_begin((fx_object *)obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
|
||||
}
|
||||
|
||||
static enum b_status iterator_move_next(const b_iterator *obj)
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct b_directory_iterator_p *it
|
||||
= b_object_get_private(obj, B_TYPE_DIRECTORY_ITERATOR);
|
||||
struct fx_directory_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||
if (!it || !it->fts) {
|
||||
return B_ERR_NO_DATA;
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
bool done = false;
|
||||
@@ -573,7 +573,7 @@ static enum b_status iterator_move_next(const b_iterator *obj)
|
||||
it->ent = fts_read(it->fts);
|
||||
|
||||
if (!it->ent) {
|
||||
return B_ERR_NO_DATA;
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
if (it->ent->fts_level == 0) {
|
||||
@@ -587,13 +587,13 @@ static enum b_status iterator_move_next(const b_iterator *obj)
|
||||
done = true;
|
||||
break;
|
||||
case FTS_D:
|
||||
if (it->flags & B_DIRECTORY_ITERATE_PARENT_LAST) {
|
||||
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_LAST) {
|
||||
continue;
|
||||
}
|
||||
done = true;
|
||||
break;
|
||||
case FTS_DP:
|
||||
if (it->flags & B_DIRECTORY_ITERATE_PARENT_FIRST) {
|
||||
if (it->flags & FX_DIRECTORY_ITERATE_PARENT_FIRST) {
|
||||
continue;
|
||||
}
|
||||
done = true;
|
||||
@@ -605,79 +605,79 @@ static enum b_status iterator_move_next(const b_iterator *obj)
|
||||
}
|
||||
|
||||
update_iterator_data(it);
|
||||
return B_SUCCESS;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static enum b_status iterator_erase(b_iterator *obj)
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct b_directory_iterator_p *it
|
||||
= b_object_get_private(obj, B_TYPE_DIRECTORY_ITERATOR);
|
||||
b_result result = b_directory_path_unlink(it->root, it->entry.filepath);
|
||||
if (b_result_is_error(result)) {
|
||||
enum b_status status = b_error_get_status_code(result);
|
||||
b_error_discard(result);
|
||||
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);
|
||||
if (fx_result_is_error(result)) {
|
||||
enum fx_status status = fx_error_get_status_code(result);
|
||||
fx_error_discard(result);
|
||||
return status;
|
||||
}
|
||||
|
||||
return iterator_move_next(obj);
|
||||
}
|
||||
|
||||
static b_iterator_value iterator_get_value(b_iterator *obj)
|
||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
||||
{
|
||||
struct b_directory_iterator_p *it
|
||||
= b_object_get_private(obj, B_TYPE_DIRECTORY_ITERATOR);
|
||||
struct fx_directory_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||
|
||||
return B_ITERATOR_VALUE_PTR(&it->entry);
|
||||
return FX_ITERATOR_VALUE_PTR(&it->entry);
|
||||
}
|
||||
|
||||
static const b_iterator_value iterator_get_cvalue(const b_iterator *obj)
|
||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
||||
{
|
||||
struct b_directory_iterator_p *it
|
||||
= b_object_get_private(obj, B_TYPE_DIRECTORY_ITERATOR);
|
||||
struct fx_directory_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
|
||||
|
||||
return B_ITERATOR_VALUE_CPTR(&it->entry);
|
||||
return FX_ITERATOR_VALUE_CPTR(&it->entry);
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
// ---- b_directory DEFINITION
|
||||
B_TYPE_CLASS_DEFINITION_BEGIN(b_directory)
|
||||
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
|
||||
B_INTERFACE_ENTRY(to_string) = NULL;
|
||||
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
|
||||
// ---- fx_directory DEFINITION
|
||||
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory)
|
||||
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = NULL;
|
||||
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
B_TYPE_CLASS_INTERFACE_BEGIN(b_iterable, B_TYPE_ITERABLE)
|
||||
B_INTERFACE_ENTRY(it_begin) = iterator_begin;
|
||||
B_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
|
||||
B_TYPE_CLASS_INTERFACE_END(b_iterable, B_TYPE_ITERABLE)
|
||||
B_TYPE_CLASS_DEFINITION_END(b_directory)
|
||||
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
|
||||
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
|
||||
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
|
||||
FX_TYPE_CLASS_DEFINITION_END(fx_directory)
|
||||
|
||||
B_TYPE_DEFINITION_BEGIN(b_directory)
|
||||
B_TYPE_ID(0x10d36546, 0x7f96, 0x464b, 0xbc4d, 0xe504b283fa45);
|
||||
B_TYPE_CLASS(b_directory_class);
|
||||
B_TYPE_IMPLEMENTS(B_TYPE_ITERABLE);
|
||||
B_TYPE_INSTANCE_PRIVATE(struct b_directory_p);
|
||||
B_TYPE_INSTANCE_INIT(directory_init);
|
||||
B_TYPE_INSTANCE_FINI(directory_fini);
|
||||
B_TYPE_DEFINITION_END(b_directory)
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_directory)
|
||||
FX_TYPE_ID(0x10d36546, 0x7f96, 0x464b, 0xbc4d, 0xe504b283fa45);
|
||||
FX_TYPE_CLASS(fx_directory_class);
|
||||
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_directory_p);
|
||||
FX_TYPE_INSTANCE_INIT(directory_init);
|
||||
FX_TYPE_INSTANCE_FINI(directory_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_directory)
|
||||
|
||||
// ---- b_directory_iterator DEFINITION
|
||||
B_TYPE_CLASS_DEFINITION_BEGIN(b_directory_iterator)
|
||||
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
|
||||
B_INTERFACE_ENTRY(to_string) = NULL;
|
||||
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
|
||||
// ---- fx_directory_iterator DEFINITION
|
||||
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory_iterator)
|
||||
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = NULL;
|
||||
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
B_TYPE_CLASS_INTERFACE_BEGIN(b_iterator, B_TYPE_ITERATOR)
|
||||
B_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||
B_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||
B_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||
B_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
||||
B_TYPE_CLASS_INTERFACE_END(b_iterator, B_TYPE_ITERATOR)
|
||||
B_TYPE_CLASS_DEFINITION_END(b_directory_iterator)
|
||||
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
||||
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_DEFINITION_END(fx_directory_iterator)
|
||||
|
||||
B_TYPE_DEFINITION_BEGIN(b_directory_iterator)
|
||||
B_TYPE_ID(0xc707fce6, 0xc895, 0x4925, 0x8700, 0xa60641dee0cc);
|
||||
B_TYPE_EXTENDS(B_TYPE_ITERATOR);
|
||||
B_TYPE_CLASS(b_directory_iterator_class);
|
||||
B_TYPE_INSTANCE_PRIVATE(struct b_directory_iterator_p);
|
||||
B_TYPE_DEFINITION_END(b_directory_iterator)
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_directory_iterator)
|
||||
FX_TYPE_ID(0xc707fce6, 0xc895, 0x4925, 0x8700, 0xa60641dee0cc);
|
||||
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
|
||||
FX_TYPE_CLASS(fx_directory_iterator_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_directory_iterator_p);
|
||||
FX_TYPE_DEFINITION_END(fx_directory_iterator)
|
||||
|
||||
Reference in New Issue
Block a user