Debian Codegraph @

debian/rust-vendor/selinux/src/errors.rs

raw
1 use core::num::TryFromIntError;
2 use core::str::Utf8Error;
3 use std::io;
4 use std::path::PathBuf;
5
6 use selinux_sys::pid_t;
7
8 /// Result of a fallible function.
9 pub type Result<T> = core::result::Result<T, Error>;
10
11 /// Error.
12 #[derive(thiserror::Error, Debug)]
13 #[non_exhaustive]
14 pub enum Error {
15 /// Path is invalid.
16 #[error("path is invalid: '{}'", .0.display())]
17 PathIsInvalid(PathBuf),
18
19 /// Input security contexts have different formats.
20 #[error("input security contexts have different formats")]
21 SecurityContextFormatMismatch,
22
23 /// Security context has an expected format.
24 #[error("security context has an expected format")]
25 UnexpectedSecurityContextFormat,
26
27 /// Lock was poisoned.
28 #[error("{operation} failed due to poisoned lock")]
29 LockPoisoned {
30 /// Operation.
31 operation: &'static str,
32 },
33
34 /// Input/Output operation failed.
35 #[error("{operation} failed")]
36 IO {
37 /// Cause.
38 source: io::Error,
39 /// Operation.
40 operation: &'static str,
41 },
42
43 /// Operation failed on a process.
44 #[error("{operation} failed on process with ID '{process_id}'")]
45 IO1Process {
46 /// Cause.
47 source: io::Error,
48 /// Operation.
49 operation: &'static str,
50 /// Process identifier.
51 process_id: pid_t,
52 },
53
54 /// Operation failed on a named object.
55 #[error("{operation} failed with '{name}'")]
56 IO1Name {
57 /// Cause.
58 source: io::Error,
59 /// Operation.
60 operation: &'static str,
61 /// Name.
62 name: String,
63 },
64
65 /// Operation failed on a file system object.
66 #[error("{operation} failed on path '{path}'")]
67 IO1Path {
68 /// Cause.
69 source: io::Error,
70 /// Operation.
71 operation: &'static str,
72 /// Path.
73 path: PathBuf,
74 },
75
76 /// Data is not encoded as UTF-8.
77 #[error(transparent)]
78 NotUTF8(#[from] Utf8Error),
79
80 /// Integer is out of valid range.
81 #[error(transparent)]
82 IntegerOutOfRange(#[from] TryFromIntError),
83 }
84
85 impl Error {
86 pub(crate) fn from_io(operation: &'static str, source: io::Error) -> Self {
87 Error::IO { source, operation }
88 }
89
90 pub(crate) fn last_io_error(operation: &'static str) -> Self {
91 Error::IO {
92 source: io::Error::last_os_error(),
93 operation,
94 }
95 }
96
97 pub(crate) fn from_io_pid(
98 operation: &'static str,
99 process_id: pid_t,
100 source: io::Error,
101 ) -> Self {
102 Error::IO1Process {
103 source,
104 operation,
105 process_id,
106 }
107 }
108
109 pub(crate) fn from_io_path(
110 operation: &'static str,
111 path: impl Into<PathBuf>,
112 source: io::Error,
113 ) -> Self {
114 Error::IO1Path {
115 source,
116 operation,
117 path: path.into(),
118 }
119 }
120
121 pub(crate) fn from_io_name(
122 operation: &'static str,
123 name: impl Into<String>,
124 source: io::Error,
125 ) -> Self {
126 Error::IO1Name {
127 source,
128 operation,
129 name: name.into(),
130 }
131 }
132
133 pub(crate) fn clear_errno() {
134 errno::set_errno(errno::Errno(0));
135 }
136
137 #[cfg_attr(not(test), expect(dead_code, reason = "used by unit tests"))]
138 pub(crate) fn io_source(&self) -> Option<&io::Error> {
139 match self {
140 Self::IO { source, .. }
141 | Self::IO1Process { source, .. }
142 | Self::IO1Name { source, .. }
143 | Self::IO1Path { source, .. } => Some(source),
144
145 Self::PathIsInvalid(_)
146 | Self::SecurityContextFormatMismatch
147 | Self::UnexpectedSecurityContextFormat
148 | Self::LockPoisoned { .. }
149 | Self::NotUTF8(_)
150 | Self::IntegerOutOfRange(_) => None,
151 }
152 }
153 }