1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FsError {
NotFound,
NotADirectory,
IsADirectory,
InvalidArgument,
BadFileDescriptor,
PermissionDenied,
AlreadyExists,
NotEmpty,
}
impl FsError {
/// Convert error to errno-style error code
pub fn to_errno(&self) -> i32 {
match self {
FsError::NotFound => -2, // ENOENT
FsError::NotADirectory => -20, // ENOTDIR
FsError::IsADirectory => -21, // EISDIR
FsError::InvalidArgument => -22, // EINVAL
FsError::BadFileDescriptor => -9, // EBADF
FsError::PermissionDenied => -13, // EACCES
FsError::AlreadyExists => -17, // EEXIST
FsError::NotEmpty => -39, // ENOTEMPTY
}
}
/// Convert error to WASI error-code (u8)
/// Reference: WASI Preview 2 filesystem error-code enum
/// https://github.com/WebAssembly/wasi-filesystem/blob/main/wit/types.wit
pub fn to_wasi_error_code(&self) -> u8 {
match self {
FsError::NotFound => 44, // noent
FsError::NotADirectory => 54, // notdir
FsError::IsADirectory => 31, // isdir
FsError::InvalidArgument => 28, // inval
FsError::BadFileDescriptor => 8, // badf
FsError::PermissionDenied => 2, // access
FsError::AlreadyExists => 20, // exist
FsError::NotEmpty => 55, // notempty
}
}
}