132 lines
3.0 KiB
C
132 lines
3.0 KiB
C
#include <blue/core/error.h>
|
|
#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 EPERM:
|
|
case EACCES:
|
|
return B_ERR_PERMISSION_DENIED;
|
|
case ENOTSUP:
|
|
case ENOSYS:
|
|
return B_ERR_NOT_SUPPORTED;
|
|
default:
|
|
return default_value;
|
|
}
|
|
}
|
|
|
|
b_result b_result_from_errno_with_filepath(
|
|
int error, const char *path, enum b_status default_value)
|
|
{
|
|
switch (error) {
|
|
case 0:
|
|
return B_RESULT_SUCCESS;
|
|
case ENOENT:
|
|
return B_RESULT_STATUS_WITH_STRING(
|
|
B_ERR_NO_ENTRY, "Path @i{%s} does not exist", path);
|
|
case ENOTDIR:
|
|
return B_RESULT_STATUS_WITH_STRING(
|
|
B_ERR_NOT_DIRECTORY, "Path @i{%s} is not a directory",
|
|
path);
|
|
case EISDIR:
|
|
return B_RESULT_STATUS_WITH_STRING(
|
|
B_ERR_IS_DIRECTORY, "Path @i{%s} is a directory", path);
|
|
case EPERM:
|
|
case EACCES:
|
|
return B_RESULT_STATUS_WITH_STRING(
|
|
B_ERR_PERMISSION_DENIED,
|
|
"Permission denied while accessing path @i{%s}", path);
|
|
default:
|
|
return B_RESULT_STATUS(b_status_from_errno(error, default_value));
|
|
}
|
|
|
|
return B_RESULT_SUCCESS;
|
|
}
|
|
|
|
b_result b_result_from_errno_with_subfilepath(
|
|
int error, const char *path, const char *dir_path,
|
|
enum b_status default_value)
|
|
{
|
|
if (!dir_path) {
|
|
return b_result_propagate(b_result_from_errno_with_filepath(
|
|
error, path, default_value));
|
|
}
|
|
|
|
switch (error) {
|
|
case 0:
|
|
return B_RESULT_SUCCESS;
|
|
case ENOENT:
|
|
return B_RESULT_STATUS_WITH_STRING(
|
|
B_ERR_NO_ENTRY,
|
|
"Path @i{%s} in directory @i{%s} does not exist", path,
|
|
dir_path);
|
|
case ENOTDIR:
|
|
return B_RESULT_STATUS_WITH_STRING(
|
|
B_ERR_NOT_DIRECTORY,
|
|
"Path @i{%s} in directory @i{%s} is not a directory",
|
|
path, dir_path);
|
|
case EISDIR:
|
|
return B_RESULT_STATUS_WITH_STRING(
|
|
B_ERR_IS_DIRECTORY,
|
|
"Path @i{%s} in directory @i{%s} is a directory", path,
|
|
dir_path);
|
|
case EPERM:
|
|
case EACCES:
|
|
return B_RESULT_STATUS_WITH_STRING(
|
|
B_ERR_PERMISSION_DENIED,
|
|
"Permission denied while accessing path @i{%s} in "
|
|
"directory @i{%s}",
|
|
path, dir_path);
|
|
default:
|
|
return B_RESULT_STATUS(b_status_from_errno(error, default_value));
|
|
}
|
|
|
|
return B_RESULT_SUCCESS;
|
|
}
|
|
|
|
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;
|
|
}
|