io: add lots of directory and path manipulation functions
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#include "status.h"
|
||||
#include "posix.h"
|
||||
|
||||
#include <blue/io/directory.h>
|
||||
#include <errno.h>
|
||||
@@ -79,6 +79,112 @@ enum b_status b_directory_open(
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
const struct b_path *b_directory_get_path(const struct b_directory *dir)
|
||||
{
|
||||
return dir->abs_path;
|
||||
}
|
||||
|
||||
bool b_directory_path_exists(
|
||||
const struct b_directory *root, const struct b_path *path)
|
||||
{
|
||||
const struct b_path *parts[] = {
|
||||
root ? root->abs_path : NULL,
|
||||
path,
|
||||
};
|
||||
|
||||
struct b_path *abs_path
|
||||
= b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool result = b_path_exists(abs_path);
|
||||
b_path_release(abs_path);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool b_directory_path_is_file(
|
||||
const struct b_directory *root, const struct b_path *path)
|
||||
{
|
||||
const struct b_path *parts[] = {
|
||||
root ? root->abs_path : NULL,
|
||||
path,
|
||||
};
|
||||
|
||||
struct b_path *abs_path
|
||||
= b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool result = b_path_is_file(abs_path);
|
||||
b_path_release(abs_path);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool b_directory_path_is_directory(
|
||||
const struct b_directory *root, const struct b_path *path)
|
||||
{
|
||||
const struct b_path *parts[] = {
|
||||
root ? root->abs_path : NULL,
|
||||
path,
|
||||
};
|
||||
|
||||
struct b_path *abs_path
|
||||
= b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool result = b_path_is_directory(abs_path);
|
||||
b_path_release(abs_path);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
enum b_status b_directory_path_stat(
|
||||
const struct b_directory *root, const struct b_path *path,
|
||||
struct b_file_info *out)
|
||||
{
|
||||
const struct b_path *parts[] = {
|
||||
root ? root->abs_path : NULL,
|
||||
path,
|
||||
};
|
||||
|
||||
struct b_path *abs_path
|
||||
= b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return B_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
enum b_status status = b_path_stat(abs_path, out);
|
||||
b_path_release(abs_path);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
enum b_status b_directory_path_unlink(
|
||||
const struct b_directory *root, const struct b_path *path)
|
||||
{
|
||||
const struct b_path *parts[] = {
|
||||
root ? root->abs_path : NULL,
|
||||
path,
|
||||
};
|
||||
|
||||
struct b_path *abs_path
|
||||
= b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
if (!abs_path) {
|
||||
return B_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
enum b_status status = b_path_unlink(abs_path);
|
||||
b_path_release(abs_path);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
static int ftsent_compare(const FTSENT **one, const FTSENT **two)
|
||||
{
|
||||
return (strcmp((*one)->fts_name, (*two)->fts_name));
|
||||
@@ -151,6 +257,8 @@ int b_directory_iterator_begin(
|
||||
struct b_directory *directory, struct b_directory_iterator *it,
|
||||
enum b_directory_iterator_flags flags)
|
||||
{
|
||||
memset(it, 0x0, sizeof *it);
|
||||
|
||||
it->flags = flags;
|
||||
it->root = directory;
|
||||
|
||||
@@ -286,4 +394,8 @@ bool b_directory_iterator_is_valid(const struct b_directory_iterator *it)
|
||||
|
||||
static void directory_release(struct b_object *obj)
|
||||
{
|
||||
struct b_directory *dir = B_DIRECTORY(obj);
|
||||
|
||||
close(dir->fd);
|
||||
b_path_release(dir->abs_path);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,390 @@
|
||||
#include "posix.h"
|
||||
|
||||
#include <blue/core/random.h>
|
||||
#include <blue/io/directory.h>
|
||||
#include <blue/io/file.h>
|
||||
#include <blue/io/path.h>
|
||||
#include <blue/object/string.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct b_file {
|
||||
struct b_object base;
|
||||
enum b_file_mode mode;
|
||||
int fd;
|
||||
struct b_path *path;
|
||||
};
|
||||
|
||||
static void file_release(struct b_object *obj);
|
||||
|
||||
static struct b_object_type file_type = {
|
||||
.t_name = "corelib::file",
|
||||
.t_flags = B_OBJECT_FUNDAMENTAL,
|
||||
.t_id = B_OBJECT_TYPE_FILE,
|
||||
.t_instance_size = sizeof(struct b_file),
|
||||
.t_release = file_release,
|
||||
};
|
||||
|
||||
static unsigned int b_mode_to_unix_mode(enum b_file_mode mode)
|
||||
{
|
||||
unsigned int result = 0;
|
||||
|
||||
if (mode & B_FILE_READ_WRITE) {
|
||||
result |= O_RDWR;
|
||||
} else if (mode & B_FILE_READ_ONLY) {
|
||||
result |= O_RDONLY;
|
||||
} else if (mode & B_FILE_WRITE_ONLY) {
|
||||
result |= O_WRONLY;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (mode & B_FILE_TRUNCATE) {
|
||||
result |= O_TRUNC;
|
||||
}
|
||||
|
||||
if (mode & B_FILE_CREATE) {
|
||||
result |= O_CREAT;
|
||||
}
|
||||
|
||||
if ((mode & B_FILE_CREATE_ONLY) == B_FILE_CREATE_ONLY) {
|
||||
result |= O_EXCL;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
enum b_status b_file_open(
|
||||
struct b_directory *root, const struct b_path *path,
|
||||
enum b_file_mode mode, struct b_file **out)
|
||||
{
|
||||
struct b_path *file_path = b_path_retain((struct b_path *)path);
|
||||
unsigned int flags = b_mode_to_unix_mode(mode);
|
||||
|
||||
if (flags == 0) {
|
||||
b_path_release(file_path);
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
struct b_path *root_path = NULL;
|
||||
if (root) {
|
||||
root_path = b_path_retain(
|
||||
(struct b_path *)b_directory_get_path(root));
|
||||
} else {
|
||||
root_path = b_path_create_cwd();
|
||||
}
|
||||
|
||||
const struct b_path *parts[] = {
|
||||
root_path,
|
||||
file_path,
|
||||
};
|
||||
|
||||
struct b_path *abs_path
|
||||
= b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
b_path_release(root_path);
|
||||
b_path_release(file_path);
|
||||
|
||||
if (!abs_path) {
|
||||
return B_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
int fd = open(b_path_ptr(abs_path), flags, 0644);
|
||||
if (fd == -1) {
|
||||
b_path_release(abs_path);
|
||||
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
struct b_file *file
|
||||
= (struct b_file *)b_object_type_instantiate(&file_type);
|
||||
if (!file) {
|
||||
close(fd);
|
||||
b_path_release(abs_path);
|
||||
return B_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
file->fd = fd;
|
||||
file->path = abs_path;
|
||||
file->mode = mode;
|
||||
|
||||
*out = file;
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
static void generate_tmp_filename(char *out, size_t len)
|
||||
{
|
||||
static const char *alphabet
|
||||
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
"01234567"
|
||||
"89+=-_.";
|
||||
static const size_t alphabet_len = 67;
|
||||
|
||||
b_random_ctx *ctx = b_random_global_ctx();
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
int v = b_random_next_int64(ctx) % alphabet_len;
|
||||
out[i] = alphabet[v];
|
||||
}
|
||||
|
||||
out[len - 1] = 0;
|
||||
}
|
||||
|
||||
enum b_status b_file_open_temp(enum b_file_mode mode, struct b_file **out)
|
||||
{
|
||||
mode |= B_FILE_DELETE_ON_CLOSE;
|
||||
|
||||
char name[16];
|
||||
char path[128];
|
||||
|
||||
while (1) {
|
||||
generate_tmp_filename(name, sizeof name);
|
||||
snprintf(path, sizeof path, "/tmp/%s", name);
|
||||
struct b_path *rpath = b_path_create_from_cstr(path);
|
||||
|
||||
enum b_status status = b_file_open(
|
||||
B_DIRECTORY_ROOT, rpath, mode | B_FILE_CREATE_ONLY, out);
|
||||
|
||||
if (status == B_ERR_NAME_EXISTS) {
|
||||
b_path_release(rpath);
|
||||
continue;
|
||||
}
|
||||
|
||||
b_path_unlink(rpath);
|
||||
b_path_release(rpath);
|
||||
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
enum b_status b_file_open_shadow(
|
||||
struct b_file *original, enum b_file_mode mode, struct b_file **out)
|
||||
{
|
||||
mode |= B_FILE_SHADOW | B_FILE_DELETE_ON_CLOSE | B_FILE_CREATE;
|
||||
|
||||
struct b_path *dir;
|
||||
b_path_get_directory(original->path, &dir);
|
||||
|
||||
struct b_string *filename = b_string_create();
|
||||
b_path_get_filename(original->path, filename);
|
||||
|
||||
b_string_prepend_cstr(filename, ".~");
|
||||
|
||||
struct b_path *shadow_filename
|
||||
= b_path_create_from_cstr(b_string_ptr(filename));
|
||||
b_string_release(filename);
|
||||
|
||||
const struct b_path *parts[] = {
|
||||
dir,
|
||||
shadow_filename,
|
||||
};
|
||||
|
||||
struct b_path *shadow_filepath
|
||||
= b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
b_path_release(dir);
|
||||
b_path_release(shadow_filename);
|
||||
|
||||
if (b_path_exists(shadow_filepath)) {
|
||||
b_path_unlink(shadow_filepath);
|
||||
}
|
||||
|
||||
struct b_file *shadow_file;
|
||||
enum b_status status = b_file_open(
|
||||
B_DIRECTORY_ROOT, shadow_filepath, mode, &shadow_file);
|
||||
b_path_release(shadow_filepath);
|
||||
|
||||
if (!B_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
|
||||
*out = shadow_file;
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
enum b_status b_file_stat(struct b_file *file, struct b_file_info *out)
|
||||
{
|
||||
struct stat st;
|
||||
int err = fstat(file->fd, &st);
|
||||
|
||||
if (err != 0) {
|
||||
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
return b_file_info_from_stat(&st, out);
|
||||
}
|
||||
|
||||
enum b_status b_file_size(struct b_file *file, size_t *out_len)
|
||||
{
|
||||
off_t cur = lseek(file->fd, 0, SEEK_CUR);
|
||||
if (cur == (off_t)-1) {
|
||||
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
off_t len = lseek(file->fd, 0, SEEK_END);
|
||||
if (len == (off_t)-1) {
|
||||
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
cur = lseek(file->fd, cur, SEEK_SET);
|
||||
if (cur == (off_t)-1) {
|
||||
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
*out_len = (size_t)len;
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
enum b_status b_file_cursor(struct b_file *file, size_t *out_pos)
|
||||
{
|
||||
off_t cur = lseek(file->fd, 0, SEEK_CUR);
|
||||
if (cur == (off_t)-1) {
|
||||
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
*out_pos = (size_t)cur;
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
enum b_status b_file_resize(struct b_file *file, size_t len)
|
||||
{
|
||||
int err = ftruncate(file->fd, len);
|
||||
if (err == 0) {
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
enum b_status b_file_seek(
|
||||
struct b_file *file, long long offset, enum b_seek_basis basis)
|
||||
{
|
||||
int whence;
|
||||
switch (basis) {
|
||||
case B_SEEK_BEGINNING:
|
||||
whence = SEEK_SET;
|
||||
break;
|
||||
case B_SEEK_CURRENT:
|
||||
whence = SEEK_CUR;
|
||||
break;
|
||||
case B_SEEK_END:
|
||||
whence = SEEK_END;
|
||||
break;
|
||||
default:
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
int err = lseek(file->fd, offset, whence);
|
||||
if (err == 0) {
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
enum b_status b_file_swap_shadow(struct b_file *main_file, struct b_file *shadow_file)
|
||||
{
|
||||
if (main_file->mode & B_FILE_SHADOW) {
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
if (!(shadow_file->mode & B_FILE_SHADOW)) {
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
struct b_path *dir_path;
|
||||
b_path_get_directory(main_file->path, &dir_path);
|
||||
|
||||
struct b_path *tmp_path = NULL;
|
||||
|
||||
while (1) {
|
||||
char tmp_name[16];
|
||||
generate_tmp_filename(tmp_name, sizeof tmp_name);
|
||||
struct b_path *tmp_name_p = b_path_create_from_cstr(tmp_name);
|
||||
|
||||
const struct b_path *parts[] = {
|
||||
dir_path,
|
||||
tmp_name_p,
|
||||
};
|
||||
|
||||
tmp_path = b_path_join(parts, sizeof parts / sizeof parts[0]);
|
||||
|
||||
b_path_release(tmp_name_p);
|
||||
|
||||
if (!b_path_exists(tmp_path)) {
|
||||
break;
|
||||
}
|
||||
|
||||
b_path_release(tmp_path);
|
||||
tmp_path = NULL;
|
||||
}
|
||||
|
||||
b_path_release(dir_path);
|
||||
|
||||
int err;
|
||||
|
||||
err = rename(b_path_ptr(main_file->path), b_path_ptr(tmp_path));
|
||||
err = rename(b_path_ptr(shadow_file->path), b_path_ptr(main_file->path));
|
||||
err = rename(b_path_ptr(tmp_path), b_path_ptr(shadow_file->path));
|
||||
|
||||
b_path_release(tmp_path);
|
||||
|
||||
int fd = main_file->fd;
|
||||
main_file->fd = shadow_file->fd;
|
||||
shadow_file->fd = fd;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
enum b_status b_file_read(
|
||||
struct b_file *file, size_t offset, size_t len, void *buf, size_t *nr_read)
|
||||
{
|
||||
if (offset != B_OFFSET_CURRENT) {
|
||||
lseek(file->fd, offset, SEEK_SET);
|
||||
}
|
||||
|
||||
long r = read(file->fd, buf, len);
|
||||
|
||||
enum b_status status = B_SUCCESS;
|
||||
|
||||
if (r < 0) {
|
||||
status = b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
*nr_read = r;
|
||||
return status;
|
||||
}
|
||||
|
||||
enum b_status b_file_write(
|
||||
struct b_file *file, size_t offset, size_t len, const void *buf,
|
||||
size_t *nr_written)
|
||||
{
|
||||
if (offset != B_OFFSET_CURRENT) {
|
||||
lseek(file->fd, offset, SEEK_SET);
|
||||
}
|
||||
|
||||
long w = write(file->fd, buf, len);
|
||||
|
||||
enum b_status status = B_SUCCESS;
|
||||
|
||||
if (w < 0) {
|
||||
status = b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
*nr_written = w;
|
||||
return status;
|
||||
}
|
||||
|
||||
static void file_release(struct b_object *obj)
|
||||
{
|
||||
struct b_file *file = (struct b_file *)obj;
|
||||
close(file->fd);
|
||||
|
||||
if (file->mode & B_FILE_DELETE_ON_CLOSE) {
|
||||
b_path_unlink(file->path);
|
||||
}
|
||||
|
||||
b_path_release(file->path);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "status.h"
|
||||
#include "posix.h"
|
||||
|
||||
#include <blue/core/stringstream.h>
|
||||
#include <blue/io/file.h>
|
||||
@@ -216,27 +216,65 @@ enum b_status b_path_stat(const b_path *path, struct b_file_info *out)
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
out->length = st.st_size;
|
||||
return b_file_info_from_stat(&st, out);
|
||||
}
|
||||
|
||||
if (S_ISREG(st.st_flags)) {
|
||||
out->attrib |= B_FILE_ATTRIB_NORMAL;
|
||||
enum b_status b_path_unlink(const b_path *path)
|
||||
{
|
||||
int err = unlink(b_string_ptr(path->pathstr));
|
||||
return err == 0 ? B_SUCCESS : b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
enum b_status b_path_get_directory(
|
||||
const struct b_path *path, struct b_path **out_dir_path)
|
||||
{
|
||||
struct b_string *path_str = path->pathstr;
|
||||
long len = b_string_get_size(path_str, B_STRLEN_NORMAL);
|
||||
|
||||
const char *path_cstr = b_string_ptr(path_str);
|
||||
char *sep = strrchr(path_cstr, '/');
|
||||
|
||||
if (!sep) {
|
||||
*out_dir_path = b_path_create();
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
if (S_ISDIR(st.st_flags)) {
|
||||
out->attrib |= B_FILE_ATTRIB_DIRECTORY;
|
||||
size_t dir_path_len = (size_t)(sep - path_cstr);
|
||||
struct b_string *dir_path_s = b_string_substr(path_str, 0, dir_path_len);
|
||||
|
||||
struct b_path *dir_path
|
||||
= b_path_create_from_cstr(b_string_ptr(dir_path_s));
|
||||
b_string_release(dir_path_s);
|
||||
|
||||
*out_dir_path = dir_path;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
enum b_status b_path_get_filename(
|
||||
const struct b_path *path, struct b_string *out_name)
|
||||
{
|
||||
struct b_string *path_str = path->pathstr;
|
||||
long len = b_string_get_size(path_str, B_STRLEN_NORMAL);
|
||||
|
||||
char *sep = strrchr(b_string_ptr(path_str), '/');
|
||||
|
||||
if (!sep) {
|
||||
b_string_append_s(out_name, path_str);
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
if (S_ISBLK(st.st_flags)) {
|
||||
out->attrib |= B_FILE_ATTRIB_BLOCK_DEVICE;
|
||||
const char *filename = sep;
|
||||
while (*filename == '/' && *filename != '\0') {
|
||||
filename++;
|
||||
}
|
||||
|
||||
if (S_ISCHR(st.st_flags)) {
|
||||
out->attrib |= B_FILE_ATTRIB_CHAR_DEVICE;
|
||||
if (*filename == '\0') {
|
||||
b_string_clear(out_name);
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
if (S_ISLNK(st.st_flags)) {
|
||||
out->attrib |= B_FILE_ATTRIB_SYMLINK;
|
||||
}
|
||||
b_string_append_cstr(out_name, filename);
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
58
io/sys/darwin/posix.c
Normal file
58
io/sys/darwin/posix.c
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <blue/core/status.h>
|
||||
#include <blue/io/file.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
enum b_status b_status_from_errno(int error, enum b_status default_value)
|
||||
{
|
||||
switch (error) {
|
||||
case 0:
|
||||
return B_SUCCESS;
|
||||
case ENOENT:
|
||||
return B_ERR_NO_ENTRY;
|
||||
case EEXIST:
|
||||
return B_ERR_NAME_EXISTS;
|
||||
case ENOMEM:
|
||||
return B_ERR_NO_MEMORY;
|
||||
case EINVAL:
|
||||
return B_ERR_INVALID_ARGUMENT;
|
||||
case EIO:
|
||||
return B_ERR_IO_FAILURE;
|
||||
case EISDIR:
|
||||
return B_ERR_IS_DIRECTORY;
|
||||
case ENOTDIR:
|
||||
return B_ERR_NOT_DIRECTORY;
|
||||
case ENOTSUP:
|
||||
case ENOSYS:
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
default:
|
||||
return default_value;
|
||||
}
|
||||
}
|
||||
|
||||
enum b_status b_file_info_from_stat(const struct stat *st, struct b_file_info *out)
|
||||
{
|
||||
out->length = st->st_size;
|
||||
|
||||
if (S_ISREG(st->st_mode)) {
|
||||
out->attrib |= B_FILE_ATTRIB_NORMAL;
|
||||
}
|
||||
|
||||
if (S_ISDIR(st->st_mode)) {
|
||||
out->attrib |= B_FILE_ATTRIB_DIRECTORY;
|
||||
}
|
||||
|
||||
if (S_ISBLK(st->st_mode)) {
|
||||
out->attrib |= B_FILE_ATTRIB_BLOCK_DEVICE;
|
||||
}
|
||||
|
||||
if (S_ISCHR(st->st_mode)) {
|
||||
out->attrib |= B_FILE_ATTRIB_CHAR_DEVICE;
|
||||
}
|
||||
|
||||
if (S_ISLNK(st->st_mode)) {
|
||||
out->attrib |= B_FILE_ATTRIB_SYMLINK;
|
||||
}
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
13
io/sys/darwin/posix.h
Normal file
13
io/sys/darwin/posix.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef _IO_DARWIN_POSIX_H_
|
||||
#define _IO_DARWIN_POSIX_H_
|
||||
|
||||
#include <blue/core/status.h>
|
||||
|
||||
struct stat;
|
||||
struct b_file_info;
|
||||
|
||||
extern enum b_status b_status_from_errno(int error, enum b_status default_value);
|
||||
extern enum b_status b_file_info_from_stat(
|
||||
const struct stat *in, struct b_file_info *out);
|
||||
|
||||
#endif
|
||||
@@ -1,12 +0,0 @@
|
||||
#include <blue/core/status.h>
|
||||
#include <errno.h>
|
||||
|
||||
enum b_status b_status_from_errno(int error, enum b_status default_value)
|
||||
{
|
||||
switch (error) {
|
||||
case ENOENT:
|
||||
return B_ERR_NO_ENTRY;
|
||||
default:
|
||||
return default_value;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
#ifndef _IO_DARWIN_STATUS_H_
|
||||
#define _IO_DARWIN_STATUS_H_
|
||||
|
||||
#include <blue/core/status.h>
|
||||
|
||||
extern enum b_status b_status_from_errno(int error, enum b_status default_value);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user