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 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)]
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 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)]
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, replace_if_exists: bool) -> 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 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<()>;
}
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, _replace: bool) -> SmbResult<()> {
Err(SmbError::NotSupported)
}
fn capabilities(&self) -> BackendCapabilities {
BackendCapabilities {
is_read_only: true,
case_sensitive: false,
}
}
}