use async_trait::async_trait;
use std::time::SystemTime;
use crate::error::{SmbError, SmbResult};
use crate::path::SmbPath;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum OpenIntent {
Open,
Create,
OpenOrCreate,
OverwriteOrCreate,
Truncate,
}
#[derive(Debug, Clone, Copy)]
pub struct OpenOptions {
pub read: bool,
pub write: bool,
pub write_attributes: bool,
pub intent: OpenIntent,
pub directory: bool,
pub non_directory: bool,
pub delete_on_close: bool,
}
impl Default for OpenOptions {
fn default() -> Self {
Self {
read: true,
write: false,
write_attributes: false,
intent: OpenIntent::Open,
directory: false,
non_directory: false,
delete_on_close: false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OpenAction {
Opened,
Created,
Overwritten,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct FileInfo {
pub name: String,
pub end_of_file: u64,
pub allocation_size: u64,
pub creation_time: u64,
pub last_access_time: u64,
pub last_write_time: u64,
pub change_time: u64,
pub is_directory: bool,
pub read_only: bool,
pub file_index: u64,
}
pub(crate) const FILE_ATTRIBUTE_READONLY: u32 = 0x0000_0001;
pub(crate) const FILE_ATTRIBUTE_HIDDEN: u32 = 0x0000_0002;
pub(crate) const FILE_ATTRIBUTE_SYSTEM: u32 = 0x0000_0004;
pub(crate) const FILE_ATTRIBUTE_DIRECTORY: u32 = 0x0000_0010;
pub(crate) const FILE_ATTRIBUTE_ARCHIVE: u32 = 0x0000_0020;
pub(crate) const FILE_ATTRIBUTE_NORMAL: u32 = 0x0000_0080;
pub(crate) const FILE_ATTRIBUTE_TEMPORARY: u32 = 0x0000_0100;
pub(crate) const FILE_ATTRIBUTE_OFFLINE: u32 = 0x0000_1000;
pub(crate) const FILE_ATTRIBUTE_NOT_CONTENT_INDEXED: u32 = 0x0000_2000;
pub(crate) const VALID_SET_ATTRIBUTES: u32 = FILE_ATTRIBUTE_READONLY
| FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_ARCHIVE
| FILE_ATTRIBUTE_TEMPORARY
| FILE_ATTRIBUTE_OFFLINE
| FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
impl FileInfo {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
end_of_file: 0,
allocation_size: 0,
creation_time: 0,
last_access_time: 0,
last_write_time: 0,
change_time: 0,
is_directory: false,
read_only: false,
file_index: 0,
}
}
pub fn attributes(&self) -> u32 {
let mut attributes = 0;
if self.is_directory {
attributes |= FILE_ATTRIBUTE_DIRECTORY;
}
if self.read_only {
attributes |= FILE_ATTRIBUTE_READONLY;
}
if attributes == 0 {
FILE_ATTRIBUTE_NORMAL
} else {
attributes
}
}
}
#[derive(Debug, Clone)]
pub struct DirEntry {
pub info: FileInfo,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct FileTimes {
pub creation_time: Option<u64>,
pub last_access_time: Option<u64>,
pub last_write_time: Option<u64>,
pub change_time: Option<u64>,
}
impl FileTimes {
pub fn all(t: SystemTime) -> Self {
let ft = crate::utils::system_time_to_filetime(t);
Self {
creation_time: Some(ft),
last_access_time: Some(ft),
last_write_time: Some(ft),
change_time: Some(ft),
}
}
}
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct BackendCapabilities {
pub is_read_only: bool,
pub case_sensitive: bool,
}
#[async_trait]
pub trait ShareBackend: Send + Sync + 'static {
async fn open(
&self,
path: &SmbPath,
opts: OpenOptions,
) -> SmbResult<(Box<dyn Handle>, OpenAction)>;
async fn unlink(&self, path: &SmbPath) -> SmbResult<()>;
async fn rename(&self, from: &SmbPath, to: &SmbPath, replace_if_exists: bool) -> SmbResult<()>;
fn capabilities(&self) -> BackendCapabilities;
async fn volume_info(&self) -> SmbResult<VolumeInfo> {
Ok(VolumeInfo::default())
}
}
#[async_trait]
pub trait Handle: Send + Sync {
async fn read(&self, offset: u64, len: u32) -> SmbResult<bytes::Bytes>;
async fn write(&self, offset: u64, data: &[u8]) -> SmbResult<u32>;
async fn write_owned(&self, offset: u64, data: Vec<u8>) -> SmbResult<u32> {
self.write(offset, &data).await
}
async fn flush(&self) -> SmbResult<()>;
async fn stat(&self) -> SmbResult<FileInfo>;
async fn set_times(&self, times: FileTimes) -> SmbResult<()>;
async fn set_attributes(&self, attributes: u32) -> SmbResult<()>;
async fn truncate(&self, len: u64) -> SmbResult<()>;
async fn list_dir(&self, pattern: Option<&str>) -> SmbResult<Vec<DirEntry>>;
async fn close(self: Box<Self>) -> SmbResult<()>;
async fn link_count(&self) -> SmbResult<u32> {
Ok(1)
}
async fn dir_is_empty(&self) -> SmbResult<bool> {
Ok(self.list_dir(None).await?.is_empty())
}
async fn watch(&self) -> SmbResult<Option<Box<dyn DirWatch>>> {
Ok(None)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum DirChangeKind {
Added,
Removed,
Renamed { from: String },
Modified,
AttributesChanged,
Lost,
Moved,
Deleted,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct DirChange {
pub name: String,
pub kind: DirChangeKind,
pub is_directory: bool,
}
impl DirChange {
pub fn new(name: impl Into<String>, kind: DirChangeKind, is_directory: bool) -> Self {
Self {
name: name.into(),
kind,
is_directory,
}
}
}
#[async_trait]
pub trait DirWatch: Send {
async fn next(&mut self) -> Option<Vec<DirChange>>;
}
pub(crate) struct NotSupportedBackend;
#[async_trait]
impl ShareBackend for NotSupportedBackend {
async fn open(
&self,
_path: &SmbPath,
_opts: OpenOptions,
) -> SmbResult<(Box<dyn Handle>, OpenAction)> {
Err(SmbError::NotSupported)
}
async fn unlink(&self, _path: &SmbPath) -> SmbResult<()> {
Err(SmbError::NotSupported)
}
async fn rename(&self, _from: &SmbPath, _to: &SmbPath, _replace: bool) -> SmbResult<()> {
Err(SmbError::NotSupported)
}
fn capabilities(&self) -> BackendCapabilities {
BackendCapabilities {
is_read_only: true,
case_sensitive: false,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct VolumeInfo {
pub total_allocation_units: u64,
pub caller_available_allocation_units: u64,
pub actual_available_allocation_units: u64,
pub sectors_per_allocation_unit: u32,
pub bytes_per_sector: u32,
pub serial_number: u32,
pub label: Option<String>,
pub max_name_length: u32,
}
impl Default for VolumeInfo {
fn default() -> Self {
Self {
total_allocation_units: 1 << 40,
caller_available_allocation_units: 1 << 39,
actual_available_allocation_units: 1 << 39,
sectors_per_allocation_unit: 1,
bytes_per_sector: 4096,
serial_number: crate::info_class::VOLUME_SERIAL_NUMBER,
label: None,
max_name_length: 255,
}
}
}