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    /// Whether a failed `clone_range` left its target as it was. Any other
64    /// error may follow a clone of part of the range.
65    pub(crate) fn leaves_target_unchanged(&self) -> bool {
66        matches!(
67            self,
68            SmbError::NotSupported | SmbError::InvalidParameter | SmbError::NotSameDevice
69        )
70    }
71
72    /// NTSTATUS code for this error.
73    pub fn to_nt_status(&self) -> u32 {
74        match self {
75            SmbError::NotFound => ntstatus::STATUS_OBJECT_NAME_NOT_FOUND,
76            SmbError::PathNotFound => ntstatus::STATUS_OBJECT_PATH_NOT_FOUND,
77            SmbError::AccessDenied => ntstatus::STATUS_ACCESS_DENIED,
78            SmbError::Exists => ntstatus::STATUS_OBJECT_NAME_COLLISION,
79            SmbError::NotEmpty => ntstatus::STATUS_DIRECTORY_NOT_EMPTY,
80            SmbError::IsDirectory => ntstatus::STATUS_FILE_IS_A_DIRECTORY,
81            SmbError::NotADirectory => ntstatus::STATUS_NOT_A_DIRECTORY,
82            SmbError::NameInvalid => ntstatus::STATUS_OBJECT_NAME_INVALID,
83            SmbError::Sharing => ntstatus::STATUS_SHARING_VIOLATION,
84            SmbError::NotSupported => ntstatus::STATUS_NOT_SUPPORTED,
85            SmbError::NotSameDevice => ntstatus::STATUS_NOT_SAME_DEVICE,
86            SmbError::Io(_) => ntstatus::STATUS_UNEXPECTED_IO_ERROR,
87            SmbError::DiskFull => ntstatus::STATUS_DISK_FULL,
88            SmbError::QuotaExceeded => ntstatus::STATUS_DISK_QUOTA_EXCEEDED,
89            SmbError::InvalidParameter => ntstatus::STATUS_INVALID_PARAMETER,
90            SmbError::SpecialFile => ntstatus::STATUS_IO_REPARSE_TAG_NOT_HANDLED,
91            SmbError::EaTooLarge => ntstatus::STATUS_EA_TOO_LARGE,
92            SmbError::EaNameInvalid => ntstatus::STATUS_INVALID_EA_NAME,
93        }
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn maps_each_variant_to_nt_status() {
103        assert_eq!(SmbError::NotFound.to_nt_status(), 0xC000_0034);
104        assert_eq!(SmbError::PathNotFound.to_nt_status(), 0xC000_003A);
105        assert_eq!(SmbError::AccessDenied.to_nt_status(), 0xC000_0022);
106        assert_eq!(SmbError::Exists.to_nt_status(), 0xC000_0035);
107        assert_eq!(SmbError::NotEmpty.to_nt_status(), 0xC000_0101);
108        assert_eq!(SmbError::IsDirectory.to_nt_status(), 0xC000_00BA);
109        assert_eq!(SmbError::NotADirectory.to_nt_status(), 0xC000_0103);
110        assert_eq!(SmbError::NameInvalid.to_nt_status(), 0xC000_0033);
111        assert_eq!(SmbError::Sharing.to_nt_status(), 0xC000_0043);
112        assert_eq!(SmbError::NotSupported.to_nt_status(), 0xC000_00BB);
113        assert_eq!(SmbError::NotSameDevice.to_nt_status(), 0xC000_00D4);
114        assert_eq!(SmbError::InvalidParameter.to_nt_status(), 0xC000_000D);
115        assert_eq!(SmbError::SpecialFile.to_nt_status(), 0xC000_0279);
116        assert_eq!(SmbError::EaTooLarge.to_nt_status(), 0xC000_0050);
117        assert_eq!(SmbError::EaNameInvalid.to_nt_status(), 0x8000_0013);
118
119        let io_err = SmbError::Io(std::io::Error::other("boom"));
120        assert_eq!(io_err.to_nt_status(), 0xC000_00E9);
121    }
122}