io: implement b_error support for file/directory operations
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
#include <blue/core/error.h>
|
||||
#include <blue/core/status.h>
|
||||
#include <blue/io/file.h>
|
||||
#include <errno.h>
|
||||
@@ -23,6 +24,7 @@ enum b_status b_status_from_errno(int error, enum b_status default_value)
|
||||
case ENOTDIR:
|
||||
return B_ERR_NOT_DIRECTORY;
|
||||
case EPERM:
|
||||
case EACCES:
|
||||
return B_ERR_PERMISSION_DENIED;
|
||||
case ENOTSUP:
|
||||
case ENOSYS:
|
||||
@@ -32,6 +34,75 @@ enum b_status b_status_from_errno(int error, enum b_status 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;
|
||||
|
||||
Reference in New Issue
Block a user