use std::collections::HashMap;
use std::hash::{DefaultHasher, Hash, Hasher};
use std::io;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard, mpsc};
use std::thread;
use std::time::{Duration, Instant};
use crate::error::{CoreError, ErrorCode};
use crate::fs::Fs;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "specta", derive(specta::Type))]
#[serde(rename_all = "snake_case")]
pub enum ReadOnlyKind {
Acl,
Attribute,
Owner,
Share,
}
impl ReadOnlyKind {
pub const fn as_str(self) -> &'static str {
match self {
ReadOnlyKind::Acl => "acl",
ReadOnlyKind::Attribute => "attribute",
ReadOnlyKind::Owner => "owner",
ReadOnlyKind::Share => "share",
}
}
pub const fn describe(self) -> &'static str {
match self {
ReadOnlyKind::Acl => "its permissions deny this account writing",
ReadOnlyKind::Attribute => "it is marked read only",
ReadOnlyKind::Owner => "another user owns it and this account may not write it",
ReadOnlyKind::Share => "it is on a read-only share or mount",
}
}
}
impl std::fmt::Display for ReadOnlyKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
pub fn read_only_json<S: serde::Serializer>(
kind: &Option<ReadOnlyKind>,
serializer: S,
) -> Result<S::Ok, S::Error> {
#[derive(serde::Serialize)]
struct ReadOnly {
kind: ReadOnlyKind,
}
serde::Serialize::serialize(&kind.map(|kind| ReadOnly { kind }), serializer)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum WriteAccess {
Writable,
ReadOnly(ReadOnlyKind),
Unknown,
}
impl WriteAccess {
pub const fn read_only(self) -> Option<ReadOnlyKind> {
match self {
WriteAccess::ReadOnly(kind) => Some(kind),
_ => None,
}
}
}
pub type WriteProbe = Arc<dyn Fn(&Path) -> WriteAccess + Send + Sync>;
pub const PROBE_BUDGET: Duration = Duration::from_secs(2);
pub fn probe(path: &Path) -> WriteAccess {
platform::probe(&crate::fs::long(path))
}
pub fn fingerprint(bytes: Option<&[u8]>) -> Option<u64> {
bytes.map(|bytes| {
let mut hasher = DefaultHasher::new();
bytes.hash(&mut hasher);
hasher.finish()
})
}
#[derive(Debug)]
pub struct ProbeCache {
budget: Duration,
entries: Arc<Mutex<HashMap<PathBuf, Cached>>>,
}
#[derive(Debug, Clone, Copy)]
enum Cached {
Done(Option<u64>, WriteAccess),
Running(Option<u64>),
}
impl Default for ProbeCache {
fn default() -> Self {
Self::with_budget(PROBE_BUDGET)
}
}
impl ProbeCache {
pub fn new() -> Self {
Self::default()
}
pub fn with_budget(budget: Duration) -> Self {
Self {
budget,
entries: Arc::default(),
}
}
pub fn clear(&self) {
lock(&self.entries).retain(|_, cached| matches!(cached, Cached::Running(_)));
}
}
fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
mutex
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
pub fn probe_files(
fs: &dyn Fs,
files: &[(PathBuf, Option<u64>)],
cache: Option<&ProbeCache>,
) -> HashMap<PathBuf, WriteAccess> {
let mut out: HashMap<PathBuf, WriteAccess> = HashMap::new();
let Some(probe) = fs.write_probe() else {
for (path, _) in files {
out.insert(path.clone(), WriteAccess::Unknown);
}
return out;
};
let local;
let cache = match cache {
Some(cache) => cache,
None => {
local = ProbeCache::new();
&local
}
};
let (tx, rx) = mpsc::channel::<(PathBuf, WriteAccess)>();
let mut running: Vec<(PathBuf, thread::JoinHandle<()>)> = Vec::new();
{
let mut entries = lock(&cache.entries);
for (path, print) in files {
if out.contains_key(path) || running.iter().any(|(p, _)| p == path) {
continue;
}
match entries.get(path) {
Some(Cached::Done(seen, access)) if seen == print => {
out.insert(path.clone(), *access);
continue;
}
Some(Cached::Running(seen)) if seen == print => {
out.insert(path.clone(), WriteAccess::Unknown);
continue;
}
_ => {}
}
entries.insert(path.clone(), Cached::Running(*print));
let (probe, tx, shared) = (probe.clone(), tx.clone(), cache.entries.clone());
let (owned, print) = (path.clone(), *print);
let spawned = thread::Builder::new()
.name("surjection-probe".to_owned())
.spawn(move || {
let access = probe(&owned);
{
let mut entries = lock(&shared);
if matches!(entries.get(&owned), Some(Cached::Running(seen)) if *seen == print)
{
entries.insert(owned.clone(), Cached::Done(print, access));
}
}
let _ = tx.send((owned, access));
});
match spawned {
Ok(handle) => running.push((path.clone(), handle)),
Err(_) => {
entries.remove(path);
out.insert(path.clone(), WriteAccess::Unknown);
}
}
}
}
drop(tx);
let deadline = Instant::now() + cache.budget;
let mut left = running.len();
while left > 0 {
let now = Instant::now();
if now >= deadline {
break;
}
match rx.recv_timeout(deadline - now) {
Ok((path, access)) => {
out.insert(path, access);
left -= 1;
}
Err(_) => break,
}
}
for (path, handle) in running {
if out.contains_key(&path) {
continue;
}
if handle.is_finished()
&& let Err(panic) = handle.join()
{
lock(&cache.entries).remove(&path);
std::panic::resume_unwind(panic);
}
out.insert(path, WriteAccess::Unknown);
}
out
}
pub fn probe_path(fs: &dyn Fs, path: &Path) -> WriteAccess {
probe_files(fs, &[(path.to_path_buf(), None)], None)
.remove(path)
.unwrap_or(WriteAccess::Unknown)
}
pub fn denied(fs: &dyn Fs, err: &io::Error, path: &Path) -> Option<ReadOnlyKind> {
if cfg!(windows) && matches!(err.raw_os_error(), Some(32 | 33)) {
return None;
}
if err.kind() == io::ErrorKind::ReadOnlyFilesystem
|| (cfg!(windows) && matches!(err.raw_os_error(), Some(19 | 65)))
{
return Some(ReadOnlyKind::Share);
}
if err.kind() != io::ErrorKind::PermissionDenied {
return None;
}
let fallback = if is_unc(path) {
ReadOnlyKind::Share
} else {
ReadOnlyKind::Acl
};
match fs.write_probe().map(|probe| probe(path)) {
Some(WriteAccess::ReadOnly(kind)) => Some(kind),
Some(WriteAccess::Writable) => None,
Some(WriteAccess::Unknown) | None => Some(fallback),
}
}
pub fn read_only_target(kind: ReadOnlyKind, path: &Path) -> CoreError {
CoreError::new(
ErrorCode::ReadOnlyTarget,
format!(
"this account cannot write here ({}); Surjection leaves it as it is",
kind.describe()
),
)
.with_path(path)
.with_read_only(kind)
}
pub fn write_error(fs: &dyn Fs, what: &str, path: &Path, err: &io::Error) -> CoreError {
match denied(fs, err, path) {
Some(kind) => read_only_target(kind, path),
None => CoreError::new(ErrorCode::IoError, format!("{what}: {err}")).with_path(path),
}
}
fn is_unc(path: &Path) -> bool {
if !cfg!(windows) {
return false;
}
let text = path.as_os_str().to_string_lossy();
text.starts_with(r"\\?\UNC\")
|| (text.starts_with(r"\\") && !text.starts_with(r"\\?\") && !text.starts_with(r"\\.\"))
}
fn existing_parent(path: &Path) -> Option<(&Path, bool)> {
let mut direct = true;
let mut at = path.parent();
while let Some(dir) = at {
if dir.is_dir() {
return Some((dir, direct));
}
if dir.exists() {
return None;
}
direct = false;
at = dir.parent();
}
None
}
#[cfg(windows)]
mod platform {
use std::fs::OpenOptions;
use std::io;
use std::os::windows::fs::{MetadataExt, OpenOptionsExt};
use std::path::Path;
use super::{ReadOnlyKind, WriteAccess, existing_parent, is_unc};
const FILE_ATTRIBUTE_READONLY: u32 = 0x1;
const FILE_ADD_FILE: u32 = 0x2;
const FILE_ADD_SUBDIRECTORY: u32 = 0x4;
const FILE_DELETE_CHILD: u32 = 0x40;
const DELETE: u32 = 0x0001_0000;
const GENERIC_WRITE: u32 = 0x4000_0000;
const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x0200_0000;
pub fn probe(path: &Path) -> WriteAccess {
match std::fs::metadata(path) {
Ok(meta) if meta.is_dir() => open_dir(
path,
FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY | FILE_DELETE_CHILD,
),
Ok(meta) => {
if meta.file_attributes() & FILE_ATTRIBUTE_READONLY != 0 {
return WriteAccess::ReadOnly(ReadOnlyKind::Attribute);
}
if let Err(err) = OpenOptions::new()
.access_mode(GENERIC_WRITE | DELETE)
.open(path)
{
return classify(&err, path);
}
match path.parent() {
Some(dir) => open_dir(dir, FILE_ADD_FILE),
None => WriteAccess::Writable,
}
}
Err(err) if err.kind() == io::ErrorKind::NotFound => match existing_parent(path) {
Some((dir, true)) => open_dir(dir, FILE_ADD_FILE),
Some((dir, false)) => open_dir(dir, FILE_ADD_SUBDIRECTORY),
None => WriteAccess::Unknown,
},
Err(_) => WriteAccess::Unknown,
}
}
fn open_dir(dir: &Path, access: u32) -> WriteAccess {
match OpenOptions::new()
.access_mode(access)
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
.open(dir)
{
Ok(_) => WriteAccess::Writable,
Err(err) => classify(&err, dir),
}
}
fn classify(err: &io::Error, path: &Path) -> WriteAccess {
match err.raw_os_error() {
Some(5) if is_unc(path) => WriteAccess::ReadOnly(ReadOnlyKind::Share),
Some(5) => WriteAccess::ReadOnly(ReadOnlyKind::Acl),
Some(19 | 65) => WriteAccess::ReadOnly(ReadOnlyKind::Share),
_ if err.kind() == io::ErrorKind::ReadOnlyFilesystem => {
WriteAccess::ReadOnly(ReadOnlyKind::Share)
}
_ => WriteAccess::Unknown,
}
}
}
#[cfg(unix)]
mod platform {
use std::fs::Metadata;
use std::io;
use std::os::unix::fs::MetadataExt;
use std::path::Path;
use rustix::fs::{Access, access};
use rustix::io::Errno;
use super::{ReadOnlyKind, WriteAccess, existing_parent};
pub fn probe(path: &Path) -> WriteAccess {
match std::fs::metadata(path) {
Ok(meta) if meta.is_dir() => check(path, &meta, Access::WRITE_OK | Access::EXEC_OK),
Ok(meta) => {
let file = check(path, &meta, Access::WRITE_OK);
if file != WriteAccess::Writable {
return file;
}
match path.parent() {
Some(dir) => folder(dir),
None => WriteAccess::Writable,
}
}
Err(err) if err.kind() == io::ErrorKind::NotFound => match existing_parent(path) {
Some((dir, _)) => folder(dir),
None => WriteAccess::Unknown,
},
Err(_) => WriteAccess::Unknown,
}
}
fn folder(dir: &Path) -> WriteAccess {
match std::fs::metadata(dir) {
Ok(meta) => check(dir, &meta, Access::WRITE_OK | Access::EXEC_OK),
Err(_) => WriteAccess::Unknown,
}
}
fn check(path: &Path, meta: &Metadata, mode: Access) -> WriteAccess {
match access(path, mode) {
Ok(()) => WriteAccess::Writable,
Err(Errno::ROFS) => WriteAccess::ReadOnly(ReadOnlyKind::Share),
Err(Errno::ACCESS | Errno::PERM) => WriteAccess::ReadOnly(kind(meta)),
Err(_) => WriteAccess::Unknown,
}
}
fn kind(meta: &Metadata) -> ReadOnlyKind {
let mode = meta.mode();
if meta.uid() == rustix::process::geteuid().as_raw() {
if mode & 0o200 == 0 {
ReadOnlyKind::Attribute
} else {
ReadOnlyKind::Acl
}
} else if mode & 0o022 == 0 {
ReadOnlyKind::Owner
} else {
ReadOnlyKind::Acl
}
}
}
#[cfg(not(any(windows, unix)))]
mod platform {
use std::path::Path;
use super::WriteAccess;
pub fn probe(_path: &Path) -> WriteAccess {
WriteAccess::Unknown
}
}