io: implement b_error support for file/directory operations
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "posix.h"
|
||||
|
||||
#include <blue/io/directory.h>
|
||||
#include <blue/object/string.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <fts.h>
|
||||
@@ -12,6 +13,7 @@
|
||||
struct b_directory {
|
||||
struct b_object base;
|
||||
int fd;
|
||||
struct b_path *rel_path;
|
||||
struct b_path *abs_path;
|
||||
};
|
||||
|
||||
@@ -30,12 +32,95 @@ static struct b_object_type directory_type = {
|
||||
.t_release = directory_release,
|
||||
};
|
||||
|
||||
enum b_status b_directory_open(
|
||||
static b_result create_directory(struct b_directory *root, const char *path)
|
||||
{
|
||||
int root_fd = root->fd;
|
||||
int err = mkdirat(root_fd, path, 0755);
|
||||
if (err == 0 || errno == EEXIST) {
|
||||
return B_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
return b_result_from_errno_with_subfilepath(
|
||||
errno, path, b_directory_get_rel_path_cstr(root), B_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
static b_result create_directory_hierarchy(struct b_directory *root, const char *path)
|
||||
{
|
||||
int root_fd = root->fd;
|
||||
|
||||
char *path_buf = b_strdup(path);
|
||||
if (!path_buf) {
|
||||
return B_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
b_result result = B_RESULT_SUCCESS;
|
||||
for (size_t i = 0; path_buf[i]; i++) {
|
||||
if (path_buf[i] != '/') {
|
||||
continue;
|
||||
}
|
||||
|
||||
path_buf[i] = 0;
|
||||
|
||||
int err = mkdirat(root_fd, path_buf, 0755);
|
||||
if (err != 0 && errno != EEXIST) {
|
||||
result = b_result_from_errno_with_subfilepath(
|
||||
errno, path_buf,
|
||||
b_directory_get_rel_path_cstr(root),
|
||||
B_ERR_IO_FAILURE);
|
||||
break;
|
||||
}
|
||||
|
||||
path_buf[i] = '/';
|
||||
}
|
||||
|
||||
int err = mkdirat(root_fd, path_buf, 0755);
|
||||
if (err != 0 && errno != EEXIST) {
|
||||
result = b_result_from_errno_with_subfilepath(
|
||||
errno, path_buf, b_directory_get_rel_path_cstr(root),
|
||||
B_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
free(path_buf);
|
||||
return result;
|
||||
}
|
||||
|
||||
b_result b_directory_open(
|
||||
struct b_directory *root, const struct b_path *path,
|
||||
struct b_directory **out)
|
||||
b_directory_open_flags flags, struct b_directory **out)
|
||||
{
|
||||
enum b_status status = B_SUCCESS;
|
||||
|
||||
int root_fd = root ? root->fd : AT_FDCWD;
|
||||
|
||||
const char *path_cstr = b_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) {
|
||||
result = create_directory_hierarchy(root, path_cstr);
|
||||
} else if ((flags & B_DIRECTORY_OPEN_CREATE) == B_DIRECTORY_OPEN_CREATE) {
|
||||
result = create_directory(root, path_cstr);
|
||||
}
|
||||
|
||||
if (b_result_is_error(result)) {
|
||||
return b_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);
|
||||
}
|
||||
|
||||
struct b_directory *dir
|
||||
= (struct b_directory *)b_object_type_instantiate(&directory_type);
|
||||
|
||||
struct b_path *cwd = NULL;
|
||||
|
||||
if (!root) {
|
||||
@@ -49,41 +134,58 @@ enum b_status b_directory_open(
|
||||
|
||||
b_path *new_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!new_path) {
|
||||
return B_ERR_NO_MEMORY;
|
||||
return B_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
if (cwd) {
|
||||
b_path_release(cwd);
|
||||
}
|
||||
|
||||
int root_fd = root ? root->fd : AT_FDCWD;
|
||||
|
||||
int fd = openat(root_fd, b_path_ptr(path), O_DIRECTORY);
|
||||
|
||||
if (fd == -1) {
|
||||
status = b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
||||
|
||||
b_path_release(new_path);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
struct b_directory *dir
|
||||
= (struct b_directory *)b_object_type_instantiate(&directory_type);
|
||||
|
||||
dir->abs_path = new_path;
|
||||
dir->rel_path = b_path_duplicate(path);
|
||||
dir->fd = fd;
|
||||
|
||||
*out = dir;
|
||||
|
||||
return B_SUCCESS;
|
||||
return B_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
const struct b_path *b_directory_get_path(const struct b_directory *dir)
|
||||
{
|
||||
if (!dir) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return dir->abs_path;
|
||||
}
|
||||
|
||||
const struct b_path *b_directory_get_rel_path(const struct b_directory *dir)
|
||||
{
|
||||
if (!dir) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return dir->rel_path;
|
||||
}
|
||||
|
||||
const char *b_directory_get_path_cstr(const struct b_directory *dir)
|
||||
{
|
||||
if (!dir) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return b_path_ptr(dir->abs_path);
|
||||
}
|
||||
|
||||
const char *b_directory_get_rel_path_cstr(const struct b_directory *dir)
|
||||
{
|
||||
if (!dir) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return b_path_ptr(dir->rel_path);
|
||||
}
|
||||
|
||||
bool b_directory_path_exists(
|
||||
const struct b_directory *root, const struct b_path *path)
|
||||
{
|
||||
@@ -144,7 +246,7 @@ bool b_directory_path_is_directory(
|
||||
return result;
|
||||
}
|
||||
|
||||
enum b_status b_directory_path_stat(
|
||||
b_result b_directory_path_stat(
|
||||
const struct b_directory *root, const struct b_path *path,
|
||||
struct b_file_info *out)
|
||||
{
|
||||
@@ -156,16 +258,16 @@ enum b_status b_directory_path_stat(
|
||||
struct b_path *abs_path
|
||||
= b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return B_ERR_NO_MEMORY;
|
||||
return B_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
enum b_status status = b_path_stat(abs_path, out);
|
||||
b_path_release(abs_path);
|
||||
|
||||
return status;
|
||||
return B_RESULT_STATUS(status);
|
||||
}
|
||||
|
||||
enum b_status b_directory_path_unlink(
|
||||
b_result b_directory_path_unlink(
|
||||
const struct b_directory *root, const struct b_path *path)
|
||||
{
|
||||
const struct b_path *parts[] = {
|
||||
@@ -176,13 +278,13 @@ enum b_status b_directory_path_unlink(
|
||||
struct b_path *abs_path
|
||||
= b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return B_ERR_NO_MEMORY;
|
||||
return B_RESULT_ERR(NO_MEMORY);
|
||||
}
|
||||
|
||||
enum b_status status = b_path_unlink(abs_path);
|
||||
b_path_release(abs_path);
|
||||
|
||||
return status;
|
||||
return B_RESULT_STATUS(status);
|
||||
}
|
||||
|
||||
static int ftsent_compare(const FTSENT **one, const FTSENT **two)
|
||||
|
||||
Reference in New Issue
Block a user