#include #include #include #include #include enum fx_status fx_status_from_errno(int error, enum fx_status default_value) { switch (error) { case 0: return FX_SUCCESS; case ENOENT: return FX_ERR_NO_ENTRY; case EEXIST: return FX_ERR_NAME_EXISTS; case ENOMEM: return FX_ERR_NO_MEMORY; case EINVAL: return FX_ERR_INVALID_ARGUMENT; case EIO: return FX_ERR_IO_FAILURE; case EISDIR: return FX_ERR_IS_DIRECTORY; case ENOTDIR: return FX_ERR_NOT_DIRECTORY; case EPERM: case EACCES: return FX_ERR_PERMISSION_DENIED; case ENOTSUP: case ENOSYS: return FX_ERR_NOT_SUPPORTED; default: return default_value; } } fx_result fx_result_from_errno_with_filepath( int error, const char *path, enum fx_status default_value) { switch (error) { case 0: return FX_RESULT_SUCCESS; case ENOENT: return FX_RESULT_STATUS_WITH_STRING( FX_ERR_NO_ENTRY, "Path @i{%s} does not exist", path); case ENOTDIR: return FX_RESULT_STATUS_WITH_STRING( FX_ERR_NOT_DIRECTORY, "Path @i{%s} is not a directory", path); case EISDIR: return FX_RESULT_STATUS_WITH_STRING( FX_ERR_IS_DIRECTORY, "Path @i{%s} is a directory", path); case EPERM: case EACCES: return FX_RESULT_STATUS_WITH_STRING( FX_ERR_PERMISSION_DENIED, "Permission denied while accessing path @i{%s}", path); default: return FX_RESULT_STATUS(fx_status_from_errno(error, default_value)); } return FX_RESULT_SUCCESS; } fx_result fx_result_from_errno_with_subfilepath( int error, const char *path, const char *dir_path, enum fx_status default_value) { if (!dir_path) { return fx_result_propagate(fx_result_from_errno_with_filepath( error, path, default_value)); } switch (error) { case 0: return FX_RESULT_SUCCESS; case ENOENT: return FX_RESULT_STATUS_WITH_STRING( FX_ERR_NO_ENTRY, "Path @i{%s} in directory @i{%s} does not exist", path, dir_path); case ENOTDIR: return FX_RESULT_STATUS_WITH_STRING( FX_ERR_NOT_DIRECTORY, "Path @i{%s} in directory @i{%s} is not a directory", path, dir_path); case EISDIR: return FX_RESULT_STATUS_WITH_STRING( FX_ERR_IS_DIRECTORY, "Path @i{%s} in directory @i{%s} is a directory", path, dir_path); case EPERM: case EACCES: return FX_RESULT_STATUS_WITH_STRING( FX_ERR_PERMISSION_DENIED, "Permission denied while accessing path @i{%s} in " "directory @i{%s}", path, dir_path); default: return FX_RESULT_STATUS(fx_status_from_errno(error, default_value)); } return FX_RESULT_SUCCESS; } enum fx_status fx_file_info_from_stat(const struct stat *st, struct fx_file_info *out) { out->length = st->st_size; if (S_ISREG(st->st_mode)) { out->attrib |= FX_FILE_ATTRIB_NORMAL; } if (S_ISDIR(st->st_mode)) { out->attrib |= FX_FILE_ATTRIB_DIRECTORY; } if (S_ISBLK(st->st_mode)) { out->attrib |= FX_FILE_ATTRIB_BLOCK_DEVICE; } if (S_ISCHR(st->st_mode)) { out->attrib |= FX_FILE_ATTRIB_CHAR_DEVICE; } if (S_ISLNK(st->st_mode)) { out->attrib |= FX_FILE_ATTRIB_SYMLINK; } return FX_SUCCESS; }