forked from uutils/util-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.rs
More file actions
105 lines (94 loc) · 3.33 KB
/
Copy patherrors.rs
File metadata and controls
105 lines (94 loc) · 3.33 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// This file is part of the uutils util-linux package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use std::ffi::c_int;
use std::fmt;
use uucore::error::UError;
#[derive(Debug)]
pub enum LsnsError {
/// Generic I/O error with context message
IOError(String, std::io::Error),
/// CString conversion error (null byte in string)
NulError(String, std::ffi::NulError),
/// Invalid namespace type index
InvalidNamespaceType(usize),
/// Unsupported platform
#[cfg(not(target_os = "linux"))]
UnsupportedPlatform,
/// Invalid namespace inode format
InvalidNamespaceInodeFormat(String),
/// Invalid process stat format
InvalidProcessStatFormat(String),
/// Failed to get UID from directory entry
FailedToGetUid(String),
/// Failed to get PID from directory entry
FailedToGetPid(String),
/// Failed to read process information
FailedToReadProcess(String),
}
impl LsnsError {
/// Create an I/O error with a context message
pub(crate) fn io0(message: impl Into<String>, error: impl Into<std::io::Error>) -> Self {
Self::IOError(message.into(), error.into())
}
/// Helper to convert negative errno to Result
pub(crate) fn io_from_neg_errno(
message: impl Into<String>,
result: c_int,
) -> Result<usize, LsnsError> {
if let Ok(result) = usize::try_from(result) {
Ok(result)
} else {
let err = std::io::Error::from_raw_os_error(-result);
Err(Self::IOError(message.into(), err))
}
}
}
impl fmt::Display for LsnsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::IOError(message, err) => write!(f, "{message}: {err}"),
Self::NulError(message, err) => write!(f, "{message}: {err}"),
Self::InvalidNamespaceType(idx) => write!(f, "Invalid namespace type index: {}", idx),
#[cfg(not(target_os = "linux"))]
Self::UnsupportedPlatform => write!(f, "lsns is only supported on Linux"),
Self::InvalidNamespaceInodeFormat(s) => {
write!(f, "Invalid namespace inode format: {}", s)
}
Self::InvalidProcessStatFormat(s) => {
write!(f, "Invalid process stat format: {}", s)
}
Self::FailedToGetUid(s) => {
write!(f, "Failed to get UID from directory entry: {}", s)
}
Self::FailedToGetPid(s) => {
write!(f, "Failed to get PID from directory entry: {}", s)
}
Self::FailedToReadProcess(s) => {
write!(f, "Failed to read process information: {}", s)
}
}
}
}
impl UError for LsnsError {
fn code(&self) -> i32 {
1
}
fn usage(&self) -> bool {
false
}
}
impl std::error::Error for LsnsError {}
// Implement From trait for automatic conversion from std::io::Error
impl From<std::io::Error> for LsnsError {
fn from(err: std::io::Error) -> Self {
Self::IOError(String::new(), err)
}
}
// Implement From trait for automatic conversion from std::ffi::NulError
impl From<std::ffi::NulError> for LsnsError {
fn from(err: std::ffi::NulError) -> Self {
Self::NulError(String::new(), err)
}
}