use std::collections::HashMap;
use std::fs::File;
use std::io;
use rustix::fs::XattrFlags;
use rustix::io::Errno;
use super::streams::read_sized;
use crate::ea::{self, Ea};
use crate::error::SmbError;
const USER: &str = "user.";
const MAX_NAME: usize = 255;
const PRIVATE: [&str; 5] = [
"DOSATTRIB",
"SAMBA_PAI",
"SAMBA_STREAMS",
"SmbReparse",
"org.netatalk.Metadata",
];
const PRIVATE_PREFIXES: [&str; 2] = ["DosStream.", "smb-server."];
fn private(name: &str) -> bool {
PRIVATE.iter().any(|p| p.eq_ignore_ascii_case(name))
|| PRIVATE_PREFIXES.iter().any(|p| {
name.get(..p.len())
.is_some_and(|start| start.eq_ignore_ascii_case(p))
})
}
pub(super) enum Source<'a> {
File(&'a File),
Path {
path: String,
follow: bool,
},
}
impl Source<'_> {
pub(super) fn entry(dir: &impl std::os::fd::AsRawFd, name: &str) -> Source<'static> {
Source::Path {
path: format!("/proc/self/fd/{}/{name}", dir.as_raw_fd()),
follow: false,
}
}
pub(super) fn fd(fd: &impl std::os::fd::AsRawFd) -> Source<'static> {
Source::Path {
path: format!("/proc/self/fd/{}", fd.as_raw_fd()),
follow: true,
}
}
fn list(&self) -> io::Result<Vec<u8>> {
read_sized(
|| match self {
Source::File(fd) => rustix::fs::flistxattr_query(fd),
Source::Path { path, follow: true } => rustix::fs::listxattr_query(path),
Source::Path { path, .. } => rustix::fs::llistxattr_query(path),
},
|buf| match self {
Source::File(fd) => rustix::fs::flistxattr(fd, buf),
Source::Path { path, follow: true } => rustix::fs::listxattr(path, buf),
Source::Path { path, .. } => rustix::fs::llistxattr(path, buf),
},
)
}
fn size(&self, attr: &str) -> io::Result<usize> {
Ok(match self {
Source::File(fd) => rustix::fs::fgetxattr_query(fd, attr),
Source::Path { path, follow: true } => rustix::fs::getxattr_query(path, attr),
Source::Path { path, .. } => rustix::fs::lgetxattr_query(path, attr),
}?)
}
}
struct Stored {
names: Vec<String>,
}
impl Stored {
fn main(&self) -> &str {
&self.names[0]
}
fn ea_name(&self) -> &str {
&self.main()[USER.len()..]
}
}
fn stored(source: &Source<'_>) -> io::Result<(Vec<Stored>, HashMap<String, usize>)> {
let list = match source.list() {
Ok(list) => list,
Err(e) if e.raw_os_error() == Some(Errno::NOTSUP.raw_os_error()) => Vec::new(),
Err(e) => return Err(e),
};
let mut out: Vec<Stored> = Vec::new();
let mut at: HashMap<String, usize> = HashMap::new();
for attr in list.split(|&b| b == 0) {
let Some(name) = std::str::from_utf8(attr)
.ok()
.and_then(|a| a.strip_prefix(USER))
else {
continue;
};
if private(name) || !ea::valid_name(name.as_bytes()) {
continue;
}
let attr = format!("{USER}{name}");
match at.get(&ea::name_key(name)) {
Some(&i) => out[i].names.push(attr),
None => {
at.insert(ea::name_key(name), out.len());
out.push(Stored { names: vec![attr] });
}
}
}
for stored in &mut out {
stored.names.sort();
}
Ok((out, at))
}
fn is_gone(e: &io::Error) -> bool {
e.raw_os_error() == Some(Errno::NODATA.raw_os_error())
}
pub(super) fn list(fd: &File) -> io::Result<Vec<Ea>> {
let mut out = Vec::new();
for stored in stored(&Source::File(fd))?.0 {
let value = match get(fd, stored.main()) {
Ok(value) => value,
Err(e) if is_gone(&e) => continue,
Err(e) => return Err(e),
};
if (1..=ea::MAX_VALUE).contains(&value.len()) {
out.push(Ea::new(stored.ea_name(), value));
}
}
Ok(out)
}
pub(super) fn size(source: &Source<'_>) -> io::Result<u32> {
let mut sizes = Vec::new();
for stored in stored(source)?.0 {
match source.size(stored.main()) {
Ok(len @ 1..=ea::MAX_VALUE) => sizes.push((stored.ea_name().len(), len)),
Ok(_) => {}
Err(e) if is_gone(&e) => {}
Err(e) => return Err(e),
}
}
Ok(ea::list_length(sizes))
}
fn get(fd: &File, attr: &str) -> io::Result<Vec<u8>> {
read_sized(
|| rustix::fs::fgetxattr_query(fd, attr),
|buf| rustix::fs::fgetxattr(fd, attr, buf),
)
}
fn to_smb(e: io::Error) -> SmbError {
match e.raw_os_error().map(Errno::from_raw_os_error) {
Some(Errno::TOOBIG | Errno::NOSPC) => SmbError::EaTooLarge,
Some(Errno::NOTSUP) => SmbError::NotSupported,
_ => super::local::io_to_smb(e),
}
}
struct Undo {
attr: String,
value: Option<Vec<u8>>,
}
pub(super) fn set(fd: &File, eas: &[Ea]) -> Result<(), SmbError> {
for ea in eas {
if private(&ea.name) {
return Err(SmbError::AccessDenied);
}
if USER.len() + ea.name.len() > MAX_NAME {
return Err(SmbError::EaNameInvalid);
}
}
let (current, at) = stored(&Source::File(fd)).map_err(to_smb)?;
let mut undo = Vec::new();
let Err(e) = apply(fd, eas, ¤t, &at, &mut undo) else {
return Ok(());
};
let mut lost = false;
for step in undo.into_iter().rev() {
let back = match &step.value {
Some(value) => rustix::fs::fsetxattr(fd, &step.attr, value, XattrFlags::empty()),
None => rustix::fs::fremovexattr(fd, &step.attr),
};
if let Err(back) = back {
tracing::warn!(error = %back, attr = %step.attr, "an extended attribute was not put back");
lost = true;
}
}
if lost {
return Err(SmbError::Io(io::Error::other(format!(
"extended attributes left half set after: {e}"
))));
}
Err(to_smb(e))
}
fn apply(
fd: &File,
eas: &[Ea],
current: &[Stored],
at: &HashMap<String, usize>,
undo: &mut Vec<Undo>,
) -> io::Result<()> {
for ea in eas {
let names = match at.get(&ea::name_key(&ea.name)) {
Some(&i) => current[i].names.clone(),
None => vec![format!("{USER}{}", ea.name)],
};
for (n, attr) in names.iter().enumerate() {
let old = match get(fd, attr) {
Ok(value) => Some(value),
Err(e) if is_gone(&e) => None,
Err(e) => return Err(e),
};
if n == 0 && !ea.value.is_empty() {
rustix::fs::fsetxattr(fd, attr, &ea.value, XattrFlags::empty())?;
} else if old.is_some() {
match rustix::fs::fremovexattr(fd, attr) {
Ok(()) | Err(Errno::NODATA) => {}
Err(e) => return Err(e.into()),
}
} else {
continue;
}
undo.push(Undo {
attr: attr.clone(),
value: old,
});
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn private_names_match_in_any_case() {
for name in [
"DosStream.s:$DATA",
"dosstream.x",
"DOSATTRIB",
"dosattrib",
"smb-server.streams",
"SMB-SERVER.other",
"org.netatalk.metadata",
] {
assert!(private(name), "{name}");
}
for name in [
"DosStream",
"DOSATTRIB2",
"smb-server",
"test",
"xdg.origin.url",
] {
assert!(!private(name), "{name}");
}
}
}