Skip to main content

smb_server/
error.rs

1//! Backend error type and its NTSTATUS mapping.
2
3use thiserror::Error;
4
5use crate::ntstatus;
6
7pub type SmbResult<T> = Result<T, SmbError>;
8
9/// Errors returned by `ShareBackend` and surfaced through the SMB protocol.
10///
11/// `to_nt_status` maps each variant to one NTSTATUS code. Protocol-layer
12/// failures (malformed frames, bad signatures) are handled by the dispatcher
13/// and never become `SmbError`.
14#[derive(Debug, Error)]
15#[non_exhaustive]
16pub enum SmbError {
17    #[error("not found")]
18    NotFound,
19    #[error("path not found")]
20    PathNotFound,
21    #[error("access denied")]
22    AccessDenied,
23    #[error("exists")]
24    Exists,
25    #[error("not empty")]
26    NotEmpty,
27    #[error("is a directory")]
28    IsDirectory,
29    #[error("not a directory")]
30    NotADirectory,
31    #[error("name too long / invalid")]
32    NameInvalid,
33    #[error("sharing violation")]
34    Sharing,
35    #[error("not supported")]
36    NotSupported,
37    #[error("source and target are on different devices")]
38    NotSameDevice,
39    #[error("io: {0}")]
40    Io(#[from] std::io::Error),
41}
42
43impl SmbError {
44    /// NTSTATUS code for this error.
45    pub fn to_nt_status(&self) -> u32 {
46        match self {
47            SmbError::NotFound => ntstatus::STATUS_OBJECT_NAME_NOT_FOUND,
48            SmbError::PathNotFound => ntstatus::STATUS_OBJECT_PATH_NOT_FOUND,
49            SmbError::AccessDenied => ntstatus::STATUS_ACCESS_DENIED,
50            SmbError::Exists => ntstatus::STATUS_OBJECT_NAME_COLLISION,
51            SmbError::NotEmpty => ntstatus::STATUS_DIRECTORY_NOT_EMPTY,
52            SmbError::IsDirectory => ntstatus::STATUS_FILE_IS_A_DIRECTORY,
53            SmbError::NotADirectory => ntstatus::STATUS_NOT_A_DIRECTORY,
54            SmbError::NameInvalid => ntstatus::STATUS_OBJECT_NAME_INVALID,
55            SmbError::Sharing => ntstatus::STATUS_SHARING_VIOLATION,
56            SmbError::NotSupported => ntstatus::STATUS_NOT_SUPPORTED,
57            SmbError::NotSameDevice => ntstatus::STATUS_NOT_SAME_DEVICE,
58            SmbError::Io(_) => ntstatus::STATUS_UNEXPECTED_IO_ERROR,
59        }
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn maps_each_variant_to_nt_status() {
69        assert_eq!(SmbError::NotFound.to_nt_status(), 0xC000_0034);
70        assert_eq!(SmbError::PathNotFound.to_nt_status(), 0xC000_003A);
71        assert_eq!(SmbError::AccessDenied.to_nt_status(), 0xC000_0022);
72        assert_eq!(SmbError::Exists.to_nt_status(), 0xC000_0035);
73        assert_eq!(SmbError::NotEmpty.to_nt_status(), 0xC000_0101);
74        assert_eq!(SmbError::IsDirectory.to_nt_status(), 0xC000_00BA);
75        assert_eq!(SmbError::NotADirectory.to_nt_status(), 0xC000_0103);
76        assert_eq!(SmbError::NameInvalid.to_nt_status(), 0xC000_0033);
77        assert_eq!(SmbError::Sharing.to_nt_status(), 0xC000_0043);
78        assert_eq!(SmbError::NotSupported.to_nt_status(), 0xC000_00BB);
79        assert_eq!(SmbError::NotSameDevice.to_nt_status(), 0xC000_00D4);
80
81        let io_err = SmbError::Io(std::io::Error::other("boom"));
82        assert_eq!(io_err.to_nt_status(), 0xC000_00E9);
83    }
84}