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}
46
47impl SmbError {
48    /// NTSTATUS code for this error.
49    pub fn to_nt_status(&self) -> u32 {
50        match self {
51            SmbError::NotFound => ntstatus::STATUS_OBJECT_NAME_NOT_FOUND,
52            SmbError::PathNotFound => ntstatus::STATUS_OBJECT_PATH_NOT_FOUND,
53            SmbError::AccessDenied => ntstatus::STATUS_ACCESS_DENIED,
54            SmbError::Exists => ntstatus::STATUS_OBJECT_NAME_COLLISION,
55            SmbError::NotEmpty => ntstatus::STATUS_DIRECTORY_NOT_EMPTY,
56            SmbError::IsDirectory => ntstatus::STATUS_FILE_IS_A_DIRECTORY,
57            SmbError::NotADirectory => ntstatus::STATUS_NOT_A_DIRECTORY,
58            SmbError::NameInvalid => ntstatus::STATUS_OBJECT_NAME_INVALID,
59            SmbError::Sharing => ntstatus::STATUS_SHARING_VIOLATION,
60            SmbError::NotSupported => ntstatus::STATUS_NOT_SUPPORTED,
61            SmbError::NotSameDevice => ntstatus::STATUS_NOT_SAME_DEVICE,
62            SmbError::Io(_) => ntstatus::STATUS_UNEXPECTED_IO_ERROR,
63            SmbError::DiskFull => ntstatus::STATUS_DISK_FULL,
64            SmbError::QuotaExceeded => ntstatus::STATUS_DISK_QUOTA_EXCEEDED,
65        }
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn maps_each_variant_to_nt_status() {
75        assert_eq!(SmbError::NotFound.to_nt_status(), 0xC000_0034);
76        assert_eq!(SmbError::PathNotFound.to_nt_status(), 0xC000_003A);
77        assert_eq!(SmbError::AccessDenied.to_nt_status(), 0xC000_0022);
78        assert_eq!(SmbError::Exists.to_nt_status(), 0xC000_0035);
79        assert_eq!(SmbError::NotEmpty.to_nt_status(), 0xC000_0101);
80        assert_eq!(SmbError::IsDirectory.to_nt_status(), 0xC000_00BA);
81        assert_eq!(SmbError::NotADirectory.to_nt_status(), 0xC000_0103);
82        assert_eq!(SmbError::NameInvalid.to_nt_status(), 0xC000_0033);
83        assert_eq!(SmbError::Sharing.to_nt_status(), 0xC000_0043);
84        assert_eq!(SmbError::NotSupported.to_nt_status(), 0xC000_00BB);
85        assert_eq!(SmbError::NotSameDevice.to_nt_status(), 0xC000_00D4);
86
87        let io_err = SmbError::Io(std::io::Error::other("boom"));
88        assert_eq!(io_err.to_nt_status(), 0xC000_00E9);
89    }
90}