1use std::{num::TryFromIntError, path::PathBuf};
2
3const MLOCK_HINT: &str = "\
4\n\nPossible fixes:\
5\n ulimit -l unlimited\
6\n setcap cap_ipc_lock+ep <binary>\
7\n # Kubernetes:\
8\n securityContext:\
9\n capabilities:\
10\n add: [\"IPC_LOCK\"]";
11
12#[derive(Debug, thiserror::Error)]
13pub enum MlockError {
14 #[error("{call} failed: permission denied (EPERM){}", MLOCK_HINT)]
15 PermissionDenied { call: &'static str },
16
17 #[error(
18 "{call} failed: cannot lock {len} bytes — RLIMIT_MEMLOCK too low (ENOMEM){}",
19 MLOCK_HINT
20 )]
21 OutOfMemory { call: &'static str, len: usize },
22
23 #[error("{call}: {source}")]
24 Other {
25 call: &'static str,
26 source: std::io::Error,
27 },
28}
29
30#[derive(Debug, thiserror::Error)]
31pub enum Error {
32 #[error("operation cancelled")]
33 Cancelled,
34
35 #[error("file worker thread panicked")]
36 WorkerPanic,
37
38 #[error("{context}: {source}")]
39 Io {
40 context: String,
41 source: std::io::Error,
42 },
43
44 #[error(transparent)]
45 Mlock(#[from] MlockError),
46
47 #[error("{0}")]
48 Syscall(#[from] nix::errno::Errno),
49
50 #[error("{0}")]
51 TryFromInt(#[from] TryFromIntError),
52
53 #[error("invalid path pattern: {0}")]
54 PathPattern(#[from] ignore::Error),
55
56 #[error("{path}: offset {offset} beyond file size {file_len}")]
57 OffsetBeyondFile {
58 path: PathBuf,
59 offset: u64,
60 file_len: u64,
61 },
62
63 #[error("range length must be greater than zero")]
64 EmptyRange,
65
66 #[error("range offset {offset} is not aligned to page size {page_size}")]
67 UnalignedRange { offset: u64, page_size: u64 },
68
69 #[error("range end overflows: offset {offset}, length {max_len}")]
70 RangeOverflow { offset: u64, max_len: u64 },
71}
72
73impl Error {
74 pub fn io(context: impl Into<String>, source: std::io::Error) -> Self {
75 Self::Io {
76 context: context.into(),
77 source,
78 }
79 }
80}
81
82impl From<std::io::Error> for Error {
83 fn from(source: std::io::Error) -> Self {
84 Self::Io {
85 context: String::new(),
86 source,
87 }
88 }
89}
90
91pub type Result<T> = std::result::Result<T, Error>;
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96
97 #[test]
98 fn test_io_error_display_with_context() {
99 let err = Error::io(
100 "/tmp/test.dat",
101 std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"),
102 );
103 let msg = err.to_string();
104 assert!(msg.contains("/tmp/test.dat"), "msg: {msg}");
105 assert!(msg.contains("file not found"), "msg: {msg}");
106 }
107
108 #[test]
109 fn test_io_error_from_std() {
110 let io_err = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "denied");
111 let err: Error = io_err.into();
112 assert!(matches!(err, Error::Io { .. }));
113 }
114
115 #[test]
116 fn test_syscall_error_from_errno() {
117 let err: Error = nix::errno::Errno::EBADF.into();
118 assert!(matches!(err, Error::Syscall(_)));
119 assert!(err.to_string().contains("EBADF"));
120 }
121
122 #[test]
123 fn test_offset_beyond_file_display() {
124 let err = Error::OffsetBeyondFile {
125 path: PathBuf::from("/data/big.bin"),
126 offset: 1000,
127 file_len: 500,
128 };
129 let msg = err.to_string();
130 assert!(msg.contains("/data/big.bin"), "msg: {msg}");
131 assert!(msg.contains("1000"), "msg: {msg}");
132 assert!(msg.contains("500"), "msg: {msg}");
133 }
134}