use std::collections::HashMap;
use std::ffi::{CStr, CString};
use std::fs::File;
use std::io;
use std::os::fd::AsRawFd as _;
use std::os::unix::fs::FileExt as _;
use std::path::PathBuf;
use std::sync::Arc;
use async_trait::async_trait;
use bytes::Bytes;
use cap_std::fs::{Dir, Metadata};
use tokio::task::spawn_blocking;
use tracing::debug;
use super::local::{
file_index, file_info_from_metadata, host_file_times, io_to_smb, join_to_io, link_count,
set_host_attributes,
};
use super::open_streams::{OpenStreams, StreamFile, lock, open_base, open_entry, touch};
use crate::backend::{DirEntry, FileInfo, FileTimes, Handle, OpenAction, OpenIntent, OpenOptions};
use crate::error::{SmbError, SmbResult};
use crate::stream::{StreamInfo, StreamName, from_sfm, to_sfm};
const AFP_INFO: &str = "AFP_AfpInfo";
const AFP_RESOURCE: &str = "AFP_Resource";
const FINDER_INFO: &str = "com.apple.FinderInfo";
const RESOURCE_FORK: &str = "com.apple.ResourceFork";
const AFP_INFO_SIZE: usize = 60;
const FINDER_INFO_AT: std::ops::Range<usize> = 16..48;
const MAX_NAME: usize = 127;
const MAX_PLAIN: u64 = 1 << 20;
const MAX_FORK: u64 = 16 << 20;
const MAX_STREAMS: usize = 4096;
fn checked(r: isize) -> io::Result<usize> {
usize::try_from(r).map_err(|_| io::Error::last_os_error())
}
fn value_size(fd: &File, name: &CStr) -> io::Result<usize> {
checked(unsafe {
libc::fgetxattr(fd.as_raw_fd(), name.as_ptr(), std::ptr::null_mut(), 0, 0, 0)
})
}
fn read_value(fd: &File, name: &CStr, buf: &mut [u8], position: u32) -> io::Result<usize> {
if buf.is_empty() {
return Ok(0);
}
checked(unsafe {
libc::fgetxattr(
fd.as_raw_fd(),
name.as_ptr(),
buf.as_mut_ptr().cast(),
buf.len(),
position,
0,
)
})
}
fn write_value(
fd: &File,
name: &CStr,
data: &[u8],
position: u32,
flags: libc::c_int,
) -> io::Result<()> {
let r = unsafe {
libc::fsetxattr(
fd.as_raw_fd(),
name.as_ptr(),
data.as_ptr().cast(),
data.len(),
position,
flags,
)
};
if r != 0 {
return Err(io::Error::last_os_error());
}
Ok(())
}
fn remove_value(fd: &File, name: &CStr) -> io::Result<()> {
if unsafe { libc::fremovexattr(fd.as_raw_fd(), name.as_ptr(), 0) } != 0 {
return Err(io::Error::last_os_error());
}
Ok(())
}
fn remove_if_any(fd: &File, name: &CStr) -> io::Result<()> {
match remove_value(fd, name) {
Err(e) if is_missing(&e) => Ok(()),
removed => removed,
}
}
fn list_names(fd: &File) -> io::Result<Vec<u8>> {
for _ in 0..8 {
let size =
checked(unsafe { libc::flistxattr(fd.as_raw_fd(), std::ptr::null_mut(), 0, 0) })?;
let mut buf = vec![0u8; size];
if buf.is_empty() {
return Ok(buf);
}
let read =
unsafe { libc::flistxattr(fd.as_raw_fd(), buf.as_mut_ptr().cast(), buf.len(), 0) };
match checked(read) {
Ok(len) => {
buf.truncate(len);
return Ok(buf);
}
Err(e) if e.raw_os_error() == Some(libc::ERANGE) => continue,
Err(e) => return Err(e),
}
}
Err(io::Error::from_raw_os_error(libc::ERANGE))
}
fn get(fd: &File, name: &CStr) -> io::Result<Vec<u8>> {
for _ in 0..8 {
let mut buf = vec![0u8; value_size(fd, name)?];
match read_value(fd, name, &mut buf, 0) {
Ok(len) => {
buf.truncate(len);
return Ok(buf);
}
Err(e) if e.raw_os_error() == Some(libc::ERANGE) => continue,
Err(e) => return Err(e),
}
}
Err(io::Error::from_raw_os_error(libc::ERANGE))
}
fn is_missing(e: &io::Error) -> bool {
e.raw_os_error() == Some(libc::ENOATTR)
}
fn bad_data() -> io::Error {
io::ErrorKind::InvalidData.into()
}
fn to_smb(e: io::Error) -> SmbError {
if e.kind() == io::ErrorKind::InvalidData {
return SmbError::InvalidParameter;
}
match e.raw_os_error() {
Some(libc::ENOATTR) => SmbError::NotFound,
Some(libc::E2BIG | libc::ENOSPC) => SmbError::DiskFull,
Some(libc::ENOTSUP | libc::EOPNOTSUPP) => SmbError::AccessDenied,
_ => io_to_smb(e),
}
}
pub(super) fn supported(root: &Dir) -> bool {
use std::os::fd::AsFd as _;
#[repr(C)]
struct Reply {
length: u32,
capabilities: libc::vol_capabilities_attr_t,
}
let mut request = libc::attrlist {
bitmapcount: libc::ATTR_BIT_MAP_COUNT,
reserved: 0,
commonattr: 0,
volattr: libc::ATTR_VOL_INFO | libc::ATTR_VOL_CAPABILITIES,
dirattr: 0,
fileattr: 0,
forkattr: 0,
};
let mut reply = Reply {
length: 0,
capabilities: libc::vol_capabilities_attr_t {
capabilities: [0; 4],
valid: [0; 4],
},
};
let r = unsafe {
libc::fgetattrlist(
root.as_fd().as_raw_fd(),
(&raw mut request).cast(),
(&raw mut reply).cast(),
std::mem::size_of::<Reply>(),
0,
)
};
let interfaces = libc::VOL_CAPABILITIES_INTERFACES;
let bit = libc::VOL_CAP_INT_EXTENDED_ATTR;
r == 0
&& reply.capabilities.valid[interfaces] & bit != 0
&& reply.capabilities.capabilities[interfaces] & bit != 0
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Kind {
FinderInfo,
ResourceFork,
Plain,
}
impl Kind {
fn max(self) -> u64 {
match self {
Kind::FinderInfo => AFP_INFO_SIZE as u64,
Kind::ResourceFork => MAX_FORK,
Kind::Plain => MAX_PLAIN,
}
}
}
#[derive(Debug, Clone)]
struct Attr {
name: StreamName,
xattr: CString,
kind: Kind,
}
fn same_stream(a: &str, b: &str) -> bool {
a.to_lowercase() == b.to_lowercase()
}
impl Attr {
fn of(name: &StreamName) -> io::Result<Self> {
let (xattr, kind) = if same_stream(name.as_str(), AFP_INFO) {
(FINDER_INFO.to_string(), Kind::FinderInfo)
} else if same_stream(name.as_str(), AFP_RESOURCE) {
(RESOURCE_FORK.to_string(), Kind::ResourceFork)
} else {
let xattr = from_sfm(name.as_str());
if xattr.len() > MAX_NAME
|| to_sfm(&xattr) != name.as_str()
|| same_stream(&xattr, FINDER_INFO)
|| same_stream(&xattr, RESOURCE_FORK)
{
return Err(io::ErrorKind::InvalidFilename.into());
}
(xattr, Kind::Plain)
};
Ok(Self {
name: name.clone(),
xattr: CString::new(xattr).map_err(|_| io::ErrorKind::InvalidFilename)?,
kind,
})
}
fn listed(xattr: &[u8]) -> Option<Self> {
let xattr = std::str::from_utf8(xattr).ok()?;
let name = match xattr {
FINDER_INFO => AFP_INFO.to_string(),
RESOURCE_FORK => AFP_RESOURCE.to_string(),
xattr => to_sfm(xattr),
};
let attr = Self::of(&name.parse().ok()?).ok()?;
(attr.xattr.as_bytes() == xattr.as_bytes()).then_some(attr)
}
}
struct Stored {
main: Attr,
others: Vec<Attr>,
}
struct View {
stored: Vec<Stored>,
at: HashMap<String, usize>,
}
impl View {
fn load(fd: &File) -> io::Result<Self> {
let names = list_names(fd)?;
let mut stored: Vec<Stored> = Vec::new();
let mut at: HashMap<String, usize> = HashMap::new();
for attr in names.split(|&b| b == 0).filter_map(Attr::listed) {
let key = attr.name.as_str().to_lowercase();
match at.get(&key) {
Some(&i) => {
let group = &mut stored[i];
let mut attr = attr;
if attr.name.as_str() < group.main.name.as_str() {
std::mem::swap(&mut group.main, &mut attr);
}
group.others.push(attr);
}
None if stored.len() < MAX_STREAMS => {
at.insert(key, stored.len());
stored.push(Stored {
main: attr,
others: Vec::new(),
});
}
None => {}
}
}
if stored.len() == MAX_STREAMS {
debug!("named streams of a file past {MAX_STREAMS} left out");
}
Ok(Self { stored, at })
}
fn find(&self, stream: &StreamName) -> Option<&Stored> {
let at = *self.at.get(&stream.as_str().to_lowercase())?;
Some(&self.stored[at])
}
}
fn remove_others(fd: &File, stored: &Stored) {
for other in &stored.others {
if let Err(e) = remove_value(fd, &other.xattr) {
debug!(error = %e, stream = other.name.as_str(), "name of a removed stream left");
}
}
}
fn afp_info(finder_info: &[u8]) -> [u8; AFP_INFO_SIZE] {
let mut info = [0u8; AFP_INFO_SIZE];
info[..4].copy_from_slice(b"AFP\0");
info[4..8].copy_from_slice(&[0, 0, 1, 0]);
info[12..16].copy_from_slice(&[0x80, 0, 0, 0]);
let len = finder_info.len().min(FINDER_INFO_AT.len());
info[FINDER_INFO_AT][..len].copy_from_slice(&finder_info[..len]);
info
}
fn finder_info(data: &[u8]) -> io::Result<&[u8]> {
if data.len() != AFP_INFO_SIZE || !data.starts_with(b"AFP\0") {
return Err(bad_data());
}
Ok(&data[FINDER_INFO_AT])
}
fn set_finder_info(fd: &File, attr: &Attr, info: &[u8]) -> io::Result<()> {
if info.iter().all(|&b| b == 0) {
remove_if_any(fd, &attr.xattr)
} else {
write_value(fd, &attr.xattr, info, 0, 0)
}
}
fn slice(data: &[u8], offset: u64, len: u32) -> Bytes {
let start = usize::try_from(offset)
.unwrap_or(usize::MAX)
.min(data.len());
let end = start.saturating_add(len as usize).min(data.len());
Bytes::copy_from_slice(&data[start..end])
}
fn end_within_limit(attr: &Attr, offset: u64, len: usize) -> io::Result<u64> {
offset
.checked_add(len as u64)
.filter(|&end| end <= attr.kind.max())
.ok_or(io::ErrorKind::StorageFull.into())
}
fn get_bounded(fd: &File, attr: &Attr, too_large: io::ErrorKind) -> io::Result<Vec<u8>> {
if value_size(fd, &attr.xattr)? as u64 > attr.kind.max() {
return Err(too_large.into());
}
get(fd, &attr.xattr)
}
fn size(fd: &File, attr: &Attr) -> io::Result<u64> {
match (attr.kind, value_size(fd, &attr.xattr)) {
(Kind::FinderInfo, Ok(_)) => Ok(AFP_INFO_SIZE as u64),
(_, Ok(len)) => Ok(len as u64),
(Kind::FinderInfo, Err(e)) if is_missing(&e) => Ok(AFP_INFO_SIZE as u64),
(Kind::ResourceFork, Err(e)) if is_missing(&e) => Ok(0),
(_, Err(e)) => Err(e),
}
}
fn read(fd: &File, attr: &Attr, offset: u64, len: u32) -> io::Result<Bytes> {
match attr.kind {
Kind::FinderInfo => {
let info = match get(fd, &attr.xattr) {
Err(e) if is_missing(&e) => Vec::new(),
info => info?,
};
Ok(slice(&afp_info(&info), offset, len))
}
Kind::ResourceFork => {
let Ok(position) = u32::try_from(offset) else {
return Ok(Bytes::new());
};
let mut buf = vec![0u8; len as usize];
match read_value(fd, &attr.xattr, &mut buf, position) {
Ok(read) => buf.truncate(read),
Err(e) if is_missing(&e) => buf.clear(),
Err(e) => return Err(e),
}
Ok(Bytes::from(buf))
}
Kind::Plain => Ok(slice(
&get_bounded(fd, attr, io::ErrorKind::PermissionDenied)?,
offset,
len,
)),
}
}
fn write(fd: &File, attr: &Attr, offset: u64, data: &[u8]) -> io::Result<()> {
match attr.kind {
Kind::FinderInfo => {
if offset != 0 {
return Err(bad_data());
}
set_finder_info(fd, attr, finder_info(data)?)?;
}
Kind::ResourceFork => {
end_within_limit(attr, offset, data.len())?;
write_value(fd, &attr.xattr, data, offset as u32, 0)?;
}
Kind::Plain => {
let end = end_within_limit(attr, offset, data.len())? as usize;
let mut value = get_bounded(fd, attr, io::ErrorKind::StorageFull)?;
if value.len() < end {
value.resize(end, 0);
}
value[offset as usize..end].copy_from_slice(data);
write_value(fd, &attr.xattr, &value, 0, libc::XATTR_REPLACE)?;
}
}
touch(fd);
Ok(())
}
fn truncate(fd: &File, attr: &Attr, len: u64) -> io::Result<()> {
if len > attr.kind.max() {
return Err(io::ErrorKind::StorageFull.into());
}
match attr.kind {
Kind::FinderInfo if len != 0 => return Ok(()),
Kind::FinderInfo => remove_if_any(fd, &attr.xattr)?,
Kind::ResourceFork => {
let current = size(fd, attr)?;
if len == current {
return Ok(());
}
if len == 0 {
remove_if_any(fd, &attr.xattr)?;
} else if len < current {
shrink_fork(fd, attr, len as usize)?;
} else {
write_value(fd, &attr.xattr, &[0], (len - 1) as u32, 0)?;
}
}
Kind::Plain => {
let mut value = get_bounded(fd, attr, io::ErrorKind::StorageFull)?;
value.resize(len as usize, 0);
write_value(fd, &attr.xattr, &value, 0, libc::XATTR_REPLACE)?;
}
}
touch(fd);
Ok(())
}
fn shrink_fork(fd: &File, attr: &Attr, len: usize) -> io::Result<()> {
let fork = get_bounded(fd, attr, io::ErrorKind::StorageFull)?;
remove_value(fd, &attr.xattr)?;
let Err(e) = write_value(fd, &attr.xattr, &fork[..len.min(fork.len())], 0, 0) else {
return Ok(());
};
if let Err(e) =
remove_if_any(fd, &attr.xattr).and_then(|()| write_value(fd, &attr.xattr, &fork, 0, 0))
{
debug!(error = %e, "resource fork lost while cut short");
}
Err(e)
}
fn data(fd: &File, attr: &Attr) -> io::Result<Vec<u8>> {
let value = get_bounded(fd, attr, io::ErrorKind::StorageFull)?;
if attr.kind == Kind::FinderInfo {
return Ok(afp_info(&value).to_vec());
}
Ok(value)
}
fn put_new(fd: &File, attr: &Attr, data: &[u8]) -> io::Result<()> {
if attr.kind != Kind::FinderInfo && data.len() as u64 > attr.kind.max() {
return Err(io::ErrorKind::StorageFull.into());
}
match attr.kind {
Kind::FinderInfo => {
let info = finder_info(data)?;
if info.iter().all(|&b| b == 0) {
return Err(bad_data());
}
write_value(fd, &attr.xattr, info, 0, libc::XATTR_CREATE)
}
Kind::ResourceFork if data.is_empty() => Err(bad_data()),
_ => write_value(fd, &attr.xattr, data, 0, libc::XATTR_CREATE),
}
}
pub(super) fn list(fd: &File) -> io::Result<Vec<StreamInfo>> {
let view = match View::load(fd) {
Err(e) if matches!(e.raw_os_error(), Some(libc::ENOTSUP | libc::EOPNOTSUPP)) => {
return Ok(Vec::new());
}
view => view?,
};
let mut out = Vec::with_capacity(view.stored.len());
for stored in &view.stored {
match value_size(fd, &stored.main.xattr) {
Ok(len) => {
let size = match stored.main.kind {
Kind::FinderInfo => AFP_INFO_SIZE as u64,
_ => len as u64,
};
out.push(StreamInfo::new(stored.main.name.clone(), size, size));
}
Err(e) if is_missing(&e) => {}
Err(e) => return Err(e),
}
}
Ok(out)
}
pub(super) fn list_smb(fd: &File) -> SmbResult<Vec<StreamInfo>> {
list(fd).map_err(to_smb)
}
struct Aside(Vec<(CString, Vec<u8>)>);
impl Aside {
fn take(fd: &File, stored: Option<&Stored>, replace: bool) -> io::Result<Self> {
let mut aside = Self(Vec::new());
let Some(stored) = stored else {
return Ok(aside);
};
if !replace {
return Err(io::ErrorKind::AlreadyExists.into());
}
for attr in std::iter::once(&stored.main).chain(&stored.others) {
let taken = get_bounded(fd, attr, io::ErrorKind::StorageFull).and_then(|value| {
remove_value(fd, &attr.xattr)?;
aside.0.push((attr.xattr.clone(), value));
Ok(())
});
if let Err(e) = taken {
aside.put_back(fd);
return Err(e);
}
}
Ok(aside)
}
fn put_back(&self, fd: &File) {
for (xattr, value) in &self.0 {
if let Err(e) = write_value(fd, xattr, value, 0, 0) {
debug!(error = %e, attr = ?xattr, "replaced stream not put back");
}
}
}
}
pub(super) fn peek_entries(
dir: &Dir,
entries: &[(String, Vec<(StreamName, u32)>)],
) -> Vec<Vec<Option<(u64, Bytes)>>> {
entries
.iter()
.map(|(name, streams)| match open_entry(dir, name) {
Ok((base, _)) => {
let mut view = None;
streams
.iter()
.map(|(stream, max)| {
peek(&base, &mut view, stream, *max).unwrap_or_else(|e| {
debug!(error = %e, name, stream = stream.as_str(), "stream of a listed entry not read");
None
})
})
.collect()
}
Err(e) => {
debug!(error = %e, name, "streams of a listed entry not read");
vec![None; streams.len()]
}
})
.collect()
}
fn peek(
fd: &File,
view: &mut Option<View>,
stream: &StreamName,
max: u32,
) -> io::Result<Option<(u64, Bytes)>> {
let attr = Attr::of(stream)?;
let attr = match attr.kind {
Kind::Plain => {
if view.is_none() {
*view = Some(View::load(fd)?);
}
match view.as_ref().and_then(|view| view.find(stream)) {
Some(found) => found.main.clone(),
None => return Ok(None),
}
}
Kind::FinderInfo | Kind::ResourceFork => attr,
};
match value_size(fd, &attr.xattr) {
Err(e) if is_missing(&e) => return Ok(None),
found => found?,
};
let size = size(fd, &attr)?;
let data = if max == 0 {
Bytes::new()
} else {
read(fd, &attr, 0, max)?
};
Ok(Some((size, data)))
}
#[derive(Clone)]
pub(super) struct Place {
pub root: Arc<Dir>,
pub root_dev: Option<u64>,
pub read_only: bool,
pub open: Arc<OpenStreams>,
}
impl Place {
fn base(
&self,
rel: &std::path::Path,
id: u64,
write: bool,
) -> io::Result<(File, Metadata, Arc<StreamFile>)> {
let (base, md, key) = open_base(&self.root, rel, write)?;
if id != 0 && id != file_index(self.root_dev, Some(key)) {
return Err(io::ErrorKind::NotFound.into());
}
Ok((base, md, self.open.file(key)))
}
pub(super) async fn open_stream(
self,
rel: PathBuf,
name: String,
id: u64,
stream: StreamName,
opts: OpenOptions,
) -> SmbResult<(Box<dyn Handle>, OpenAction)> {
let intent = opts.intent;
spawn_blocking(move || -> io::Result<_> {
let (base, md, file) = self.base(&rel, id, false)?;
let mut open = lock(&file.open);
let attr = Attr::of(&stream)?;
let view = View::load(&base)?;
let existing = view.find(&stream).map(|s| s.main.clone());
let action = match (intent, &existing) {
(OpenIntent::Open | OpenIntent::Truncate, None) => {
return Err(io::ErrorKind::NotFound.into());
}
(OpenIntent::Create, Some(_)) => return Err(io::ErrorKind::AlreadyExists.into()),
(OpenIntent::Open | OpenIntent::OpenOrCreate, Some(_)) => OpenAction::Opened,
(_, Some(existing)) => {
truncate(&base, existing, 0)?;
OpenAction::Overwritten
}
(_, None) => {
if view.stored.len() >= MAX_STREAMS {
return Err(io::ErrorKind::StorageFull.into());
}
match attr.kind {
Kind::Plain => write_value(&base, &attr.xattr, &[], 0, libc::XATTR_CREATE)?,
Kind::ResourceFork if md.is_dir() => {
return Err(io::ErrorKind::PermissionDenied.into());
}
Kind::ResourceFork | Kind::FinderInfo => {}
}
OpenAction::Created
}
};
let handle_id = open.add(existing.map_or(stream, |e| e.name));
drop(open);
let handle: Box<dyn Handle> = Box::new(StreamHandle {
file,
id: handle_id,
base: Arc::new(base),
is_dir: md.is_dir(),
name,
root_dev: self.root_dev,
read_only: self.read_only,
});
Ok((handle, action))
})
.await
.map_err(join_to_io)
.map_err(to_smb)?
.map_err(to_smb)
}
pub(super) async fn remove_stream(
self,
rel: PathBuf,
id: u64,
stream: StreamName,
) -> SmbResult<()> {
spawn_blocking(move || -> io::Result<()> {
Attr::of(&stream)?;
let (base, _, file) = self.base(&rel, id, false)?;
let _open = lock(&file.open);
let view = View::load(&base)?;
let found = view.find(&stream).ok_or(io::ErrorKind::NotFound)?;
remove_value(&base, &found.main.xattr)?;
remove_others(&base, found);
Ok(())
})
.await
.map_err(join_to_io)
.map_err(to_smb)?
.map_err(to_smb)
}
pub(super) async fn rename_stream(
self,
rel: PathBuf,
id: u64,
from: Option<StreamName>,
to: Option<StreamName>,
replace: bool,
) -> SmbResult<()> {
spawn_blocking(move || -> io::Result<()> {
if let Some(from) = &from {
Attr::of(from)?;
}
let moves_data = from.is_none() || to.is_none();
let (base, md, file) = self.base(&rel, id, moves_data)?;
let mut open = lock(&file.open);
let view = View::load(&base)?;
let source = match &from {
Some(from) => Some(view.find(from).ok_or(io::ErrorKind::NotFound)?),
None => None,
};
match (source, to) {
(Some(source), Some(to)) => {
let target = Attr::of(&to)?;
let moved = data(&base, &source.main)?;
let aside = Aside::take(&base, view.find(&to), replace)?;
if let Err(e) = put_new(&base, &target, &moved) {
aside.put_back(&base);
return Err(e);
}
if let Err(e) = remove_value(&base, &source.main.xattr) {
if let Err(e) = remove_value(&base, &target.xattr) {
debug!(error = %e, "stream rename not undone");
}
aside.put_back(&base);
return Err(e);
}
remove_others(&base, source);
open.rename(&source.main.name, &to);
}
(None, Some(to)) => {
if view.find(&to).is_none() && view.stored.len() >= MAX_STREAMS {
return Err(io::ErrorKind::StorageFull.into());
}
let target = Attr::of(&to)?;
if md.len() > target.kind.max() {
return Err(io::ErrorKind::StorageFull.into());
}
let mut moved = vec![0u8; md.len() as usize];
base.read_exact_at(&mut moved, 0)?;
let aside = Aside::take(&base, view.find(&to), replace)?;
if let Err(e) = put_new(&base, &target, &moved) {
aside.put_back(&base);
return Err(e);
}
if let Err(e) = base.set_len(0) {
if let Err(e) = remove_value(&base, &target.xattr) {
debug!(error = %e, "stream rename not undone");
}
aside.put_back(&base);
return Err(e);
}
}
(Some(source), None) => {
if !replace {
return Err(io::ErrorKind::AlreadyExists.into());
}
let moved = data(&base, &source.main)?;
let written = base
.write_all_at(&moved, 0)
.and_then(|()| base.set_len(moved.len() as u64))
.and_then(|()| remove_value(&base, &source.main.xattr));
if let Err(e) = written {
if let Err(e) = base.set_len(0) {
debug!(error = %e, "default stream not emptied again");
}
return Err(e);
}
remove_others(&base, source);
}
(None, None) => return Ok(()),
}
touch(&base);
Ok(())
})
.await
.map_err(join_to_io)
.map_err(to_smb)?
.map_err(to_smb)
}
}
struct StreamHandle {
file: Arc<StreamFile>,
id: u64,
base: Arc<File>,
is_dir: bool,
name: String,
root_dev: Option<u64>,
read_only: bool,
}
impl Drop for StreamHandle {
fn drop(&mut self) {
lock(&self.file.open).names.remove(&self.id);
}
}
impl StreamHandle {
async fn locked<T: Send + 'static>(
&self,
f: impl FnOnce(&File, &Attr) -> io::Result<T> + Send + 'static,
) -> SmbResult<T> {
let file = Arc::clone(&self.file);
let base = Arc::clone(&self.base);
let id = self.id;
spawn_blocking(move || {
let open = lock(&file.open);
let stream = open.names.get(&id).ok_or(io::ErrorKind::NotFound)?;
f(&base, &Attr::of(stream)?)
})
.await
.map_err(join_to_io)
.map_err(to_smb)?
.map_err(to_smb)
}
async fn on_base<T: Send + 'static>(
&self,
f: impl FnOnce(&File) -> io::Result<T> + Send + 'static,
) -> SmbResult<T> {
let base = Arc::clone(&self.base);
spawn_blocking(move || f(&base))
.await
.map_err(join_to_io)
.map_err(io_to_smb)?
.map_err(io_to_smb)
}
}
#[async_trait]
impl Handle for StreamHandle {
async fn read(&self, offset: u64, len: u32) -> SmbResult<Bytes> {
self.locked(move |fd, attr| read(fd, attr, offset, len))
.await
}
async fn write(&self, offset: u64, data: &[u8]) -> SmbResult<u32> {
self.write_owned(offset, data.to_vec()).await
}
async fn write_owned(&self, offset: u64, data: Vec<u8>) -> SmbResult<u32> {
if self.read_only {
return Err(SmbError::AccessDenied);
}
if data.is_empty() {
return Ok(0);
}
self.locked(move |fd, attr| {
write(fd, attr, offset, &data)?;
Ok(data.len() as u32)
})
.await
}
async fn flush(&self) -> SmbResult<()> {
self.on_base(|fd| fd.sync_all()).await
}
async fn stat(&self) -> SmbResult<FileInfo> {
let name = self.name.clone();
let root_dev = self.root_dev;
self.locked(move |fd, attr| {
let md = Metadata::from_file(fd)?;
let mut info = file_info_from_metadata(name, &md, root_dev);
info.end_of_file = size(fd, attr)?;
info.allocation_size = info.end_of_file;
Ok(info)
})
.await
}
async fn set_times(&self, times: FileTimes) -> SmbResult<()> {
if self.read_only {
return Err(SmbError::AccessDenied);
}
let times = host_file_times(times);
self.on_base(move |fd| fd.set_times(times)).await
}
async fn set_attributes(&self, attributes: u32) -> SmbResult<()> {
if self.read_only {
return Err(SmbError::AccessDenied);
}
let is_dir = self.is_dir;
self.on_base(move |fd| set_host_attributes(fd, attributes, is_dir))
.await
}
async fn truncate(&self, len: u64) -> SmbResult<()> {
if self.read_only {
return Err(SmbError::AccessDenied);
}
self.locked(move |fd, attr| truncate(fd, attr, len)).await
}
async fn list_dir(&self, _pattern: Option<&str>) -> SmbResult<Vec<DirEntry>> {
Err(SmbError::NotADirectory)
}
async fn close(self: Box<Self>) -> SmbResult<()> {
Ok(())
}
async fn link_count(&self) -> SmbResult<u32> {
let is_dir = self.is_dir;
self.on_base(move |fd| {
let md = Metadata::from_file(fd)?;
let links = link_count(&md).map_or(1, |n| u32::try_from(n).unwrap_or(u32::MAX));
Ok(if is_dir { links.min(1) } else { links })
})
.await
}
async fn dir_is_empty(&self) -> SmbResult<bool> {
Err(SmbError::NotADirectory)
}
async fn streams(&self) -> SmbResult<Vec<StreamInfo>> {
let base = Arc::clone(&self.base);
spawn_blocking(move || list(&base))
.await
.map_err(join_to_io)
.map_err(to_smb)?
.map_err(to_smb)
}
}
#[cfg(test)]
mod tests;