standard_lib/fs/file/
error.rs

1use std::io::RawOsError;
2
3use derive_more::{Display, Error, From};
4
5use libc::EOPNOTSUPP;
6use libc::{EACCES, EBADF, EBUSY, EDQUOT, EEXIST, EFAULT, EFBIG, EINTR, EINVAL, EISDIR, ELOOP, EMFILE, ENAMETOOLONG, ENFILE, ENODEV, ENOENT, ENOMEM, ENOSPC, ENOTDIR, ENXIO, EOVERFLOW, EPERM, EROFS, ETXTBSY};
7
8use crate::fs::error::*;
9use crate::fs::panic::*;
10
11#[derive(Debug, Display, Clone, From, Error)]
12#[display("error closing file or directory: {_0}")]
13pub enum CloseError {
14    Interrupt(InterruptError),
15    IO(IOError),
16    StorageExhausted(StorageExhaustedError),
17}
18
19#[derive(Debug, Display, Clone, From, Error)]
20#[display("error syncing file to disk: {_0}")]
21pub enum SyncError {
22    Interrupt(InterruptError),
23    IO(IOError),
24    StorageExhausted(StorageExhaustedError),
25    SyncUnsupported(SyncUnsupportedError),
26}
27
28#[derive(Debug, Display, Clone, From, Error)]
29#[display("error cloning file or directory: {_0}")]
30pub enum CloneError {
31    FileCount(FileCountError),
32    OOM(OOMError),
33}
34
35#[derive(Debug, Display, Clone, From, Error)]
36#[display("error locking file: {_0}")]
37pub enum LockError {
38    Interrupt(InterruptError),
39    LockMem(LockMemError),
40}
41
42#[derive(Debug, Display, Clone, From, Error)]
43#[display("error locking file: {_0}")]
44pub enum TryLockError {
45    Interrupt(InterruptError),
46    LockMem(LockMemError),
47    WouldBlock(WouldBlockError),
48}
49
50#[derive(Debug, Display, Clone, From, Error)]
51#[display("error retrieving metadata: {_0}")]
52pub enum MetadataError {
53    OOM(OOMError),
54    MetadataOverflow(MetadataOverflowError),
55}
56
57#[derive(Debug, Display, Clone, From, Error)]
58pub(crate) enum FileTypeError {
59    OOM(OOMError),
60    IncorrectType(IncorrectTypeError),
61    MetadataOverflow(MetadataOverflowError),
62}
63
64impl From<MetadataError> for FileTypeError {
65    fn from(value: MetadataError) -> Self {
66        match value {
67            MetadataError::OOM(e) => e.into(),
68            MetadataError::MetadataOverflow(e) => e.into(),
69        }
70    }
71}
72
73#[derive(Debug, Display, Clone, From, Error)]
74#[display("error opening file or directory: {_0}")]
75pub enum OpenError {
76    Access(AccessError),
77    Interrupt(InterruptError),
78    StorageExhausted(StorageExhaustedError),
79    OversizedFile(OversizedFileError),
80    IncorrectType(IncorrectTypeError),
81    InvalidBasename(InvalidBasenameError),
82    ExcessiveLinks(ExcessiveLinksError),
83    FileCount(FileCountError),
84    PathLength(PathLengthError),
85    MetadataOverflow(MetadataOverflowError),
86    MissingComponent(MissingComponentError),
87    OOM(OOMError),
88    NonDirComponent(NonDirComponentError),
89    Permission(PermissionError),
90    ReadOnlyFS(ReadOnlyFSError),
91    BusyExecutable(BusyExecutableError),
92}
93
94impl OpenError {
95    pub(crate) fn interpret_raw_error(error: RawOsError) -> Self {
96        match error {
97            EACCES            => AccessError.into(),
98            EBADF             => BadFdPanic.panic(),
99            EBUSY | EISDIR | ENODEV | ENXIO => IncorrectTypeError.into(),
100            EDQUOT | ENOSPC   => StorageExhaustedError.into(),
101            EFAULT            => BadStackAddrPanic.panic(),
102            EFBIG | EOVERFLOW => OversizedFileError.into(),
103            EINTR             => InterruptError.into(),
104            EINVAL            => InvalidBasenameError.into(),
105            ELOOP             => ExcessiveLinksError.into(),
106            EMFILE | ENFILE   => FileCountError.into(),
107            ENAMETOOLONG      => PathLengthError.into(),
108            ENOENT            => MissingComponentError.into(),
109            ENOMEM            => OOMError.into(),
110            ENOTDIR           => NonDirComponentError.into(),
111            EPERM             => PermissionError.into(),
112            EROFS             => ReadOnlyFSError.into(),
113            ETXTBSY           => BusyExecutableError.into(),
114            e                 => UnexpectedErrorPanic(e).panic(),
115        }
116    }
117}
118
119impl From<FileTypeError> for OpenError {
120    fn from(value: FileTypeError) -> Self {
121        match value {
122            FileTypeError::OOM(e) => e.into(),
123            FileTypeError::IncorrectType(e) => e.into(),
124            FileTypeError::MetadataOverflow(e) => e.into(),
125        }
126    }
127}
128
129#[derive(Debug, Display, Clone, From, Error)]
130#[display("error creating file: {_0}")]
131pub enum CreateError {
132    Access(AccessError),
133    Interrupt(InterruptError),
134    StorageExhausted(StorageExhaustedError),
135    InvalidBasename(InvalidBasenameError),
136    ExcessiveLinks(ExcessiveLinksError),
137    FileCount(FileCountError),
138    PathLength(PathLengthError),
139    MissingComponent(MissingComponentError),
140    OOM(OOMError),
141    NonDirComponent(NonDirComponentError),
142    Permission(PermissionError),
143    ReadOnlyFS(ReadOnlyFSError),
144    BusyExecutable(BusyExecutableError),
145    AlreadyExists(AlreadyExistsError),
146}
147
148impl CreateError {
149    pub(crate) fn interpret_raw_error(error: RawOsError) -> Self {
150        match error {
151            EACCES          => AccessError.into(),
152            EBADF           => BadFdPanic.panic(),
153            EBUSY | EEXIST | EFBIG | EISDIR | ENODEV | ENXIO | EOVERFLOW => AlreadyExistsError.into(),
154            EDQUOT | ENOSPC => StorageExhaustedError.into(),
155            EFAULT          => BadStackAddrPanic.panic(),
156            EINTR           => InterruptError.into(),
157            EINVAL          => InvalidBasenameError.into(),
158            ELOOP           => ExcessiveLinksError.into(),
159            EMFILE | ENFILE => FileCountError.into(),
160            ENAMETOOLONG    => PathLengthError.into(),
161            ENOENT          => MissingComponentError.into(),
162            ENOMEM          => OOMError.into(),
163            ENOTDIR         => NonDirComponentError.into(),
164            EPERM           => PermissionError.into(),
165            EROFS           => ReadOnlyFSError.into(),
166            ETXTBSY         => BusyExecutableError.into(),
167            e               => UnexpectedErrorPanic(e).panic(),
168        }
169    }
170}
171
172#[derive(Debug, Display, Clone, From, Error)]
173#[display("error creating temporary file: {_0}")]
174pub enum TempError {
175    Access(AccessError),
176    Interrupt(InterruptError),
177    StorageExhausted(StorageExhaustedError),
178    OversizedFile(OversizedFileError),
179    InvalidBasename(InvalidBasenameError),
180    ExcessiveLinks(ExcessiveLinksError),
181    FileCount(FileCountError),
182    PathLength(PathLengthError),
183    MissingComponent(MissingComponentError),
184    OOM(OOMError),
185    NonDirComponent(NonDirComponentError),
186    Permission(PermissionError),
187    ReadOnlyFS(ReadOnlyFSError),
188    BusyExecutable(BusyExecutableError),
189    TempFileUnsupported(TempFileUnsupportedError),
190}
191
192impl TempError {
193    pub(crate) fn interpret_raw_error(error: RawOsError) -> Self {
194        match error {
195            EACCES          => AccessError.into(),
196            EBADF           => BadFdPanic.panic(),
197            EBUSY | EFBIG | ENODEV | ENXIO | EISDIR | EOPNOTSUPP | EOVERFLOW => TempFileUnsupportedError.into(),
198            EDQUOT | ENOSPC => StorageExhaustedError.into(),
199            EFAULT          => BadStackAddrPanic.panic(),
200            EINTR           => InterruptError.into(),
201            EINVAL          => InvalidFlagPanic.panic(),
202            ELOOP           => ExcessiveLinksError.into(),
203            EMFILE | ENFILE => FileCountError.into(),
204            ENAMETOOLONG    => PathLengthError.into(),
205            ENOENT          => MissingComponentError.into(),
206            ENOMEM          => OOMError.into(),
207            ENOTDIR         => NonDirComponentError.into(),
208            EPERM           => PermissionError.into(),
209            EROFS           => ReadOnlyFSError.into(),
210            ETXTBSY         => BusyExecutableError.into(),
211            e               => UnexpectedErrorPanic(e).panic(),
212        }
213    }
214}