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    #[error("disk full")]
42    DiskFull,
43    #[error("disk quota exceeded")]
44    QuotaExceeded,
45    /// The request carries a value the backend does not take, such as data
46    /// a stream with a fixed layout cannot hold.
47    #[error("invalid parameter")]
48    InvalidParameter,
49    /// The name is a FIFO, a socket or a device node, which the backend
50    /// does not open.
51    #[error("not a regular file or directory")]
52    SpecialFile,
53    /// The extended attributes of a file would take more room than the
54    /// backend gives them.
55    #[error("extended attributes too large")]
56    EaTooLarge,
57    /// The backend cannot store an extended attribute under this name.
58    #[error("invalid extended attribute name")]
59    EaNameInvalid,
60}
61
62impl SmbError {
63    /// NTSTATUS code for this error.
64    pub fn to_nt_status(&self) -> u32 {
65        match self {
66            SmbError::NotFound => ntstatus::STATUS_OBJECT_NAME_NOT_FOUND,
67            SmbError::PathNotFound => ntstatus::STATUS_OBJECT_PATH_NOT_FOUND,
68            SmbError::AccessDenied => ntstatus::STATUS_ACCESS_DENIED,
69            SmbError::Exists => ntstatus::STATUS_OBJECT_NAME_COLLISION,
70            SmbError::NotEmpty => ntstatus::STATUS_DIRECTORY_NOT_EMPTY,
71            SmbError::IsDirectory => ntstatus::STATUS_FILE_IS_A_DIRECTORY,
72            SmbError::NotADirectory => ntstatus::STATUS_NOT_A_DIRECTORY,
73            SmbError::NameInvalid => ntstatus::STATUS_OBJECT_NAME_INVALID,
74            SmbError::Sharing => ntstatus::STATUS_SHARING_VIOLATION,
75            SmbError::NotSupported => ntstatus::STATUS_NOT_SUPPORTED,
76            SmbError::NotSameDevice => ntstatus::STATUS_NOT_SAME_DEVICE,
77            SmbError::Io(_) => ntstatus::STATUS_UNEXPECTED_IO_ERROR,
78            SmbError::DiskFull => ntstatus::STATUS_DISK_FULL,
79            SmbError::QuotaExceeded => ntstatus::STATUS_DISK_QUOTA_EXCEEDED,
80            SmbError::InvalidParameter => ntstatus::STATUS_INVALID_PARAMETER,
81            SmbError::SpecialFile => ntstatus::STATUS_IO_REPARSE_TAG_NOT_HANDLED,
82            SmbError::EaTooLarge => ntstatus::STATUS_EA_TOO_LARGE,
83            SmbError::EaNameInvalid => ntstatus::STATUS_INVALID_EA_NAME,
84        }
85    }
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[test]
93    fn maps_each_variant_to_nt_status() {
94        assert_eq!(SmbError::NotFound.to_nt_status(), 0xC000_0034);
95        assert_eq!(SmbError::PathNotFound.to_nt_status(), 0xC000_003A);
96        assert_eq!(SmbError::AccessDenied.to_nt_status(), 0xC000_0022);
97        assert_eq!(SmbError::Exists.to_nt_status(), 0xC000_0035);
98        assert_eq!(SmbError::NotEmpty.to_nt_status(), 0xC000_0101);
99        assert_eq!(SmbError::IsDirectory.to_nt_status(), 0xC000_00BA);
100        assert_eq!(SmbError::NotADirectory.to_nt_status(), 0xC000_0103);
101        assert_eq!(SmbError::NameInvalid.to_nt_status(), 0xC000_0033);
102        assert_eq!(SmbError::Sharing.to_nt_status(), 0xC000_0043);
103        assert_eq!(SmbError::NotSupported.to_nt_status(), 0xC000_00BB);
104        assert_eq!(SmbError::NotSameDevice.to_nt_status(), 0xC000_00D4);
105        assert_eq!(SmbError::InvalidParameter.to_nt_status(), 0xC000_000D);
106        assert_eq!(SmbError::SpecialFile.to_nt_status(), 0xC000_0279);
107        assert_eq!(SmbError::EaTooLarge.to_nt_status(), 0xC000_0050);
108        assert_eq!(SmbError::EaNameInvalid.to_nt_status(), 0x8000_0013);
109
110        let io_err = SmbError::Io(std::io::Error::other("boom"));
111        assert_eq!(io_err.to_nt_status(), 0xC000_00E9);
112    }
113}