|
| 1 | +// This file is part of the uutils util-linux package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | + |
| 6 | +use std::ffi::c_int; |
| 7 | +use std::fmt; |
| 8 | + |
| 9 | +use uucore::error::UError; |
| 10 | + |
| 11 | +#[derive(Debug)] |
| 12 | +pub enum LsnsError { |
| 13 | + /// Generic I/O error with context message |
| 14 | + IOError(String, std::io::Error), |
| 15 | + /// CString conversion error (null byte in string) |
| 16 | + NulError(String, std::ffi::NulError), |
| 17 | + /// Invalid namespace type index |
| 18 | + InvalidNamespaceType(usize), |
| 19 | + /// Unsupported platform |
| 20 | + #[cfg(not(target_os = "linux"))] |
| 21 | + UnsupportedPlatform, |
| 22 | + /// Invalid namespace inode format |
| 23 | + InvalidNamespaceInodeFormat(String), |
| 24 | + /// Invalid process stat format |
| 25 | + InvalidProcessStatFormat(String), |
| 26 | + /// Failed to get UID from directory entry |
| 27 | + FailedToGetUid(String), |
| 28 | + /// Failed to get PID from directory entry |
| 29 | + FailedToGetPid(String), |
| 30 | + /// Failed to read process information |
| 31 | + FailedToReadProcess(String), |
| 32 | +} |
| 33 | + |
| 34 | +impl LsnsError { |
| 35 | + /// Create an I/O error with a context message |
| 36 | + pub(crate) fn io0(message: impl Into<String>, error: impl Into<std::io::Error>) -> Self { |
| 37 | + Self::IOError(message.into(), error.into()) |
| 38 | + } |
| 39 | + |
| 40 | + /// Helper to convert negative errno to Result |
| 41 | + pub(crate) fn io_from_neg_errno( |
| 42 | + message: impl Into<String>, |
| 43 | + result: c_int, |
| 44 | + ) -> Result<usize, LsnsError> { |
| 45 | + if let Ok(result) = usize::try_from(result) { |
| 46 | + Ok(result) |
| 47 | + } else { |
| 48 | + let err = std::io::Error::from_raw_os_error(-result); |
| 49 | + Err(Self::IOError(message.into(), err)) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl fmt::Display for LsnsError { |
| 55 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 56 | + match self { |
| 57 | + Self::IOError(message, err) => write!(f, "{message}: {err}"), |
| 58 | + Self::NulError(message, err) => write!(f, "{message}: {err}"), |
| 59 | + Self::InvalidNamespaceType(idx) => write!(f, "Invalid namespace type index: {}", idx), |
| 60 | + #[cfg(not(target_os = "linux"))] |
| 61 | + Self::UnsupportedPlatform => write!(f, "lsns is only supported on Linux"), |
| 62 | + Self::InvalidNamespaceInodeFormat(s) => { |
| 63 | + write!(f, "Invalid namespace inode format: {}", s) |
| 64 | + } |
| 65 | + Self::InvalidProcessStatFormat(s) => { |
| 66 | + write!(f, "Invalid process stat format: {}", s) |
| 67 | + } |
| 68 | + Self::FailedToGetUid(s) => { |
| 69 | + write!(f, "Failed to get UID from directory entry: {}", s) |
| 70 | + } |
| 71 | + Self::FailedToGetPid(s) => { |
| 72 | + write!(f, "Failed to get PID from directory entry: {}", s) |
| 73 | + } |
| 74 | + Self::FailedToReadProcess(s) => { |
| 75 | + write!(f, "Failed to read process information: {}", s) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl UError for LsnsError { |
| 82 | + fn code(&self) -> i32 { |
| 83 | + 1 |
| 84 | + } |
| 85 | + |
| 86 | + fn usage(&self) -> bool { |
| 87 | + false |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl std::error::Error for LsnsError {} |
| 92 | + |
| 93 | +// Implement From trait for automatic conversion from std::io::Error |
| 94 | +impl From<std::io::Error> for LsnsError { |
| 95 | + fn from(err: std::io::Error) -> Self { |
| 96 | + Self::IOError(String::new(), err) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// Implement From trait for automatic conversion from std::ffi::NulError |
| 101 | +impl From<std::ffi::NulError> for LsnsError { |
| 102 | + fn from(err: std::ffi::NulError) -> Self { |
| 103 | + Self::NulError(String::new(), err) |
| 104 | + } |
| 105 | +} |
0 commit comments