A treemap for finding and removing what fills your disk, for Omarchy. Rust + GPUI.
Rust
core: add read-only SSH scanner and checked helper deployment
Tobi Lutke48b0284fa740
diff --git a/Cargo.lock b/Cargo.lockindex 71482b5..32f18ec 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1440,6 +1440,9 @@ dependencies = ["rayon","rustc-hash 2.1.3","rustix",+ "serde",+ "serde_json",+ "sha2 0.10.9","sysinfo","tempfile","trash",@@ -1447,6 +1450,15 @@ dependencies = ["windows-sys 0.61.2",]+[[package]]+name = "disktree-scan"+version = "0.11.0"+dependencies = [+ "disktree-core",+ "serde_json",+ "tempfile",+]+[[package]]name = "dispatch"version = "0.2.0"@@ -8672,6 +8684,9 @@ checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9"[[package]]name = "xtask"version = "0.11.0"+dependencies = [+ "disktree-core",+][[package]]name = "y4m"diff --git a/Cargo.toml b/Cargo.tomlindex 30dfed0..19d0d6e 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -1,6 +1,6 @@[workspace]resolver = "3"-members = ["crates/disktree-app", "crates/disktree-core", "xtask"]+members = ["crates/disktree-app", "crates/disktree-core", "crates/disktree-scan", "xtask"][workspace.package]version = "0.11.0"diff --git a/crates/disktree-core/Cargo.toml b/crates/disktree-core/Cargo.tomlindex 8f121eb..b4fc0d0 100644--- a/crates/disktree-core/Cargo.toml+++ b/crates/disktree-core/Cargo.toml@@ -13,6 +13,9 @@ workspace = truechrono = { version = "0.4.45", default-features = false, features = ["clock", "std"] }rayon = { workspace = true }rustc-hash = { workspace = true }+serde = { version = "1", features = ["derive"] }+serde_json = "1"+sha2 = "0.10"# rustix has no filesystem calls on Windows; `windows.rs` covers them there.[target.'cfg(not(windows))'.dependencies]diff --git a/crates/disktree-core/src/deployment.rs b/crates/disktree-core/src/deployment.rsnew file mode 100644index 0000000..d856074--- /dev/null+++ b/crates/disktree-core/src/deployment.rs@@ -0,0 +1,88 @@+//! Deploy only the matching embedded scanner, over the existing SSH pipe.++use crate::payload::Archive;+use std::io::{self, BufRead, Read as _, Write};++// The cache is content-addressed, private and atomically published. We never+// overwrite a system helper or trust an unchecked cached executable. The+// ready handshake keeps scan JSON out of the binary transfer's stdin.+pub const COMMAND: &str = r#"set -eu+printf 'DISKTREE_BOOTSTRAP_V1 %s %s\n' "$(uname -s)" "$(uname -m)"+IFS=' ' read -r digest size+case "$digest" in ''|*[!0-9a-f]*) exit 2;; esac+[ "${#digest}" -eq 64 ] || exit 2+case "$size" in ''|*[!0-9]*) exit 2;; esac+[ "$size" -gt 0 ] && [ "$size" -le 67108864 ] || exit 2+umask 077+dir="${XDG_CACHE_HOME:-$HOME/.cache}/disktree/ssh/$digest"+mkdir -p "$dir"+helper="$dir/disktree-scan"+valid() {+ [ -x "$helper" ] || return 1+ actual=$(sha256sum "$helper") || return 1+ [ "${actual%% *}" = "$digest" ]+}+if ! valid; then+ temporary=$(mktemp "$dir/.upload.XXXXXX")+ trap 'rm -f "$temporary"' EXIT HUP INT TERM+ printf 'DISKTREE_UPLOAD_V1\n'+ head -c "$size" > "$temporary"+ [ "$(wc -c < "$temporary")" -eq "$size" ] || exit 3+ actual=$(sha256sum "$temporary")+ [ "${actual%% *}" = "$digest" ] || exit 3+ chmod 700 "$temporary"+ mv -f "$temporary" "$helper"+ trap - EXIT HUP INT TERM+fi+printf 'DISKTREE_READY_V1\n'+exec "$helper" --stdio-v1+"#;++fn control_line(input: &mut impl BufRead) -> io::Result<String> {+ let mut line = String::new();+ input.take(1024).read_line(&mut line)?;+ if !line.ends_with('\n') {+ return Err(io::Error::other("incomplete SSH deployment handshake"));+ }+ Ok(line.trim_end().into())+}++pub fn deploy(+ archive: &Archive,+ input: &mut impl BufRead,+ output: &mut impl Write,+) -> io::Result<()> {+ let hello = control_line(input)?;+ let parts: Vec<_> = hello.split_whitespace().collect();+ if parts.len() != 3 || parts[0] != "DISKTREE_BOOTSTRAP_V1" {+ return Err(io::Error::other("unexpected SSH deployment handshake"));+ }+ if parts[1] != "Linux" {+ return Err(io::Error::other(format!(+ "embedded scanners support Linux, not {}",+ parts[1],+ )));+ }+ let arch = match parts[2] {+ "x86_64" => "x86_64",+ "aarch64" | "arm64" => "aarch64",+ other => {+ return Err(io::Error::other(format!(+ "unsupported remote CPU: {other}",+ )));+ }+ };+ let (entry, bytes) = archive.load(arch)?;+ writeln!(output, "{} {}", entry.sha256, entry.length)?;+ output.flush()?;+ let mut state = control_line(input)?;+ if state == "DISKTREE_UPLOAD_V1" {+ output.write_all(&bytes)?;+ output.flush()?;+ state = control_line(input)?;+ }+ if state != "DISKTREE_READY_V1" {+ return Err(io::Error::other("remote scanner deployment failed"));+ }+ Ok(())+}diff --git a/crates/disktree-core/src/identity.rs b/crates/disktree-core/src/identity.rsnew file mode 100644index 0000000..11cea93--- /dev/null+++ b/crates/disktree-core/src/identity.rs@@ -0,0 +1,39 @@+//! Identity captured on the scanned machine, never inferred from an alias.+use serde::{Deserialize, Serialize};++#[derive(Clone, Debug, Serialize, Deserialize)]+pub struct HostIdentity {+ pub hostname: String,+ pub machine_id: Option<String>,+}++impl HostIdentity {+ pub fn current() -> Option<Self> {+ let hostname = if cfg!(target_os = "linux") {+ std::fs::read_to_string("/proc/sys/kernel/hostname").ok()?+ } else {+ String::from_utf8(+ std::process::Command::new("hostname").output().ok()?.stdout,+ )+ .ok()?+ };+ let hostname = hostname.trim().to_owned();+ if hostname.is_empty()+ || !hostname+ .chars()+ .all(|c| c.is_ascii_alphanumeric() || ".-_".contains(c))+ {+ return None;+ }+ let machine_id = std::fs::read_to_string("/etc/machine-id")+ .ok()+ .map(|id| id.trim().to_owned())+ .filter(|id| {+ id.len() == 32 && id.bytes().all(|c| c.is_ascii_hexdigit())+ });+ Some(Self {+ hostname,+ machine_id,+ })+ }+}diff --git a/crates/disktree-core/src/lib.rs b/crates/disktree-core/src/lib.rsindex a620d80..410143e 100644--- a/crates/disktree-core/src/lib.rs+++ b/crates/disktree-core/src/lib.rs@@ -7,13 +7,17 @@pub mod access;pub mod classify;+pub mod deployment;pub mod export;pub mod filter;+pub mod identity;pub mod insights;#[cfg(target_os = "macos")]mod macos;#[cfg(windows)]mod mft;+pub mod payload;+pub mod remote;pub mod removal;pub mod scan;pub mod scan_threads;diff --git a/crates/disktree-core/src/payload.rs b/crates/disktree-core/src/payload.rsnew file mode 100644index 0000000..403b77c--- /dev/null+++ b/crates/disktree-core/src/payload.rs@@ -0,0 +1,264 @@+//! Checked scanner payloads appended after the desktop executable.+//!+//! Footer: manifest JSON, its little-endian u64 length, then a fixed magic.+//! Offsets are absolute; readers bound every range before allocating. This+//! detects damaged packages, not maliciously replaced desktop executables.++use std::fs::{File, OpenOptions};+use std::io::{self, Read as _, Seek as _, SeekFrom, Write as _};+use std::path::{Path, PathBuf};++use serde::{Deserialize, Serialize};+use sha2::{Digest as _, Sha256};++const MAGIC: &[u8; 16] = b"DISKTREE-SSH-V1!";+const FOOTER: u64 = 24;+const MAX_MANIFEST: u64 = 64 << 10;+const MAX_PAYLOAD: u64 = 64 << 20;++#[derive(Clone, Debug, Serialize, Deserialize)]+pub struct Entry {+ pub arch: String,+ pub offset: u64,+ pub length: u64,+ pub sha256: String,+}++#[derive(Clone, Debug, Serialize, Deserialize)]+struct Manifest {+ version: u32,+ base_length: u64,+ entries: Vec<Entry>,+}++#[derive(Clone, Debug)]+pub struct Archive {+ path: PathBuf,+ manifest: Manifest,+}++pub fn digest(bytes: &[u8]) -> String {+ format!("{:x}", Sha256::digest(bytes))+}++impl Archive {+ pub fn current() -> io::Result<Option<Self>> {+ Self::open(&std::env::current_exe()?)+ }++ pub fn open(path: &Path) -> io::Result<Option<Self>> {+ let mut file = File::open(path)?;+ let length = file.metadata()?.len();+ if length < FOOTER {+ return Ok(None);+ }+ file.seek(SeekFrom::End(-16))?;+ let mut magic = [0; 16];+ file.read_exact(&mut magic)?;+ if &magic != MAGIC {+ return Ok(None);+ }+ file.seek(SeekFrom::Start(length - FOOTER))?;+ let mut size = [0; 8];+ file.read_exact(&mut size)?;+ let size = u64::from_le_bytes(size);+ if size > MAX_MANIFEST || size > length - FOOTER {+ return Err(io::Error::other("invalid scanner manifest length"));+ }+ let start = length - FOOTER - size;+ file.seek(SeekFrom::Start(start))?;+ let mut json = vec![0; size as usize];+ file.read_exact(&mut json)?;+ let manifest: Manifest =+ serde_json::from_slice(&json).map_err(io::Error::other)?;+ if manifest.version != 1+ || manifest.entries.len() > 8+ || manifest.base_length > start+ {+ return Err(io::Error::other("unsupported scanner manifest"));+ }+ let mut end = manifest.base_length;+ let mut arches = std::collections::HashSet::new();+ for entry in &manifest.entries {+ if !matches!(entry.arch.as_str(), "x86_64" | "aarch64")+ || !arches.insert(&entry.arch)+ || entry.offset != end+ || entry.length == 0+ || entry.length > MAX_PAYLOAD+ || entry.length > start.saturating_sub(entry.offset)+ || entry.sha256.len() != 64+ || !entry.sha256.bytes().all(|c| c.is_ascii_hexdigit())+ {+ return Err(io::Error::other("invalid scanner payload range"));+ }+ end = entry+ .offset+ .checked_add(entry.length)+ .ok_or_else(|| io::Error::other("scanner range overflow"))?;+ }+ if end != start {+ return Err(io::Error::other(+ "scanner payload has unindexed bytes",+ ));+ }+ Ok(Some(Self {+ path: path.into(),+ manifest,+ }))+ }++ pub fn entries(&self) -> &[Entry] {+ &self.manifest.entries+ }++ pub fn load(&self, arch: &str) -> io::Result<(Entry, Vec<u8>)> {+ let entry = self+ .entries()+ .iter()+ .find(|entry| entry.arch == arch)+ .ok_or_else(|| {+ io::Error::other(format!(+ "this disktree build has no embedded Linux {arch} scanner"+ ))+ })?+ .clone();+ let mut file = File::open(&self.path)?;+ file.seek(SeekFrom::Start(entry.offset))?;+ let mut bytes = vec![0; entry.length as usize];+ file.read_exact(&mut bytes)?;+ if digest(&bytes) != entry.sha256 {+ return Err(io::Error::other("embedded scanner checksum mismatch"));+ }+ Ok((entry, bytes))+ }+}++/// Append after stripping, before distributing. Repeating this replaces the+/// old trailer instead of growing the executable on every incremental build.+pub fn append(binary: &Path, helpers: &[(String, PathBuf)]) -> io::Result<()> {+ let previous = Archive::open(binary)?;+ let mut file = OpenOptions::new().read(true).write(true).open(binary)?;+ let base_length = previous.map_or_else(+ || file.metadata().map(|meta| meta.len()),+ |archive| Ok(archive.manifest.base_length),+ )?;+ let mut entries = Vec::new();+ // Read and validate everything before changing the executable.+ let mut payloads = Vec::new();+ let mut offset = base_length;+ for (arch, path) in helpers {+ let bytes = std::fs::read(path)?;+ validate_static_elf(&bytes, arch)?;+ entries.push(Entry {+ arch: arch.clone(),+ offset,+ length: bytes.len() as u64,+ sha256: digest(&bytes),+ });+ offset += bytes.len() as u64;+ payloads.push(bytes);+ }+ let json = serde_json::to_vec(&Manifest {+ version: 1,+ base_length,+ entries,+ })+ .map_err(io::Error::other)?;+ file.set_len(base_length)?;+ file.seek(SeekFrom::Start(base_length))?;+ for bytes in payloads {+ file.write_all(&bytes)?;+ }+ file.write_all(&json)?;+ file.write_all(&(json.len() as u64).to_le_bytes())?;+ file.write_all(MAGIC)?;+ file.sync_all()+}++/// Our portability promise is static ELF64, not merely a musl filename.+pub fn validate_static_elf(bytes: &[u8], arch: &str) -> io::Result<()> {+ let invalid = || io::Error::other("scanner must be static Linux ELF64");+ if bytes.len() < 64 || &bytes[..6] != b"\x7fELF\x02\x01" {+ return Err(invalid());+ }+ let machine = u16::from_le_bytes([bytes[18], bytes[19]]);+ if !matches!((arch, machine), ("x86_64", 62) | ("aarch64", 183)) {+ return Err(io::Error::other("scanner ELF architecture mismatch"));+ }+ let start = u64::from_le_bytes(bytes[32..40].try_into().unwrap());+ let size = u16::from_le_bytes(bytes[54..56].try_into().unwrap()) as u64;+ let count = u16::from_le_bytes(bytes[56..58].try_into().unwrap()) as u64;+ if size < 56+ || count == 0+ || start+ .checked_add(size * count)+ .is_none_or(|end| end > bytes.len() as u64)+ {+ return Err(invalid());+ }+ for index in 0..count {+ let offset = (start + index * size) as usize;+ let kind =+ u32::from_le_bytes(bytes[offset..offset + 4].try_into().unwrap());+ // PT_INTERP would require a particular libc loader on the server.+ if kind == 3 {+ return Err(io::Error::other("scanner has a dynamic ELF loader"));+ }+ }+ Ok(())+}++#[cfg(test)]+mod tests {+ use super::*;++ fn elf() -> Vec<u8> {+ let mut bytes = vec![0; 120];+ bytes[..6].copy_from_slice(b"\x7fELF\x02\x01");+ bytes[18..20].copy_from_slice(&62_u16.to_le_bytes());+ bytes[32..40].copy_from_slice(&64_u64.to_le_bytes());+ bytes[54..56].copy_from_slice(&56_u16.to_le_bytes());+ bytes[56..58].copy_from_slice(&1_u16.to_le_bytes());+ bytes+ }++ #[test]+ fn trailer_roundtrips_replaces_and_detects_corruption() {+ let dir = tempfile::tempdir().unwrap();+ let app = dir.path().join("app");+ let helper = dir.path().join("helper");+ std::fs::write(&app, b"desktop").unwrap();+ assert!(Archive::open(&app).unwrap().is_none());+ std::fs::write(&helper, elf()).unwrap();+ let inputs = [("x86_64".into(), helper)];+ append(&app, &inputs).unwrap();+ let size = app.metadata().unwrap().len();+ append(&app, &inputs).unwrap();+ assert_eq!(app.metadata().unwrap().len(), size);+ let archive = Archive::open(&app).unwrap().unwrap();+ assert_eq!(archive.load("x86_64").unwrap().1, elf());+ assert!(archive.load("aarch64").is_err());+ let mut bytes = std::fs::read(&app).unwrap();+ bytes[8] ^= 1;+ std::fs::write(&app, bytes).unwrap();+ assert!(archive.load("x86_64").is_err());+ }++ #[test]+ fn dynamic_or_wrong_arch_helpers_are_refused() {+ let mut bytes = elf();+ assert!(validate_static_elf(&bytes, "aarch64").is_err());+ bytes[64..68].copy_from_slice(&3_u32.to_le_bytes());+ assert!(validate_static_elf(&bytes, "x86_64").is_err());+ }++ #[test]+ fn oversized_footer_is_refused_before_allocation() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("bad");+ let mut bytes = u64::MAX.to_le_bytes().to_vec();+ bytes.extend_from_slice(MAGIC);+ std::fs::write(&path, bytes).unwrap();+ assert!(Archive::open(&path).is_err());+ }+}diff --git a/crates/disktree-core/src/remote.rs b/crates/disktree-core/src/remote.rsnew file mode 100644index 0000000..b6502ad--- /dev/null+++ b/crates/disktree-core/src/remote.rs@@ -0,0 +1,706 @@+//! Read-only, versioned SSH scan protocol.+//!+//! Paths travel as JSON on stdin,+//! never as shell arguments. Postorder records avoid JSON recursion limits+//! and stream without making a second serialized copy of the whole tree.++use std::io::{self, BufRead, BufReader, Read as _, Write};+use std::path::PathBuf;+use std::process::{Child, Command, Stdio};+use std::sync::{Arc, Mutex, mpsc};++use serde::{Deserialize, Serialize};++use crate::scan::{ScanOptions, scan};+use crate::space::{SpaceInfo, space_info, volume_root_for};+use crate::tree::{Node, NodeKind, aggregate};++const VERSION: u32 = 1;+const MAX_LINE: u64 = 1 << 20;+const MAX_DIAGNOSTICS: usize = 16 << 10;++// Non-interactive shells often omit ~/.local/bin. Do not require users to+// edit the server's startup files after the documented user-local install.+const REMOTE_COMMAND: &str = r#"if command -v disktree-scan >/dev/null 2>&1; then+exec disktree-scan --stdio-v1+elif [ -x "$HOME/.local/bin/disktree-scan" ]; then+exec "$HOME/.local/bin/disktree-scan" --stdio-v1+else+echo 'disktree-scan is not installed on this server.' >&2+echo 'On the server, run make install-scan from the disktree checkout.' >&2+exit 127+fi"#;++#[derive(Clone, Debug, Serialize, Deserialize)]+pub struct Request {+ pub root: Option<PathBuf>,+ pub disk: bool,+ pub options: ScanOptions,+}++#[derive(Debug, Serialize, Deserialize)]+pub struct Header {+ version: u32,+ pub root: PathBuf,+ pub home: Option<PathBuf>,+ pub disk_root: Option<PathBuf>,+ pub space: Option<SpaceInfo>,+ pub platform: String,+ #[serde(default)]+ pub identity: Option<crate::identity::HostIdentity>,+}++#[derive(Debug)]+pub struct Snapshot {+ pub header: Header,+ pub tree: Node,+}++// Children are already on the receiver's stack; counts preserve empty dirs+// and deduplicated leaves without transmitting derived subtree totals.+#[derive(Debug, Serialize, Deserialize)]+struct Record(String, u8, u64, u64, i64, bool, usize);++fn json_error(error: serde_json::Error) -> io::Error {+ io::Error::new(io::ErrorKind::InvalidData, error)+}++fn send(out: &mut impl Write, value: &impl Serialize) -> io::Result<()> {+ serde_json::to_writer(&mut *out, value).map_err(json_error)?;+ out.write_all(b"\n")+}++fn receive<T: serde::de::DeserializeOwned>(+ input: &mut impl BufRead,+) -> io::Result<T> {+ let mut line = String::new();+ // A remote process is not trusted to allocate arbitrary amounts of RAM+ // with a single malformed record (or an accidental login banner).+ let length = input.take(MAX_LINE).read_line(&mut line)?;+ if length == 0 {+ return Err(io::Error::new(+ io::ErrorKind::UnexpectedEof,+ "remote helper closed the connection without a complete scan",+ ));+ }+ if !line.ends_with('\n') {+ let reason = if length as u64 == MAX_LINE {+ "oversized remote record"+ } else {+ "truncated remote record"+ };+ return Err(io::Error::new(io::ErrorKind::InvalidData, reason));+ }+ serde_json::from_str(&line).map_err(json_error)+}++/// The helper has exactly one operation: scan. It never removes files,+/// installs software, invokes sudo, or executes a command from the client.+pub fn serve(input: &mut impl BufRead, out: &mut impl Write) -> io::Result<()> {+ let request: Request = receive(input)?;+ let home = std::env::home_dir();+ let root = request+ .root+ .or_else(|| home.clone())+ .ok_or_else(|| io::Error::other("no remote home directory"))?;+ let root = if request.disk {+ volume_root_for(&root).unwrap_or_else(|| PathBuf::from("/"))+ } else {+ root+ }+ .canonicalize()?;+ if !root.is_dir() {+ return Err(io::Error::other("scan root is not a directory"));+ }+ let tree = scan(&root, request.options)?;+ send(+ out,+ &Header {+ version: VERSION,+ disk_root: volume_root_for(&root),+ space: space_info(&root).ok(),+ root,+ home,+ platform: std::env::consts::OS.into(),+ identity: crate::identity::HostIdentity::current(),+ },+ )?;+ write_node(out, &tree)?;+ // An explicit terminator distinguishes a complete scan from a severed SSH+ // connection after any otherwise valid prefix of the postorder stream.+ send(out, &"done")?;+ out.flush()+}++fn write_node(out: &mut impl Write, node: &Node) -> io::Result<()> {+ for child in &node.children {+ write_node(out, child)?;+ }+ let kind = match node.kind {+ NodeKind::Directory => 0,+ NodeKind::File => 1,+ NodeKind::Symlink => 2,+ NodeKind::Other => 3,+ };+ send(+ out,+ &Record(+ if node.name.contains('/') {+ "root".into()+ } else {+ node.name.to_string()+ },+ kind,+ node.own_bytes(),+ node.own_files(),+ node.modified,+ node.read_error,+ node.children.len(),+ ),+ )+}++pub fn read_snapshot(input: &mut impl BufRead) -> io::Result<Snapshot> {+ let header: Header = receive(input)?;+ if header.version != VERSION || !header.root.is_absolute() {+ return Err(io::Error::other("incompatible remote helper protocol"));+ }+ let mut stack = Vec::<Node>::new();+ loop {+ let value: serde_json::Value = receive(input)?;+ if value == "done" {+ break;+ }+ let Record(name, kind, bytes, files, modified, read_error, count) =+ serde_json::from_value(value).map_err(json_error)?;+ // Names must remain single components when joined to marked paths.+ if name.is_empty()+ || name == "."+ || name == ".."+ || name.contains(['/', '\0'])+ || (cfg!(windows) && name.contains(['\\', ':']))+ {+ return Err(io::Error::other(format!(+ "remote name cannot be represented safely: {name:?}"+ )));+ }+ if count > stack.len() {+ return Err(io::Error::other(format!(+ "incomplete remote directory {name:?}: \+ expected {count} children, received {}",+ stack.len(),+ )));+ }+ let kind = match kind {+ 0 => NodeKind::Directory,+ 1 => NodeKind::File,+ 2 => NodeKind::Symlink,+ 3 => NodeKind::Other,+ _ => return Err(io::Error::other("invalid remote node kind")),+ };+ if !kind.is_dir() && count != 0 {+ return Err(io::Error::other("remote leaf has children"));+ }+ let children = stack.split_off(stack.len() - count);+ let mut node = Node::entry(name, kind, bytes);+ node.files = files;+ node.modified = modified;+ node.read_error = read_error;+ node.children = children;+ stack.push(node);+ }+ if stack.len() != 1 {+ return Err(io::Error::other("incomplete remote tree"));+ }+ let mut tree = stack.pop().expect("one root checked above");+ aggregate(&mut tree, crate::tree::Metric::Bytes);+ crate::classify::classify(&mut tree);+ Ok(Snapshot { header, tree })+}++pub fn validate_host(host: &str) -> io::Result<()> {+ if host.is_empty()+ || host.starts_with('-')+ || !host+ .chars()+ .all(|c| c.is_ascii_alphanumeric() || "@._-:[]".contains(c))+ {+ return Err(io::Error::other("invalid SSH destination"));+ }+ Ok(())+}++fn read_diagnostics(mut input: impl io::Read) -> String {+ let mut tail = Vec::new();+ let mut chunk = [0_u8; 4096];+ while let Ok(length) = input.read(&mut chunk) {+ if length == 0 {+ break;+ }+ tail.extend_from_slice(&chunk[..length]);+ if tail.len() > MAX_DIAGNOSTICS {+ tail.drain(..tail.len() - MAX_DIAGNOSTICS);+ }+ }+ // Diagnostics are remote-controlled too; keep text, not terminal escapes.+ String::from_utf8_lossy(&tail)+ .chars()+ .filter(|c| !c.is_control() || *c == '\n')+ .collect::<String>()+ .trim()+ .to_owned()+}++fn ssh_error(host: &str, details: &str, fallback: &io::Error) -> io::Error {+ let reason = if details.is_empty() {+ fallback.to_string()+ } else {+ details.to_owned()+ };+ let hint = if reason.contains("Permission denied")+ || reason.contains("Authentication failed")+ {+ format!(+ "Run ssh {host} in a terminal to verify your credentials. \+ When starting disktree from a terminal, SSH can ask for a \+ password there; GUI launches need an agent or SSH askpass."+ )+ } else if reason.contains("Host key verification failed") {+ format!("Run ssh {host} in a terminal and verify its host key first.")+ } else if reason.contains("not installed") || reason.contains("not found") {+ format!(+ "Install disktree-scan ON {host}, not on this computer. \+ Run make install-scan from the checkout on the server."+ )+ } else {+ "Check the server's SSH connection and disktree-scan installation."+ .into()+ };+ io::Error::other(format!("SSH {host}: {reason}\n\n{hint}"))+}++/// Cancelling a rescan or closing a window kills SSH and closes the pipe.+/// SSH normally terminates its remote session too; the helper also exits+/// when a write observes the closed connection.+#[derive(Debug)]+pub struct RemoteScan {+ child: Arc<Mutex<Child>>,+ result: mpsc::Receiver<io::Result<Snapshot>>,+}++impl RemoteScan {+ pub fn spawn(host: &str, request: &Request) -> io::Result<Self> {+ validate_host(host)?;+ let archive = crate::payload::Archive::current()?;+ let remote = if archive.is_some() {+ crate::deployment::COMMAND+ } else {+ REMOTE_COMMAND+ };+ let mut command = Command::new("ssh");+ command.args([+ "-T",+ "-C",+ "-o",+ "ConnectTimeout=15",+ "-o",+ "ServerAliveInterval=15",+ "-o",+ "ServerAliveCountMax=2",+ "--",+ host,+ remote,+ ]);+ Self::spawn_with_archive(command, host, request, archive)+ }++ #[cfg(test)]+ fn spawn_command(+ command: Command,+ host: &str,+ request: &Request,+ ) -> io::Result<Self> {+ Self::spawn_with_archive(command, host, request, None)+ }++ fn spawn_with_archive(+ mut command: Command,+ host: &str,+ request: &Request,+ archive: Option<crate::payload::Archive>,+ ) -> io::Result<Self> {+ let mut child = command+ .stdin(Stdio::piped())+ .stdout(Stdio::piped())+ .stderr(Stdio::piped())+ .spawn()?;+ let stderr = child.stderr.take().expect("piped stderr");+ // Drain concurrently: a noisy login script must not fill SSH's stderr+ // pipe and deadlock a scan. Retain only a bounded tail for the window.+ let diagnostics = std::thread::spawn(move || read_diagnostics(stderr));+ let mut stdin = child.stdin.take().expect("piped stdin");+ let request = request.clone();+ let stdout = child.stdout.take().expect("piped stdout");+ let child = Arc::new(Mutex::new(child));+ let worker = child.clone();+ let metric = request.options.metric;+ let host = host.to_owned();+ let (tx, result) = mpsc::channel();+ std::thread::spawn(move || {+ let mut input = BufReader::new(stdout);+ let snapshot = (|| {+ if let Some(archive) = archive {+ crate::deployment::deploy(+ &archive, &mut input, &mut stdin,+ )?;+ }+ send(&mut stdin, &request)?;+ drop(stdin);+ read_snapshot(&mut input)+ })()+ .map(|mut snapshot| {+ if metric != crate::tree::Metric::Bytes {+ aggregate(&mut snapshot.tree, metric);+ }+ snapshot+ });+ let mut process = worker.lock().expect("SSH process lock");+ if snapshot.is_err() {+ let _ = process.kill();+ }+ let status = process.wait();+ drop(process);+ let details = diagnostics.join().unwrap_or_default();+ let outcome = match (snapshot, status) {+ (Ok(snapshot), Ok(status)) if status.success() => Ok(snapshot),+ (Err(error), _) | (_, Err(error)) => {+ Err(ssh_error(&host, &details, &error))+ }+ (_, Ok(status)) => Err(ssh_error(+ &host,+ &details,+ &io::Error::other(format!("SSH exited with {status}")),+ )),+ };+ let _ = tx.send(outcome);+ });+ Ok(Self { child, result })+ }++ pub fn poll(&self) -> Option<io::Result<Snapshot>> {+ self.result.try_recv().ok()+ }+}++impl Drop for RemoteScan {+ fn drop(&mut self) {+ if let Ok(mut child) = self.child.lock() {+ let _ = child.kill();+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ fn request() -> Request {+ Request {+ root: None,+ disk: false,+ options: ScanOptions::default(),+ }+ }++ fn wait(scan: &RemoteScan) -> io::Result<Snapshot> {+ wait_for(scan, std::time::Duration::from_secs(10))+ }++ fn wait_for(+ scan: &RemoteScan,+ timeout: std::time::Duration,+ ) -> io::Result<Snapshot> {+ let deadline = std::time::Instant::now() + timeout;+ loop {+ if let Some(result) = scan.poll() {+ return result;+ }+ assert!(std::time::Instant::now() < deadline, "SSH scan hung");+ std::thread::sleep(std::time::Duration::from_millis(5));+ }+ }++ #[cfg(unix)]+ fn failing_process(script: &str) -> io::Error {+ let mut command = Command::new("sh");+ command.args(["-c", script]);+ let handle =+ RemoteScan::spawn_command(command, "dev", &request()).unwrap();+ wait(&handle).unwrap_err()+ }++ #[cfg(unix)]+ #[test]+ fn ssh_authentication_diagnostics_reach_the_caller() {+ let error = failing_process(+ "cat >/dev/null; echo 'Permission denied (publickey,password).' \+ >&2; exit 255",+ )+ .to_string();+ assert!(error.contains("Permission denied"), "{error}");+ assert!(error.contains("verify your credentials"), "{error}");+ assert!(!error.contains("truncated"), "{error}");+ assert!(!error.contains("Install disktree-scan"), "{error}");+ }++ #[cfg(unix)]+ #[test]+ fn missing_helper_diagnostics_name_the_server() {+ let error = failing_process(+ "cat >/dev/null; echo 'disktree-scan: command not found' \+ >&2; exit 127",+ )+ .to_string();+ assert!(error.contains("command not found"), "{error}");+ assert!(error.contains("ON dev, not on this computer"), "{error}");+ }++ #[cfg(unix)]+ #[test]+ fn banner_and_truncated_output_are_reported_without_a_hang() {+ for output in ["hello", "hello\\n"] {+ let error =+ failing_process(&format!("cat >/dev/null; printf '{output}'"))+ .to_string();+ assert!(error.contains("SSH dev:"), "{error}");+ }+ }++ #[test]+ fn diagnostics_are_bounded_and_controls_are_stripped() {+ let mut data = vec![b'x'; MAX_DIAGNOSTICS * 4];+ data.extend_from_slice(b"\x1b[31mPermission denied\r\n");+ let text = read_diagnostics(data.as_slice());+ assert!(text.len() <= MAX_DIAGNOSTICS);+ assert!(text.ends_with("Permission denied"));+ assert!(!text.contains(['\x1b', '\r']));+ }++ // Uses an explicitly supplied config, never the user's default SSH host.+ #[cfg(unix)]+ #[test]+ #[ignore = "requires DISKTREE_TEST_SSH_CONFIG with isolated sshd"]+ fn real_ssh_transport_returns_a_snapshot() {+ let config = std::env::var("DISKTREE_TEST_SSH_CONFIG").unwrap();+ let dir = tempfile::tempdir().unwrap();+ std::fs::write(dir.path().join("keep"), b"hello").unwrap();+ let home = tempfile::tempdir().unwrap();+ let bin = home.path().join(".local/bin");+ std::fs::create_dir_all(&bin).unwrap();+ let helper = std::env::var("DISKTREE_TEST_HELPER").unwrap();+ std::os::unix::fs::symlink(helper, bin.join("disktree-scan")).unwrap();+ // Force the documented ~/.local/bin fallback, not the ambient PATH.+ let remote = format!(+ "HOME={} PATH=/nonexistent; export HOME PATH;\n{REMOTE_COMMAND}",+ crate::export::shell_quote(home.path().to_str().unwrap()),+ );+ let mut command = Command::new("ssh");+ command.args(["-F", &config, "-T", "-C", "fixture", &remote]);+ let request = Request {+ root: Some(dir.path().into()),+ options: ScanOptions {+ apparent_size: true,+ ..ScanOptions::default()+ },+ ..request()+ };+ let handle =+ RemoteScan::spawn_command(command, "fixture", &request).unwrap();+ let snapshot = wait(&handle).unwrap();+ assert_eq!(snapshot.tree.bytes, 5);+ assert_eq!(snapshot.tree.files, 1);+ assert_eq!(std::fs::read(dir.path().join("keep")).unwrap(), b"hello");+ }++ #[cfg(unix)]+ #[test]+ #[ignore = "requires DISKTREE_TEST_SSH_CONFIG with isolated sshd"]+ fn real_ssh_transport_reports_auth_and_install_errors() {+ let config = std::env::var("DISKTREE_TEST_SSH_CONFIG").unwrap();+ let mut denied = Command::new("ssh");+ denied.args([+ "-F",+ &config,+ "-T",+ "-o",+ "PubkeyAuthentication=no",+ "-o",+ "PreferredAuthentications=none",+ "fixture",+ REMOTE_COMMAND,+ ]);+ let scan =+ RemoteScan::spawn_command(denied, "fixture", &request()).unwrap();+ let error = wait(&scan).unwrap_err().to_string();+ assert!(error.contains("Permission denied"), "{error}");++ let home = tempfile::tempdir().unwrap();+ let remote = format!(+ "HOME={} PATH=/nonexistent; export HOME PATH;\n{REMOTE_COMMAND}",+ crate::export::shell_quote(home.path().to_str().unwrap()),+ );+ let mut missing = Command::new("ssh");+ missing.args(["-F", &config, "-T", "fixture", &remote]);+ let scan =+ RemoteScan::spawn_command(missing, "fixture", &request()).unwrap();+ let error = wait(&scan).unwrap_err().to_string();+ assert!(error.contains("not installed on this server"), "{error}");+ assert!(error.contains("ON fixture"), "{error}");+ }++ #[cfg(unix)]+ #[test]+ #[ignore = "requires isolated sshd and DISKTREE_TEST_ARCHIVE"]+ fn real_ssh_transport_bootstraps_and_checks_cached_payloads() {+ let config = std::env::var("DISKTREE_TEST_SSH_CONFIG").unwrap();+ let archive = crate::payload::Archive::open(+ PathBuf::from(std::env::var("DISKTREE_TEST_ARCHIVE").unwrap())+ .as_path(),+ )+ .unwrap()+ .unwrap();+ let home = tempfile::tempdir().unwrap();+ let data = tempfile::tempdir().unwrap();+ std::fs::write(data.path().join("keep"), b"hello").unwrap();+ let cache = home.path().join("cache");+ let remote = format!(+ "HOME={} XDG_CACHE_HOME={}; export HOME XDG_CACHE_HOME;\n{}",+ crate::export::shell_quote(home.path().to_str().unwrap()),+ crate::export::shell_quote(cache.to_str().unwrap()),+ crate::deployment::COMMAND,+ );+ let arch = std::env::consts::ARCH;+ let (entry, bytes) = archive.load(arch).unwrap();+ let cached = cache+ .join("disktree/ssh")+ .join(&entry.sha256)+ .join("disktree-scan");+ let request = Request {+ root: Some(data.path().into()),+ options: ScanOptions {+ apparent_size: true,+ ..ScanOptions::default()+ },+ ..request()+ };+ for attempt in 0..3 {+ let mut command = Command::new("ssh");+ command.args(["-F", &config, "-T", "fixture", &remote]);+ let handle = RemoteScan::spawn_with_archive(+ command,+ "fixture",+ &request,+ Some(archive.clone()),+ )+ .unwrap();+ let snapshot = wait(&handle).unwrap();+ assert_eq!(snapshot.tree.bytes, 5);+ assert!(snapshot.header.identity.is_some());+ assert_eq!(std::fs::read(&cached).unwrap(), bytes);+ if attempt == 1 {+ // A damaged cache must be repaired, never executed.+ let damaged = cached.with_extension("damaged");+ std::fs::write(&damaged, b"broken").unwrap();+ std::fs::rename(damaged, &cached).unwrap();+ }+ }+ assert!(!home.path().join(".local/bin/disktree-scan").exists());+ }++ /// Explicit opt-in only: never contact a real machine in the test gate.+ #[test]+ #[ignore = "requires DISKTREE_TEST_SSH_HOST and DISKTREE_TEST_SSH_ROOT"]+ fn live_ssh_scan_uses_the_production_transport() {+ let host = std::env::var("DISKTREE_TEST_SSH_HOST").unwrap();+ let root = std::env::var("DISKTREE_TEST_SSH_ROOT").unwrap();+ let scan = RemoteScan::spawn(+ &host,+ &Request {+ root: Some(PathBuf::from(&root)),+ ..request()+ },+ )+ .unwrap();+ let snapshot =+ wait_for(&scan, std::time::Duration::from_mins(3)).unwrap();+ assert!(snapshot.tree.is_dir());+ assert!(snapshot.header.root.is_absolute());+ println!(+ "SSH {}: {} — {} bytes, {} files, {} directories; space: {:?}",+ host,+ snapshot.header.root.display(),+ snapshot.tree.bytes,+ snapshot.tree.files,+ snapshot.tree.dirs,+ snapshot.header.space,+ );+ }++ #[test]+ fn rejects_ssh_options_and_shell_syntax() {+ for host in ["-oProxyCommand=oops", "dev; touch x", "dev\nother", ""] {+ assert!(validate_host(host).is_err());+ }+ for host in ["dev", "user@dev", "[::1]"] {+ assert!(validate_host(host).is_ok());+ }+ }++ #[cfg(unix)]+ #[test]+ fn unix_backslashes_in_names_roundtrip_without_becoming_separators() {+ let dir = tempfile::tempdir().unwrap();+ std::fs::write(dir.path().join(r"back\slash"), b"x").unwrap();+ let request = Request {+ root: Some(dir.path().into()),+ disk: false,+ options: ScanOptions {+ apparent_size: true,+ ..ScanOptions::default()+ },+ };+ let mut input = Vec::new();+ send(&mut input, &request).unwrap();+ let mut data = Vec::new();+ serve(&mut input.as_slice(), &mut data).unwrap();+ let snapshot = read_snapshot(&mut data.as_slice()).unwrap();+ assert_eq!(snapshot.tree.bytes, 1);+ assert!(snapshot.tree.child_named(r"back\slash").is_some());+ }++ #[test]+ fn protocol_roundtrip_and_truncation() {+ let dir = tempfile::tempdir().unwrap();+ std::fs::write(dir.path().join("a ' file"), b"hello").unwrap();+ std::fs::create_dir(dir.path().join("empty")).unwrap();+ let request = Request {+ root: Some(dir.path().into()),+ disk: false,+ options: ScanOptions {+ apparent_size: true,+ ..ScanOptions::default()+ },+ };+ let mut data = Vec::new();+ let mut input = Vec::new();+ send(&mut input, &request).unwrap();+ serve(&mut input.as_slice(), &mut data).unwrap();+ let snapshot = read_snapshot(&mut data.as_slice()).unwrap();+ assert_eq!(snapshot.tree.bytes, 5);+ assert_eq!(snapshot.tree.files, 1);+ assert!(snapshot.tree.child_named("empty").unwrap().is_dir());+ data.truncate(data.len() - 7);+ assert!(read_snapshot(&mut data.as_slice()).is_err());+ assert!(dir.path().join("a ' file").exists());+ }+}diff --git a/crates/disktree-core/src/scan.rs b/crates/disktree-core/src/scan.rsindex 693253a..312879b 100644--- a/crates/disktree-core/src/scan.rs+++ b/crates/disktree-core/src/scan.rs@@ -36,7 +36,7 @@ use crate::tree::{Metric, Node, NodeKind, Seen, aggregate, aggregate_deduped};const MAX_ERROR_DETAIL: usize = 50;/// How a scan measures and filters the tree.-#[derive(Clone, Debug)]+#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]pub struct ScanOptions {/// Measure apparent length instead of allocated blocks. Apparent size is/// what `ls -l` shows; blocks are what the volume actually spends, which is@@ -62,6 +62,7 @@ pub struct ScanOptions {/// Whether children are ranked by bytes or by file count.pub metric: Metric,/// Directory admission for this scan; other scans have their own budget.+ #[serde(default)]pub threads: ScanThreads,}diff --git a/crates/disktree-core/src/scan_threads.rs b/crates/disktree-core/src/scan_threads.rsindex bd1767a..5b0b43a 100644--- a/crates/disktree-core/src/scan_threads.rs+++ b/crates/disktree-core/src/scan_threads.rs@@ -4,7 +4,7 @@ use std::sync::{Mutex, MutexGuard, PoisonError};/// Filesystem concurrency for one scan, independent of the global Rayon pool./// A scan crossing several volumes shares this budget; separate scans do not.-#[derive(Clone, Copy, Debug)]+#[derive(Clone, Copy, Debug, serde::Serialize, serde::Deserialize)]pub struct ScanThreads {/// Fixed scan workers, capped by available CPUs; `usize::MAX` uses the/// shared platform-default pool. With `adaptive`, maximum admitted directorydiff --git a/crates/disktree-core/src/space.rs b/crates/disktree-core/src/space.rsindex 8a2bc4e..f5ca414 100644--- a/crates/disktree-core/src/space.rs+++ b/crates/disktree-core/src/space.rs@@ -8,7 +8,16 @@ use std::io;use std::path::{Path, PathBuf};/// A volume's capacity in bytes.-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]+#[derive(+ Clone,+ Copy,+ Debug,+ Default,+ PartialEq,+ Eq,+ serde::Serialize,+ serde::Deserialize,+)]pub struct SpaceInfo {/// Total size of the volume.pub total: u64,diff --git a/crates/disktree-core/src/tree.rs b/crates/disktree-core/src/tree.rsindex 6991a86..42b1566 100644--- a/crates/disktree-core/src/tree.rs+++ b/crates/disktree-core/src/tree.rs@@ -27,7 +27,16 @@ impl NodeKind {}/// How a node's importance is measured.-#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]+#[derive(+ Clone,+ Copy,+ Debug,+ Default,+ PartialEq,+ Eq,+ serde::Serialize,+ serde::Deserialize,+)]pub enum Metric {/// Bytes, apparent or on-disk depending on [`crate::scan::ScanOptions`].#[default]diff --git a/crates/disktree-scan/Cargo.toml b/crates/disktree-scan/Cargo.tomlnew file mode 100644index 0000000..90f0c9e--- /dev/null+++ b/crates/disktree-scan/Cargo.toml@@ -0,0 +1,17 @@+[package]+name = "disktree-scan"+version.workspace = true+edition.workspace = true+rust-version.workspace = true+license.workspace = true+description = "Read-only headless scanner for disktree SSH sessions"++[lints]+workspace = true++[dependencies]+disktree-core.workspace = true++[dev-dependencies]+serde_json = "1"+tempfile = "3"diff --git a/crates/disktree-scan/src/main.rs b/crates/disktree-scan/src/main.rsnew file mode 100644index 0000000..befc429--- /dev/null+++ b/crates/disktree-scan/src/main.rs@@ -0,0 +1,23 @@+//! Headless helper: no GPUI dependency and no destructive command surface.+use std::io::{self, BufWriter};++fn main() -> io::Result<()> {+ let args: Vec<String> = std::env::args().skip(1).collect();+ if args == ["--stdio-v1"] {+ disktree_core::remote::serve(+ &mut io::stdin().lock(),+ &mut BufWriter::new(io::stdout().lock()),+ )+ } else if args.is_empty() || args == ["--help"] || args == ["-h"] {+ println!(+ "disktree-scan — read-only SSH scan helper\n\+ usage: disktree-scan --stdio-v1\n\+ Used by: disktree ssh HOST [PATH]\n\+ Reads a versioned JSON request from stdin and streams a \+ snapshot to stdout. No deletion commands."+ );+ Ok(())+ } else {+ Err(io::Error::other("unknown option; try --help"))+ }+}diff --git a/crates/disktree-scan/tests/stdio.rs b/crates/disktree-scan/tests/stdio.rsnew file mode 100644index 0000000..19910bc--- /dev/null+++ b/crates/disktree-scan/tests/stdio.rs@@ -0,0 +1,39 @@+//! Exercise the shipped executable, not just its protocol functions.+use std::io::{BufReader, Write as _};+use std::process::{Command, Stdio};++use disktree_core::remote::{Request, read_snapshot};+use disktree_core::scan::ScanOptions;++#[test]+fn executable_returns_a_read_only_snapshot() {+ let dir = tempfile::tempdir().unwrap();+ let file = dir.path().join("data");+ std::fs::write(&file, b"hello").unwrap();+ let mut child = Command::new(env!("CARGO_BIN_EXE_disktree-scan"))+ .arg("--stdio-v1")+ .stdin(Stdio::piped())+ .stdout(Stdio::piped())+ .spawn()+ .unwrap();+ let request = Request {+ root: Some(dir.path().into()),+ disk: false,+ options: ScanOptions {+ apparent_size: true,+ ..ScanOptions::default()+ },+ };+ let mut input = child.stdin.take().unwrap();+ serde_json::to_writer(&mut input, &request).unwrap();+ input.write_all(b"\n").unwrap();+ drop(input);+ let snapshot =+ read_snapshot(&mut BufReader::new(child.stdout.take().unwrap()))+ .unwrap();+ assert!(child.wait().unwrap().success());+ assert_eq!(snapshot.tree.bytes, 5);+ assert_eq!(snapshot.tree.files, 1);+ assert_eq!(snapshot.header.root, dir.path().canonicalize().unwrap());+ assert_eq!(std::fs::read(file).unwrap(), b"hello");+}diff --git a/xtask/Cargo.toml b/xtask/Cargo.tomlindex 3571573..5d0a6e2 100644--- a/xtask/Cargo.toml+++ b/xtask/Cargo.toml@@ -7,6 +7,9 @@ license.workspace = truepublish = falsedescription = "Check and packaging entry points for disktree: one command per gate"+[dependencies]+disktree-core.workspace = true+[lints]workspace = true