use std::collections::{HashMap, VecDeque};
use std::fmt::Debug;
use std::io;
use std::mem;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use async_trait::async_trait;
use bytes::Bytes;
use tokio::sync::Notify;
use tokio::task::JoinHandle;
use tokio::time::Instant;
use zeroize::Zeroizing;
use super::backend::{EncryptedBackend, Settings};
use super::block::FileCipher;
use super::builder::EncryptedBackendBuilder;
use super::cache::{Entry, NameCache, Snapshot};
use super::change::Random;
use super::error::EncryptionError;
use super::format::{BlockShift, FILE_ID_LEN, HEADER_LEN, MARKER_NAME, NONCE_LEN, OVERHEAD};
use super::handle::{EncryptedHandle, OpenResult};
use super::header::Header;
use super::keys::KeySchedule;
use super::marker::{MarkerSettings, parse_marker};
use super::names::{StoredName, encode_name, encode_unchecked};
use super::random::{random_file_id, random_nonces};
use super::size::stored_size;
use crate::backend::{
BackendCapabilities, DirChange, DirChangeKind, DirEntry, DirWatch, FileInfo, FileTimes, Handle,
OpenAction, OpenIntent, OpenOptions, ShareBackend, VolumeInfo,
};
use crate::error::{SmbError, SmbResult};
use crate::path::SmbPath;
use crate::tests::data::{apply, pattern};
use crate::tests::faulty::Faulty;
use crate::tests::gated::{Gate, Gated, Op};
use crate::tests::memfs::MemFsBackend;
use crate::tests::util::{dir_opts, opts, path};
pub(super) fn unhex(s: &str) -> Vec<u8> {
hex::decode(s).unwrap()
}
pub(super) fn key() -> [u8; 32] {
std::array::from_fn(|i| i as u8)
}
pub(super) fn other_key() -> [u8; 32] {
[7; 32]
}
pub(super) fn key_schedule() -> KeySchedule {
KeySchedule::new(Zeroizing::new(key()))
}
pub(super) fn other_key_schedule() -> KeySchedule {
KeySchedule::new(Zeroizing::new(other_key()))
}
pub(super) fn file_id() -> [u8; 16] {
std::array::from_fn(|i| 0xa0 + i as u8)
}
pub(super) fn other_file_id() -> [u8; 16] {
std::array::from_fn(|i| 0xb0 + i as u8)
}
pub(super) type Builder = EncryptedBackendBuilder<MemFsBackend>;
pub(super) async fn build(
files: &MemFsBackend,
setup: impl FnOnce(Builder) -> Builder,
) -> Result<EncryptedBackend<MemFsBackend>, EncryptionError> {
let built = setup(EncryptedBackend::builder(files.clone(), key()))
.build()
.await;
assert_eq!(files.open_handles(), 0, "build closed every handle");
built
}
pub(crate) async fn clear_over<B: ShareBackend>(inner: B) -> EncryptedBackend<B> {
clear_blocks(inner, BlockShift::DEFAULT).await
}
pub(super) async fn clear_blocks<B: ShareBackend>(
inner: B,
shift: BlockShift,
) -> EncryptedBackend<B> {
EncryptedBackend::builder(inner, key())
.encrypt_names(false)
.block_size(u32::try_from(shift.block_size()).expect("a block size fits in 32 bits"))
.build()
.await
.expect("a backend with names in the clear")
}
pub(super) fn clear_settings() -> Settings {
Settings {
encrypt_names: false,
case_insensitive: false,
block_shift: BlockShift::DEFAULT,
long_name_threshold: 0,
name_cache_ttl: Duration::from_secs(30),
}
}
pub(super) fn names_settings() -> Settings {
Settings {
encrypt_names: true,
case_insensitive: true,
long_name_threshold: 255,
..clear_settings()
}
}
pub(crate) async fn names_over<B: ShareBackend>(inner: B) -> EncryptedBackend<B> {
names_with(inner, |builder| builder).await
}
pub(crate) async fn names_with<B: ShareBackend>(
inner: B,
setup: impl FnOnce(EncryptedBackendBuilder<B>) -> EncryptedBackendBuilder<B>,
) -> EncryptedBackend<B> {
setup(EncryptedBackend::builder(inner, key()))
.build()
.await
.expect("a backend that encrypts names")
}
pub(super) async fn layer() -> (MemFsBackend, EncryptedBackend<MemFsBackend>) {
let files = MemFsBackend::new();
let layer = names_over(files.clone()).await;
(files, layer)
}
pub(super) fn many_names(count: usize) -> Arc<Snapshot> {
let entries = (0..count)
.map(|n| Entry {
plain: format!("n{n}"),
stored: format!("s{n}"),
directory: false,
})
.collect();
Arc::new(Snapshot::new(entries, Instant::now()))
}
pub(super) fn with_name_limit<B>(
mut layer: EncryptedBackend<B>,
limit: usize,
) -> EncryptedBackend<B> {
let shared = Arc::get_mut(&mut layer.shared).expect("a backend nobody cloned");
shared.names = NameCache::new(shared.settings.name_cache_ttl, limit);
layer
}
pub(super) fn stored_form(name: &str) -> StoredName {
encode_name(&key_schedule(), name, 255).expect("a valid name")
}
pub(super) fn stored_name(name: &str) -> String {
stored_form(name).entry
}
pub(super) fn stored_at(plain: &str) -> String {
plain
.split('\\')
.filter(|name| !name.is_empty())
.map(stored_name)
.collect::<Vec<_>>()
.join("\\")
}
fn seed(files: &MemFsBackend, plain: &str, contents: Option<&[u8]>) {
let names: Vec<_> = plain.split('\\').collect();
let mut dir = String::new();
for (index, name) in names.iter().enumerate() {
let stored = stored_form(name);
let beside = |name: &str| {
if dir.is_empty() {
name.to_string()
} else {
format!("{dir}\\{name}")
}
};
let at = beside(&stored.entry);
match contents {
Some(bytes) if index + 1 == names.len() => files.clone().with_file(&at, bytes),
_ => files.clone().with_dir(&at),
};
if let Some(side) = &stored.side_object {
files.clone().with_file(&beside(&side.name), &side.content);
}
dir = at;
}
}
pub(super) fn seed_dir(files: &MemFsBackend, plain: &str) {
seed(files, plain, None);
}
pub(super) fn seed_file(files: &MemFsBackend, plain: &str, bytes: &[u8]) {
seed(files, plain, Some(bytes));
}
pub(super) fn long_name(fill: &str) -> String {
fill.repeat(150)
}
pub(super) fn seed_orphan(files: &MemFsBackend, dir: &str, name: &str) -> String {
let side = stored_form(name).side_object.expect("a long name");
let at = if dir.is_empty() {
side.name
} else {
format!("{dir}\\{}", side.name)
};
files.clone().with_file(&at, &side.content);
at
}
pub(super) fn injected() -> SmbError {
SmbError::Io(io::Error::other("injected"))
}
pub(super) fn has(names: &Snapshot, name: &str) -> bool {
names.entry(name, &stored_name(name)).is_some()
}
pub(super) const PRIVATE_WORD: &str = "secret";
pub(super) fn sealed_invalid_names() -> Vec<String> {
["slash/secret", "bell\u{7}secret"]
.map(|plain| encode_unchecked(&key_schedule(), plain))
.to_vec()
}
pub(super) fn seed_hidden_names(files: &MemFsBackend) -> Vec<String> {
let orphan = "o".repeat(150);
seed_file(files, &orphan, b"");
files.remove(&stored_form(&orphan).entry);
let other_key = encode_name(&other_key_schedule(), "other", 255)
.expect("a valid name")
.entry;
let mut foreign = vec![
"junk".to_string(),
"UPPER-case.txt".to_string(),
"a".repeat(28),
format!("{}.n.n", "a".repeat(32)),
other_key,
];
foreign.extend(sealed_invalid_names());
for name in &foreign {
files.clone().with_file(name, b"");
}
foreign
}
pub(super) fn spawn_open<B: ShareBackend>(
layer: &EncryptedBackend<B>,
name: &str,
intent: OpenIntent,
) -> JoinHandle<OpenResult<B>> {
let layer = layer.clone();
let name = path(name);
tokio::spawn(async move { layer.open_handle(&name, opts(intent)).await })
}
pub(super) fn shares_node<B: ShareBackend>(a: &EncryptedHandle<B>, b: &EncryptedHandle<B>) -> bool {
Arc::ptr_eq(a.node(), b.node())
}
pub(super) async fn open<B: ShareBackend>(
layer: &EncryptedBackend<B>,
name: &str,
intent: OpenIntent,
) -> (EncryptedHandle<B>, OpenAction) {
layer
.open_handle(&path(name), opts(intent))
.await
.unwrap_or_else(|error| panic!("open {name} {intent:?}: {error:?}"))
}
pub(super) async fn open_file<B: ShareBackend>(
layer: &EncryptedBackend<B>,
name: &str,
) -> EncryptedHandle<B> {
open(layer, name, OpenIntent::Open).await.0
}
pub(super) async fn open_dir<B: ShareBackend>(
layer: &EncryptedBackend<B>,
name: &str,
) -> EncryptedHandle<B> {
open_dir_as(layer, name, OpenIntent::Open).await.0
}
pub(super) async fn open_dir_as<B: ShareBackend>(
layer: &EncryptedBackend<B>,
name: &str,
intent: OpenIntent,
) -> (EncryptedHandle<B>, OpenAction) {
layer
.open_handle(&path(name), dir_opts(intent))
.await
.unwrap_or_else(|error| panic!("open directory {name} {intent:?}: {error:?}"))
}
pub(super) async fn refused<B: ShareBackend>(
layer: &EncryptedBackend<B>,
name: &str,
intent: OpenIntent,
) -> SmbError {
match layer.open_handle(&path(name), opts(intent)).await {
Err(error) => error,
Ok(_) => panic!("open {name} {intent:?} succeeded"),
}
}
pub(super) async fn refused_dir<B: ShareBackend>(
layer: &EncryptedBackend<B>,
name: &str,
intent: OpenIntent,
) -> SmbError {
match layer.open_handle(&path(name), dir_opts(intent)).await {
Err(error) => error,
Ok(_) => panic!("open directory {name} {intent:?} succeeded"),
}
}
pub(super) async fn settle() {
for _ in 0..16 {
tokio::task::yield_now().await;
}
}
pub(super) fn stored_file(keys: &KeySchedule, shift: BlockShift, plain: &[u8]) -> Vec<u8> {
stored_file_with_id(keys, shift, file_id(), plain)
}
pub(super) fn stored_bytes(plain: &[u8]) -> Vec<u8> {
stored_file(&key_schedule(), BlockShift::DEFAULT, plain)
}
pub(super) fn stored_file_with_id(
keys: &KeySchedule,
shift: BlockShift,
id: [u8; 16],
plain: &[u8],
) -> Vec<u8> {
let header = Header::new(shift, id).unwrap();
let cipher = FileCipher::new(keys, header);
let mut stored = header.as_bytes().to_vec();
for (index, block) in plain.chunks(shift.block_len()).enumerate() {
let nonce = [u8::try_from(index % 255 + 1).unwrap(); NONCE_LEN];
let at = stored.len();
stored.resize(at + block.len() + OVERHEAD, 0);
cipher
.seal_block(index as u64, &nonce, block, &mut stored[at..])
.unwrap();
}
stored
}
pub(super) fn stored_hole(shift: BlockShift, len: usize) -> Vec<u8> {
let header = Header::new(shift, file_id()).unwrap();
let mut stored = header.as_bytes().to_vec();
stored.resize(HEADER_LEN + len + OVERHEAD, 0);
stored
}
pub(super) const BITS: u8 = 13;
pub(super) const B: usize = 1 << BITS;
pub(super) const H: usize = HEADER_LEN;
pub(super) fn shift() -> BlockShift {
BlockShift::new(BITS).unwrap()
}
pub(super) fn stored(plain: &[u8]) -> Vec<u8> {
stored_file(&key_schedule(), shift(), plain)
}
pub(super) async fn layer_over<I: ShareBackend>(inner: I) -> EncryptedBackend<I> {
clear_blocks(inner, shift()).await
}
pub(super) async fn faulty_file(
bytes: &[u8],
) -> (
MemFsBackend,
Faulty<MemFsBackend>,
EncryptedBackend<Faulty<MemFsBackend>>,
) {
faulty_file_in(shift(), bytes).await
}
pub(super) async fn faulty_file_in(
shift: BlockShift,
bytes: &[u8],
) -> (
MemFsBackend,
Faulty<MemFsBackend>,
EncryptedBackend<Faulty<MemFsBackend>>,
) {
let files = MemFsBackend::new();
let faulty = Faulty::new(files.clone());
let layer = clear_blocks(faulty.clone(), shift).await;
files.clone().with_file("f", bytes);
(files, faulty, layer)
}
#[derive(Default)]
pub(super) struct Counting {
pub(super) draws: Vec<usize>,
pub(super) ids: usize,
}
impl Random for Counting {
fn nonces(&mut self, nonces: &mut [[u8; NONCE_LEN]]) -> Result<(), getrandom::Error> {
self.draws.push(nonces.len());
random_nonces(nonces)
}
fn file_id(&mut self) -> Result<[u8; FILE_ID_LEN], getrandom::Error> {
self.ids += 1;
random_file_id()
}
}
pub(super) struct Down;
impl Random for Down {
fn nonces(&mut self, _: &mut [[u8; NONCE_LEN]]) -> Result<(), getrandom::Error> {
Err(getrandom::Error::UNEXPECTED)
}
fn file_id(&mut self) -> Result<[u8; FILE_ID_LEN], getrandom::Error> {
Err(getrandom::Error::UNEXPECTED)
}
}
pub(super) const C: usize = B + OVERHEAD;
pub(super) fn at(index: usize) -> usize {
H + index * C
}
pub(super) fn stored_len(logical: usize) -> usize {
usize::try_from(stored_size(shift(), logical as u64).unwrap()).unwrap()
}
pub(super) async fn new_file() -> (
MemFsBackend,
Faulty<MemFsBackend>,
EncryptedHandle<Faulty<MemFsBackend>>,
) {
let files = MemFsBackend::new();
let faulty = Faulty::new(files.clone());
let layer = layer_over(faulty.clone()).await;
let (handle, action) = open(&layer, "f", OpenIntent::Create).await;
assert_eq!(action, OpenAction::Created);
(files, faulty, handle)
}
pub(super) async fn read_all<H: Handle>(handle: &H) -> Vec<u8> {
handle.read(0, u32::MAX).await.unwrap().to_vec()
}
pub(super) async fn assert_no_wait<T>(call: impl Future<Output = T>) -> T {
tokio::select! {
biased;
output = call => output,
() = std::future::ready(()) => panic!("the call waited for the lock"),
}
}
pub(super) async fn drop_at<T>(gate: &Gate, call: impl Future<Output = SmbResult<T>>) {
let mut call = Box::pin(call);
tokio::select! {
biased;
_ = &mut call => panic!("the call ended before it reached the gate"),
() = gate.reached() => {}
}
}
pub(super) fn writes_since(faulty: &Faulty<MemFsBackend>, from: usize) -> Vec<(u64, usize)> {
faulty.write_calls().split_off(from)
}
pub(super) fn fail_open_of(faulty: &Faulty<MemFsBackend>, name: &str) {
let name = name.to_string();
faulty.fail_open(
1,
move |at, _| at.file_name() == Some(name.as_str()),
injected(),
);
}
pub(super) const KEPT: usize = 100;
#[derive(Clone, Copy)]
pub(super) enum Edit {
Write { offset: usize, len: usize },
Truncate { len: usize },
}
pub(super) async fn perform<H: Handle>(handle: &H, edit: Edit) -> SmbResult<()> {
match edit {
Edit::Write { offset, len } => handle
.write(offset as u64, &pattern(len, 2))
.await
.map(|_| ()),
Edit::Truncate { len } => handle.truncate(len as u64).await,
}
}
pub(super) fn resized(plain: &[u8], len: usize) -> Vec<u8> {
let mut bytes = plain.to_vec();
bytes.resize(len, 0);
bytes
}
pub(super) fn edited(plain: &[u8], edit: Edit) -> Vec<u8> {
match edit {
Edit::Write { offset, len } => {
let mut bytes = plain.to_vec();
apply(&mut bytes, offset, &pattern(len, 2));
bytes
}
Edit::Truncate { len } => resized(plain, len),
}
}
pub(super) fn nonce_at(bytes: &[u8], start: usize) -> [u8; NONCE_LEN] {
bytes[start..start + NONCE_LEN].try_into().unwrap()
}
pub(super) async fn gated_file(
bytes: &[u8],
) -> (
MemFsBackend,
Gated<MemFsBackend>,
EncryptedBackend<Gated<MemFsBackend>>,
) {
let files = MemFsBackend::new();
let gated = Gated::new(files.clone());
let layer = layer_over(gated.clone()).await;
files.clone().with_file("f", bytes);
(files, gated, layer)
}
pub(super) fn header_reads<I: ShareBackend>(faulty: &Faulty<I>) -> usize {
faulty
.reads()
.iter()
.filter(|&&(offset, _)| offset == 0)
.count()
}
pub(super) fn block_reads<I: ShareBackend>(faulty: &Faulty<I>) -> Vec<(u64, u32)> {
faulty
.reads()
.into_iter()
.filter(|&(offset, _)| offset != 0)
.collect()
}
#[track_caller]
pub(super) fn assert_io<T: Debug>(result: SmbResult<T>, kind: io::ErrorKind) {
match result {
Err(SmbError::Io(error)) => assert_eq!(error.kind(), kind),
other => panic!("expected an Io error of kind {kind:?}, got {other:?}"),
}
}
#[track_caller]
pub(super) fn assert_err<T: Debug>(result: SmbResult<T>, expected: SmbError) {
match result {
Err(error) if mem::discriminant(&error) == mem::discriminant(&expected) => {}
other => panic!("expected {expected:?}, got {other:?}"),
}
}
pub(super) fn marker_bytes(files: &MemFsBackend) -> Vec<u8> {
files.contents(MARKER_NAME).expect("the root has a marker")
}
pub(super) fn marker_settings(files: &MemFsBackend) -> MarkerSettings {
parse_marker(&marker_bytes(files), &key_schedule()).expect("a marker of the key")
}
pub(super) fn read_only(files: &MemFsBackend) -> Reporting<MemFsBackend> {
let caps = BackendCapabilities {
is_read_only: true,
..files.capabilities()
};
Reporting::new(files.clone(), caps)
}
#[track_caller]
pub(super) fn assert_mismatch(error: &EncryptionError, setting: &str) {
assert!(
matches!(error, EncryptionError::SettingMismatch { setting: got } if *got == setting),
"expected a mismatch of `{setting}`, got {error:?}"
);
}
#[track_caller]
pub(super) fn assert_invalid(error: &EncryptionError, setting: &str) {
assert!(
matches!(error, EncryptionError::InvalidSetting { setting: got } if *got == setting),
"expected `{setting}` to be invalid, got {error:?}"
);
}
#[track_caller]
pub(super) fn assert_key_mismatch(error: &EncryptionError) {
assert!(
matches!(error, EncryptionError::KeyMismatch),
"expected a key mismatch, got {error:?}"
);
}
pub(super) fn exact_names(files: &MemFsBackend) -> Reporting<MemFsBackend> {
let caps = BackendCapabilities {
case_sensitive: true,
..files.capabilities()
};
Reporting::new(files.clone(), caps)
}
#[derive(Clone)]
pub(super) struct Reporting<B> {
inner: B,
caps: BackendCapabilities,
volume: Option<VolumeInfo>,
}
impl<B> Reporting<B> {
pub(super) fn new(inner: B, caps: BackendCapabilities) -> Self {
Self {
inner,
caps,
volume: None,
}
}
pub(super) fn with_volume(mut self, volume: VolumeInfo) -> Self {
self.volume = Some(volume);
self
}
}
#[async_trait]
impl<B: ShareBackend> ShareBackend for Reporting<B> {
async fn open(
&self,
path: &SmbPath,
opts: OpenOptions,
) -> SmbResult<(Box<dyn Handle>, OpenAction)> {
let writes = opts.write || opts.write_attributes || opts.delete_on_close;
if self.caps.is_read_only && (writes || opts.intent != OpenIntent::Open) {
return Err(SmbError::AccessDenied);
}
self.inner.open(path, opts).await
}
async fn unlink(&self, path: &SmbPath) -> SmbResult<()> {
self.inner.unlink(path).await
}
async fn rename(&self, from: &SmbPath, to: &SmbPath, replace_if_exists: bool) -> SmbResult<()> {
self.inner.rename(from, to, replace_if_exists).await
}
fn capabilities(&self) -> BackendCapabilities {
self.caps
}
async fn volume_info(&self) -> SmbResult<VolumeInfo> {
match &self.volume {
Some(volume) => Ok(volume.clone()),
None => self.inner.volume_info().await,
}
}
}
#[derive(Clone)]
pub(super) struct Strict<B> {
inner: B,
}
impl<B> Strict<B> {
pub(super) fn new(inner: B) -> Self {
Self { inner }
}
}
#[async_trait]
impl<B: ShareBackend> ShareBackend for Strict<B> {
async fn open(
&self,
path: &SmbPath,
opts: OpenOptions,
) -> SmbResult<(Box<dyn Handle>, OpenAction)> {
self.inner.open(path, opts).await
}
async fn unlink(&self, path: &SmbPath) -> SmbResult<()> {
if let Ok((handle, _)) = self.inner.open(path, dir_opts(OpenIntent::Open)).await {
let listed = handle.list_dir(None).await;
handle.close().await?;
if !listed?.is_empty() {
return Err(SmbError::NotEmpty);
}
}
self.inner.unlink(path).await
}
async fn rename(&self, from: &SmbPath, to: &SmbPath, replace_if_exists: bool) -> SmbResult<()> {
self.inner.rename(from, to, replace_if_exists).await
}
fn capabilities(&self) -> BackendCapabilities {
self.inner.capabilities()
}
}
pub(super) struct Hiding;
#[async_trait]
impl Handle for Hiding {
async fn read(&self, _offset: u64, _len: u32) -> SmbResult<Bytes> {
Err(SmbError::NotSupported)
}
async fn write(&self, _offset: u64, _data: &[u8]) -> SmbResult<u32> {
Err(SmbError::NotSupported)
}
async fn flush(&self) -> SmbResult<()> {
Ok(())
}
async fn stat(&self) -> SmbResult<FileInfo> {
Ok(FileInfo::new(""))
}
async fn set_times(&self, _times: FileTimes) -> SmbResult<()> {
Ok(())
}
async fn set_attributes(&self, _attributes: u32) -> SmbResult<()> {
Ok(())
}
async fn truncate(&self, _len: u64) -> SmbResult<()> {
Err(SmbError::NotSupported)
}
async fn list_dir(&self, _pattern: Option<&str>) -> SmbResult<Vec<DirEntry>> {
Ok(Vec::new())
}
async fn close(self: Box<Self>) -> SmbResult<()> {
Ok(())
}
async fn dir_is_empty(&self) -> SmbResult<bool> {
Ok(false)
}
}
pub(super) fn names(entries: &[DirEntry]) -> Vec<String> {
let mut names: Vec<_> = entries
.iter()
.map(|entry| entry.info.name.clone())
.collect();
names.sort();
names
}
pub(super) fn summary(entries: &[DirEntry]) -> Vec<(String, u64, u64, u32)> {
let mut summary: Vec<_> = entries
.iter()
.map(|entry| {
let info = &entry.info;
(
info.name.clone(),
info.end_of_file,
info.allocation_size,
info.ea_size,
)
})
.collect();
summary.sort();
summary
}
pub(super) type GatedLayer = EncryptedBackend<Gated<MemFsBackend>>;
pub(super) async fn parked() -> (MemFsBackend, GatedLayer, Gate) {
let files = MemFsBackend::new();
let gated = Gated::new(files.clone());
let layer = names_over(gated.clone()).await;
seed_file(&files, "a.txt", b"");
let gate = gated.arm_after_at(Op::ListDir, "");
(files, layer, gate)
}
pub(super) fn change(name: &str, kind: DirChangeKind) -> DirChange {
DirChange::new(name, kind, false)
}
pub(super) fn renamed(from: &str, to: &str) -> DirChange {
change(
to,
DirChangeKind::Renamed {
from: from.to_string(),
},
)
}
pub(super) struct Script {
batches: VecDeque<Vec<DirChange>>,
}
impl Script {
pub(super) fn new(batches: Vec<Vec<DirChange>>) -> Self {
Self {
batches: batches.into(),
}
}
}
#[async_trait]
impl DirWatch for Script {
async fn next(&mut self) -> Option<Vec<DirChange>> {
self.batches.pop_front()
}
}
pub(super) struct Parked {
gate: Arc<Notify>,
batch: Option<Vec<DirChange>>,
}
impl Parked {
pub(super) fn new(gate: Arc<Notify>, batch: Vec<DirChange>) -> Self {
Self {
gate,
batch: Some(batch),
}
}
}
#[async_trait]
impl DirWatch for Parked {
async fn next(&mut self) -> Option<Vec<DirChange>> {
self.gate.notified().await;
self.batch.take()
}
}
#[derive(Clone, Copy)]
enum Answer {
NotFound,
PathNotFound,
}
#[derive(Clone)]
pub(super) struct Hidden {
inner: MemFsBackend,
paths: Arc<Mutex<HashMap<String, Answer>>>,
}
impl Hidden {
pub(super) fn new(inner: MemFsBackend) -> Self {
Self {
inner,
paths: Arc::default(),
}
}
pub(super) fn hide(&self, stored: &str) {
let mut paths = self.paths.lock().unwrap();
paths.insert(stored.to_string(), Answer::NotFound);
}
pub(super) fn cut_off(&self, stored: &str) {
let mut paths = self.paths.lock().unwrap();
paths.insert(stored.to_string(), Answer::PathNotFound);
}
pub(super) fn show(&self, stored: &str) {
self.paths.lock().unwrap().remove(stored);
}
fn refusal(&self, at: &SmbPath) -> SmbResult<()> {
let answer = self
.paths
.lock()
.unwrap()
.get(&at.display_backslash())
.copied();
match answer {
Some(Answer::NotFound) => Err(SmbError::NotFound),
Some(Answer::PathNotFound) => Err(SmbError::PathNotFound),
None => Ok(()),
}
}
}
#[async_trait]
impl ShareBackend for Hidden {
async fn open(
&self,
at: &SmbPath,
opts: OpenOptions,
) -> SmbResult<(Box<dyn Handle>, OpenAction)> {
self.refusal(at)?;
self.inner.open(at, opts).await
}
async fn unlink(&self, at: &SmbPath) -> SmbResult<()> {
self.refusal(at)?;
self.inner.unlink(at).await
}
async fn rename(&self, from: &SmbPath, to: &SmbPath, replace: bool) -> SmbResult<()> {
self.refusal(from)?;
self.inner.rename(from, to, replace).await
}
fn capabilities(&self) -> BackendCapabilities {
self.inner.capabilities()
}
}