use async_trait::async_trait;
use std::time::SystemTime;
use crate::error::{SmbError, SmbResult};
use crate::path::SmbPath;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OpenIntent {
Open,
Create,
OpenOrCreate,
OverwriteOrCreate,
Truncate,
}
#[derive(Debug, Clone, Copy)]
pub struct OpenOptions {
pub read: bool,
pub write: 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,
intent: OpenIntent::Open,
directory: false,
non_directory: false,
delete_on_close: false,
}
}
}
#[derive(Debug, Clone)]
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 file_index: u64,
}
impl FileInfo {
pub fn attributes(&self) -> u32 {
const FILE_ATTRIBUTE_DIRECTORY: u32 = 0x0000_0010;
const FILE_ATTRIBUTE_NORMAL: u32 = 0x0000_0080;
if self.is_directory {
FILE_ATTRIBUTE_DIRECTORY
} else {
FILE_ATTRIBUTE_NORMAL
}
}
}
#[derive(Debug, Clone)]
pub struct DirEntry {
pub info: FileInfo,
}
#[derive(Debug, Clone, Copy, Default)]
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)]
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>>;
async fn unlink(&self, path: &SmbPath) -> SmbResult<()>;
async fn rename(&self, from: &SmbPath, to: &SmbPath) -> SmbResult<()>;
fn capabilities(&self) -> BackendCapabilities;
}
#[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 truncate(&self, len: u64) -> SmbResult<()>;
async fn list_dir(&self, pattern: Option<&str>) -> SmbResult<Vec<DirEntry>>;
async fn close(self: Box<Self>) -> SmbResult<()>;
}
pub(crate) struct NotSupportedBackend;
#[async_trait]
impl ShareBackend for NotSupportedBackend {
async fn open(&self, _path: &SmbPath, _opts: OpenOptions) -> SmbResult<Box<dyn Handle>> {
Err(SmbError::NotSupported)
}
async fn unlink(&self, _path: &SmbPath) -> SmbResult<()> {
Err(SmbError::NotSupported)
}
async fn rename(&self, _from: &SmbPath, _to: &SmbPath) -> SmbResult<()> {
Err(SmbError::NotSupported)
}
fn capabilities(&self) -> BackendCapabilities {
BackendCapabilities {
is_read_only: true,
case_sensitive: false,
}
}
}