Jobs with progress, cancel, conflict and error requests; scan pass, copy_file_range and stream copies, rename moves; per-filesystem trash with .trashinfo, restore and empty; ops smoke example. Co-Authored-By: Claude Opus 5.5 <[email protected]>
158 lines
4.6 KiB
Rust
158 lines
4.6 KiB
Rust
//! Location URIs: `scheme://authority/path`, path kept as raw bytes.
|
|
|
|
use std::ffi::{OsStr, OsString};
|
|
use std::fmt;
|
|
use std::os::unix::ffi::{OsStrExt, OsStringExt};
|
|
use std::path::{Path, PathBuf};
|
|
|
|
/// Scheme of local files.
|
|
pub const FILE: &str = "file";
|
|
|
|
/// A location address. The path is raw bytes, so non-UTF-8 names survive;
|
|
/// for `file://` it maps 1:1 to an `OsString`.
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
pub struct Uri {
|
|
scheme: String,
|
|
authority: String,
|
|
path: Vec<u8>,
|
|
}
|
|
|
|
impl Uri {
|
|
/// Builds a URI; an empty path becomes `/`.
|
|
pub fn new(scheme: &str, authority: &str, path: impl Into<Vec<u8>>) -> Self {
|
|
let mut path = path.into();
|
|
if path.first() != Some(&b'/') {
|
|
path.insert(0, b'/');
|
|
}
|
|
Self {
|
|
scheme: scheme.to_ascii_lowercase(),
|
|
authority: authority.to_owned(),
|
|
path,
|
|
}
|
|
}
|
|
|
|
/// `file://` URI for a local path (made absolute against the current dir if relative).
|
|
pub fn from_path(path: &Path) -> Self {
|
|
let path = std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf());
|
|
Self::new(FILE, "", path.into_os_string().into_vec())
|
|
}
|
|
|
|
/// Parses `scheme://authority/path` with `%XX` escapes in the path.
|
|
pub fn parse(s: &str) -> Option<Self> {
|
|
let (scheme, rest) = s.split_once("://")?;
|
|
let valid = scheme.starts_with(|c: char| c.is_ascii_alphabetic())
|
|
&& scheme
|
|
.chars()
|
|
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'));
|
|
if !valid {
|
|
return None;
|
|
}
|
|
let (authority, path) = rest.split_at(rest.find('/').unwrap_or(rest.len()));
|
|
Some(Self::new(scheme, authority, percent_decode(path)?))
|
|
}
|
|
|
|
pub fn scheme(&self) -> &str {
|
|
&self.scheme
|
|
}
|
|
|
|
pub fn authority(&self) -> &str {
|
|
&self.authority
|
|
}
|
|
|
|
/// Raw path bytes, always starting with `/`.
|
|
pub fn path(&self) -> &[u8] {
|
|
&self.path
|
|
}
|
|
|
|
/// Local path for `file://` URIs.
|
|
pub fn to_path(&self) -> Option<PathBuf> {
|
|
(self.scheme == FILE).then(|| PathBuf::from(OsString::from_vec(self.path.clone())))
|
|
}
|
|
|
|
/// Last path component, `None` at the root.
|
|
pub fn file_name(&self) -> Option<&[u8]> {
|
|
let trimmed = self.trimmed();
|
|
let name = &trimmed[trimmed.iter().rposition(|&b| b == b'/')? + 1..];
|
|
(!name.is_empty()).then_some(name)
|
|
}
|
|
|
|
/// Parent folder, `None` at the root.
|
|
pub fn parent(&self) -> Option<Self> {
|
|
let trimmed = self.trimmed();
|
|
let cut = trimmed.iter().rposition(|&b| b == b'/')?;
|
|
(trimmed.len() > 1).then(|| Self::new(&self.scheme, &self.authority, &trimmed[..cut]))
|
|
}
|
|
|
|
/// Child `name` (a single component) inside this folder.
|
|
pub fn join(&self, name: &[u8]) -> Self {
|
|
let mut path = self.trimmed().to_vec();
|
|
if path.last() != Some(&b'/') {
|
|
path.push(b'/');
|
|
}
|
|
path.extend_from_slice(name);
|
|
Self::new(&self.scheme, &self.authority, path)
|
|
}
|
|
|
|
/// Path without trailing slashes (but `/` stays `/`).
|
|
fn trimmed(&self) -> &[u8] {
|
|
let end = self
|
|
.path
|
|
.iter()
|
|
.rposition(|&b| b != b'/')
|
|
.map_or(1, |i| i + 1);
|
|
&self.path[..end]
|
|
}
|
|
}
|
|
|
|
impl From<&Path> for Uri {
|
|
fn from(path: &Path) -> Self {
|
|
Self::from_path(path)
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for Uri {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(
|
|
f,
|
|
"{}://{}{}",
|
|
self.scheme,
|
|
self.authority,
|
|
percent_encode(&self.path)
|
|
)
|
|
}
|
|
}
|
|
|
|
/// Escapes a raw path as `%XX`, keeping `/` and the URI path characters.
|
|
pub(crate) fn percent_encode(bytes: &[u8]) -> String {
|
|
let mut out = String::with_capacity(bytes.len());
|
|
for &b in bytes {
|
|
if b.is_ascii_alphanumeric() || b"/-._~!$&'()*+,;=:@".contains(&b) {
|
|
out.push(b as char);
|
|
} else {
|
|
out.push_str(&format!("%{b:02X}"));
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
/// Lossy display form of a raw name or path.
|
|
pub fn display_name(bytes: &[u8]) -> String {
|
|
OsStr::from_bytes(bytes).to_string_lossy().into_owned()
|
|
}
|
|
|
|
pub(crate) fn percent_decode(s: &str) -> Option<Vec<u8>> {
|
|
let bytes = s.as_bytes();
|
|
let mut out = Vec::with_capacity(bytes.len());
|
|
let mut i = 0;
|
|
while i < bytes.len() {
|
|
if bytes[i] == b'%' {
|
|
let hex = std::str::from_utf8(bytes.get(i + 1..i + 3)?).ok()?;
|
|
out.push(u8::from_str_radix(hex, 16).ok()?);
|
|
i += 3;
|
|
} else {
|
|
out.push(bytes[i]);
|
|
i += 1;
|
|
}
|
|
}
|
|
Some(out)
|
|
}
|