229 lines
8.1 KiB
Rust
229 lines
8.1 KiB
Rust
//! Captured namespace evidence for a missing final file. The binding describes
|
|
//! a destination, never a mutable role or an authorization to read its key.
|
|
use super::*;
|
|
use std::io;
|
|
|
|
pub(super) const MAX_OBSERVATIONS: usize = 1_000_000;
|
|
pub(super) const MAX_PROBE_OBSERVATIONS: usize = 82;
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub enum NamespaceKind {
|
|
File,
|
|
Directory,
|
|
Symlink,
|
|
Other,
|
|
}
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct NamespaceMetadata {
|
|
pub kind: NamespaceKind,
|
|
pub bytes: u64,
|
|
pub readonly: bool,
|
|
pub modified: Result<SystemTime, String>,
|
|
pub unix_identity: Option<(u64, u64)>,
|
|
}
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub enum NamespaceObservation {
|
|
SymlinkMetadata {
|
|
path: PathBuf,
|
|
/// None is an actual NotFound result; other failures remain errors.
|
|
result: Result<Option<NamespaceMetadata>, String>,
|
|
},
|
|
ReadLink {
|
|
path: PathBuf,
|
|
result: Result<PathBuf, String>,
|
|
},
|
|
Canonicalize {
|
|
path: PathBuf,
|
|
result: Result<PathBuf, String>,
|
|
},
|
|
}
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct DeferredProbe {
|
|
/// Only a missing final file with an existing directory parent has a
|
|
/// destination. Missing ancestors and directory-only spellings have none.
|
|
pub target: Result<Option<PathBuf>, String>,
|
|
pub observations: Vec<NamespaceObservation>,
|
|
}
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct NamespaceCaptureFailure {
|
|
pub candidate: PathBuf,
|
|
pub probe: DeferredProbe,
|
|
pub error: String,
|
|
}
|
|
/// No eligible resource file with this spelling was listed in a completely
|
|
/// observed directory. This does not claim the path has no directory node.
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct DirectoryAbsence {
|
|
pub candidate: PathBuf,
|
|
pub root: PathBuf,
|
|
pub logical_directory: PathBuf,
|
|
pub canonical_directory: PathBuf,
|
|
pub replayed_namespace: bool,
|
|
}
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub enum GeneratedLookupOrigin {
|
|
Deferred(DeferredProbe),
|
|
Directory {
|
|
root: PathBuf,
|
|
logical_directory: PathBuf,
|
|
canonical_directory: PathBuf,
|
|
},
|
|
}
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub enum GeneratedLookupRole {
|
|
Mutable,
|
|
PromotedResource,
|
|
}
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct GeneratedLookup {
|
|
pub candidate: PathBuf,
|
|
pub target: PathBuf,
|
|
pub origin: GeneratedLookupOrigin,
|
|
pub role: GeneratedLookupRole,
|
|
/// True means the binding came from an imported captured view. Namespace
|
|
/// observations inside origin are not claimed as fresh filesystem calls.
|
|
pub replayed_namespace: bool,
|
|
/// None for an already pinned promoted resource; otherwise the actual
|
|
/// filesystem probe before checking that the target still resolves itself.
|
|
pub observed: Option<Result<Option<PathBuf>, String>>,
|
|
pub result: Result<Option<PathBuf>, String>,
|
|
}
|
|
|
|
impl DeferredProbe {
|
|
pub(super) fn observe(path: &Path) -> Self {
|
|
let mut observations = Vec::new();
|
|
let target = locate(path, &mut observations);
|
|
Self {
|
|
target,
|
|
observations,
|
|
}
|
|
}
|
|
pub(super) fn validate(&self) -> Result<(), String> {
|
|
if self.observations.len() > MAX_PROBE_OBSERVATIONS {
|
|
return Err("deferred namespace observation capacity exceeded".into());
|
|
}
|
|
if let Ok(Some(path)) = &self.target {
|
|
if !canonical_path(path) {
|
|
return Err("deferred namespace destination must be canonical and absolute".into());
|
|
}
|
|
}
|
|
for observation in &self.observations {
|
|
let path = match observation {
|
|
NamespaceObservation::SymlinkMetadata { path, .. }
|
|
| NamespaceObservation::ReadLink { path, .. }
|
|
| NamespaceObservation::Canonicalize { path, .. } => path,
|
|
};
|
|
if !path.is_absolute() {
|
|
return Err("deferred namespace observation must be absolute".into());
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
pub(super) fn canonical_path(path: &Path) -> bool {
|
|
path.is_absolute()
|
|
&& path.components().collect::<PathBuf>().as_os_str() == path.as_os_str()
|
|
&& !path.components().any(|c| {
|
|
matches!(
|
|
c,
|
|
std::path::Component::ParentDir | std::path::Component::CurDir
|
|
)
|
|
})
|
|
}
|
|
fn metadata_value(m: &fs::Metadata) -> NamespaceMetadata {
|
|
#[cfg(unix)]
|
|
let unix_identity = {
|
|
use std::os::unix::fs::MetadataExt;
|
|
Some((m.dev(), m.ino()))
|
|
};
|
|
#[cfg(not(unix))]
|
|
let unix_identity = None;
|
|
NamespaceMetadata {
|
|
kind: if m.file_type().is_symlink() {
|
|
NamespaceKind::Symlink
|
|
} else if m.is_dir() {
|
|
NamespaceKind::Directory
|
|
} else if m.is_file() {
|
|
NamespaceKind::File
|
|
} else {
|
|
NamespaceKind::Other
|
|
},
|
|
bytes: m.len(),
|
|
readonly: m.permissions().readonly(),
|
|
modified: m.modified().map_err(|e| e.to_string()),
|
|
unix_identity,
|
|
}
|
|
}
|
|
pub(super) fn directory_only(path: &Path) -> bool {
|
|
let raw = path.as_os_str().to_string_lossy();
|
|
raw.ends_with(std::path::is_separator)
|
|
|| raw
|
|
.strip_suffix('.')
|
|
.is_some_and(|p| p.ends_with(std::path::is_separator))
|
|
|| path.file_name().is_none()
|
|
}
|
|
fn canonicalize(path: &Path, observations: &mut Vec<NamespaceObservation>) -> io::Result<PathBuf> {
|
|
let result = path.canonicalize();
|
|
observations.push(NamespaceObservation::Canonicalize {
|
|
path: path.into(),
|
|
result: result.as_ref().cloned().map_err(ToString::to_string),
|
|
});
|
|
result
|
|
}
|
|
fn locate(
|
|
path: &Path,
|
|
observations: &mut Vec<NamespaceObservation>,
|
|
) -> Result<Option<PathBuf>, String> {
|
|
if !path.is_absolute() {
|
|
return Err("deferred lookup must be absolute".into());
|
|
}
|
|
let mut current = path.to_owned();
|
|
let mut links = 0;
|
|
loop {
|
|
if directory_only(¤t) {
|
|
return Ok(None);
|
|
}
|
|
let metadata = fs::symlink_metadata(¤t);
|
|
observations.push(NamespaceObservation::SymlinkMetadata {
|
|
path: current.clone(),
|
|
result: match &metadata {
|
|
Ok(m) => Ok(Some(metadata_value(m))),
|
|
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
|
|
Err(e) => Err(e.to_string()),
|
|
},
|
|
});
|
|
match metadata {
|
|
Ok(m) if m.file_type().is_symlink() => {
|
|
links += 1;
|
|
if links > 40 {
|
|
return Err("deferred lookup alias depth exceeded".into());
|
|
}
|
|
let target = fs::read_link(¤t);
|
|
observations.push(NamespaceObservation::ReadLink {
|
|
path: current.clone(),
|
|
result: target.as_ref().cloned().map_err(ToString::to_string),
|
|
});
|
|
current = current
|
|
.parent()
|
|
.unwrap()
|
|
.join(target.map_err(|e| e.to_string())?);
|
|
}
|
|
Ok(m) if m.is_dir() => return Ok(None),
|
|
Ok(_) => {
|
|
return canonicalize(¤t, observations)
|
|
.map(Some)
|
|
.map_err(|e| e.to_string());
|
|
}
|
|
Err(e) if e.kind() == io::ErrorKind::NotFound => {
|
|
let parent = current.parent().ok_or("deferred file requires a parent")?;
|
|
return match canonicalize(parent, observations) {
|
|
Ok(parent) => Ok(Some(parent.join(current.file_name().unwrap()))),
|
|
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
|
|
Err(e) => Err(e.to_string()),
|
|
};
|
|
}
|
|
Err(e) => return Err(e.to_string()),
|
|
}
|
|
}
|
|
}
|