From 5928e76d7d4e18be4c9a298da75baffb92070f63 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 04:28:19 +0000 Subject: [PATCH 01/15] fix(redo): redone operations keep their undo data; redo restores what the operation produced `redo` re-executed an operation and recorded it with no undo data, so a second undo could not restore the earlier state: `rm f; undo; redo; undo` brought `f` back empty, a redone `chmod` or `chown` changed nothing and then failed to undo, and a second redo of `cp` or `ln -s` failed for want of the source path or link target. Each operation now has one `apply_*` function that checks its preconditions, captures its undo data and changes the filesystem. The command and `redo` both call it, so a redone record is checked and captured exactly like the original. What an operation produced (the mode of a `chmod`, the owner of a `chown`, the bytes a `>`/`>>` redirection wrote, a variable's value) was never recorded. `undo` now reads it just before reversing the operation and keeps it on the in-memory redo entry (`state::RedoData`, not persisted). File content over `VSH_REDO_CAPTURE_LIMIT` bytes (shell or environment variable, default 10 MiB, 0 for no limit) is not kept; redo of that entry fails with the reason. Also: - A redo that fails changes nothing and stays on the redo stack. - Redo of `cp`, `mv` and `touch` re-checks preconditions, so a path that reappeared after the undo is not overwritten. `touch` creates with `create_new`. - Redirections recorded the resolved absolute path, which undo resolved under the sandbox root a second time, so undo of `>`/`>>` failed unless the root was `/`. They now record the path relative to the root. Tests: `tests/redo_roundtrip_tests.rs` (22 tests: rm, chmod, cp and ln -s round trips byte for byte and mode for mode; a whole-sandbox round-trip check for 19 operation scenarios; 48 random sequences; the capture limit) and `test_redo_then_undo_correspondence`, which compares history records as well as the filesystem. Known and not fixed here; the tests exclude them: undo of `rm`/`rmdir` does not restore the mode, undo of `unset` does not restore the exported flag, and `rm`, `chmod` and redirections follow symbolic links without their undo accounting for it. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- impl/rust-cli/src/commands.rs | 912 +++++++++++++------ impl/rust-cli/src/redirection.rs | 17 +- impl/rust-cli/src/state.rs | 76 +- impl/rust-cli/tests/correspondence_tests.rs | 60 ++ impl/rust-cli/tests/redo_roundtrip_tests.rs | 920 ++++++++++++++++++++ 5 files changed, 1676 insertions(+), 309 deletions(-) create mode 100644 impl/rust-cli/tests/redo_roundtrip_tests.rs diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index 4f91625d..1747abe2 100644 --- a/impl/rust-cli/src/commands.rs +++ b/impl/rust-cli/src/commands.rs @@ -8,10 +8,11 @@ use anyhow::{Context, Result}; use colored::Colorize; use std::fs; +use std::io::Write; use thiserror::Error; use crate::proof_refs; -use crate::state::{Operation, OperationType, ShellState}; +use crate::state::{Operation, OperationType, RedoData, ShellState}; use crate::verification; // Secure deletion (RMO - Remove-Match-Obliterate) @@ -76,26 +77,7 @@ pub enum CommandError { /// # Ok::<(), anyhow::Error>(()) /// ``` pub fn mkdir(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { - let full_path = state.resolve_path(path); - - // Runtime check of the Lean 4 precondition (see verification.rs) - verification::verify_mkdir(state.root(), path)?; - - // Check preconditions (matching Coq) - if full_path.exists() { - anyhow::bail!("Path already exists (EEXIST)"); - } - - let parent = full_path.parent().context("Invalid path")?; - if !parent.exists() { - anyhow::bail!("Parent directory does not exist (ENOENT)"); - } - - // Execute operation - fs::create_dir(&full_path).context("mkdir failed")?; - - // Record in history - let op = Operation::new(OperationType::Mkdir, path.to_string(), None); + let op = apply_mkdir(state, path)?; let op_id = op.id; state.record_operation(op); @@ -116,6 +98,34 @@ pub fn mkdir(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { Ok(()) } +/// Check, execute and describe a `mkdir`, without recording it. +/// +/// The `apply_*` functions are the single place each operation checks its +/// preconditions, captures its undo data and touches the filesystem. The +/// command and [`redo`] both go through them, so a redone operation is checked +/// and recorded exactly like the original. +fn apply_mkdir(state: &ShellState, path: &str) -> Result { + let full_path = state.resolve_path(path); + + // Runtime check of the Lean 4 precondition (see verification.rs) + verification::verify_mkdir(state.root(), path)?; + + // Check preconditions (matching Coq) + if full_path.exists() { + anyhow::bail!("Path already exists (EEXIST)"); + } + + let parent = full_path.parent().context("Invalid path")?; + if !parent.exists() { + anyhow::bail!("Parent directory does not exist (ENOENT)"); + } + + // Execute operation + fs::create_dir(&full_path).context("mkdir failed")?; + + Ok(Operation::new(OperationType::Mkdir, path.to_string(), None)) +} + /// Remove an empty directory at the specified path. /// /// This operation is reversible via [`undo`] and corresponds to the Lean 4 theorem @@ -143,6 +153,27 @@ pub fn mkdir(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { /// # Ok::<(), anyhow::Error>(()) /// ``` pub fn rmdir(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { + let op = apply_rmdir(state, path)?; + let op_id = op.id; + state.record_operation(op); + + println!( + "{} {} {}", + format!("[op:{}]", &op_id.to_string()[..8]).bright_black(), + "rmdir".bright_yellow(), + path + ); + + if verbose { + let proof = OperationType::Rmdir.proof_reference(); + println!(" {} {}", "Proof:".bright_black(), proof.format_short()); + } + + Ok(()) +} + +/// Check, execute and describe an `rmdir`, without recording it. +fn apply_rmdir(state: &ShellState, path: &str) -> Result { let full_path = state.resolve_path(path); // Runtime check of the Lean 4 precondition (see verification.rs) @@ -162,24 +193,7 @@ pub fn rmdir(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { // Execute fs::remove_dir(&full_path).context("rmdir failed")?; - // Record - let op = Operation::new(OperationType::Rmdir, path.to_string(), None); - let op_id = op.id; - state.record_operation(op); - - println!( - "{} {} {}", - format!("[op:{}]", &op_id.to_string()[..8]).bright_black(), - "rmdir".bright_yellow(), - path - ); - - if verbose { - let proof = OperationType::Rmdir.proof_reference(); - println!(" {} {}", "Proof:".bright_black(), proof.format_short()); - } - - Ok(()) + Ok(Operation::new(OperationType::Rmdir, path.to_string(), None)) } /// Create an empty file at the specified path. @@ -207,23 +221,7 @@ pub fn rmdir(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { /// # Ok::<(), anyhow::Error>(()) /// ``` pub fn touch(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { - let full_path = state.resolve_path(path); - - // Runtime check of the Lean 4 precondition (see verification.rs) - verification::verify_create_file(state.root(), path)?; - - if full_path.exists() { - anyhow::bail!("Path already exists (EEXIST)"); - } - - let parent = full_path.parent().context("Invalid path")?; - if !parent.exists() { - anyhow::bail!("Parent directory does not exist (ENOENT)"); - } - - fs::write(&full_path, "").context("touch failed")?; - - let op = Operation::new(OperationType::CreateFile, path.to_string(), None); + let op = apply_create_file(state, path, &[])?; let op_id = op.id; state.record_operation(op); @@ -243,6 +241,39 @@ pub fn touch(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { Ok(()) } +/// Check, execute and describe the creation of a new file holding `content`, +/// without recording it. `touch` creates it empty; redo of a file that a `>` +/// redirection created puts back the bytes undo removed. +fn apply_create_file(state: &ShellState, path: &str, content: &[u8]) -> Result { + let full_path = state.resolve_path(path); + + // Runtime check of the Lean 4 precondition (see verification.rs) + verification::verify_create_file(state.root(), path)?; + + if full_path.exists() { + anyhow::bail!("Path already exists (EEXIST)"); + } + + let parent = full_path.parent().context("Invalid path")?; + if !parent.exists() { + anyhow::bail!("Parent directory does not exist (ENOENT)"); + } + + // `create_new` so a file that appeared since the check is never truncated. + let mut file = fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&full_path) + .context("touch failed")?; + file.write_all(content).context("touch failed")?; + + Ok(Operation::new( + OperationType::CreateFile, + path.to_string(), + None, + )) +} + /// Remove a file at the specified path. /// /// The file's content is preserved for undo. This operation is reversible via [`undo`] @@ -270,6 +301,28 @@ pub fn touch(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { /// # Ok::<(), anyhow::Error>(()) /// ``` pub fn rm(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { + let op = apply_rm(state, path)?; + let op_id = op.id; + state.record_operation(op); + + println!( + "{} {} {}", + format!("[op:{}]", &op_id.to_string()[..8]).bright_black(), + "rm".bright_red(), + path + ); + + if verbose { + let proof = OperationType::DeleteFile.proof_reference(); + println!(" {} {}", "Proof:".bright_black(), proof.format_short()); + } + + Ok(()) +} + +/// Check, execute and describe an `rm`, without recording it. The returned +/// operation carries the deleted file's content as its undo data. +fn apply_rm(state: &ShellState, path: &str) -> Result { let full_path = state.resolve_path(path); // Runtime check of the Lean 4 precondition (see verification.rs) @@ -288,24 +341,7 @@ pub fn rm(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { fs::remove_file(&full_path).context("rm failed")?; - let op = - Operation::new(OperationType::DeleteFile, path.to_string(), None).with_undo_data(content); - let op_id = op.id; - state.record_operation(op); - - println!( - "{} {} {}", - format!("[op:{}]", &op_id.to_string()[..8]).bright_black(), - "rm".bright_red(), - path - ); - - if verbose { - let proof = OperationType::DeleteFile.proof_reference(); - println!(" {} {}", "Proof:".bright_black(), proof.format_short()); - } - - Ok(()) + Ok(Operation::new(OperationType::DeleteFile, path.to_string(), None).with_undo_data(content)) } /// Copy a file from source to destination. @@ -337,6 +373,30 @@ pub fn rm(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { /// # Ok::<(), anyhow::Error>(()) /// ``` pub fn cp(state: &mut ShellState, src: &str, dst: &str, verbose: bool) -> Result<()> { + let op = apply_cp(state, src, dst)?; + let op_id = op.id; + state.record_operation(op); + + println!( + "{} {} {} → {}", + format!("[op:{}]", &op_id.to_string()[..8]).bright_black(), + "cp".bright_green(), + src, + dst + ); + + if verbose { + let proof = OperationType::CopyFile.proof_reference(); + println!(" {} {}", "Proof:".bright_black(), proof.format_short()); + println!(" {} rm {}", "Undo:".bright_black(), dst); + } + + Ok(()) +} + +/// Check, execute and describe a `cp`, without recording it. The returned +/// operation carries the source path, which is what a redo copies from. +fn apply_cp(state: &ShellState, src: &str, dst: &str) -> Result { let src_path = state.resolve_path(src); let dst_path = state.resolve_path(dst); @@ -361,28 +421,12 @@ pub fn cp(state: &mut ShellState, src: &str, dst: &str, verbose: bool) -> Result // Execute operation fs::copy(&src_path, &dst_path).context("cp failed")?; - // Record in history: path = dst, undo_data = src path for reference + // path = dst, undo_data = src path for reference // Undo = delete the destination file - let op = Operation::new(OperationType::CopyFile, dst.to_string(), None) - .with_undo_data(src.as_bytes().to_vec()); - let op_id = op.id; - state.record_operation(op); - - println!( - "{} {} {} → {}", - format!("[op:{}]", &op_id.to_string()[..8]).bright_black(), - "cp".bright_green(), - src, - dst - ); - - if verbose { - let proof = OperationType::CopyFile.proof_reference(); - println!(" {} {}", "Proof:".bright_black(), proof.format_short()); - println!(" {} rm {}", "Undo:".bright_black(), dst); - } - - Ok(()) + Ok( + Operation::new(OperationType::CopyFile, dst.to_string(), None) + .with_undo_data(src.as_bytes().to_vec()), + ) } /// Move/rename a file or directory. @@ -414,6 +458,29 @@ pub fn cp(state: &mut ShellState, src: &str, dst: &str, verbose: bool) -> Result /// # Ok::<(), anyhow::Error>(()) /// ``` pub fn mv(state: &mut ShellState, src: &str, dst: &str, verbose: bool) -> Result<()> { + let op = apply_mv(state, src, dst)?; + let op_id = op.id; + state.record_operation(op); + + println!( + "{} {} {} → {}", + format!("[op:{}]", &op_id.to_string()[..8]).bright_black(), + "mv".bright_green(), + src, + dst + ); + + if verbose { + let proof = OperationType::Move.proof_reference(); + println!(" {} {}", "Proof:".bright_black(), proof.format_short()); + println!(" {} mv {} {}", "Undo:".bright_black(), dst, src); + } + + Ok(()) +} + +/// Check, execute and describe an `mv`, without recording it. +fn apply_mv(state: &ShellState, src: &str, dst: &str) -> Result { let src_path = state.resolve_path(src); let dst_path = state.resolve_path(dst); @@ -442,28 +509,10 @@ pub fn mv(state: &mut ShellState, src: &str, dst: &str, verbose: bool) -> Result // Execute operation fs::rename(&src_path, &dst_path).context("mv failed")?; - // Record: store both paths null-separated for undo + // Store both paths null-separated for undo: // path = "src\0dst" so undo can move dst back to src let combined_path = format!("{}\0{}", src, dst); - let op = Operation::new(OperationType::Move, combined_path, None); - let op_id = op.id; - state.record_operation(op); - - println!( - "{} {} {} → {}", - format!("[op:{}]", &op_id.to_string()[..8]).bright_black(), - "mv".bright_green(), - src, - dst - ); - - if verbose { - let proof = OperationType::Move.proof_reference(); - println!(" {} {}", "Proof:".bright_black(), proof.format_short()); - println!(" {} mv {} {}", "Undo:".bright_black(), dst, src); - } - - Ok(()) + Ok(Operation::new(OperationType::Move, combined_path, None)) } /// Create a symbolic link. @@ -493,6 +542,30 @@ pub fn mv(state: &mut ShellState, src: &str, dst: &str, verbose: bool) -> Result /// # Ok::<(), anyhow::Error>(()) /// ``` pub fn symlink(state: &mut ShellState, target: &str, link: &str, verbose: bool) -> Result<()> { + let op = apply_symlink(state, target, link)?; + let op_id = op.id; + state.record_operation(op); + + println!( + "{} {} {} → {}", + format!("[op:{}]", &op_id.to_string()[..8]).bright_black(), + "ln -s".bright_green(), + target, + link + ); + + if verbose { + let proof = OperationType::Symlink.proof_reference(); + println!(" {} {}", "Proof:".bright_black(), proof.format_short()); + println!(" {} unlink {}", "Undo:".bright_black(), link); + } + + Ok(()) +} + +/// Check, execute and describe an `ln -s`, without recording it. The returned +/// operation carries the link target, which is what a redo links to. +fn apply_symlink(state: &ShellState, target: &str, link: &str) -> Result { let link_path = state.resolve_path(link); // Runtime check of the Lean 4 precondition (see verification.rs) @@ -514,37 +587,60 @@ pub fn symlink(state: &mut ShellState, target: &str, link: &str, verbose: bool) #[cfg(not(unix))] anyhow::bail!("Symbolic links not supported on this platform"); - // Record: path = link path, undo_data = target for re-creation - let op = Operation::new(OperationType::Symlink, link.to_string(), None) - .with_undo_data(target.as_bytes().to_vec()); + // path = link path, undo_data = target for re-creation + Ok( + Operation::new(OperationType::Symlink, link.to_string(), None) + .with_undo_data(target.as_bytes().to_vec()), + ) +} + +/// Change file permissions (reversible — captures old mode for undo). +/// +/// Supports octal modes (755, 0644) and symbolic modes (u+x, go-w, a=r). +/// Records the previous permissions in undo_data for perfect reversal. +/// +/// # Proof Reference +/// PermissionOperations.lean: chmod_reversible +pub fn chmod(state: &mut ShellState, mode_str: &str, path: &str, verbose: bool) -> Result<()> { + let (op, old_mode, new_mode) = apply_chmod(state, path, |old_mode| { + parse_chmod_mode(mode_str, old_mode).context(format!("chmod: invalid mode: '{}'", mode_str)) + })?; let op_id = op.id; state.record_operation(op); println!( - "{} {} {} → {}", + "{} {} {:o} {}", format!("[op:{}]", &op_id.to_string()[..8]).bright_black(), - "ln -s".bright_green(), - target, - link + "chmod".bright_green(), + new_mode & 0o7777, + path ); if verbose { - let proof = OperationType::Symlink.proof_reference(); + let proof = OperationType::Chmod.proof_reference(); println!(" {} {}", "Proof:".bright_black(), proof.format_short()); - println!(" {} unlink {}", "Undo:".bright_black(), link); + println!( + " {} chmod {:o} {}", + "Undo:".bright_black(), + old_mode & 0o7777, + path + ); } Ok(()) } -/// Change file permissions (reversible — captures old mode for undo). -/// -/// Supports octal modes (755, 0644) and symbolic modes (u+x, go-w, a=r). -/// Records the previous permissions in undo_data for perfect reversal. -/// -/// # Proof Reference -/// PermissionOperations.lean: chmod_reversible -pub fn chmod(state: &mut ShellState, mode_str: &str, path: &str, verbose: bool) -> Result<()> { +/// Check, execute and describe a `chmod`, without recording it. +/// +/// `new_mode` maps the file's current mode to the mode to set: the command +/// parses its mode string against it, redo returns the mode undo replaced. +/// Returns the operation (carrying the old mode as undo data), the old mode +/// and the new mode. +fn apply_chmod( + state: &ShellState, + path: &str, + new_mode: impl FnOnce(u32) -> Result, +) -> Result<(Operation, u32, u32)> { let file_path = state.resolve_path(path); if !file_path.exists() && file_path.symlink_metadata().is_err() { @@ -562,9 +658,7 @@ pub fn chmod(state: &mut ShellState, mode_str: &str, path: &str, verbose: bool) #[cfg(not(unix))] let old_mode: u32 = 0; - // Parse mode - let new_mode = parse_chmod_mode(mode_str, old_mode) - .context(format!("chmod: invalid mode: '{}'", mode_str))?; + let new_mode = new_mode(old_mode)?; // Apply permissions #[cfg(unix)] @@ -576,32 +670,10 @@ pub fn chmod(state: &mut ShellState, mode_str: &str, path: &str, verbose: bool) #[cfg(not(unix))] anyhow::bail!("chmod not supported on this platform"); - // Record operation with old mode for undo + // Old mode is the undo data let op = Operation::new(OperationType::Chmod, path.to_string(), None) .with_undo_data(old_mode.to_le_bytes().to_vec()); - let op_id = op.id; - state.record_operation(op); - - println!( - "{} {} {:o} {}", - format!("[op:{}]", &op_id.to_string()[..8]).bright_black(), - "chmod".bright_green(), - new_mode & 0o7777, - path - ); - - if verbose { - let proof = OperationType::Chmod.proof_reference(); - println!(" {} {}", "Proof:".bright_black(), proof.format_short()); - println!( - " {} chmod {:o} {}", - "Undo:".bright_black(), - old_mode & 0o7777, - path - ); - } - - Ok(()) + Ok((op, old_mode, new_mode)) } /// Parse chmod mode string — supports octal (755) and symbolic (u+x, go-w, a=rwx). @@ -701,29 +773,9 @@ fn parse_chmod_mode(mode_str: &str, current_mode: u32) -> Result { /// PermissionOperations.lean: chown_reversible #[cfg(unix)] pub fn chown(state: &mut ShellState, owner_str: &str, path: &str, verbose: bool) -> Result<()> { - let file_path = state.resolve_path(path); - - if !file_path.exists() && file_path.symlink_metadata().is_err() { - anyhow::bail!("chown: cannot access '{}': No such file or directory", path); - } - - // Capture current ownership for undo - use std::os::unix::fs::MetadataExt; - let metadata = fs::symlink_metadata(&file_path).context("chown: failed to read metadata")?; - let old_uid = metadata.uid(); - let old_gid = metadata.gid(); - - // Parse owner[:group] - let (new_uid, new_gid) = parse_chown_spec(owner_str, old_uid, old_gid)?; - - // Apply ownership via libc - std::os::unix::fs::chown(&file_path, Some(new_uid), Some(new_gid)) - .map_err(|e| anyhow::anyhow!("chown failed: {}", e))?; - - // Record operation with old uid:gid for undo - let undo_str = format!("{}:{}", old_uid, old_gid); - let op = Operation::new(OperationType::Chown, path.to_string(), None) - .with_undo_data(undo_str.into_bytes()); + let (op, (old_uid, old_gid)) = apply_chown(state, path, |old_uid, old_gid| { + parse_chown_spec(owner_str, old_uid, old_gid) + })?; let op_id = op.id; state.record_operation(op); @@ -750,6 +802,43 @@ pub fn chown(state: &mut ShellState, owner_str: &str, path: &str, verbose: bool) Ok(()) } +/// Check, execute and describe a `chown`, without recording it. +/// +/// `new_owner` maps the file's current `(uid, gid)` to the ownership to set: +/// the command parses its owner spec against it, redo returns the ownership +/// undo replaced. Returns the operation (carrying the old `uid:gid` as undo +/// data) and the old `(uid, gid)`. +#[cfg(unix)] +fn apply_chown( + state: &ShellState, + path: &str, + new_owner: impl FnOnce(u32, u32) -> Result<(u32, u32)>, +) -> Result<(Operation, (u32, u32))> { + let file_path = state.resolve_path(path); + + if !file_path.exists() && file_path.symlink_metadata().is_err() { + anyhow::bail!("chown: cannot access '{}': No such file or directory", path); + } + + // Capture current ownership for undo + use std::os::unix::fs::MetadataExt; + let metadata = fs::symlink_metadata(&file_path).context("chown: failed to read metadata")?; + let old_uid = metadata.uid(); + let old_gid = metadata.gid(); + + let (new_uid, new_gid) = new_owner(old_uid, old_gid)?; + + // Apply ownership via libc + std::os::unix::fs::chown(&file_path, Some(new_uid), Some(new_gid)) + .map_err(|e| anyhow::anyhow!("chown failed: {}", e))?; + + // Old uid:gid is the undo data + let undo_str = format!("{}:{}", old_uid, old_gid); + let op = Operation::new(OperationType::Chown, path.to_string(), None) + .with_undo_data(undo_str.into_bytes()); + Ok((op, (old_uid, old_gid))) +} + /// Parse chown owner spec: `user`, `user:group`, `:group`, `user:`. /// Returns (uid, gid) where unchanged values keep the original. /// @@ -848,6 +937,9 @@ pub fn undo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { let path = state.resolve_path(&op.path); + // Read what this undo is about to replace, so redo can put it back. + let redo_data = capture_redo_data(state, op); + // Execute inverse operation match inverse_type { OperationType::Rmdir => { @@ -993,6 +1085,21 @@ pub fn undo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { let orig_id = op.id; state.mark_undone(orig_id, undo_id); + if let Some(RedoData::OverLimit { size, limit }) = redo_data { + eprintln!( + "{} {} was {} bytes, over the {} byte {} limit: this undo cannot be redone", + "Warning:".bright_yellow(), + op.path, + size, + limit, + REDO_CAPTURE_LIMIT_VAR + ); + } + if let Some(entry) = state.redo_stack.front_mut() { + if entry.id == orig_id { + entry.redo_data = redo_data; + } + } println!( "{} {} {} {} {}", @@ -1017,15 +1124,26 @@ pub fn undo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { /// Re-applies operations that were reversed with [`undo`]. Redo moves forward /// in the operation history, restoring the previous state. /// +/// Each operation is re-applied through the same function the original +/// command used, so its preconditions are checked again and its undo data is +/// captured again from the current state. The state an operation produced +/// (the mode a `chmod` set, the bytes a redirection wrote) is what [`undo`] +/// read just before reversing it. A redone operation can therefore be undone +/// and redone again any number of times. +/// +/// The redo stack is held in memory only, so redo is not available after a +/// restart. +/// /// # Arguments /// * `state` - Mutable shell state for accessing redo stack /// * `count` - Number of operations to redo (default: 1) /// * `verbose` - Whether to show proof references /// /// # Errors -/// Returns error if: -/// - No operations to redo -/// - Re-executing operation fails (filesystem changed externally) +/// Returns error if re-applying an operation fails, for example because a +/// precondition no longer holds (the destination of a `cp` exists again). +/// Nothing is changed or recorded for that operation, and it stays on the +/// redo stack. Redoing with nothing to redo is not an error. /// /// # Examples /// ```no_run @@ -1047,112 +1165,17 @@ pub fn redo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { } }; - let path = state.resolve_path(&op.path); - - // Re-execute the original operation - match op.op_type { - OperationType::Mkdir => { - fs::create_dir(&path).context("Redo mkdir failed")?; - } - OperationType::Rmdir => { - fs::remove_dir(&path).context("Redo rmdir failed")?; - } - OperationType::CreateFile => { - fs::write(&path, "").context("Redo touch failed")?; - } - OperationType::DeleteFile => { - fs::remove_file(&path).context("Redo rm failed")?; - } - OperationType::WriteFile => { - // Would need new content - anyhow::bail!("WriteFile redo not yet implemented"); - } - OperationType::FileTruncated => { - // Redo truncate: truncate file (original undo_data was the old content) - fs::write(&path, "").context("Redo truncate failed")?; - } - OperationType::FileAppended => { - // Cannot redo append without knowing what was appended - anyhow::bail!( - "FileAppended redo not yet implemented (would need appended content)" - ); - } - OperationType::CopyFile => { - // Redo copy: copy again from src (stored in undo_data) to dst (path) - let src = op - .undo_data - .as_ref() - .map(|d| String::from_utf8_lossy(d).to_string()) - .context("Missing source path for redo cp")?; - let src_path = state.resolve_path(&src); - fs::copy(&src_path, &path).context("Redo cp failed")?; - } - OperationType::Move => { - // Redo move: move src -> dst again - let parts: Vec<&str> = op.path.splitn(2, '\0').collect(); - if parts.len() != 2 { - anyhow::bail!("Invalid move operation record for redo"); - } - let src_path = state.resolve_path(parts[0]); - let dst_path = state.resolve_path(parts[1]); - fs::rename(&src_path, &dst_path).context("Redo mv failed")?; - } - OperationType::Symlink => { - // Redo symlink: create symlink again - let target = op - .undo_data - .as_ref() - .map(|d| String::from_utf8_lossy(d).to_string()) - .context("Missing symlink target for redo")?; - #[cfg(unix)] - std::os::unix::fs::symlink(&target, &path).context("Redo ln -s failed")?; - #[cfg(not(unix))] - anyhow::bail!("Symbolic links not supported on this platform"); - } - OperationType::Unlink => { - // Redo unlink: remove the symlink again - fs::remove_file(&path).context("Redo unlink failed")?; - } - OperationType::SetVariable => { - // Redo set: re-apply the variable value - // The undo_data stores the PREVIOUS value, not the new one. - // For redo, we need the value that was set, but we don't have it. - // Redo of SetVariable is a no-op — variable state has moved on. - // This is correct: variable redo is best-effort; filesystem redo is exact. - println!( - "{}", - "Variable set cannot be exactly redone (state-dependent)".bright_yellow() - ); - } - OperationType::UnsetVariable => { - // Redo unset: unset the variable again - state.variables.remove(&op.path); - state.exported_vars.remove(&op.path); - } - OperationType::Chmod => { - // Redo chmod: cannot exactly redo without storing new mode - println!( - "{}", - "chmod cannot be exactly redone (state-dependent)".bright_yellow() - ); - } - OperationType::Chown => { - // Redo chown: cannot exactly redo without storing new uid:gid - println!( - "{}", - "chown cannot be exactly redone (state-dependent)".bright_yellow() - ); - } - OperationType::HardwareErase | OperationType::Obliterate => { - // Cannot redo - irreversible operations - anyhow::bail!( - "{:?} cannot be redone - operation is irreversible", - op.op_type - ); + // Re-apply through the same path the original command took, so the + // new record carries freshly captured undo data. + let new_op = match reapply(state, &op) { + Ok(new_op) => new_op, + Err(e) => { + // Nothing was re-applied: keep the entry so redo can be + // retried once whatever blocked it is resolved. + state.redo_stack.push_front(op); + return Err(e); } - } - - let new_op = Operation::new(op.op_type, op.path.clone(), None); + }; let new_id = new_op.id; state.record_redo_operation(new_op); @@ -1172,6 +1195,293 @@ pub fn redo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { Ok(()) } +/// Shell or environment variable that bounds how many bytes of file content +/// one undo keeps in memory for redo. A plain number of bytes; `0` means no +/// limit. +pub const REDO_CAPTURE_LIMIT_VAR: &str = "VSH_REDO_CAPTURE_LIMIT"; + +/// Limit used when [`REDO_CAPTURE_LIMIT_VAR`] is unset or not a number: 10 MiB, +/// the size at which a redirection already warns about saving a file for undo. +pub const DEFAULT_REDO_CAPTURE_LIMIT: u64 = 10 * 1024 * 1024; + +/// Turn the setting's text into a limit in bytes; `None` is no limit. +fn parse_redo_capture_limit(setting: Option<&str>) -> Option { + match setting.map(str::trim).map(str::parse::) { + Some(Ok(0)) => None, + Some(Ok(limit)) => Some(limit), + Some(Err(_)) | None => Some(DEFAULT_REDO_CAPTURE_LIMIT), + } +} + +/// The capture limit in force: the shell variable if set, else the +/// environment variable, else the default. +fn redo_capture_limit(state: &ShellState) -> Option { + let setting = state + .get_variable(REDO_CAPTURE_LIMIT_VAR) + .map(str::to_string) + .or_else(|| std::env::var(REDO_CAPTURE_LIMIT_VAR).ok()); + parse_redo_capture_limit(setting.as_deref()) +} + +/// Read the state an undo of `op` is about to replace. +/// +/// `undo_data` holds the state before an operation; redo needs the state after +/// it. For operations whose record does not already say what that was, read it +/// here, immediately before undo reverses it. Returns `None` when the record +/// is enough (or when the state cannot be read, in which case redo of that +/// operation reports that it has nothing to restore). +/// +/// File content is held in memory until the redo stack is cleared, so content +/// larger than the capture limit is not kept; redo of that entry then says so. +fn capture_redo_data(state: &ShellState, op: &Operation) -> Option { + let path = state.resolve_path(&op.path); + let over_limit = |size: u64| { + redo_capture_limit(state) + .filter(|limit| size > *limit) + .map(|limit| RedoData::OverLimit { size, limit }) + }; + match op.op_type { + OperationType::CreateFile | OperationType::FileTruncated | OperationType::WriteFile => { + let size = fs::metadata(&path).ok()?.len(); + if let Some(skipped) = over_limit(size) { + return Some(skipped); + } + crate::fs_pure::read_to_end(&path) + .ok() + .map(RedoData::Content) + } + OperationType::FileAppended => { + use std::io::{Read, Seek, SeekFrom}; + let original_size = + u64::from_le_bytes(op.undo_data.as_ref()?.get(..8)?.try_into().ok()?); + let size = fs::metadata(&path).ok()?.len(); + if let Some(skipped) = over_limit(size.saturating_sub(original_size)) { + return Some(skipped); + } + let mut file = crate::fs_pure::open_pure(&path).ok()?; + file.seek(SeekFrom::Start(original_size)).ok()?; + let mut appended = Vec::new(); + file.read_to_end(&mut appended).ok()?; + Some(RedoData::Appended(appended)) + } + OperationType::Chmod => { + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = fs::symlink_metadata(&path).ok()?.permissions().mode(); + Some(RedoData::Mode(mode)) + } + #[cfg(not(unix))] + None + } + OperationType::Chown => { + #[cfg(unix)] + { + use std::os::unix::fs::MetadataExt; + let metadata = fs::symlink_metadata(&path).ok()?; + Some(RedoData::Owner { + uid: metadata.uid(), + gid: metadata.gid(), + }) + } + #[cfg(not(unix))] + None + } + OperationType::SetVariable => state + .variables + .get(&op.path) + .cloned() + .map(RedoData::Variable), + OperationType::Mkdir + | OperationType::Rmdir + | OperationType::DeleteFile + | OperationType::CopyFile + | OperationType::Move + | OperationType::Symlink + | OperationType::Unlink + | OperationType::UnsetVariable + | OperationType::HardwareErase + | OperationType::Obliterate => None, + } +} + +/// Re-apply an undone operation and return the record of doing so. +/// +/// Every arm goes through the function the original command used, so the +/// preconditions are checked again and the undo data is captured again from +/// the current state. Nothing is recorded here. +fn reapply(state: &mut ShellState, op: &Operation) -> Result { + // What undo saved for this entry, or an error naming what is missing. + let missing = || match op.redo_data { + Some(RedoData::OverLimit { size, limit }) => anyhow::anyhow!( + "Cannot redo {} {}: its {} bytes were over the {} byte limit when it was undone \ + (set {} to raise it, 0 for no limit)", + op.op_type, + op.path, + size, + limit, + REDO_CAPTURE_LIMIT_VAR + ), + _ => anyhow::anyhow!( + "Cannot redo {} {}: the state to restore was not captured when it was undone", + op.op_type, + op.path + ), + }; + // The text an operation keeps in `undo_data` (cp source, ln -s target). + let recorded_text = |what: &str| -> Result { + op.undo_data + .as_ref() + .map(|d| String::from_utf8_lossy(d).to_string()) + .with_context(|| format!("Missing {} for redo", what)) + }; + + match op.op_type { + OperationType::Mkdir => apply_mkdir(state, &op.path), + OperationType::Rmdir => apply_rmdir(state, &op.path), + OperationType::CreateFile => { + // Empty for `touch`; the redirected output for a file `>` created. + let content: &[u8] = match &op.redo_data { + Some(RedoData::Content(content)) => content, + Some(RedoData::OverLimit { .. }) => return Err(missing()), + _ => &[], + }; + apply_create_file(state, &op.path, content) + } + OperationType::DeleteFile => apply_rm(state, &op.path), + OperationType::WriteFile | OperationType::FileTruncated => { + let Some(RedoData::Content(content)) = &op.redo_data else { + return Err(missing()); + }; + apply_replace_content(state, op.op_type, &op.path, content) + } + OperationType::FileAppended => { + let Some(RedoData::Appended(appended)) = &op.redo_data else { + return Err(missing()); + }; + apply_append(state, &op.path, appended) + } + OperationType::CopyFile => { + let src = recorded_text("source path")?; + apply_cp(state, &src, &op.path) + } + OperationType::Move => { + let Some((src, dst)) = op.path.split_once('\0') else { + anyhow::bail!("Invalid move operation record for redo"); + }; + apply_mv(state, src, dst) + } + OperationType::Symlink => { + let target = recorded_text("symlink target")?; + apply_symlink(state, &target, &op.path) + } + OperationType::Unlink => apply_unlink(state, &op.path), + OperationType::SetVariable => { + let Some(RedoData::Variable(value)) = &op.redo_data else { + return Err(missing()); + }; + Ok(state.apply_set_variable(op.path.clone(), value.clone())) + } + OperationType::UnsetVariable => state + .apply_unset_variable(&op.path) + .with_context(|| format!("Cannot redo unset {}: it is not set", op.path)), + OperationType::Chmod => { + let Some(RedoData::Mode(mode)) = op.redo_data else { + return Err(missing()); + }; + apply_chmod(state, &op.path, |_| Ok(mode)).map(|(new_op, _, _)| new_op) + } + OperationType::Chown => { + let Some(RedoData::Owner { uid, gid }) = op.redo_data else { + return Err(missing()); + }; + #[cfg(unix)] + { + apply_chown(state, &op.path, |_, _| Ok((uid, gid))).map(|(new_op, _)| new_op) + } + #[cfg(not(unix))] + { + let _ = (uid, gid); + anyhow::bail!("chown not supported on this platform") + } + } + OperationType::HardwareErase | OperationType::Obliterate => { + // Cannot redo - irreversible operations + anyhow::bail!( + "{:?} cannot be redone - operation is irreversible", + op.op_type + ); + } + } +} + +/// Replace a file's whole content and describe it, without recording it. The +/// returned operation (`FileTruncated` or `WriteFile`) carries the content +/// that was replaced as its undo data, as the redirection that first recorded +/// it did. +fn apply_replace_content( + state: &ShellState, + op_type: OperationType, + path: &str, + content: &[u8], +) -> Result { + let full_path = state.resolve_path(path); + if !full_path.is_file() { + anyhow::bail!("Cannot redo {}: {} is not a file", op_type, path); + } + + let original = crate::fs_pure::read_to_end(&full_path) + .with_context(|| format!("Failed to read {} before redo", path))?; + // Open without `create`, so the write lands in the file that was read. + let mut file = fs::OpenOptions::new() + .write(true) + .truncate(true) + .open(&full_path) + .with_context(|| format!("Redo {} failed", op_type))?; + file.write_all(content) + .with_context(|| format!("Redo {} failed", op_type))?; + + Ok(Operation::new(op_type, path.to_string(), None).with_undo_data(original)) +} + +/// Append bytes to a file and describe it, without recording it. The returned +/// `FileAppended` operation carries the size before the append as its undo +/// data, as the `>>` redirection that first recorded it did. +fn apply_append(state: &ShellState, path: &str, appended: &[u8]) -> Result { + let full_path = state.resolve_path(path); + if !full_path.is_file() { + anyhow::bail!("Cannot redo append: {} is not a file", path); + } + + let original_size = fs::metadata(&full_path) + .with_context(|| format!("Failed to stat {} before redo", path))? + .len(); + let mut file = fs::OpenOptions::new() + .append(true) + .open(&full_path) + .context("Redo append failed")?; + file.write_all(appended).context("Redo append failed")?; + + Ok( + Operation::new(OperationType::FileAppended, path.to_string(), None) + .with_undo_data(original_size.to_le_bytes().to_vec()), + ) +} + +/// Remove a symbolic link and describe it, without recording it. The returned +/// `Unlink` operation carries the link's target as its undo data. +fn apply_unlink(state: &ShellState, path: &str) -> Result { + let full_path = state.resolve_path(path); + let target = fs::read_link(&full_path) + .with_context(|| format!("Cannot redo unlink: {} is not a symbolic link", path))?; + fs::remove_file(&full_path).context("Redo unlink failed")?; + + Ok( + Operation::new(OperationType::Unlink, path.to_string(), None) + .with_undo_data(target.to_string_lossy().as_bytes().to_vec()), + ) +} + /// Show operation history with optional proof references. /// /// Displays the last N operations in reverse chronological order, showing operation @@ -2668,6 +2978,28 @@ mod tests { assert!(parse_chmod_mode("xyz", 0o100644).is_err()); } + #[test] + fn test_parse_redo_capture_limit() { + // Unset or unreadable: the default. + assert_eq!( + parse_redo_capture_limit(None), + Some(DEFAULT_REDO_CAPTURE_LIMIT) + ); + assert_eq!( + parse_redo_capture_limit(Some("lots")), + Some(DEFAULT_REDO_CAPTURE_LIMIT) + ); + assert_eq!( + parse_redo_capture_limit(Some("-1")), + Some(DEFAULT_REDO_CAPTURE_LIMIT) + ); + // Zero: no limit. + assert_eq!(parse_redo_capture_limit(Some("0")), None); + // A number of bytes, surrounding whitespace ignored. + assert_eq!(parse_redo_capture_limit(Some("4096")), Some(4096)); + assert_eq!(parse_redo_capture_limit(Some(" 1 \n")), Some(1)); + } + #[test] fn test_chmod_undo_data_roundtrip() { let mode: u32 = 0o100755; diff --git a/impl/rust-cli/src/redirection.rs b/impl/rust-cli/src/redirection.rs index 8c8c1558..bc0bc76e 100644 --- a/impl/rust-cli/src/redirection.rs +++ b/impl/rust-cli/src/redirection.rs @@ -398,12 +398,23 @@ impl RedirectSetup { pub fn record_for_undo(self, state: &mut ShellState) -> Result<()> { use crate::state::{Operation, OperationType}; + // History paths are relative to the sandbox root: undo and redo resolve + // them under the root again, so a resolved absolute path would be + // resolved twice and miss the file. + let root = PathBuf::from(state.root()); + let history_path = |path: &Path| { + path.strip_prefix(&root) + .unwrap_or(path) + .to_string_lossy() + .to_string() + }; + // Clone modifications to avoid moving out of Drop type for modification in self.modifications.clone() { let (op_type, path, undo_data) = match modification { FileModification::Created { path } => { // File was created by redirection - let path_str = path.to_string_lossy().to_string(); + let path_str = history_path(&path); (OperationType::CreateFile, path_str, None) } @@ -412,7 +423,7 @@ impl RedirectSetup { original_content, } => { // File was truncated by > redirection - let path_str = path.to_string_lossy().to_string(); + let path_str = history_path(&path); ( OperationType::FileTruncated, path_str, @@ -425,7 +436,7 @@ impl RedirectSetup { original_size, } => { // File was appended to by >> redirection - let path_str = path.to_string_lossy().to_string(); + let path_str = history_path(&path); // Encode size as bytes let size_bytes = original_size.to_le_bytes().to_vec(); (OperationType::FileAppended, path_str, Some(size_bytes)) diff --git a/impl/rust-cli/src/state.rs b/impl/rust-cli/src/state.rs index 7f75389b..2a517b58 100644 --- a/impl/rust-cli/src/state.rs +++ b/impl/rust-cli/src/state.rs @@ -73,7 +73,7 @@ pub enum OperationType { /// undo_data contains original file size (u64 encoded as bytes) FileAppended, /// Copy file: reversible via deleting the destination - /// undo_data contains destination path (src stored in path field) + /// path stores the destination, undo_data stores the source path CopyFile, /// Move/rename: reversible via moving back /// path stores "src\0dst" (null-separated pair) @@ -157,6 +157,34 @@ impl std::fmt::Display for OperationType { } } +/// What [`crate::commands::redo`] needs in order to re-apply an undone operation. +/// +/// `undo_data` records the state *before* an operation, so undo can go back. +/// Going forward again needs the state *after* it, which some operations never +/// recorded (the mode a `chmod` set, the bytes a `>` redirection wrote). +/// [`crate::commands::undo`] reads that state just before it reverses the +/// operation and keeps it here, on the redo-stack entry. +/// +/// This lives in memory only (the redo stack is not persisted), so it adds +/// nothing to the saved state. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum RedoData { + /// Whole content of the file undo deleted or replaced + /// (`CreateFile`, `FileTruncated`, `WriteFile`). + Content(Vec), + /// The bytes undo cut off the end of the file (`FileAppended`). + Appended(Vec), + /// The mode undo replaced (`Chmod`). + Mode(u32), + /// The ownership undo replaced (`Chown`). + Owner { uid: u32, gid: u32 }, + /// The value undo replaced (`SetVariable`). + Variable(VariableValue), + /// The content was larger than the capture limit, so undo did not keep it + /// and this entry cannot be redone. + OverLimit { size: u64, limit: u64 }, +} + /// A single filesystem operation with complete undo/redo information. /// /// Each operation is assigned a unique UUID and records all data needed for reversal. @@ -195,6 +223,11 @@ pub struct Operation { /// The operation ID that undid this one (if any) pub undone_by: Option, + + /// The state undo replaced, kept on redo-stack entries so redo can put it + /// back. Never persisted; `None` everywhere else. + #[serde(skip)] + pub redo_data: Option, } impl Operation { @@ -208,6 +241,7 @@ impl Operation { undo_data: None, undone: false, undone_by: None, + redo_data: None, } } @@ -630,40 +664,50 @@ impl ShellState { /// /// Records the previous value (or absence) so the operation can be reversed. pub fn set_variable_tracked(&mut self, name: impl Into, value: impl Into) { - let name = name.into(); - let value = value.into(); + let op = self.apply_set_variable(name.into(), VariableValue::Scalar(value.into())); + self.record_operation(op); + } + /// Set a variable and return the operation describing it, without + /// recording it. The operation's undo data is the previous value (or its + /// absence). Shared by [`Self::set_variable_tracked`] and redo, so both + /// capture the same undo data. + pub fn apply_set_variable(&mut self, name: String, value: VariableValue) -> Operation { // Capture previous state for undo let previous = self.variables.get(&name).cloned(); let undo_data = serde_json::to_vec(&previous).unwrap_or_default(); // Perform the set - self.variables - .insert(name.clone(), VariableValue::Scalar(value)); + self.variables.insert(name.clone(), value); - // Record operation - let op = Operation::new(OperationType::SetVariable, name, None).with_undo_data(undo_data); - self.record_operation(op); + Operation::new(OperationType::SetVariable, name, None).with_undo_data(undo_data) } /// Unset a shell variable with undo tracking. pub fn unset_variable_tracked(&mut self, name: &str) { + // Nothing to unset is a no-op with no record. + if let Some(op) = self.apply_unset_variable(name) { + self.record_operation(op); + } + } + + /// Unset a variable and return the operation describing it, without + /// recording it; `None` if the variable was not set. Shared by + /// [`Self::unset_variable_tracked`] and redo. + pub fn apply_unset_variable(&mut self, name: &str) -> Option { // Capture previous state for undo let previous = self.variables.get(name).cloned(); - if previous.is_none() { - // Nothing to unset — no-op, no record - return; - } + previous.as_ref()?; let undo_data = serde_json::to_vec(&previous).unwrap_or_default(); // Perform the unset self.variables.remove(name); self.exported_vars.remove(name); - // Record operation - let op = Operation::new(OperationType::UnsetVariable, name.to_string(), None) - .with_undo_data(undo_data); - self.record_operation(op); + Some( + Operation::new(OperationType::UnsetVariable, name.to_string(), None) + .with_undo_data(undo_data), + ) } /// Get a shell variable value (scalar only) diff --git a/impl/rust-cli/tests/correspondence_tests.rs b/impl/rust-cli/tests/correspondence_tests.rs index 771ef538..bc2d9966 100644 --- a/impl/rust-cli/tests/correspondence_tests.rs +++ b/impl/rust-cli/tests/correspondence_tests.rs @@ -559,6 +559,66 @@ fn test_redo_correspondence() -> Result<()> { Ok(()) } +/// Test: Redo then undo returns to the undone state, and redo restores the +/// whole executed state: the filesystem and the history record. +/// +/// Model: `proofs/idris2/src/Filesystem/Composition.idr` +/// - `undoRedoIdentity`: `undo (execute op s) >>= redo = Just (execute op s)`. +/// The equality is on the whole `UndoState`, so it covers the undo stack, +/// not only the filesystem. +/// - `undoRedoUndoIdentity` and `redoUndoIdentity`: a redo followed by an +/// undo is the identity. +/// +/// The Rust counterpart of "the same undo-stack entry" is a history record +/// with the same type, path and undo data. Redo used to record the operation +/// with no undo data, so the filesystem matched after redo but the record did +/// not, and the next undo restored an empty file. This is `tested-only` +/// evidence about the Rust code, not a mechanised correspondence. +#[test] +fn test_redo_then_undo_correspondence() -> Result<()> { + let temp = tempdir()?; + let mut state = ShellState::new(temp.path().to_str().unwrap())?; + let dir = temp.path().join("dir"); + let file = temp.path().join("dir/file"); + let content: &[u8] = &[0x00, 0xff, b'v', b's', b'h', b'\n']; + + mkdir(&mut state, "dir", false)?; + fs::write(&file, content)?; + rm(&mut state, "dir/file", false)?; + rmdir(&mut state, "dir", false)?; + + // The executed state: nothing left, three records. + let executed: Vec<_> = state + .history + .iter() + .map(|op| (op.op_type, op.path.clone(), op.undo_data.clone())) + .collect(); + assert!(!dir.exists()); + + for cycle in 0..3 { + // undo: back to the directory holding the file, byte for byte. + vsh::commands::undo(&mut state, 2, false)?; + assert_eq!(fs::read(&file)?, content, "cycle {cycle}: undo"); + + // redo: the executed state again, records included. + vsh::commands::redo(&mut state, 2, false)?; + assert!(!dir.exists(), "cycle {cycle}: redo"); + let live: Vec<_> = state + .history + .iter() + .filter(|op| !op.undone) + .map(|op| (op.op_type, op.path.clone(), op.undo_data.clone())) + .collect(); + assert_eq!(live, executed, "cycle {cycle}: records after redo"); + } + + // redo then undo: the undone state, byte for byte. + vsh::commands::undo(&mut state, 2, false)?; + assert_eq!(fs::read(&file)?, content); + + Ok(()) +} + /// Test: Multiple undo/redo cycles maintain correctness #[test] fn test_undo_redo_cycles() -> Result<()> { diff --git a/impl/rust-cli/tests/redo_roundtrip_tests.rs b/impl/rust-cli/tests/redo_roundtrip_tests.rs new file mode 100644 index 00000000..ef179d71 --- /dev/null +++ b/impl/rust-cli/tests/redo_roundtrip_tests.rs @@ -0,0 +1,920 @@ +// SPDX-License-Identifier: MPL-2.0 +// Copyright (c) Jonathan D.A. Jewell +//! Undo / redo / undo round-trip regression tests. +//! +//! `redo` used to re-execute an operation and record it again with no undo +//! data, so a second undo could not restore the earlier state (`rm f; undo; +//! redo; undo` brought `f` back empty). These tests pin the exact round trip +//! for `rm`, `chmod`, `cp` and `ln -s`: contents are compared byte for byte +//! and modes bit for bit. +//! +//! These are tests of the Rust implementation only. They are `tested-only` +//! evidence for the undo/redo round-trip behaviour that the Lean/Coq theorems +//! assume of the model; they are not a mechanised correspondence proof. + +use std::fs; +use std::path::Path; + +use tempfile::TempDir; +use vsh::commands; +use vsh::state::{OperationType, ShellState}; + +/// Bytes that are not valid UTF-8 and contain NUL, CR and LF, so a restore +/// that goes through a `String` or trims anything is caught. +const BINARY: &[u8] = &[ + 0x00, 0xff, 0xfe, b'\r', b'\n', 0x80, b'h', b'i', 0x00, b'\n', +]; + +fn setup() -> (TempDir, ShellState) { + let temp = TempDir::new().expect("tempdir"); + let state = ShellState::new(temp.path().to_str().expect("utf8")).expect("state"); + (temp, state) +} + +fn undo(state: &mut ShellState) { + commands::undo(state, 1, false).expect("undo"); +} + +fn redo(state: &mut ShellState) { + commands::redo(state, 1, false).expect("redo"); +} + +fn read(path: &Path) -> Vec { + fs::read(path).unwrap_or_else(|e| panic!("read {}: {e}", path.display())) +} + +#[cfg(unix)] +fn mode_of(path: &Path) -> u32 { + use std::os::unix::fs::PermissionsExt; + fs::symlink_metadata(path) + .unwrap_or_else(|e| panic!("stat {}: {e}", path.display())) + .permissions() + .mode() + & 0o7777 +} + +#[cfg(unix)] +fn set_mode(path: &Path, mode: u32) { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(path, fs::Permissions::from_mode(mode)).expect("set mode"); +} + +// --------------------------------------------------------------------------- +// rm +// --------------------------------------------------------------------------- + +/// The reported reproduction: `rm f; undo; redo; undo` must restore "hello". +#[test] +fn rm_undo_redo_undo_restores_content() { + let (temp, mut state) = setup(); + let f = temp.path().join("f"); + fs::write(&f, b"hello").unwrap(); + + commands::rm(&mut state, "f", false).unwrap(); + assert!(!f.exists()); + + undo(&mut state); + assert_eq!(read(&f), b"hello"); + + redo(&mut state); + assert!(!f.exists(), "redo rm must remove the file again"); + + undo(&mut state); + assert_eq!(read(&f), b"hello", "second undo must restore the content"); +} + +/// Same round trip with non-UTF-8 content, repeated, so the bytes survive +/// any number of undo/redo cycles. +#[test] +fn rm_roundtrip_is_byte_exact_over_many_cycles() { + let (temp, mut state) = setup(); + let f = temp.path().join("blob.bin"); + fs::write(&f, BINARY).unwrap(); + + commands::rm(&mut state, "blob.bin", false).unwrap(); + + for cycle in 0..5 { + undo(&mut state); + assert_eq!(read(&f), BINARY, "cycle {cycle}: undo"); + redo(&mut state); + assert!(!f.exists(), "cycle {cycle}: redo"); + } + + undo(&mut state); + assert_eq!(read(&f), BINARY); +} + +/// The operation `redo` records must carry the same undo data the original +/// `rm` captured. +#[test] +fn rm_redo_records_the_undo_data_rm_captured() { + let (temp, mut state) = setup(); + fs::write(temp.path().join("f"), BINARY).unwrap(); + + commands::rm(&mut state, "f", false).unwrap(); + let original = state.history.last().unwrap().undo_data.clone(); + assert_eq!(original.as_deref(), Some(BINARY)); + + undo(&mut state); + redo(&mut state); + + let redone = state.history.last().unwrap(); + assert_eq!(redone.op_type, OperationType::DeleteFile); + assert!(!redone.undone); + assert_eq!(redone.undo_data, original); +} + +/// The file's mode is not part of what `rm` records, so this only pins that +/// redo of an `rm` whose file was changed between undo and redo captures the +/// content that redo actually deleted, not the content of the first `rm`. +#[test] +fn rm_redo_recaptures_content_present_at_redo_time() { + let (temp, mut state) = setup(); + let f = temp.path().join("f"); + fs::write(&f, b"first").unwrap(); + + commands::rm(&mut state, "f", false).unwrap(); + undo(&mut state); + + // Changed outside the shell between undo and redo. + fs::write(&f, b"second, and longer").unwrap(); + + redo(&mut state); + assert!(!f.exists()); + + undo(&mut state); + assert_eq!(read(&f), b"second, and longer"); +} + +// --------------------------------------------------------------------------- +// chmod +// --------------------------------------------------------------------------- + +#[cfg(unix)] +#[test] +fn chmod_undo_redo_undo_restores_mode() { + let (temp, mut state) = setup(); + let f = temp.path().join("f"); + fs::write(&f, b"hello").unwrap(); + set_mode(&f, 0o644); + + commands::chmod(&mut state, "600", "f", false).unwrap(); + assert_eq!(mode_of(&f), 0o600); + + undo(&mut state); + assert_eq!(mode_of(&f), 0o644); + + redo(&mut state); + assert_eq!(mode_of(&f), 0o600, "redo chmod must re-apply the mode"); + + undo(&mut state); + assert_eq!(mode_of(&f), 0o644, "second undo must restore the old mode"); + + assert_eq!( + read(&f), + b"hello", + "chmod round trip must not touch content" + ); +} + +/// A symbolic mode is relative to the mode at the time of the command. Redo +/// must land on the same resulting mode, over repeated cycles, including the +/// setuid/setgid/sticky bits. +#[cfg(unix)] +#[test] +fn chmod_symbolic_roundtrip_over_many_cycles() { + let (temp, mut state) = setup(); + let f = temp.path().join("f"); + fs::write(&f, b"x").unwrap(); + set_mode(&f, 0o1640); + + commands::chmod(&mut state, "u+x", "f", false).unwrap(); + assert_eq!(mode_of(&f), 0o1740); + + for cycle in 0..4 { + undo(&mut state); + assert_eq!(mode_of(&f), 0o1640, "cycle {cycle}: undo"); + redo(&mut state); + assert_eq!(mode_of(&f), 0o1740, "cycle {cycle}: redo"); + } + + undo(&mut state); + assert_eq!(mode_of(&f), 0o1640); +} + +/// Two stacked chmods: every intermediate mode comes back in order. +#[cfg(unix)] +#[test] +fn chmod_stacked_roundtrip() { + let (temp, mut state) = setup(); + let f = temp.path().join("f"); + fs::write(&f, b"x").unwrap(); + set_mode(&f, 0o644); + + commands::chmod(&mut state, "600", "f", false).unwrap(); + commands::chmod(&mut state, "755", "f", false).unwrap(); + + commands::undo(&mut state, 2, false).unwrap(); + assert_eq!(mode_of(&f), 0o644); + + commands::redo(&mut state, 2, false).unwrap(); + assert_eq!(mode_of(&f), 0o755); + + undo(&mut state); + assert_eq!(mode_of(&f), 0o600); + undo(&mut state); + assert_eq!(mode_of(&f), 0o644); +} + +// --------------------------------------------------------------------------- +// cp +// --------------------------------------------------------------------------- + +#[test] +fn cp_undo_redo_undo_redo_roundtrip() { + let (temp, mut state) = setup(); + let src = temp.path().join("src.bin"); + let dst = temp.path().join("dst.bin"); + fs::write(&src, BINARY).unwrap(); + + commands::cp(&mut state, "src.bin", "dst.bin", false).unwrap(); + assert_eq!(read(&dst), BINARY); + + for cycle in 0..3 { + undo(&mut state); + assert!(!dst.exists(), "cycle {cycle}: undo cp removes the copy"); + assert_eq!(read(&src), BINARY, "cycle {cycle}: source untouched"); + + redo(&mut state); + assert_eq!(read(&dst), BINARY, "cycle {cycle}: redo cp copies again"); + assert_eq!(read(&src), BINARY, "cycle {cycle}: source untouched"); + } + + undo(&mut state); + assert!(!dst.exists()); + assert_eq!(read(&src), BINARY); +} + +/// The operation `redo` records must carry the same data the original `cp` +/// recorded (the source path), or a later redo has nothing to copy from. +#[test] +fn cp_redo_records_the_data_cp_recorded() { + let (temp, mut state) = setup(); + fs::write(temp.path().join("src"), b"data").unwrap(); + + commands::cp(&mut state, "src", "dst", false).unwrap(); + let original = state.history.last().unwrap().undo_data.clone(); + assert_eq!(original.as_deref(), Some(b"src".as_slice())); + + undo(&mut state); + redo(&mut state); + + let redone = state.history.last().unwrap(); + assert_eq!(redone.op_type, OperationType::CopyFile); + assert_eq!(redone.path, "dst"); + assert_eq!(redone.undo_data, original); +} + +/// `cp` refuses an existing destination. Redo must not be a way around that: +/// a destination that appeared between undo and redo is not overwritten, and +/// the redo entry is kept so redo works once the way is clear. +#[test] +fn cp_redo_does_not_overwrite_a_destination_that_reappeared() { + let (temp, mut state) = setup(); + let src = temp.path().join("src"); + let dst = temp.path().join("dst"); + fs::write(&src, b"from source").unwrap(); + + commands::cp(&mut state, "src", "dst", false).unwrap(); + undo(&mut state); + assert!(!dst.exists()); + + // Created outside the shell between undo and redo. + fs::write(&dst, BINARY).unwrap(); + + let history_len = state.history.len(); + assert!( + commands::redo(&mut state, 1, false).is_err(), + "redo cp over an existing destination must fail" + ); + assert_eq!(read(&dst), BINARY, "the existing destination is intact"); + assert_eq!( + state.history.len(), + history_len, + "a failed redo records nothing" + ); + + // Once the obstacle is gone the same redo still works. + fs::remove_file(&dst).unwrap(); + redo(&mut state); + assert_eq!(read(&dst), b"from source"); + + undo(&mut state); + assert!(!dst.exists()); +} + +// --------------------------------------------------------------------------- +// ln -s +// --------------------------------------------------------------------------- + +#[cfg(unix)] +#[test] +fn symlink_undo_redo_undo_redo_roundtrip() { + let (temp, mut state) = setup(); + let real = temp.path().join("real.bin"); + let link = temp.path().join("link"); + fs::write(&real, BINARY).unwrap(); + + commands::symlink(&mut state, "real.bin", "link", false).unwrap(); + assert_eq!(fs::read_link(&link).unwrap(), Path::new("real.bin")); + + for cycle in 0..3 { + undo(&mut state); + assert!( + fs::symlink_metadata(&link).is_err(), + "cycle {cycle}: undo ln -s removes the link" + ); + assert_eq!(read(&real), BINARY, "cycle {cycle}: target untouched"); + + redo(&mut state); + assert_eq!( + fs::read_link(&link).unwrap(), + Path::new("real.bin"), + "cycle {cycle}: redo ln -s points at the same target" + ); + assert_eq!(read(&link), BINARY, "cycle {cycle}: link resolves"); + assert_eq!(read(&real), BINARY, "cycle {cycle}: target untouched"); + } + + undo(&mut state); + assert!(fs::symlink_metadata(&link).is_err()); + assert_eq!(read(&real), BINARY); +} + +/// The operation `redo` records must carry the link target the original +/// `ln -s` recorded. +#[cfg(unix)] +#[test] +fn symlink_redo_records_the_target_ln_recorded() { + let (temp, mut state) = setup(); + fs::write(temp.path().join("real"), b"x").unwrap(); + + commands::symlink(&mut state, "real", "link", false).unwrap(); + let original = state.history.last().unwrap().undo_data.clone(); + assert_eq!(original.as_deref(), Some(b"real".as_slice())); + + undo(&mut state); + redo(&mut state); + + let redone = state.history.last().unwrap(); + assert_eq!(redone.op_type, OperationType::Symlink); + assert_eq!(redone.path, "link"); + assert_eq!(redone.undo_data, original); +} + +/// `ln -s` refuses an existing link path. A file that appeared at the link +/// path between undo and redo is left intact, byte for byte, and the redo +/// entry is kept. +#[cfg(unix)] +#[test] +fn symlink_redo_does_not_replace_a_file_that_reappeared() { + let (temp, mut state) = setup(); + let link = temp.path().join("link"); + fs::write(temp.path().join("real"), b"x").unwrap(); + + commands::symlink(&mut state, "real", "link", false).unwrap(); + undo(&mut state); + + // Created outside the shell between undo and redo. + fs::write(&link, BINARY).unwrap(); + + let history_len = state.history.len(); + assert!( + commands::redo(&mut state, 1, false).is_err(), + "redo ln -s over an existing path must fail" + ); + assert!(!fs::symlink_metadata(&link) + .unwrap() + .file_type() + .is_symlink()); + assert_eq!(read(&link), BINARY, "the existing file is intact"); + assert_eq!( + state.history.len(), + history_len, + "a failed redo records nothing" + ); + + fs::remove_file(&link).unwrap(); + redo(&mut state); + assert_eq!(fs::read_link(&link).unwrap(), Path::new("real")); +} + +// --------------------------------------------------------------------------- +// Redo re-checks preconditions (mv, touch) +// --------------------------------------------------------------------------- + +/// `mv` refuses an existing destination; `fs::rename` alone would replace it. +#[test] +fn mv_redo_does_not_overwrite_a_destination_that_reappeared() { + let (temp, mut state) = setup(); + let src = temp.path().join("src"); + let dst = temp.path().join("dst"); + fs::write(&src, b"moved").unwrap(); + + commands::mv(&mut state, "src", "dst", false).unwrap(); + undo(&mut state); + assert_eq!(read(&src), b"moved"); + + // Created outside the shell between undo and redo. + fs::write(&dst, BINARY).unwrap(); + + assert!(commands::redo(&mut state, 1, false).is_err()); + assert_eq!(read(&dst), BINARY, "the existing destination is intact"); + assert_eq!(read(&src), b"moved", "the source was not moved"); + + fs::remove_file(&dst).unwrap(); + redo(&mut state); + assert_eq!(read(&dst), b"moved"); + assert!(!src.exists()); +} + +/// `touch` refuses an existing path; redo used to truncate whatever was there. +#[test] +fn touch_redo_does_not_truncate_a_file_that_reappeared() { + let (temp, mut state) = setup(); + let f = temp.path().join("f"); + + commands::touch(&mut state, "f", false).unwrap(); + undo(&mut state); + assert!(!f.exists()); + + // Created outside the shell between undo and redo. + fs::write(&f, BINARY).unwrap(); + + assert!(commands::redo(&mut state, 1, false).is_err()); + assert_eq!(read(&f), BINARY, "the existing file is intact"); +} + +/// `touch` on a dangling symbolic link fails and creates nothing: it must not +/// write through the link to a file the history knows nothing about. +#[cfg(unix)] +#[test] +fn touch_does_not_write_through_a_dangling_symlink() { + let (temp, mut state) = setup(); + std::os::unix::fs::symlink("elsewhere", temp.path().join("f")).unwrap(); + + assert!(commands::touch(&mut state, "f", false).is_err()); + assert!(!temp.path().join("elsewhere").exists()); + assert!(state.history.is_empty()); +} + +// --------------------------------------------------------------------------- +// Mixed sequence +// --------------------------------------------------------------------------- + +/// All four operations in one history: undo everything, redo everything, +/// undo everything again. Every state is the one first observed. +#[cfg(unix)] +#[test] +fn mixed_sequence_undo_all_redo_all_undo_all() { + let (temp, mut state) = setup(); + let root = temp.path(); + fs::write(root.join("a"), BINARY).unwrap(); + fs::write(root.join("gone"), b"hello").unwrap(); + set_mode(&root.join("a"), 0o644); + + commands::cp(&mut state, "a", "b", false).unwrap(); + commands::symlink(&mut state, "a", "l", false).unwrap(); + commands::chmod(&mut state, "400", "a", false).unwrap(); + commands::rm(&mut state, "gone", false).unwrap(); + + let assert_after = |when: &str| { + assert_eq!(read(&root.join("b")), BINARY, "{when}: copy"); + assert_eq!( + fs::read_link(root.join("l")).unwrap(), + Path::new("a"), + "{when}: link" + ); + assert_eq!(mode_of(&root.join("a")), 0o400, "{when}: mode"); + assert!(!root.join("gone").exists(), "{when}: rm"); + }; + let assert_before = |when: &str| { + assert!(!root.join("b").exists(), "{when}: copy"); + assert!( + fs::symlink_metadata(root.join("l")).is_err(), + "{when}: link" + ); + assert_eq!(mode_of(&root.join("a")), 0o644, "{when}: mode"); + assert_eq!(read(&root.join("gone")), b"hello", "{when}: rm"); + assert_eq!(read(&root.join("a")), BINARY, "{when}: source"); + }; + + assert_after("applied"); + commands::undo(&mut state, 4, false).unwrap(); + assert_before("first undo"); + commands::redo(&mut state, 4, false).unwrap(); + assert_after("redo"); + commands::undo(&mut state, 4, false).unwrap(); + assert_before("second undo"); +} + +// --------------------------------------------------------------------------- +// The round-trip law, for every operation that can reach the history +// --------------------------------------------------------------------------- + +/// Whole-sandbox comparison: every operation type is driven through the real +/// parse -> execute path, and the sandbox (kinds, bytes, modes, owners, link +/// targets) plus the tracked shell variable is compared after each step. +#[cfg(unix)] +mod laws { + use std::collections::BTreeMap; + use std::fs; + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + use std::path::Path; + + use proptest::prelude::*; + use tempfile::TempDir; + use vsh::commands; + use vsh::executable::ExecutableCommand; + use vsh::parser::parse_command; + use vsh::state::ShellState; + + use super::BINARY; + + #[derive(Debug, Clone, PartialEq, Eq)] + enum Entry { + Dir { + mode: u32, + owner: (u32, u32), + }, + File { + bytes: Vec, + mode: u32, + owner: (u32, u32), + }, + Link { + target: String, + }, + } + + /// Everything undo and redo are meant to preserve. + #[derive(Debug, Clone, PartialEq, Eq)] + struct Snapshot { + fs: BTreeMap, + /// Value and exported flag of the shell variable `X`. + x: (Option, bool), + } + + fn walk(root: &Path, dir: &Path, out: &mut BTreeMap) { + for entry in fs::read_dir(dir).expect("read_dir") { + let entry = entry.expect("dir entry"); + // The shell's own state and audit files are not user data. + if entry.file_name().to_string_lossy().starts_with(".vsh") { + continue; + } + let path = entry.path(); + let rel = path + .strip_prefix(root) + .expect("under root") + .to_string_lossy() + .to_string(); + let meta = fs::symlink_metadata(&path).expect("stat"); + let owner = (meta.uid(), meta.gid()); + let mode = meta.permissions().mode() & 0o7777; + if meta.file_type().is_symlink() { + let target = fs::read_link(&path) + .expect("read_link") + .to_string_lossy() + .to_string(); + out.insert(rel, Entry::Link { target }); + } else if meta.is_dir() { + out.insert(rel, Entry::Dir { mode, owner }); + walk(root, &path, out); + } else { + let bytes = fs::read(&path).expect("read"); + out.insert(rel, Entry::File { bytes, mode, owner }); + } + } + } + + fn snapshot(root: &Path, state: &ShellState) -> Snapshot { + let mut fs_map = BTreeMap::new(); + walk(root, root, &mut fs_map); + Snapshot { + fs: fs_map, + x: ( + state.get_variable("X").map(str::to_string), + state.is_exported("X"), + ), + } + } + + fn sandbox() -> (TempDir, ShellState) { + let temp = TempDir::new().expect("tempdir"); + let state = ShellState::new(temp.path().to_str().expect("utf8")).expect("state"); + (temp, state) + } + + fn run(state: &mut ShellState, line: &str) { + parse_command(line) + .unwrap_or_else(|e| panic!("parse {line:?}: {e}")) + .execute(state) + .unwrap_or_else(|e| panic!("execute {line:?}: {e}")); + } + + fn put(root: &Path, name: &str, mode: u32) { + let path = root.join(name); + fs::write(&path, BINARY).expect("write"); + fs::set_permissions(&path, fs::Permissions::from_mode(mode)).expect("chmod"); + } + + fn is_root() -> bool { + fs::metadata("/proc/self").map(|m| m.uid()).unwrap_or(1) == 0 + } + + struct Scenario { + name: &'static str, + setup: fn(&Path, &mut ShellState), + /// The one command under test. Redirections use an external + /// `/bin/echo`: the built-in `echo` writes through `println!`, which + /// the test harness captures before it reaches the redirected file. + line: &'static str, + /// `false` where undo itself does not restore the original state. + /// Those are defects in what the command records for undo, not in + /// redo, and are out of scope here; the redo laws are still asserted. + undo_is_exact: bool, + needs_root: bool, + } + + fn scenarios() -> Vec { + let s = |name, setup, line| Scenario { + name, + setup, + line, + undo_is_exact: true, + needs_root: false, + }; + vec![ + s("mkdir", |_, _| {}, "mkdir d"), + s( + "rmdir", + |r, _| fs::create_dir(r.join("d")).expect("mkdir"), + "rmdir d", + ), + Scenario { + // Known undo defect: rmdir does not record the directory's mode. + undo_is_exact: false, + ..s( + "rmdir of a 0700 directory", + |r, _| { + fs::create_dir(r.join("d")).expect("mkdir"); + fs::set_permissions(r.join("d"), fs::Permissions::from_mode(0o700)) + .expect("chmod"); + }, + "rmdir d", + ) + }, + s("touch", |_, _| {}, "touch f"), + s("rm", |r, _| put(r, "f", 0o644), "rm f"), + Scenario { + // Known undo defect: rm does not record the file's mode. + undo_is_exact: false, + ..s("rm of a 0600 file", |r, _| put(r, "f", 0o600), "rm f") + }, + s("cp", |r, _| put(r, "a", 0o640), "cp a b"), + s("mv", |r, _| put(r, "a", 0o640), "mv a b"), + s("ln -s", |r, _| put(r, "a", 0o640), "ln -s a l"), + s("chmod octal", |r, _| put(r, "f", 0o644), "chmod 600 f"), + s("chmod symbolic", |r, _| put(r, "f", 0o1640), "chmod u+x f"), + Scenario { + needs_root: true, + ..s("chown", |r, _| put(r, "f", 0o644), "chown 1000:1000 f") + }, + s("> creating a file", |_, _| {}, "/bin/echo hi > new"), + s( + "> replacing a file", + |r, _| put(r, "f", 0o640), + "/bin/echo hi > f", + ), + s( + ">> appending to a file", + |r, _| put(r, "f", 0o640), + "/bin/echo hi >> f", + ), + s("assigning a new variable", |_, _| {}, "X=1"), + s("reassigning a variable", |_, s| run(s, "X=1"), "X=2"), + s("unset", |_, s| run(s, "X=1"), "unset X"), + Scenario { + // Known undo defect: unset does not record the exported flag. + undo_is_exact: false, + ..s( + "unset of an exported variable", + |_, s| { + run(s, "X=1"); + run(s, "export X"); + }, + "unset X", + ) + }, + ] + } + + /// S0 -op-> S1 -undo-> A -redo-> B -undo-> C -redo-> D. + /// Redo restores the state the operation produced (B == S1, D == S1), a + /// second undo lands where the first did (C == A), and where the command + /// records enough for undo, that is the original state (A == S0). + #[test] + fn every_operation_round_trips() { + for sc in scenarios() { + if sc.needs_root && !is_root() { + eprintln!("skipping {:?}: needs root", sc.name); + continue; + } + let (temp, mut state) = sandbox(); + let root = temp.path(); + (sc.setup)(root, &mut state); + let s0 = snapshot(root, &state); + run(&mut state, sc.line); + let s1 = snapshot(root, &state); + assert_ne!(s0, s1, "{}: the command changed nothing", sc.name); + + commands::undo(&mut state, 1, false) + .unwrap_or_else(|e| panic!("{}: undo: {e}", sc.name)); + let a = snapshot(root, &state); + assert_ne!(a, s1, "{}: undo changed nothing", sc.name); + if sc.undo_is_exact { + assert_eq!(a, s0, "{}: undo restores the original state", sc.name); + } + + commands::redo(&mut state, 1, false) + .unwrap_or_else(|e| panic!("{}: redo: {e}", sc.name)); + assert_eq!( + snapshot(root, &state), + s1, + "{}: redo restores the state the command produced", + sc.name + ); + + commands::undo(&mut state, 1, false) + .unwrap_or_else(|e| panic!("{}: undo after redo: {e}", sc.name)); + assert_eq!( + snapshot(root, &state), + a, + "{}: undo after redo lands where the first undo did", + sc.name + ); + + commands::redo(&mut state, 1, false) + .unwrap_or_else(|e| panic!("{}: second redo: {e}", sc.name)); + assert_eq!( + snapshot(root, &state), + s1, + "{}: a second redo restores the state the command produced", + sc.name + ); + } + } + + /// Undo of a redirection used to fail outright unless the sandbox root was + /// `/`: the history held the resolved absolute path, which undo resolved + /// under the root a second time. + #[test] + fn redirection_history_paths_are_relative_to_the_root() { + let (temp, mut state) = sandbox(); + fs::create_dir(temp.path().join("sub")).expect("mkdir"); + run(&mut state, "/bin/echo hi > sub/out"); + assert_eq!(state.history.last().expect("recorded").path, "sub/out"); + + commands::undo(&mut state, 1, false).expect("undo"); + assert!(!temp.path().join("sub/out").exists()); + commands::redo(&mut state, 1, false).expect("redo"); + assert_eq!( + fs::read(temp.path().join("sub/out")).expect("read"), + b"hi\n" + ); + } + + // -- capture limit ------------------------------------------------------ + + /// File content that undo keeps for redo is bounded. Over the limit, the + /// undo still happens, redo of that entry fails with the reason, and the + /// entry stays on the redo stack. + #[test] + fn content_over_the_capture_limit_is_not_kept_for_redo() { + let (temp, mut state) = sandbox(); + let new = temp.path().join("new"); + state.set_variable(commands::REDO_CAPTURE_LIMIT_VAR, "2"); + run(&mut state, "/bin/echo hi > new"); + assert_eq!(fs::read(&new).expect("read"), b"hi\n"); + + commands::undo(&mut state, 1, false).expect("undo is not limited"); + assert!(!new.exists()); + + let err = commands::redo(&mut state, 1, false).expect_err("redo over the limit"); + let message = err.to_string(); + assert!( + message.contains(commands::REDO_CAPTURE_LIMIT_VAR) && message.contains("3 bytes"), + "the error names the limit and the size: {message}" + ); + assert!(!new.exists(), "a refused redo creates nothing"); + assert_eq!(state.get_redo_stack().len(), 1, "the entry is kept"); + } + + /// `0` removes the limit; unset, the default applies. + #[test] + fn capture_limit_zero_is_unlimited_and_default_is_ten_mebibytes() { + let size = commands::DEFAULT_REDO_CAPTURE_LIMIT as usize + 1; + for (setting, redo_works) in [(None, false), (Some("0"), true)] { + let (temp, mut state) = sandbox(); + let big = temp.path().join("big"); + fs::write(&big, vec![0xab_u8; size]).expect("write"); + if let Some(setting) = setting { + state.set_variable(commands::REDO_CAPTURE_LIMIT_VAR, setting); + } + // External commands run in the process's directory, not the + // sandbox root, so the source is named absolutely. + run(&mut state, &format!("/bin/cat {} > new", big.display())); + commands::undo(&mut state, 1, false).expect("undo"); + + let redone = commands::redo(&mut state, 1, false); + assert_eq!(redone.is_ok(), redo_works, "limit setting {setting:?}"); + if redo_works { + let restored = fs::read(temp.path().join("new")).expect("read"); + assert!(restored == fs::read(&big).expect("read"), "bytes differ"); + } + } + } + + // -- random sequences --------------------------------------------------- + + /// One command line over a four-name namespace. Symbolic links are left + /// out: `rm`, `chmod` and redirections follow them, and undo of those is + /// a known, separate defect that would mask what is tested here. + fn command_line() -> impl Strategy { + let name = prop::sample::select(vec!["a", "b", "c", "d"]); + let mode = prop::sample::select(vec!["600", "644", "640", "755", "700", "u+x"]); + prop_oneof![ + name.clone().prop_map(|n| format!("mkdir {n}")), + name.clone().prop_map(|n| format!("rmdir {n}")), + name.clone().prop_map(|n| format!("touch {n}")), + name.clone().prop_map(|n| format!("rm {n}")), + (name.clone(), name.clone()).prop_map(|(n, m)| format!("cp {n} {m}")), + (name.clone(), name.clone()).prop_map(|(n, m)| format!("mv {n} {m}")), + (mode, name.clone()).prop_map(|(mode, n)| format!("chmod {mode} {n}")), + (0..1000u32, name.clone()).prop_map(|(v, n)| format!("/bin/echo w{v} > {n}")), + (0..1000u32, name).prop_map(|(v, n)| format!("/bin/echo a{v} >> {n}")), + (0..5u32).prop_map(|v| format!("X={v}")), + Just("unset X".to_string()), + ] + } + + proptest! { + #![proptest_config(ProptestConfig::with_cases(48))] + + /// Run a random sequence (commands whose preconditions fail record + /// nothing), then undo everything, redo everything and undo + /// everything again, one history entry at a time. + /// + /// * Redo inverts undo: redoing back up to k entries gives the state + /// undo left at k entries, and redoing all of them gives the state + /// the sequence ended in. + /// * Undo is repeatable: the second pass of undos visits the same + /// states as the first. + #[test] + fn random_sequences_round_trip(lines in prop::collection::vec(command_line(), 1..12)) { + let (temp, mut state) = sandbox(); + let root = temp.path(); + for line in &lines { + if let Ok(command) = parse_command(line) { + let _ = command.execute(&mut state); + } + } + let entries = state.history.len(); + let end = snapshot(root, &state); + + // after_undo[k] is the state with k entries still applied. + let mut after_undo = vec![end.clone(); entries + 1]; + for k in (0..entries).rev() { + commands::undo(&mut state, 1, false) + .map_err(|e| TestCaseError::fail(format!("undo to {k}: {e}")))?; + after_undo[k] = snapshot(root, &state); + } + for (k, want) in after_undo.iter().enumerate().skip(1) { + commands::redo(&mut state, 1, false) + .map_err(|e| TestCaseError::fail(format!("redo to {k}: {e}")))?; + prop_assert_eq!(&snapshot(root, &state), want, "redo to {} entries", k); + } + prop_assert_eq!(&snapshot(root, &state), &end); + for k in (0..entries).rev() { + commands::undo(&mut state, 1, false) + .map_err(|e| TestCaseError::fail(format!("second undo to {k}: {e}")))?; + prop_assert_eq!( + &snapshot(root, &state), + &after_undo[k], + "second undo to {} entries", + k + ); + } + } + } +} From d07f70b006c8030fa7a896e3e772112db2651876 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 04:28:19 +0000 Subject: [PATCH 02/15] proofs(idris2): state the redo-then-undo laws on the undo/redo stack model `Filesystem.Composition` proved that undo then redo is the identity (`undoRedoIdentity`, `undoRedoComposition`) but did not state the other direction. Added: - `redoUndoIdentity`: from any state with an operation to redo, redo then undo is the identity, given that the operation inverts at the current filesystem. Dropping that premise is rejected by the checker. - `undoRedoUndoIdentity`: undo, redo, undo equals undo. A corollary of `undoRedoIdentity`, which equates whole states including the undo stack. - `Redoable`, `redoableStep`, `redoUndoComposition`: n redos followed by n undos is the identity, the mirror of `undoRedoComposition`. Checked with Idris2 0.8.0, `idris2 --build valence-shell.ipkg` (`--total`); the believe_me gate passes with the same two registered axioms. These are theorems about the five-operation flat model. They say nothing about the Rust `redo`, whose evidence is tests only. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- proofs/idris2/README.adoc | 8 ++ proofs/idris2/src/Filesystem/Composition.idr | 92 ++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/proofs/idris2/README.adoc b/proofs/idris2/README.adoc index a39d376a..9f0d4f04 100644 --- a/proofs/idris2/README.adoc +++ b/proofs/idris2/README.adoc @@ -87,6 +87,14 @@ via `+equivAddSwap+` — `+=+` was a non-theorem |`+undoRedoComposition+` |induction over an `+Undoable+` witness + `+Maybe+`-monad laws (`+applyNSnoc+`, `+bindAssoc+`, `+bindCong+`) +|`+redoUndoIdentity+` |`+cong+` over the per-step reversibility premise +(redo then undo, from any redo stack) + +|`+undoRedoUndoIdentity+` |corollary of `+undoRedoIdentity+` + +|`+redoUndoComposition+` |induction over a `+Redoable+` witness (mirror of +`+undoRedoComposition+`); `+redoableStep+` builds the witness + |`+overwriteIrreversible+` |redesigned to non-injectivity (`+updateEntryDeterminedByFilter+`); prior `+recovery randomData = Nothing+` was refuted by `+recovery = Just+` diff --git a/proofs/idris2/src/Filesystem/Composition.idr b/proofs/idris2/src/Filesystem/Composition.idr index 3dfc6d9e..c05680b4 100644 --- a/proofs/idris2/src/Filesystem/Composition.idr +++ b/proofs/idris2/src/Filesystem/Composition.idr @@ -369,3 +369,95 @@ undoRedoComposition state (S n) (US s' uEq rEq inner) = (trans (sym (bindAssoc (applyN n Composition.undo s') (applyN n Composition.redo) Composition.redo)) (rewrite undoRedoComposition s' n inner in rEq)) + +-------------------------------------------------------------------------------- +-- Redo then undo +-------------------------------------------------------------------------------- + +||| Redo followed by undo is the identity, from any state with something to +||| redo, provided the redone operation genuinely inverts at the current +||| filesystem. +||| +||| This is the direction `undoRedoIdentity` does not cover: it starts from an +||| arbitrary redo stack, not from a state reached by `execute`. The premise +||| is the same per-step reversibility fact `TraceReversible` records. It is +||| a real premise: an operation re-applied by `redo` must carry everything +||| its inverse needs. (An implementation that redoes an operation but drops +||| the data its inverse needs satisfies `undoRedoIdentity` on the filesystem +||| component and still breaks this law.) +export +redoUndoIdentity : + (fs : Filesystem) -> + (undos, redos : List Operation) -> + (op : Operation) -> + applyOp (Composition.inverse op) (applyOp op fs) = fs -> + (Composition.redo (MkUndoState fs undos (op :: redos)) >>= Composition.undo) + = Just (MkUndoState fs undos (op :: redos)) +redoUndoIdentity fs undos redos op prf = + cong (\x => Just (MkUndoState x undos (op :: redos))) prf + +||| Undo, redo, undo is the same as undo. +||| +||| After `execute op`, undoing, redoing and undoing again lands on the state +||| the first undo reached: filesystem, undo stack and redo stack alike. A +||| corollary of `undoRedoIdentity`, which equates whole `UndoState`s and so +||| already fixes the undo stack that the second undo reads. +export +undoRedoUndoIdentity : + (state : UndoState) -> + (op : Operation) -> + Applicable op (current state) -> + ((Composition.undo (execute op state) >>= Composition.redo) >>= Composition.undo) + = Composition.undo (execute op state) +undoRedoUndoIdentity state op prf = + cong (>>= Composition.undo) (undoRedoIdentity state op prf) + +||| A state is `n`-fold redoable when each of the next `n` redos is exactly +||| cancelled by an undo. The mirror image of `Undoable`. +public export +data Redoable : Nat -> UndoState -> Type where + ||| Zero redos: trivially redoable. + RZ : Redoable Z st + ||| One redo to `s'` that an `undo` cancels, then `n` more from `s'`. + RS : {0 st : UndoState} -> {0 n : Nat} -> + (s' : UndoState) -> + redo st = Just s' -> + undo s' = Just st -> + Redoable n s' -> + Redoable (S n) st + +||| `Redoable` is inhabited whenever the operation on top of the redo stack +||| inverts at the current filesystem: one step of the witness, built from +||| the same premise as `redoUndoIdentity`. +export +redoableStep : + (fs : Filesystem) -> + (undos, redos : List Operation) -> + (op : Operation) -> + applyOp (Composition.inverse op) (applyOp op fs) = fs -> + Redoable n (MkUndoState (applyOp op fs) (op :: undos) redos) -> + Redoable (S n) (MkUndoState fs undos (op :: redos)) +redoableStep fs undos redos op prf rest = + RS (MkUndoState (applyOp op fs) (op :: undos) redos) + Refl + (cong (\x => Just (MkUndoState x undos (op :: redos))) prf) + rest + +||| Multiple redos can all be undone: `n` redos followed by `n` undos is the +||| identity, for any `n`-fold-redoable state. The mirror image of +||| `undoRedoComposition`, by the same induction with the roles of `undo` +||| and `redo` exchanged. +export +redoUndoComposition : + (state : UndoState) -> + (n : Nat) -> + Redoable n state -> + (applyN n Composition.redo state >>= applyN n Composition.undo) = Just state +redoUndoComposition state Z RZ = Refl +redoUndoComposition state (S n) (RS s' rEq uEq inner) = + rewrite rEq in + trans (bindCong (applyN n Composition.redo s') + (\z => applyNSnoc n Composition.undo z)) + (trans (sym (bindAssoc (applyN n Composition.redo s') + (applyN n Composition.undo) Composition.undo)) + (rewrite redoUndoComposition s' n inner in uEq)) From b66092b726ae0acf1f8eb2f1cbde464e4072b682 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 04:28:20 +0000 Subject: [PATCH 03/15] docs: record the redo fix, its tests and the new model laws - `docs/LEAN4_RUST_CORRESPONDENCE.adoc`: a redo section saying which model statements exist (Idris2 only; Lean and Coq have no redo stack), what the Rust `redo` does now, and what is not modelled. - `EXPLAINME.adoc` proof ledger: one `proven-with-postulates` row for the undo/redo stack laws and one `tested-only` row for the Rust round trip. - `CHANGELOG.adoc`: the fix and the defects found and not fixed. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- CHANGELOG.adoc | 31 +++++++++++++++ EXPLAINME.adoc | 12 ++++++ docs/LEAN4_RUST_CORRESPONDENCE.adoc | 60 +++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index de48c3d5..10e7bad3 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -47,6 +47,37 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 row can be `proven`; the Mizar target swallows failure (`|| echo`). Both are logged as AP-6, AP-7 and AP-8 in the audit. +=== Fixed — 2026-11-17 (redo keeps undo data; undo of redirections) + +- `redo` recorded the re-applied operation with no undo data, so + `rm f; undo; redo; undo` restored `f` empty, a redone `chmod`/`chown` changed + nothing and could not be undone, and a second redo of `cp`/`ln -s` failed. + Each operation now has one function that checks its preconditions, captures + its undo data and applies it; the command and `redo` both call it. +- `redo` now restores what an operation produced (the mode of a `chmod`, the + owner of a `chown`, the bytes a `>`/`>>` redirection wrote, a variable's + value). `undo` reads that state just before reversing it and keeps it in + memory with the redo entry. File content over `VSH_REDO_CAPTURE_LIMIT` bytes + (shell or environment variable; default 10 MiB; `0` = no limit) is not kept, + and redo of that entry says so. +- A `redo` that fails changes nothing and stays on the redo stack. `redo` of + `cp`, `mv` and `touch` no longer overwrites a path that reappeared after the + undo. +- Undo of a `>`/`>>` redirection failed whenever the sandbox root was not `/` + (the default root is the current directory): the history held an absolute + path that undo resolved under the root a second time. History paths for + redirections are now relative to the root. +- `touch` creates its file with `create_new`, so it no longer writes through a + dangling symbolic link. +- Idris2 model: `redoUndoIdentity`, `undoRedoUndoIdentity`, + `redoUndoComposition` added to `Filesystem.Composition` (`proven` for the + model; the Rust link stays `tested-only`). +- Known and *not* fixed here (tests exclude them, see + `impl/rust-cli/tests/redo_roundtrip_tests.rs`): undo of `rm`/`rmdir` does + not restore the mode; undo of `unset` does not restore the exported flag; + `rm`, `chmod` and redirections follow symbolic links and their undo does not + account for it. + === Planned - Glob expansion (*.txt, ?.rs, [a-z]*) - Quote processing (proper escaping, single/double quotes) diff --git a/EXPLAINME.adoc b/EXPLAINME.adoc index 7f0fc4d8..13d4f5f0 100644 --- a/EXPLAINME.adoc +++ b/EXPLAINME.adoc @@ -144,6 +144,18 @@ reproduce. Absence of a tool is an open obligation, not a passing leg | `impl/rust-cli/tests/model_oracle_correspondence.rs` | Sampled, precondition-respecting sequences only; a divergence would be a real bug but agreement is not a proof +| Undo then redo, and redo then undo, are identities on the undo/redo stack model (single step and `n`-fold) +| proven-with-postulates +| Idris2 0.8.0, `idris2 --build valence-shell.ipkg` (`--total`), re-run 2026-11-17, plus the `believe_me` gate; CI `idris-verification.yml` +| `proofs/idris2/src/Filesystem/Composition.idr`: `undoRedoIdentity`, `undoRedoComposition`, and (2026-11-17) `redoUndoIdentity`, `undoRedoUndoIdentity`, `redoUndoComposition` +| `redoUndoIdentity` and `redoUndoComposition` use no axiom; they are conditional on a per-step reversibility premise. `undoRedoIdentity` and its corollary `undoRedoUndoIdentity` discharge that premise through lemmas resting on the registered `axStringEqRefl`. The model has five operations (mkdir, rmdir, touch, rm, write) on a flat path map: no cp, mv, links, modes, ownership, variables or redirections. It says nothing about the Rust `redo` + +| The Rust `undo`/`redo` round trip restores state for every operation type that reaches the history +| tested-only +| `cargo test --test redo_roundtrip_tests` (22 tests: 19 operation scenarios, 48 random sequences via `proptest`) and `test_redo_then_undo_correspondence` +| `impl/rust-cli/tests/redo_roundtrip_tests.rs`, `impl/rust-cli/tests/correspondence_tests.rs` +| Whole-sandbox comparison (bytes, modes, owners, link targets, one shell variable). Three scenarios assert only the redo laws because undo itself loses data there (mode on `rm`/`rmdir`, exported flag on `unset`); random sequences exclude symbolic links for the same reason; the `chown` scenario runs only as root. Not a proof and not a refinement of the Idris2 model + | Idris2 ABI layout, encodings and round-trips match the C header | tested-only | `--total`, plus `scripts/check-abi-layout.sh` diff --git a/docs/LEAN4_RUST_CORRESPONDENCE.adoc b/docs/LEAN4_RUST_CORRESPONDENCE.adoc index 9aae04c8..994d0eb1 100644 --- a/docs/LEAN4_RUST_CORRESPONDENCE.adoc +++ b/docs/LEAN4_RUST_CORRESPONDENCE.adoc @@ -478,6 +478,66 @@ let op = Operation::new(OperationType::DeleteFile, path.to_string(), None) * ✅ Restored on undo * ✅ Correspondence maintained +==== Redo, and redo followed by undo (added 2026-11-17) + +The redo stack is modelled only in Idris2 +(`+proofs/idris2/src/Filesystem/Composition.idr+`); the Lean and Coq +developments state single-step and sequence undo, with no redo stack. + +[cols="2,3",options="header"] +|=== +| Model statement (Idris2) | What it says + +| `+undoRedoIdentity+` +| `+undo (execute op s) >>= redo = Just (execute op s)+`. The equality is on +the whole `+UndoState+`: filesystem, undo stack and redo stack. + +| `+undoRedoUndoIdentity+` +| Undo, redo, undo equals undo. Corollary of the above. + +| `+redoUndoIdentity+` +| From any state with an operation to redo, redo then undo is the identity, +given that the operation inverts at the current filesystem. + +| `+redoUndoComposition+` +| `+n+` redos followed by `+n+` undos is the identity, over a `+Redoable+` +witness (mirror of `+undoRedoComposition+`). +|=== + +Status: `proven` for the model (five operations: mkdir, rmdir, touch, rm, +write, on a flat path map). The link to the Rust code is `tested-only`. + +The model re-applies the *same* operation value on redo, so the redone entry +on the undo stack is identical to the original. The Rust `+redo+` did not do +that before 2026-11-17: it re-executed the operation and recorded it with no +undo data, so the filesystem matched after a redo but the history record did +not, and the next undo restored an empty file (`+rm f; undo; redo; undo+`). +The existing tests compared only the filesystem after a redo, so they did not +see it. + +What the Rust does now (`+impl/rust-cli/src/commands.rs+`): + +* Each operation has one `+apply_*+` function that checks its preconditions, +captures its undo data and changes the filesystem. The command and `+redo+` +both call it, so a redone record carries freshly captured undo data. +* What an operation *produced* (the mode a `+chmod+` set, the bytes a +redirection wrote, a variable's new value) is read by `+undo+` just before it +reverses the operation and kept on the in-memory redo entry +(`+state::RedoData+`). File content over `+VSH_REDO_CAPTURE_LIMIT+` bytes +(default 10 MiB, `+0+` for no limit) is not kept; redo of that entry fails +with the reason. +* A redo that fails changes nothing and stays on the redo stack. + +Tests (`tested-only`): `+impl/rust-cli/tests/redo_roundtrip_tests.rs+` +(every operation type, random sequences, the capture limit) and +`+test_redo_then_undo_correspondence+` in `+correspondence_tests.rs+`, which +compares the history records as well as the filesystem. + +Not covered by the model: cp, mv, symbolic links, chmod, chown, variables, +redirections, nested directories. Known undo defects that these tests +exclude are listed in the test file (`+undo_is_exact: false+` and the note on +symbolic links). + ''''' === Composition Theorems From 7ddfaf123278eaef5a4009c198df3033f3a3761d Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 05:18:24 +0000 Subject: [PATCH 04/15] fix(chmod): build the invalid-mode error text only on failure SonarCloud rust:S2629 on the chmod wrapper: `context(format!(..))` built the message on every call. Use `with_context`. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- impl/rust-cli/src/commands.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index 1747abe2..b3eacddf 100644 --- a/impl/rust-cli/src/commands.rs +++ b/impl/rust-cli/src/commands.rs @@ -603,7 +603,8 @@ fn apply_symlink(state: &ShellState, target: &str, link: &str) -> Result Result<()> { let (op, old_mode, new_mode) = apply_chmod(state, path, |old_mode| { - parse_chmod_mode(mode_str, old_mode).context(format!("chmod: invalid mode: '{}'", mode_str)) + parse_chmod_mode(mode_str, old_mode) + .with_context(|| format!("chmod: invalid mode: '{}'", mode_str)) })?; let op_id = op.id; state.record_operation(op); From ad51965827284ede0a55f89b29a4e66279e50ec0 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 05:34:24 +0000 Subject: [PATCH 05/15] fix(redo): a redo whose write fails part-way leaves the file as it found it Review findings on #231 (CodeRabbit), all three valid. - `apply_create_file` left a partial file behind when the write failed after `create_new`, which then blocked the retry with EEXIST. It now removes the file. - `apply_replace_content` truncated before writing, so a failed write left the file empty or partial and a retry captured that as the undo data. It now puts the previous content back, and says so if that fails too. - `apply_append` now cuts off a partial append. - New test `tests/redo_write_failure_tests.rs` forces a real write failure by lowering `RLIMIT_FSIZE` (EFBIG after 4096 bytes) for all three paths. It fails without this change. - `CHANGELOG.adoc` and `docs/LEAN4_RUST_CORRESPONDENCE.adoc` said `proven` for the Idris2 undo/redo laws where the proof ledger says `proven-with-postulates`. Both now defer to the ledger. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- CHANGELOG.adoc | 9 +- EXPLAINME.adoc | 2 +- docs/LEAN4_RUST_CORRESPONDENCE.adoc | 10 +- impl/rust-cli/src/commands.rs | 33 +++- .../tests/redo_write_failure_tests.rs | 155 ++++++++++++++++++ 5 files changed, 198 insertions(+), 11 deletions(-) create mode 100644 impl/rust-cli/tests/redo_write_failure_tests.rs diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 10e7bad3..55957175 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -60,7 +60,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 memory with the redo entry. File content over `VSH_REDO_CAPTURE_LIMIT` bytes (shell or environment variable; default 10 MiB; `0` = no limit) is not kept, and redo of that entry says so. -- A `redo` that fails changes nothing and stays on the redo stack. `redo` of +- A `redo` that fails changes nothing and stays on the redo stack, including + when a file write fails part-way (the partial file is removed, or the + previous content put back). `redo` of `cp`, `mv` and `touch` no longer overwrites a path that reappeared after the undo. - Undo of a `>`/`>>` redirection failed whenever the sandbox root was not `/` @@ -70,8 +72,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `touch` creates its file with `create_new`, so it no longer writes through a dangling symbolic link. - Idris2 model: `redoUndoIdentity`, `undoRedoUndoIdentity`, - `redoUndoComposition` added to `Filesystem.Composition` (`proven` for the - model; the Rust link stays `tested-only`). + `redoUndoComposition` added to `Filesystem.Composition`. Their status is + the proof ledger's row for the undo/redo stack laws + (`proven-with-postulates`); the Rust link stays `tested-only`. - Known and *not* fixed here (tests exclude them, see `impl/rust-cli/tests/redo_roundtrip_tests.rs`): undo of `rm`/`rmdir` does not restore the mode; undo of `unset` does not restore the exported flag; diff --git a/EXPLAINME.adoc b/EXPLAINME.adoc index 13d4f5f0..43fab3ca 100644 --- a/EXPLAINME.adoc +++ b/EXPLAINME.adoc @@ -153,7 +153,7 @@ reproduce. Absence of a tool is an open obligation, not a passing leg | The Rust `undo`/`redo` round trip restores state for every operation type that reaches the history | tested-only | `cargo test --test redo_roundtrip_tests` (22 tests: 19 operation scenarios, 48 random sequences via `proptest`) and `test_redo_then_undo_correspondence` -| `impl/rust-cli/tests/redo_roundtrip_tests.rs`, `impl/rust-cli/tests/correspondence_tests.rs` +| `impl/rust-cli/tests/redo_roundtrip_tests.rs`, `impl/rust-cli/tests/redo_write_failure_tests.rs` (a redo whose write fails part-way, forced with `RLIMIT_FSIZE`), `impl/rust-cli/tests/correspondence_tests.rs` | Whole-sandbox comparison (bytes, modes, owners, link targets, one shell variable). Three scenarios assert only the redo laws because undo itself loses data there (mode on `rm`/`rmdir`, exported flag on `unset`); random sequences exclude symbolic links for the same reason; the `chown` scenario runs only as root. Not a proof and not a refinement of the Idris2 model | Idris2 ABI layout, encodings and round-trips match the C header diff --git a/docs/LEAN4_RUST_CORRESPONDENCE.adoc b/docs/LEAN4_RUST_CORRESPONDENCE.adoc index 994d0eb1..8a419365 100644 --- a/docs/LEAN4_RUST_CORRESPONDENCE.adoc +++ b/docs/LEAN4_RUST_CORRESPONDENCE.adoc @@ -504,8 +504,10 @@ given that the operation inverts at the current filesystem. witness (mirror of `+undoRedoComposition+`). |=== -Status: `proven` for the model (five operations: mkdir, rmdir, touch, rm, -write, on a flat path map). The link to the Rust code is `tested-only`. +Status: recorded in the proof ledger, `EXPLAINME.adoc#proof-ledger` (the row +for the undo/redo stack model); it is not restated here. The model has five +operations (mkdir, rmdir, touch, rm, write) on a flat path map. The link to +the Rust code is `tested-only`. The model re-applies the *same* operation value on redo, so the redone entry on the undo stack is identical to the original. The Rust `+redo+` did not do @@ -526,7 +528,9 @@ reverses the operation and kept on the in-memory redo entry (`+state::RedoData+`). File content over `+VSH_REDO_CAPTURE_LIMIT+` bytes (default 10 MiB, `+0+` for no limit) is not kept; redo of that entry fails with the reason. -* A redo that fails changes nothing and stays on the redo stack. +* A redo that fails changes nothing and stays on the redo stack. That +includes a file write that fails part-way: the partial file is removed, or the +previous content is put back. Tests (`tested-only`): `+impl/rust-cli/tests/redo_roundtrip_tests.rs+` (every operation type, random sequences, the capture limit) and diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index b3eacddf..0bdfea1d 100644 --- a/impl/rust-cli/src/commands.rs +++ b/impl/rust-cli/src/commands.rs @@ -265,7 +265,13 @@ fn apply_create_file(state: &ShellState, path: &str, content: &[u8]) -> Result error, + Err(restore_error) => error.context(format!( + "{} could not be restored after a failed redo: {}", + path, restore_error + )), + }); + } Ok(Operation::new(op_type, path.to_string(), None).with_undo_data(original)) } @@ -1461,7 +1482,11 @@ fn apply_append(state: &ShellState, path: &str, appended: &[u8]) -> Result +//! A redo whose file write fails part-way must leave the file as it found it +//! and stay on the redo stack. +//! +//! The write failure is real: the test lowers this process's file-size limit +//! (`RLIMIT_FSIZE`) so that the kernel refuses the write with `EFBIG` after +//! the first 4096 bytes. That limit is process-wide, which is why this test +//! lives in a binary of its own with a single test. +#![cfg(target_os = "linux")] + +use std::fs; +use std::path::Path; + +use tempfile::TempDir; +use vsh::commands; +use vsh::executable::ExecutableCommand; +use vsh::parser::parse_command; +use vsh::state::ShellState; + +const LIMIT: u64 = 4096; + +fn file_size_limit() -> libc::rlimit { + let mut limit = libc::rlimit { + rlim_cur: 0, + rlim_max: 0, + }; + // SAFETY: `limit` is a valid, writable `rlimit` for the duration of the call. + let rc = unsafe { libc::getrlimit(libc::RLIMIT_FSIZE, &mut limit) }; + assert_eq!(rc, 0, "getrlimit"); + limit +} + +fn set_file_size_limit(soft: libc::rlim_t) { + let limit = libc::rlimit { + rlim_cur: soft, + rlim_max: file_size_limit().rlim_max, + }; + // SAFETY: `limit` is a valid `rlimit`; only the soft limit changes, and it + // never exceeds the hard limit read just above. + let rc = unsafe { libc::setrlimit(libc::RLIMIT_FSIZE, &limit) }; + assert_eq!(rc, 0, "setrlimit"); +} + +fn run(state: &mut ShellState, line: &str) { + parse_command(line) + .unwrap_or_else(|e| panic!("parse {line:?}: {e}")) + .execute(state) + .unwrap_or_else(|e| panic!("execute {line:?}: {e}")); +} + +fn bytes(fill: u8, len: usize) -> Vec { + vec![fill; len] +} + +/// Redo with the limit lowered, expecting failure, then with it restored, +/// expecting success. Returns the error text of the failed attempt. +fn redo_under_limit_then_freely(state: &mut ShellState, unlimited: libc::rlim_t) -> String { + set_file_size_limit(LIMIT); + let failed = commands::redo(state, 1, false); + set_file_size_limit(unlimited); + let message = format!("{:#}", failed.expect_err("redo over the file-size limit")); + assert_eq!( + state.get_redo_stack().len(), + 1, + "the entry is kept: {message}" + ); + message +} + +#[test] +fn redo_that_fails_part_way_through_a_write_changes_nothing() { + // With the signal ignored, a write past the limit fails with EFBIG + // instead of terminating the process. + // SAFETY: installs the "ignore" disposition for one signal; no handler runs. + unsafe { libc::signal(libc::SIGXFSZ, libc::SIG_IGN) }; + let unlimited = file_size_limit().rlim_cur; + + let temp = TempDir::new().expect("tempdir"); + let root: &Path = temp.path(); + let mut state = ShellState::new(root.to_str().expect("utf8")).expect("state"); + let big = bytes(0xb1, 2 * LIMIT as usize); + // External commands run in the process's directory, so sources are + // outside the sandbox and named absolutely. + let sources = TempDir::new().expect("tempdir"); + let big_src = sources.path().join("big"); + fs::write(&big_src, &big).expect("write"); + + // -- a file a redirection created -------------------------------------- + let created = root.join("created"); + run( + &mut state, + &format!("/bin/cat {} > created", big_src.display()), + ); + assert_eq!(fs::read(&created).expect("read"), big); + commands::undo(&mut state, 1, false).expect("undo"); + assert!(!created.exists()); + + redo_under_limit_then_freely(&mut state, unlimited); + assert!(!created.exists(), "no partial file is left behind"); + + commands::redo(&mut state, 1, false).expect("redo once the limit is lifted"); + assert_eq!(fs::read(&created).expect("read"), big); + + // -- a file a redirection replaced -------------------------------------- + let replaced = root.join("replaced"); + let before = bytes(0xa0, 1000); + fs::write(&replaced, &before).expect("write"); + run( + &mut state, + &format!("/bin/cat {} > replaced", big_src.display()), + ); + assert_eq!(fs::read(&replaced).expect("read"), big); + commands::undo(&mut state, 1, false).expect("undo"); + assert_eq!(fs::read(&replaced).expect("read"), before); + + redo_under_limit_then_freely(&mut state, unlimited); + assert_eq!( + fs::read(&replaced).expect("read"), + before, + "the previous content is put back" + ); + + commands::redo(&mut state, 1, false).expect("redo once the limit is lifted"); + assert_eq!(fs::read(&replaced).expect("read"), big); + commands::undo(&mut state, 1, false).expect("undo after the retried redo"); + assert_eq!( + fs::read(&replaced).expect("read"), + before, + "the retried redo captured the right undo data" + ); + commands::redo(&mut state, 1, false).expect("redo"); + + // -- a file a redirection appended to ----------------------------------- + let appended = root.join("appended"); + fs::write(&appended, &before).expect("write"); + run( + &mut state, + &format!("/bin/cat {} >> appended", big_src.display()), + ); + let after: Vec = before.iter().chain(big.iter()).copied().collect(); + assert_eq!(fs::read(&appended).expect("read"), after); + commands::undo(&mut state, 1, false).expect("undo"); + assert_eq!(fs::read(&appended).expect("read"), before); + + redo_under_limit_then_freely(&mut state, unlimited); + assert_eq!( + fs::read(&appended).expect("read"), + before, + "the partial append is cut off" + ); + + commands::redo(&mut state, 1, false).expect("redo once the limit is lifted"); + assert_eq!(fs::read(&appended).expect("read"), after); +} From 9be333f5bc263f82e6e12c994e485e80df324571 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 05:38:29 +0000 Subject: [PATCH 06/15] fix(redo): restoring after a failed replace no longer needs to rewrite the whole file Follow-up to ad51965, from the review thread on #231. `apply_replace_content` truncated the file and, when the write failed, wrote the whole previous content back. That restore could fail for the same reason the write did, leaving the file damaged for a retry to capture as undo data. It now writes the new content over the old without truncating and cuts the file to length afterwards. If the write fails, only the bytes before the file position can have changed, so only those are put back and the file is cut to its previous length. That never grows the file, so it does not need the space or quota the failed write ran out of. `apply_create_file` and `apply_append` no longer discard a cleanup error: if the partial file cannot be removed, or the partial append cannot be cut off, the error says so. Test: `redo_write_failure_tests.rs` gains a case whose previous content is itself larger than the file-size limit. It fails on ad51965 ("could not be restored ... File too large") and passes now. Still not guaranteed: an overwrite in place can need new space on a copy-on-write filesystem, and an I/O error can make any write fail. The error then says the file may be damaged. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- impl/rust-cli/src/commands.rs | 49 +++++++++++++------ .../tests/redo_write_failure_tests.rs | 30 ++++++++++++ 2 files changed, 64 insertions(+), 15 deletions(-) diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index 0bdfea1d..460a27d3 100644 --- a/impl/rust-cli/src/commands.rs +++ b/impl/rust-cli/src/commands.rs @@ -269,8 +269,14 @@ fn apply_create_file(state: &ShellState, path: &str, content: &[u8]) -> Result error, + Err(cleanup_error) => error.context(format!( + "the partial file {} could not be removed: {}", + path, cleanup_error + )), + }); } Ok(Operation::new( @@ -1439,25 +1445,32 @@ fn apply_replace_content( let original = crate::fs_pure::read_to_end(&full_path) .with_context(|| format!("Failed to read {} before redo", path))?; - // Open without `create`, so the write lands in the file that was read. + // Open without `create`, so the write lands in the file that was read, + // and without `truncate`: the new content is written over the old and the + // file is cut to length afterwards. The old bytes are then still in place + // if the write fails, and putting them back never grows the file, so it + // does not need the space or quota the failed write ran out of. + use std::io::{Seek, SeekFrom}; let mut file = fs::OpenOptions::new() .write(true) - .truncate(true) .open(&full_path) .with_context(|| format!("Redo {} failed", op_type))?; - if let Err(e) = file.write_all(content) { - // Put back what was there, so a failed redo leaves the file as it - // found it and a retry captures the right undo data. - use std::io::{Seek, SeekFrom}; - let restored = file - .set_len(0) - .and_then(|()| file.seek(SeekFrom::Start(0))) - .and_then(|_| file.write_all(&original)); + let written = file + .write_all(content) + .and_then(|()| file.set_len(content.len() as u64)); + if let Err(e) = written { + // Only the bytes before the current position can have changed. + let restored = file.stream_position().and_then(|position| { + let changed = original.len().min(position as usize); + file.seek(SeekFrom::Start(0))?; + file.write_all(&original[..changed])?; + file.set_len(original.len() as u64) + }); let error = anyhow::Error::new(e).context(format!("Redo {} failed", op_type)); return Err(match restored { Ok(()) => error, Err(restore_error) => error.context(format!( - "{} could not be restored after a failed redo: {}", + "{} could not be restored after a failed redo and may be damaged: {}", path, restore_error )), }); @@ -1484,8 +1497,14 @@ fn apply_append(state: &ShellState, path: &str, appended: &[u8]) -> Result error, + Err(cleanup_error) => error.context(format!( + "the partial append to {} could not be cut off: {}", + path, cleanup_error + )), + }); } Ok( diff --git a/impl/rust-cli/tests/redo_write_failure_tests.rs b/impl/rust-cli/tests/redo_write_failure_tests.rs index fa4808ea..1d9dcb5e 100644 --- a/impl/rust-cli/tests/redo_write_failure_tests.rs +++ b/impl/rust-cli/tests/redo_write_failure_tests.rs @@ -131,6 +131,36 @@ fn redo_that_fails_part_way_through_a_write_changes_nothing() { ); commands::redo(&mut state, 1, false).expect("redo"); + // -- a replaced file whose previous content is itself over the limit ---- + // Putting the previous content back must not depend on being able to + // write that much again: only the overwritten part is rewritten. + let large = root.join("large"); + let large_before = bytes(0xa1, 3 * LIMIT as usize); + fs::write(&large, &large_before).expect("write"); + run( + &mut state, + &format!("/bin/cat {} > large", big_src.display()), + ); + assert_eq!(fs::read(&large).expect("read"), big); + commands::undo(&mut state, 1, false).expect("undo"); + assert_eq!(fs::read(&large).expect("read"), large_before); + + let message = redo_under_limit_then_freely(&mut state, unlimited); + assert!( + fs::read(&large).expect("read") == large_before, + "the previous content is put back even though it is over the limit: {message}" + ); + assert!( + !message.contains("could not be restored"), + "restoring did not fail: {message}" + ); + + commands::redo(&mut state, 1, false).expect("redo once the limit is lifted"); + assert_eq!(fs::read(&large).expect("read"), big); + commands::undo(&mut state, 1, false).expect("undo after the retried redo"); + assert!(fs::read(&large).expect("read") == large_before); + commands::redo(&mut state, 1, false).expect("redo"); + // -- a file a redirection appended to ----------------------------------- let appended = root.join("appended"); fs::write(&appended, &before).expect("write"); From 94be0178e2f53c3410ca68d2e4fad59b949719cd Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 05:41:13 +0000 Subject: [PATCH 07/15] docs(redo): qualify the "failed redo changes nothing" guarantee Review follow-up on #231. The `redo` documentation, the changelog and the correspondence document said a failed redo changes nothing, without qualification. That is not true in one case: if a file write fails part-way and the clean-up fails as well (an I/O error, or no space on a copy-on-write filesystem), the file is left changed. The error says so, and a retry would record the damaged content as undo data. All three places now say what is guaranteed (nothing is recorded; the entry stays on the redo stack; the filesystem is left as found unless the clean-up itself fails) and that the double failure is not tested. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- CHANGELOG.adoc | 10 +++++++--- docs/LEAN4_RUST_CORRESPONDENCE.adoc | 11 ++++++++--- impl/rust-cli/src/commands.rs | 15 +++++++++++---- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 55957175..60461f17 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -60,9 +60,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 memory with the redo entry. File content over `VSH_REDO_CAPTURE_LIMIT` bytes (shell or environment variable; default 10 MiB; `0` = no limit) is not kept, and redo of that entry says so. -- A `redo` that fails changes nothing and stays on the redo stack, including - when a file write fails part-way (the partial file is removed, or the - previous content put back). `redo` of +- A `redo` that fails records nothing and stays on the redo stack. It also + leaves the filesystem as it found it, including when a file write fails + part-way: the partial file is removed, the overwritten bytes are put back, + or the partial append is cut off. The one exception is when that clean-up + fails too (an I/O error, or no space on a copy-on-write filesystem): the + file is then left changed, the error says so, and a retry would record the + damaged content as undo data. `redo` of `cp`, `mv` and `touch` no longer overwrites a path that reappeared after the undo. - Undo of a `>`/`>>` redirection failed whenever the sandbox root was not `/` diff --git a/docs/LEAN4_RUST_CORRESPONDENCE.adoc b/docs/LEAN4_RUST_CORRESPONDENCE.adoc index 8a419365..df0494b9 100644 --- a/docs/LEAN4_RUST_CORRESPONDENCE.adoc +++ b/docs/LEAN4_RUST_CORRESPONDENCE.adoc @@ -528,9 +528,14 @@ reverses the operation and kept on the in-memory redo entry (`+state::RedoData+`). File content over `+VSH_REDO_CAPTURE_LIMIT+` bytes (default 10 MiB, `+0+` for no limit) is not kept; redo of that entry fails with the reason. -* A redo that fails changes nothing and stays on the redo stack. That -includes a file write that fails part-way: the partial file is removed, or the -previous content is put back. +* A redo that fails records nothing and stays on the redo stack. It also +leaves the filesystem as it found it, including when a file write fails +part-way: the partial file is removed, the overwritten bytes are put back, or +the partial append is cut off. The one exception is when that clean-up fails +too (an I/O error, or no space on a copy-on-write filesystem): the file is +then left changed, the error says so, and a retry would record the damaged +content as undo data. That double failure is not tested; it needs fault +injection below the filesystem calls. Tests (`tested-only`): `+impl/rust-cli/tests/redo_roundtrip_tests.rs+` (every operation type, random sequences, the capture limit) and diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index 460a27d3..4e05d8e6 100644 --- a/impl/rust-cli/src/commands.rs +++ b/impl/rust-cli/src/commands.rs @@ -1155,8 +1155,15 @@ pub fn undo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { /// # Errors /// Returns error if re-applying an operation fails, for example because a /// precondition no longer holds (the destination of a `cp` exists again). -/// Nothing is changed or recorded for that operation, and it stays on the -/// redo stack. Redoing with nothing to redo is not an error. +/// Nothing is recorded for that operation, and it stays on the redo stack. +/// Redoing with nothing to redo is not an error. +/// +/// A failed redo leaves the filesystem as it found it, with one exception. +/// If a file write fails part-way, redo removes the partial file, puts the +/// overwritten bytes back or cuts off the partial append. If that clean-up +/// fails as well (an I/O error, or no space on a copy-on-write filesystem), +/// the file is left changed and the error says so. Retrying the redo would +/// then record the damaged content as undo data, so repair the file first. /// /// # Examples /// ```no_run @@ -1183,8 +1190,8 @@ pub fn redo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { let new_op = match reapply(state, &op) { Ok(new_op) => new_op, Err(e) => { - // Nothing was re-applied: keep the entry so redo can be - // retried once whatever blocked it is resolved. + // The operation was not re-applied: keep the entry so redo + // can be retried once whatever blocked it is resolved. state.redo_stack.push_front(op); return Err(e); } From daf6f51a5fd091df1a646d29c8ef43159fe4acba Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 05:43:37 +0000 Subject: [PATCH 08/15] docs(redo): qualify the comment on restoring after a failed replace Review follow-up on #231. The comment in `apply_replace_content` said that putting the overwritten bytes back "does not need the space or quota the failed write ran out of". That holds on a filesystem that overwrites in place. On a copy-on-write filesystem an overwrite can need new space, and an I/O error can fail any write. The comment now says so and points at the error reported when the restore fails. Comment only; no behaviour change. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- impl/rust-cli/src/commands.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index 4e05d8e6..f4b2e95b 100644 --- a/impl/rust-cli/src/commands.rs +++ b/impl/rust-cli/src/commands.rs @@ -1455,8 +1455,12 @@ fn apply_replace_content( // Open without `create`, so the write lands in the file that was read, // and without `truncate`: the new content is written over the old and the // file is cut to length afterwards. The old bytes are then still in place - // if the write fails, and putting them back never grows the file, so it - // does not need the space or quota the failed write ran out of. + // if the write fails, and putting them back never grows the file. On a + // filesystem that overwrites in place, that needs none of the space or + // quota the failed write ran out of. It is not a guarantee: a + // copy-on-write filesystem can need new space even for an overwrite, and + // an I/O error can fail any write, which is why a failed restore is + // reported below. use std::io::{Seek, SeekFrom}; let mut file = fs::OpenOptions::new() .write(true) From 409d278cdb685c9c68eca3b85b15595075914c13 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 14:07:11 +0000 Subject: [PATCH 09/15] fix(redo): refuse uncaptured content, clean up a failed copy, restore the export Review findings on #231 (Codex). Three reproduced and are fixed here; one did not reproduce and gets tests only. - Redo recreated a file empty when undo had not been able to read its content (`capture_redo_data` returned `None`, which redo treated like a `touch`). Undo now records `RedoData::NotCaptured` with the reason and warns; redo of that entry fails and creates nothing. Redo of a `CreateFile` now requires captured content. - A `cp` whose copy failed part-way left the partial destination behind (4096 bytes in the probe), which blocked every retry with EEXIST. `cp` and its redo now remove it and report a clean-up failure. - Redo of an assignment restored the value but not the export: `X=1; export X; undo; redo` left `X` unexported. The redo data for `SetVariable` now carries the exported flag. - Not reproduced: `cp`/`mv` redo onto a dangling symbolic link. Both were already refused, because `verification::verify_copy_file` and `verify_move` check the destination with `lstat`. No code change; two tests now pin it, one with a link that points outside the sandbox. Tests: three new round-trip tests, an "export after an assignment" scenario, `export X` in the random sequences, a `cp` case in `redo_write_failure_tests.rs`, and a unit test that undo records unreadable content as `NotCaptured`. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- CHANGELOG.adoc | 5 + impl/rust-cli/src/commands.rs | 130 ++++++++++++++---- impl/rust-cli/src/state.rs | 11 +- impl/rust-cli/tests/redo_roundtrip_tests.rs | 95 +++++++++++++ .../tests/redo_write_failure_tests.rs | 14 ++ 5 files changed, 224 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 60461f17..d7d0eb32 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -69,6 +69,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 damaged content as undo data. `redo` of `cp`, `mv` and `touch` no longer overwrites a path that reappeared after the undo. +- `redo` refuses to recreate a file whose content undo could not read + (recorded as not captured, with the reason), where it used to recreate it + empty. A `cp` whose copy fails part-way removes the partial destination, for + the command and for its redo. Redo of an assignment restores the exported + flag as well as the value. - Undo of a `>`/`>>` redirection failed whenever the sandbox root was not `/` (the default root is the current directory): the history held an absolute path that undo resolved under the root a second time. History paths for diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index f4b2e95b..7fbe90e8 100644 --- a/impl/rust-cli/src/commands.rs +++ b/impl/rust-cli/src/commands.rs @@ -431,7 +431,21 @@ fn apply_cp(state: &ShellState, src: &str, dst: &str) -> Result { } // Execute operation - fs::copy(&src_path, &dst_path).context("cp failed")?; + if let Err(e) = fs::copy(&src_path, &dst_path) { + // No node existed at the destination before (`verify_copy_file` + // checks with `lstat`, so not even a dangling symbolic link), so + // whatever part of the copy landed is ours to remove; left behind, it + // would block a retry. + let error = anyhow::Error::new(e).context("cp failed"); + return Err(match fs::remove_file(&dst_path) { + Ok(()) => error, + Err(cleanup_error) if cleanup_error.kind() == std::io::ErrorKind::NotFound => error, + Err(cleanup_error) => error.context(format!( + "the partial copy {} could not be removed: {}", + dst, cleanup_error + )), + }); + } // path = dst, undo_data = src path for reference // Undo = delete the destination file @@ -1108,6 +1122,14 @@ pub fn undo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { REDO_CAPTURE_LIMIT_VAR ); } + if let Some(RedoData::NotCaptured(reason)) = &redo_data { + eprintln!( + "{} {} could not be read ({}): this undo cannot be redone", + "Warning:".bright_yellow(), + op.path, + reason + ); + } if let Some(entry) = state.redo_stack.front_mut() { if entry.id == orig_id { entry.redo_data = redo_data; @@ -1262,27 +1284,33 @@ fn capture_redo_data(state: &ShellState, op: &Operation) -> Option { }; match op.op_type { OperationType::CreateFile | OperationType::FileTruncated | OperationType::WriteFile => { - let size = fs::metadata(&path).ok()?.len(); - if let Some(skipped) = over_limit(size) { - return Some(skipped); - } - crate::fs_pure::read_to_end(&path) - .ok() - .map(RedoData::Content) + // A file that cannot be read is recorded as such, never as empty: + // redo must refuse it, not recreate the file without its content. + let read = || -> std::io::Result { + let size = fs::metadata(&path)?.len(); + if let Some(skipped) = over_limit(size) { + return Ok(skipped); + } + crate::fs_pure::read_to_end(&path).map(RedoData::Content) + }; + Some(read().unwrap_or_else(|e| RedoData::NotCaptured(e.to_string()))) } OperationType::FileAppended => { use std::io::{Read, Seek, SeekFrom}; let original_size = u64::from_le_bytes(op.undo_data.as_ref()?.get(..8)?.try_into().ok()?); - let size = fs::metadata(&path).ok()?.len(); - if let Some(skipped) = over_limit(size.saturating_sub(original_size)) { - return Some(skipped); - } - let mut file = crate::fs_pure::open_pure(&path).ok()?; - file.seek(SeekFrom::Start(original_size)).ok()?; - let mut appended = Vec::new(); - file.read_to_end(&mut appended).ok()?; - Some(RedoData::Appended(appended)) + let read = || -> std::io::Result { + let size = fs::metadata(&path)?.len(); + if let Some(skipped) = over_limit(size.saturating_sub(original_size)) { + return Ok(skipped); + } + let mut file = crate::fs_pure::open_pure(&path)?; + file.seek(SeekFrom::Start(original_size))?; + let mut appended = Vec::new(); + file.read_to_end(&mut appended)?; + Ok(RedoData::Appended(appended)) + }; + Some(read().unwrap_or_else(|e| RedoData::NotCaptured(e.to_string()))) } OperationType::Chmod => { #[cfg(unix)] @@ -1307,11 +1335,16 @@ fn capture_redo_data(state: &ShellState, op: &Operation) -> Option { #[cfg(not(unix))] None } - OperationType::SetVariable => state - .variables - .get(&op.path) - .cloned() - .map(RedoData::Variable), + OperationType::SetVariable => { + state + .variables + .get(&op.path) + .cloned() + .map(|value| RedoData::Variable { + value, + exported: state.is_exported(&op.path), + }) + } OperationType::Mkdir | OperationType::Rmdir | OperationType::DeleteFile @@ -1332,7 +1365,7 @@ fn capture_redo_data(state: &ShellState, op: &Operation) -> Option { /// the current state. Nothing is recorded here. fn reapply(state: &mut ShellState, op: &Operation) -> Result { // What undo saved for this entry, or an error naming what is missing. - let missing = || match op.redo_data { + let missing = || match &op.redo_data { Some(RedoData::OverLimit { size, limit }) => anyhow::anyhow!( "Cannot redo {} {}: its {} bytes were over the {} byte limit when it was undone \ (set {} to raise it, 0 for no limit)", @@ -1342,6 +1375,12 @@ fn reapply(state: &mut ShellState, op: &Operation) -> Result { limit, REDO_CAPTURE_LIMIT_VAR ), + Some(RedoData::NotCaptured(reason)) => anyhow::anyhow!( + "Cannot redo {} {}: its content could not be read when it was undone ({})", + op.op_type, + op.path, + reason + ), _ => anyhow::anyhow!( "Cannot redo {} {}: the state to restore was not captured when it was undone", op.op_type, @@ -1361,10 +1400,10 @@ fn reapply(state: &mut ShellState, op: &Operation) -> Result { OperationType::Rmdir => apply_rmdir(state, &op.path), OperationType::CreateFile => { // Empty for `touch`; the redirected output for a file `>` created. - let content: &[u8] = match &op.redo_data { - Some(RedoData::Content(content)) => content, - Some(RedoData::OverLimit { .. }) => return Err(missing()), - _ => &[], + // Anything but captured content is refused: recreating the file + // empty would silently lose what the redirection wrote. + let Some(RedoData::Content(content)) = &op.redo_data else { + return Err(missing()); }; apply_create_file(state, &op.path, content) } @@ -1397,10 +1436,15 @@ fn reapply(state: &mut ShellState, op: &Operation) -> Result { } OperationType::Unlink => apply_unlink(state, &op.path), OperationType::SetVariable => { - let Some(RedoData::Variable(value)) = &op.redo_data else { + let Some(RedoData::Variable { value, exported }) = &op.redo_data else { return Err(missing()); }; - Ok(state.apply_set_variable(op.path.clone(), value.clone())) + let new_op = state.apply_set_variable(op.path.clone(), value.clone()); + // Undo of an assignment also drops the export; put it back. + if *exported { + state.export_variable(op.path.clone()); + } + Ok(new_op) } OperationType::UnsetVariable => state .apply_unset_variable(&op.path) @@ -3034,6 +3078,34 @@ mod tests { assert!(parse_chmod_mode("xyz", 0o100644).is_err()); } + /// Undo records content it cannot read as `NotCaptured`, never as empty. + /// (A directory in the file's place stands in for an unreadable file, + /// which root cannot produce with permissions alone.) + #[test] + fn test_capture_redo_data_records_unreadable_content() { + let temp = tempfile::TempDir::new().unwrap(); + let state = ShellState::new(temp.path().to_str().unwrap()).unwrap(); + fs::create_dir(temp.path().join("f")).unwrap(); + + for op_type in [OperationType::CreateFile, OperationType::FileTruncated] { + let op = Operation::new(op_type, "f".to_string(), None); + assert!( + matches!( + capture_redo_data(&state, &op), + Some(RedoData::NotCaptured(_)) + ), + "{op_type}" + ); + } + + let appended = Operation::new(OperationType::FileAppended, "f".to_string(), None) + .with_undo_data(0u64.to_le_bytes().to_vec()); + assert!(matches!( + capture_redo_data(&state, &appended), + Some(RedoData::NotCaptured(_)) + )); + } + #[test] fn test_parse_redo_capture_limit() { // Unset or unreadable: the default. diff --git a/impl/rust-cli/src/state.rs b/impl/rust-cli/src/state.rs index 2a517b58..0d6bbd4b 100644 --- a/impl/rust-cli/src/state.rs +++ b/impl/rust-cli/src/state.rs @@ -178,11 +178,18 @@ pub enum RedoData { Mode(u32), /// The ownership undo replaced (`Chown`). Owner { uid: u32, gid: u32 }, - /// The value undo replaced (`SetVariable`). - Variable(VariableValue), + /// The value undo replaced, and whether the variable was exported + /// (`SetVariable`). + Variable { + value: VariableValue, + exported: bool, + }, /// The content was larger than the capture limit, so undo did not keep it /// and this entry cannot be redone. OverLimit { size: u64, limit: u64 }, + /// The content could not be read when the operation was undone (the + /// reason is kept), so this entry cannot be redone. + NotCaptured(String), } /// A single filesystem operation with complete undo/redo information. diff --git a/impl/rust-cli/tests/redo_roundtrip_tests.rs b/impl/rust-cli/tests/redo_roundtrip_tests.rs index ef179d71..c82a1740 100644 --- a/impl/rust-cli/tests/redo_roundtrip_tests.rs +++ b/impl/rust-cli/tests/redo_roundtrip_tests.rs @@ -468,6 +468,89 @@ fn touch_does_not_write_through_a_dangling_symlink() { assert!(state.history.is_empty()); } +// --------------------------------------------------------------------------- +// Dangling symbolic links at a destination; content that was not captured +// --------------------------------------------------------------------------- + +/// A dangling symbolic link at the destination does not "exist" to +/// `Path::exists`, and `cp` onto it would write through the link to wherever +/// it points, which may be outside the sandbox. The runtime precondition +/// check (`verification::verify_copy_file`, which uses `lstat`) treats it as +/// occupied; this pins that for `cp` and for its redo. +#[cfg(unix)] +#[test] +fn cp_and_its_redo_refuse_a_dangling_symlink_destination() { + let (temp, mut state) = setup(); + let outside = TempDir::new().expect("tempdir"); + let escape = outside.path().join("escape"); + let dst = temp.path().join("dst"); + fs::write(temp.path().join("src"), b"payload").unwrap(); + + commands::cp(&mut state, "src", "dst", false).unwrap(); + undo(&mut state); + + // Planted between undo and redo: dst -> a path outside the sandbox. + std::os::unix::fs::symlink(&escape, &dst).unwrap(); + + assert!(commands::redo(&mut state, 1, false).is_err()); + assert!(!escape.exists(), "redo did not write through the link"); + assert_eq!(fs::read_link(&dst).unwrap(), escape, "the link is intact"); + assert_eq!(state.get_redo_stack().len(), 1, "the entry is kept"); + + // The command itself refuses too. + assert!(commands::cp(&mut state, "src", "dst", false).is_err()); + assert!(!escape.exists()); +} + +/// `mv` onto a dangling symbolic link would replace the link; +/// `verification::verify_move` refuses it, for the command and for its redo. +#[cfg(unix)] +#[test] +fn mv_and_its_redo_refuse_a_dangling_symlink_destination() { + let (temp, mut state) = setup(); + let src = temp.path().join("src"); + let dst = temp.path().join("dst"); + fs::write(&src, b"payload").unwrap(); + + commands::mv(&mut state, "src", "dst", false).unwrap(); + undo(&mut state); + + std::os::unix::fs::symlink("nowhere", &dst).unwrap(); + + assert!(commands::redo(&mut state, 1, false).is_err()); + assert_eq!(fs::read_link(&dst).unwrap(), Path::new("nowhere")); + assert_eq!(read(&src), b"payload", "the source was not moved"); + + assert!(commands::mv(&mut state, "src", "dst", false).is_err()); + assert_eq!(fs::read_link(&dst).unwrap(), Path::new("nowhere")); +} + +/// If undo could not read a file's content, redo must refuse to recreate the +/// file: recreating it empty would silently lose what was in it. +#[test] +fn redo_refuses_a_file_whose_content_was_not_captured() { + use vsh::state::RedoData; + + let (temp, mut state) = setup(); + let f = temp.path().join("f"); + + commands::touch(&mut state, "f", false).unwrap(); + fs::write(&f, b"written after the touch").unwrap(); + undo(&mut state); + assert!(!f.exists()); + + // What undo records when the read fails (see the unit test of + // `capture_redo_data` for the capture side). + let entry = state.redo_stack.front_mut().expect("redo entry"); + assert!(matches!(entry.redo_data, Some(RedoData::Content(_)))); + entry.redo_data = Some(RedoData::NotCaptured("Permission denied".to_string())); + + let err = commands::redo(&mut state, 1, false).expect_err("redo must refuse"); + assert!(err.to_string().contains("could not be read"), "{err}"); + assert!(!f.exists(), "nothing was recreated"); + assert_eq!(state.get_redo_stack().len(), 1, "the entry is kept"); +} + // --------------------------------------------------------------------------- // Mixed sequence // --------------------------------------------------------------------------- @@ -704,6 +787,17 @@ mod laws { s("assigning a new variable", |_, _| {}, "X=1"), s("reassigning a variable", |_, s| run(s, "X=1"), "X=2"), s("unset", |_, s| run(s, "X=1"), "unset X"), + Scenario { + // `export` is not a history operation, so undo reverses the + // assignment before it and does not return to the unexported + // state. Redo must bring back the value and the export. + undo_is_exact: false, + ..s( + "export after an assignment", + |_, s| run(s, "X=1"), + "export X", + ) + }, Scenario { // Known undo defect: unset does not record the exported flag. undo_is_exact: false, @@ -865,6 +959,7 @@ mod laws { (0..1000u32, name).prop_map(|(v, n)| format!("/bin/echo a{v} >> {n}")), (0..5u32).prop_map(|v| format!("X={v}")), Just("unset X".to_string()), + Just("export X".to_string()), ] } diff --git a/impl/rust-cli/tests/redo_write_failure_tests.rs b/impl/rust-cli/tests/redo_write_failure_tests.rs index 1d9dcb5e..ff6cc39d 100644 --- a/impl/rust-cli/tests/redo_write_failure_tests.rs +++ b/impl/rust-cli/tests/redo_write_failure_tests.rs @@ -161,6 +161,20 @@ fn redo_that_fails_part_way_through_a_write_changes_nothing() { assert!(fs::read(&large).expect("read") == large_before); commands::redo(&mut state, 1, false).expect("redo"); + // -- a copy --------------------------------------------------------------- + // `cp` works inside the sandbox, so its source lives there. + let copy = root.join("copy"); + fs::write(root.join("copy_src"), &big).expect("write"); + commands::cp(&mut state, "copy_src", "copy", false).expect("cp"); + commands::undo(&mut state, 1, false).expect("undo"); + assert!(!copy.exists()); + + redo_under_limit_then_freely(&mut state, unlimited); + assert!(!copy.exists(), "no partial copy is left behind"); + + commands::redo(&mut state, 1, false).expect("redo once the limit is lifted"); + assert_eq!(fs::read(©).expect("read"), big); + // -- a file a redirection appended to ----------------------------------- let appended = root.join("appended"); fs::write(&appended, &before).expect("write"); From ef87982f9260e43800d12c670fa40a83dd8141ec Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 14:16:22 +0000 Subject: [PATCH 10/15] fix(cp, redo): create the copy exclusively; restore the unexported state too Second round of review findings on #231 (Codex), both valid. - `cp` checked that the destination was absent and then called `fs::copy`, which overwrites. A file created by another process in between was overwritten, and the new clean-up after a failed copy could then delete it. `apply_cp` now creates the destination with `create_new`, copies into it and sets the source's permissions. It never writes over a file it did not create, the clean-up can only remove what this call created, and the copy is never created with a wider mode than the source. - Redo of an assignment restored `exported: true` but not `false`: with `X=1; undo`, then an export of `X` made outside the history, redo left `X` exported. Redo now restores the export state undo found, either way. Not done here: recording the overwritten export state in the undo record, so that the undo after such a redo reverses it. That needs the exported flag in the persisted undo data, which is the existing gap listed in the PR description (undo of `unset` loses the flag for the same reason). Tests: `cp_and_its_redo_keep_the_source_mode` and `assignment_redo_restores_not_exported` (fails on the previous commit). The race itself cannot be forced in a test. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- impl/rust-cli/src/commands.rs | 32 ++++++++++---- impl/rust-cli/tests/redo_roundtrip_tests.rs | 48 +++++++++++++++++++++ 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index 7fbe90e8..43f39dbe 100644 --- a/impl/rust-cli/src/commands.rs +++ b/impl/rust-cli/src/commands.rs @@ -430,16 +430,28 @@ fn apply_cp(state: &ShellState, src: &str, dst: &str) -> Result { anyhow::bail!("Parent of destination does not exist (ENOENT)"); } - // Execute operation - if let Err(e) = fs::copy(&src_path, &dst_path) { - // No node existed at the destination before (`verify_copy_file` - // checks with `lstat`, so not even a dangling symbolic link), so - // whatever part of the copy landed is ours to remove; left behind, it - // would block a retry. + // Execute operation. The destination is created exclusively, so this call + // owns it: a file that appeared since the checks above is never written + // over, and the clean-up below can only remove what was created here. + let mut source = fs::File::open(&src_path).context("cp failed")?; + let permissions = source.metadata().context("cp failed")?.permissions(); + let mut create = fs::OpenOptions::new(); + create.write(true).create_new(true); + #[cfg(unix)] + { + // Never wider than the source, even while the copy is in progress. + use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; + create.mode(permissions.mode() & 0o7777); + } + let mut destination = create.open(&dst_path).context("cp failed")?; + let copied = std::io::copy(&mut source, &mut destination) + .and_then(|_| destination.set_permissions(permissions)); + if let Err(e) = copied { + // Whatever part of the copy landed would block a retry. + drop(destination); let error = anyhow::Error::new(e).context("cp failed"); return Err(match fs::remove_file(&dst_path) { Ok(()) => error, - Err(cleanup_error) if cleanup_error.kind() == std::io::ErrorKind::NotFound => error, Err(cleanup_error) => error.context(format!( "the partial copy {} could not be removed: {}", dst, cleanup_error @@ -1440,9 +1452,13 @@ fn reapply(state: &mut ShellState, op: &Operation) -> Result { return Err(missing()); }; let new_op = state.apply_set_variable(op.path.clone(), value.clone()); - // Undo of an assignment also drops the export; put it back. + // Put the export state back as undo found it, whichever way that + // was: undo of an assignment drops the export, and something + // outside the history may have set it since. if *exported { state.export_variable(op.path.clone()); + } else { + state.exported_vars.remove(&op.path); } Ok(new_op) } diff --git a/impl/rust-cli/tests/redo_roundtrip_tests.rs b/impl/rust-cli/tests/redo_roundtrip_tests.rs index c82a1740..e8867884 100644 --- a/impl/rust-cli/tests/redo_roundtrip_tests.rs +++ b/impl/rust-cli/tests/redo_roundtrip_tests.rs @@ -551,6 +551,54 @@ fn redo_refuses_a_file_whose_content_was_not_captured() { assert_eq!(state.get_redo_stack().len(), 1, "the entry is kept"); } +/// `cp` gives the copy the source's mode, for the command and for its redo, +/// and the copy is never created with a wider mode than the source. +#[cfg(unix)] +#[test] +fn cp_and_its_redo_keep_the_source_mode() { + let (temp, mut state) = setup(); + let src = temp.path().join("secret"); + let dst = temp.path().join("copy"); + fs::write(&src, BINARY).unwrap(); + set_mode(&src, 0o600); + + commands::cp(&mut state, "secret", "copy", false).unwrap(); + assert_eq!(mode_of(&dst), 0o600); + assert_eq!(read(&dst), BINARY); + + undo(&mut state); + redo(&mut state); + assert_eq!(mode_of(&dst), 0o600); + assert_eq!(read(&dst), BINARY); +} + +/// Redo of an assignment restores the export state undo found, in both +/// directions: here the variable was not exported, and something outside the +/// history exported it between undo and redo. +#[test] +fn assignment_redo_restores_not_exported() { + use vsh::executable::ExecutableCommand; + use vsh::parser::parse_command; + + let (_temp, mut state) = setup(); + parse_command("X=1").unwrap().execute(&mut state).unwrap(); + assert!(!state.is_exported("X")); + + undo(&mut state); + assert_eq!(state.get_variable("X"), None); + + // Outside the history: these calls record nothing. + state.set_variable("X", "other"); + state.export_variable("X"); + + redo(&mut state); + assert_eq!(state.get_variable("X"), Some("1")); + assert!( + !state.is_exported("X"), + "redo restores the unexported state" + ); +} + // --------------------------------------------------------------------------- // Mixed sequence // --------------------------------------------------------------------------- From 91786fa9afc5d8f14fcfdb5c4f530b26887b61d0 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 14:40:32 +0000 Subject: [PATCH 11/15] fix(rm): do not delete a file whose content cannot be kept for undo Review finding on #231 (Codex), valid. `apply_rm` read the file with `unwrap_or_default()`, so a failed read became empty undo data and the file was deleted anyway. The operation was then recorded as reversible, and undo brought the file back empty. This affected the `rm` command and, through the shared path, redo of `rm`. `apply_rm` now fails when the read fails: nothing is deleted, nothing is recorded, a redo entry stays on the stack, and the error points to `obliterate` for irreversible deletion. This keeps the reversible path (RMR) from silently behaving like the irreversible one (RMO). Test: `tests/rm_unreadable_tests.rs`. Root can read any file, so as root the test switches the effective user to `nobody` around the calls; the directory stays writable, so only the read can fail. On the previous commit the test fails because the file is deleted. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- CHANGELOG.adoc | 4 + impl/rust-cli/src/commands.rs | 12 ++- impl/rust-cli/tests/rm_unreadable_tests.rs | 100 +++++++++++++++++++++ 3 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 impl/rust-cli/tests/rm_unreadable_tests.rs diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index d7d0eb32..de6e41c9 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -69,6 +69,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 damaged content as undo data. `redo` of `cp`, `mv` and `touch` no longer overwrites a path that reappeared after the undo. +- `rm` (and redo of `rm`) no longer deletes a file whose content it cannot + read. It used to delete it and record empty undo data, so undo brought the + file back empty. It now fails, leaves the file in place and points to + `obliterate` for irreversible deletion. - `redo` refuses to recreate a file whose content undo could not read (recorded as not captured, with the reason), where it used to recreate it empty. A `cp` whose copy fails part-way removes the partial destination, for diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index 43f39dbe..9bd86f2d 100644 --- a/impl/rust-cli/src/commands.rs +++ b/impl/rust-cli/src/commands.rs @@ -348,8 +348,16 @@ fn apply_rm(state: &ShellState, path: &str) -> Result { } // Store content for undo (pure read of the soon-to-be-deleted file; - // routed through fs_pure to honour the noatime discipline). - let content = crate::fs_pure::read_to_end(&full_path).unwrap_or_default(); + // routed through fs_pure to honour the noatime discipline). If it cannot + // be read it cannot be restored, so nothing is deleted: recording empty + // undo data would make a later undo bring the file back empty. + let content = crate::fs_pure::read_to_end(&full_path).with_context(|| { + format!( + "rm: cannot read '{}' to keep it for undo, so it was not deleted \ + (use obliterate to delete it irreversibly)", + path + ) + })?; fs::remove_file(&full_path).context("rm failed")?; diff --git a/impl/rust-cli/tests/rm_unreadable_tests.rs b/impl/rust-cli/tests/rm_unreadable_tests.rs new file mode 100644 index 00000000..3d44b8db --- /dev/null +++ b/impl/rust-cli/tests/rm_unreadable_tests.rs @@ -0,0 +1,100 @@ +// SPDX-License-Identifier: MPL-2.0 +// Copyright (c) Jonathan D.A. Jewell +//! `rm`, and redo of `rm`, must not delete a file whose content cannot be +//! read: the content is the undo data, and without it a later undo would +//! bring the file back empty. +//! +//! Root can read any file, so when the tests run as root this one switches +//! the process's effective user to `nobody` around the calls under test. +//! That switch is process-wide, which is why this test lives in a binary of +//! its own with a single test. +#![cfg(unix)] + +use std::fs; +use std::os::unix::fs::PermissionsExt; +use std::path::Path; + +use tempfile::TempDir; +use vsh::commands; +use vsh::state::ShellState; + +const NOBODY: libc::uid_t = 65534; + +fn effective_uid() -> libc::uid_t { + // SAFETY: `geteuid` has no preconditions and cannot fail. + unsafe { libc::geteuid() } +} + +fn set_effective_uid(uid: libc::uid_t) { + // SAFETY: changes only this process's effective user id; the real and + // saved ids stay root, so the switch back below is permitted. + let rc = unsafe { libc::seteuid(uid) }; + assert_eq!(rc, 0, "seteuid({uid})"); +} + +/// Run `f` as a user for whom `unreadable` cannot be read. +fn without_read_access(unreadable: &Path, f: impl FnOnce() -> T) -> T { + fs::set_permissions(unreadable, fs::Permissions::from_mode(0o000)).expect("chmod 000"); + let was_root = effective_uid() == 0; + if was_root { + set_effective_uid(NOBODY); + } + assert!( + fs::read(unreadable).is_err(), + "the file is unreadable to the user the test runs as" + ); + let result = f(); + if was_root { + set_effective_uid(0); + } + fs::set_permissions(unreadable, fs::Permissions::from_mode(0o600)).expect("chmod 600"); + result +} + +#[test] +fn rm_and_its_redo_do_not_delete_a_file_they_cannot_read() { + let temp = TempDir::new().expect("tempdir"); + let root = temp.path(); + // The directory stays writable to the unprivileged user, so the unlink + // itself would succeed; only the read can fail. + fs::set_permissions(root, fs::Permissions::from_mode(0o777)).expect("chmod dir"); + let mut state = ShellState::new(root.to_str().expect("utf8")).expect("state"); + let f = root.join("f"); + fs::write(&f, b"keep me").expect("write"); + + // -- the command ---------------------------------------------------------- + let err = without_read_access(&f, || commands::rm(&mut state, "f", false)) + .expect_err("rm of an unreadable file must fail"); + assert!(err.to_string().contains("was not deleted"), "{err}"); + assert_eq!( + fs::read(&f).expect("read"), + b"keep me", + "the file is intact" + ); + assert!(state.history.is_empty(), "nothing was recorded"); + + // -- its redo --------------------------------------------------------------- + commands::rm(&mut state, "f", false).expect("rm of a readable file"); + commands::undo(&mut state, 1, false).expect("undo"); + assert_eq!(fs::read(&f).expect("read"), b"keep me"); + + let recorded = state.history.len(); + let redone = without_read_access(&f, || commands::redo(&mut state, 1, false)); + assert!( + redone.is_err(), + "redo of rm on an unreadable file must fail" + ); + assert_eq!( + fs::read(&f).expect("read"), + b"keep me", + "the file is intact" + ); + assert_eq!(state.history.len(), recorded, "nothing was recorded"); + assert_eq!(state.get_redo_stack().len(), 1, "the entry is kept"); + + // Readable again: the same redo works, and its undo restores the bytes. + commands::redo(&mut state, 1, false).expect("redo"); + assert!(!f.exists()); + commands::undo(&mut state, 1, false).expect("undo after redo"); + assert_eq!(fs::read(&f).expect("read"), b"keep me"); +} From d27bd38dbf70617951a8ce54863cceb52f6dae8f Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 14:51:55 +0000 Subject: [PATCH 12/15] fix(undo): enforce the redo capture limit while reading undo compared a file's size with VSH_REDO_CAPTURE_LIMIT and then read the whole file. A file that grew between the two steps, or one that reports no size, could take far more memory than the limit. The capture now opens the file once, takes the size from that handle only to skip the read early, and reads at most limit + 1 bytes. More than the limit is recorded as over the limit, as before. With the limit set to 0 the read is unbounded, as documented. Tests: read_within_limit on short, exact, long and endless streams, and a capture through a link to /dev/zero (size 0, never ends) for created and appended content. The second test fails when the bound is removed. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- CHANGELOG.adoc | 3 +- impl/rust-cli/src/commands.rs | 114 +++++++++++++++++++++++++++------- 2 files changed, 94 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index de6e41c9..c7ae9a7f 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -59,7 +59,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 value). `undo` reads that state just before reversing it and keeps it in memory with the redo entry. File content over `VSH_REDO_CAPTURE_LIMIT` bytes (shell or environment variable; default 10 MiB; `0` = no limit) is not kept, - and redo of that entry says so. + and redo of that entry says so. The limit is enforced while reading, so a + file that grows during the undo cannot take more memory than the limit. - A `redo` that fails records nothing and stays on the redo stack. It also leaves the filesystem as it found it, including when a file write fails part-way: the partial file is removed, the overwritten bytes are put back, diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index 9bd86f2d..86291dd3 100644 --- a/impl/rust-cli/src/commands.rs +++ b/impl/rust-cli/src/commands.rs @@ -1285,6 +1285,26 @@ fn redo_capture_limit(state: &ShellState) -> Option { parse_redo_capture_limit(setting.as_deref()) } +/// Read `reader` to its end, or return `None` once it has given more than +/// `limit` bytes. At most `limit + 1` bytes are held, however long the stream +/// turns out to be; with no limit, everything is read. +fn read_within_limit( + reader: impl std::io::Read, + limit: Option, +) -> std::io::Result>> { + use std::io::Read; + let mut content = Vec::new(); + let Some(limit) = limit else { + let mut reader = reader; + reader.read_to_end(&mut content)?; + return Ok(Some(content)); + }; + reader + .take(limit.saturating_add(1)) + .read_to_end(&mut content)?; + Ok((content.len() as u64 <= limit).then_some(content)) +} + /// Read the state an undo of `op` is about to replace. /// /// `undo_data` holds the state before an operation; redo needs the state after @@ -1297,40 +1317,48 @@ fn redo_capture_limit(state: &ShellState) -> Option { /// larger than the capture limit is not kept; redo of that entry then says so. fn capture_redo_data(state: &ShellState, op: &Operation) -> Option { let path = state.resolve_path(&op.path); - let over_limit = |size: u64| { - redo_capture_limit(state) - .filter(|limit| size > *limit) - .map(|limit| RedoData::OverLimit { size, limit }) + let limit = redo_capture_limit(state); + // Read what is left of `file`, or say that it is over the limit. `skip` is + // how many bytes at the start of the file are not part of the capture. + let read = |mut file: fs::File, skip: u64| -> std::io::Result { + // The size is only a way to skip the read: the file can grow after + // this, and some files report no size, so the read is bounded too. + let size = file.metadata()?.len().saturating_sub(skip); + match limit { + Some(limit) if size > limit => Ok(RedoData::OverLimit { size, limit }), + _ => match read_within_limit(&mut file, limit)? { + Some(content) => Ok(RedoData::Content(content)), + None => { + let limit = limit.unwrap_or(u64::MAX); + let now = file.metadata()?.len().saturating_sub(skip); + Ok(RedoData::OverLimit { + size: now.max(limit.saturating_add(1)), + limit, + }) + } + }, + } }; match op.op_type { OperationType::CreateFile | OperationType::FileTruncated | OperationType::WriteFile => { // A file that cannot be read is recorded as such, never as empty: // redo must refuse it, not recreate the file without its content. - let read = || -> std::io::Result { - let size = fs::metadata(&path)?.len(); - if let Some(skipped) = over_limit(size) { - return Ok(skipped); - } - crate::fs_pure::read_to_end(&path).map(RedoData::Content) - }; - Some(read().unwrap_or_else(|e| RedoData::NotCaptured(e.to_string()))) + let capture = || read(crate::fs_pure::open_pure(&path)?, 0); + Some(capture().unwrap_or_else(|e| RedoData::NotCaptured(e.to_string()))) } OperationType::FileAppended => { - use std::io::{Read, Seek, SeekFrom}; + use std::io::{Seek, SeekFrom}; let original_size = u64::from_le_bytes(op.undo_data.as_ref()?.get(..8)?.try_into().ok()?); - let read = || -> std::io::Result { - let size = fs::metadata(&path)?.len(); - if let Some(skipped) = over_limit(size.saturating_sub(original_size)) { - return Ok(skipped); - } + let capture = || -> std::io::Result { let mut file = crate::fs_pure::open_pure(&path)?; file.seek(SeekFrom::Start(original_size))?; - let mut appended = Vec::new(); - file.read_to_end(&mut appended)?; - Ok(RedoData::Appended(appended)) + Ok(match read(file, original_size)? { + RedoData::Content(appended) => RedoData::Appended(appended), + other => other, + }) }; - Some(read().unwrap_or_else(|e| RedoData::NotCaptured(e.to_string()))) + Some(capture().unwrap_or_else(|e| RedoData::NotCaptured(e.to_string()))) } OperationType::Chmod => { #[cfg(unix)] @@ -3130,6 +3158,48 @@ mod tests { )); } + #[test] + fn test_read_within_limit() { + let read = |bytes: &[u8], limit| read_within_limit(bytes, limit).unwrap(); + assert_eq!(read(b"abcd", Some(4)), Some(b"abcd".to_vec())); + assert_eq!(read(b"abcde", Some(4)), None); + assert_eq!(read(b"", Some(4)), Some(Vec::new())); + assert_eq!(read(b"abcde", None), Some(b"abcde".to_vec())); + assert_eq!(read(b"a", Some(u64::MAX)), Some(b"a".to_vec())); + // A stream with no end stops at the limit instead of filling memory. + assert_eq!( + read_within_limit(std::io::repeat(0), Some(16)).unwrap(), + None + ); + } + + /// The limit holds when the size reported before the read is too small: + /// `/dev/zero` reports no size and never ends, which is the worst case of + /// a file that another process grows between the size check and the read. + #[cfg(target_os = "linux")] + #[test] + fn test_capture_redo_data_limit_holds_when_the_size_is_wrong() { + let temp = tempfile::TempDir::new().unwrap(); + let mut state = ShellState::new(temp.path().to_str().unwrap()).unwrap(); + state.set_variable(REDO_CAPTURE_LIMIT_VAR, "64"); + std::os::unix::fs::symlink("/dev/zero", temp.path().join("f")).unwrap(); + + let created = Operation::new(OperationType::CreateFile, "f".to_string(), None); + let appended = Operation::new(OperationType::FileAppended, "f".to_string(), None) + .with_undo_data(8u64.to_le_bytes().to_vec()); + for op in [created, appended] { + assert_eq!( + capture_redo_data(&state, &op), + Some(RedoData::OverLimit { + size: 65, + limit: 64 + }), + "{}", + op.op_type + ); + } + } + #[test] fn test_parse_redo_capture_limit() { // Unset or unreadable: the default. From 08ee8702da996243c594cf844d9f0fc8958e8e91 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 15:02:25 +0000 Subject: [PATCH 13/15] fix(redo): chmod/chown through a symlink; mode of a recreated file chmod and chown change the file a symbolic link points to, but recorded the link's own mode and owner. Undo of such a chmod set the file to mode 0777, and redo of either put back the wrong value. Both commands, and the redo capture, now read the mode and owner through the link. Undo of touch, or of a redirection that created a file, deletes the file. Redo recreated it with the default mode, so a file whose mode was changed outside the history in between came back with wider permissions. Undo now keeps the mode with the content (RedoData::Created) and redo creates the file with it. The owner is not restored. Tests: two new round-trip scenarios (chmod and chown through a symlink, whole-sandbox comparison), and two tests of the mode of a recreated file. All four failed before this change. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- CHANGELOG.adoc | 12 +++- impl/rust-cli/src/commands.rs | 79 ++++++++++++++++----- impl/rust-cli/src/state.rs | 7 +- impl/rust-cli/tests/redo_roundtrip_tests.rs | 72 ++++++++++++++++++- 4 files changed, 145 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index c7ae9a7f..72226cf5 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -85,6 +85,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 redirections are now relative to the root. - `touch` creates its file with `create_new`, so it no longer writes through a dangling symbolic link. +- `chmod` and `chown` on a symbolic link recorded the link's own mode and + owner while changing the file it points to. Undo of that `chmod` set the + file to mode 0777, and redo of either restored the wrong value. Both now + record the mode and owner of the file they change. +- Redo of `touch`, or of a `>` redirection that created a file, brings the + file back with the mode it had when it was undone. It used to come back + with the default mode, so a file made private in between returned readable + by others. The owner is not restored. - Idris2 model: `redoUndoIdentity`, `undoRedoUndoIdentity`, `redoUndoComposition` added to `Filesystem.Composition`. Their status is the proof ledger's row for the undo/redo stack laws @@ -92,8 +100,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Known and *not* fixed here (tests exclude them, see `impl/rust-cli/tests/redo_roundtrip_tests.rs`): undo of `rm`/`rmdir` does not restore the mode; undo of `unset` does not restore the exported flag; - `rm`, `chmod` and redirections follow symbolic links and their undo does not - account for it. + `rm` and redirections follow symbolic links and their undo does not account + for it. === Planned - Glob expansion (*.txt, ?.rs, [a-z]*) diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index 86291dd3..aeb189c5 100644 --- a/impl/rust-cli/src/commands.rs +++ b/impl/rust-cli/src/commands.rs @@ -221,7 +221,7 @@ fn apply_rmdir(state: &ShellState, path: &str) -> Result { /// # Ok::<(), anyhow::Error>(()) /// ``` pub fn touch(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { - let op = apply_create_file(state, path, &[])?; + let op = apply_create_file(state, path, &[], None)?; let op_id = op.id; state.record_operation(op); @@ -244,7 +244,12 @@ pub fn touch(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { /// Check, execute and describe the creation of a new file holding `content`, /// without recording it. `touch` creates it empty; redo of a file that a `>` /// redirection created puts back the bytes undo removed. -fn apply_create_file(state: &ShellState, path: &str, content: &[u8]) -> Result { +fn apply_create_file( + state: &ShellState, + path: &str, + content: &[u8], + mode: Option, +) -> Result { let full_path = state.resolve_path(path); // Runtime check of the Lean 4 precondition (see verification.rs) @@ -260,12 +265,27 @@ fn apply_create_file(state: &ShellState, path: &str, content: &[u8]) -> Result Option { } }; match op.op_type { - OperationType::CreateFile | OperationType::FileTruncated | OperationType::WriteFile => { - // A file that cannot be read is recorded as such, never as empty: - // redo must refuse it, not recreate the file without its content. + // A file that cannot be read is recorded as such, never as empty: + // redo must refuse it, not recreate the file without its content. + OperationType::CreateFile => { + // Undo deletes this file, so its mode goes with its content. + let capture = || -> std::io::Result { + let file = crate::fs_pure::open_pure(&path)?; + #[cfg(unix)] + let mode = { + use std::os::unix::fs::PermissionsExt; + Some(file.metadata()?.permissions().mode() & 0o7777) + }; + #[cfg(not(unix))] + let mode = None; + Ok(match read(file, 0)? { + RedoData::Content(content) => RedoData::Created { content, mode }, + other => other, + }) + }; + Some(capture().unwrap_or_else(|e| RedoData::NotCaptured(e.to_string()))) + } + OperationType::FileTruncated | OperationType::WriteFile => { let capture = || read(crate::fs_pure::open_pure(&path)?, 0); Some(capture().unwrap_or_else(|e| RedoData::NotCaptured(e.to_string()))) } @@ -1364,7 +1405,8 @@ fn capture_redo_data(state: &ShellState, op: &Operation) -> Option { #[cfg(unix)] { use std::os::unix::fs::PermissionsExt; - let mode = fs::symlink_metadata(&path).ok()?.permissions().mode(); + // Through a symbolic link, as `chmod` itself goes. + let mode = fs::metadata(&path).ok()?.permissions().mode(); Some(RedoData::Mode(mode)) } #[cfg(not(unix))] @@ -1374,7 +1416,8 @@ fn capture_redo_data(state: &ShellState, op: &Operation) -> Option { #[cfg(unix)] { use std::os::unix::fs::MetadataExt; - let metadata = fs::symlink_metadata(&path).ok()?; + // Through a symbolic link, as `chown` itself goes. + let metadata = fs::metadata(&path).ok()?; Some(RedoData::Owner { uid: metadata.uid(), gid: metadata.gid(), @@ -1450,10 +1493,10 @@ fn reapply(state: &mut ShellState, op: &Operation) -> Result { // Empty for `touch`; the redirected output for a file `>` created. // Anything but captured content is refused: recreating the file // empty would silently lose what the redirection wrote. - let Some(RedoData::Content(content)) = &op.redo_data else { + let Some(RedoData::Created { content, mode }) = &op.redo_data else { return Err(missing()); }; - apply_create_file(state, &op.path, content) + apply_create_file(state, &op.path, content, *mode) } OperationType::DeleteFile => apply_rm(state, &op.path), OperationType::WriteFile | OperationType::FileTruncated => { diff --git a/impl/rust-cli/src/state.rs b/impl/rust-cli/src/state.rs index 0d6bbd4b..1602ca8b 100644 --- a/impl/rust-cli/src/state.rs +++ b/impl/rust-cli/src/state.rs @@ -169,9 +169,12 @@ impl std::fmt::Display for OperationType { /// nothing to the saved state. #[derive(Debug, Clone, PartialEq, Eq)] pub enum RedoData { - /// Whole content of the file undo deleted or replaced - /// (`CreateFile`, `FileTruncated`, `WriteFile`). + /// Whole content of the file undo replaced (`FileTruncated`, `WriteFile`). Content(Vec), + /// Whole content and mode of the file undo deleted (`CreateFile`). The + /// mode is kept because undo removes the file itself, so nothing else + /// remembers it. `None` where the platform has no mode bits. + Created { content: Vec, mode: Option }, /// The bytes undo cut off the end of the file (`FileAppended`). Appended(Vec), /// The mode undo replaced (`Chmod`). diff --git a/impl/rust-cli/tests/redo_roundtrip_tests.rs b/impl/rust-cli/tests/redo_roundtrip_tests.rs index e8867884..1662b5f2 100644 --- a/impl/rust-cli/tests/redo_roundtrip_tests.rs +++ b/impl/rust-cli/tests/redo_roundtrip_tests.rs @@ -542,7 +542,7 @@ fn redo_refuses_a_file_whose_content_was_not_captured() { // What undo records when the read fails (see the unit test of // `capture_redo_data` for the capture side). let entry = state.redo_stack.front_mut().expect("redo entry"); - assert!(matches!(entry.redo_data, Some(RedoData::Content(_)))); + assert!(matches!(entry.redo_data, Some(RedoData::Created { .. }))); entry.redo_data = Some(RedoData::NotCaptured("Permission denied".to_string())); let err = commands::redo(&mut state, 1, false).expect_err("redo must refuse"); @@ -599,6 +599,51 @@ fn assignment_redo_restores_not_exported() { ); } +// --------------------------------------------------------------------------- +// The mode of a file that undo deletes +// --------------------------------------------------------------------------- + +/// Undo of `touch` deletes the file. If its mode was changed outside the +/// history in the meantime, redo must bring the file back with that mode, +/// not with the default one: a private file must not return readable by all. +#[cfg(unix)] +#[test] +fn touch_redo_restores_the_mode_the_file_had_when_it_was_undone() { + let (temp, mut state) = setup(); + let f = temp.path().join("f"); + + commands::touch(&mut state, "f", false).unwrap(); + for mode in [0o600, 0o400, 0o4755, 0o666, 0o640] { + set_mode(&f, mode); + undo(&mut state); + assert!(!f.exists()); + redo(&mut state); + assert_eq!(mode_of(&f), mode, "redo after undo at mode {mode:o}"); + } +} + +/// The same for a file a redirection created, with its content. +#[cfg(unix)] +#[test] +fn redirect_redo_restores_content_and_mode() { + use vsh::executable::ExecutableCommand; + let (temp, mut state) = setup(); + let f = temp.path().join("new"); + + vsh::parser::parse_command("/bin/echo hi > new") + .unwrap() + .execute(&mut state) + .unwrap(); + assert_eq!(read(&f), b"hi\n"); + set_mode(&f, 0o600); + + undo(&mut state); + assert!(!f.exists()); + redo(&mut state); + assert_eq!(read(&f), b"hi\n"); + assert_eq!(mode_of(&f), 0o600); +} + // --------------------------------------------------------------------------- // Mixed sequence // --------------------------------------------------------------------------- @@ -821,6 +866,27 @@ mod laws { needs_root: true, ..s("chown", |r, _| put(r, "f", 0o644), "chown 1000:1000 f") }, + // chmod and chown act on what a link points to, so the mode and + // owner they record must be the target's, not the link's own. + s( + "chmod through a symlink", + |r, _| { + put(r, "f", 0o600); + std::os::unix::fs::symlink("f", r.join("l")).expect("symlink"); + }, + "chmod 640 l", + ), + Scenario { + needs_root: true, + ..s( + "chown through a symlink", + |r, _| { + put(r, "f", 0o644); + std::os::unix::fs::symlink("f", r.join("l")).expect("symlink"); + }, + "chown 1000:1000 l", + ) + }, s("> creating a file", |_, _| {}, "/bin/echo hi > new"), s( "> replacing a file", @@ -990,8 +1056,8 @@ mod laws { // -- random sequences --------------------------------------------------- /// One command line over a four-name namespace. Symbolic links are left - /// out: `rm`, `chmod` and redirections follow them, and undo of those is - /// a known, separate defect that would mask what is tested here. + /// out: `rm` and redirections follow them, and undo of those is a known, + /// separate defect that would mask what is tested here. fn command_line() -> impl Strategy { let name = prop::sample::select(vec!["a", "b", "c", "d"]); let mode = prop::sample::select(vec!["600", "644", "640", "755", "700", "u+x"]); From 0b802fe0856953180172b02dc5a3a6e61a0fea49 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 15:14:03 +0000 Subject: [PATCH 14/15] fix(redo): write through one open file; restore a variable that was not set Redo of a replaced or appended file read the old content (or the old size) by path and then opened the path again to write. A file renamed over the path in between was overwritten, and the undo data recorded belonged to the other file. Both now open the file once: replace_through and append_through take the handle, not a path, so what they read, measure and change is one file by construction. fs_pure gains open_pure_read_write for the read-then-write case. Undo of an assignment whose variable was no longer set (removed outside the history, as when a function returns) recorded nothing for redo, and redo then failed with "not captured". Not set is now a captured state (RedoData::Variable holds an Option) and redo restores it. Tests: the two helpers against a file renamed over the path after the open, and without a rename; redo of an assignment whose variable was not set, over three cycles. The last fails without its fix. The rename race itself is not reproduced through redo: the tests show the helpers act on the handle. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- CHANGELOG.adoc | 7 + impl/rust-cli/src/commands.rs | 206 ++++++++++++++------ impl/rust-cli/src/fs_pure.rs | 26 ++- impl/rust-cli/src/state.rs | 27 ++- impl/rust-cli/tests/redo_roundtrip_tests.rs | 23 +++ 5 files changed, 222 insertions(+), 67 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 72226cf5..7ef09b37 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -93,6 +93,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 file back with the mode it had when it was undone. It used to come back with the default mode, so a file made private in between returned readable by others. The owner is not restored. +- Redo of a redirection that replaced or appended to a file now reads, + measures and writes through one open file. Before, a file renamed over the + path between the read and the write was overwritten, and the undo data + recorded was the other file's. +- Redo of an assignment whose variable was no longer set when it was undone + (a function's local, removed when the function returned) failed with "not + captured". It now restores the variable as not set. - Idris2 model: `redoUndoIdentity`, `undoRedoUndoIdentity`, `redoUndoComposition` added to `Filesystem.Composition`. Their status is the proof ledger's row for the undo/redo stack laws diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index aeb189c5..f7602f9b 100644 --- a/impl/rust-cli/src/commands.rs +++ b/impl/rust-cli/src/commands.rs @@ -1427,14 +1427,12 @@ fn capture_redo_data(state: &ShellState, op: &Operation) -> Option { None } OperationType::SetVariable => { - state - .variables - .get(&op.path) - .cloned() - .map(|value| RedoData::Variable { - value, - exported: state.is_exported(&op.path), - }) + // A variable that is no longer set is a state too, not a failed + // capture: something outside the history removed it. + Some(RedoData::Variable { + value: state.variables.get(&op.path).cloned(), + exported: state.is_exported(&op.path), + }) } OperationType::Mkdir | OperationType::Rmdir @@ -1530,7 +1528,7 @@ fn reapply(state: &mut ShellState, op: &Operation) -> Result { let Some(RedoData::Variable { value, exported }) = &op.redo_data else { return Err(missing()); }; - let new_op = state.apply_set_variable(op.path.clone(), value.clone()); + let new_op = state.apply_variable_state(op.path.clone(), value.clone()); // Put the export state back as undo found it, whichever way that // was: undo of an assignment drops the export, and something // outside the history may have set it since. @@ -1589,73 +1587,119 @@ fn apply_replace_content( anyhow::bail!("Cannot redo {}: {} is not a file", op_type, path); } - let original = crate::fs_pure::read_to_end(&full_path) - .with_context(|| format!("Failed to read {} before redo", path))?; - // Open without `create`, so the write lands in the file that was read, - // and without `truncate`: the new content is written over the old and the - // file is cut to length afterwards. The old bytes are then still in place - // if the write fails, and putting them back never grows the file. On a - // filesystem that overwrites in place, that needs none of the space or - // quota the failed write ran out of. It is not a guarantee: a - // copy-on-write filesystem can need new space even for an overwrite, and - // an I/O error can fail any write, which is why a failed restore is - // reported below. - use std::io::{Seek, SeekFrom}; - let mut file = fs::OpenOptions::new() - .write(true) - .open(&full_path) + let mut file = crate::fs_pure::open_pure_read_write(&full_path) .with_context(|| format!("Redo {} failed", op_type))?; + match replace_through(&mut file, content) { + Ok(original) => { + Ok(Operation::new(op_type, path.to_string(), None).with_undo_data(original)) + } + Err(failure) => { + let error = + anyhow::Error::new(failure.error).context(format!("Redo {} failed", op_type)); + Err(match failure.cleanup { + None => error, + Some(restore_error) => error.context(format!( + "{} could not be restored after a failed redo and may be damaged: {}", + path, restore_error + )), + }) + } + } +} + +/// A write through an open file that failed, and the error from putting the +/// file back as it was, if that failed too. +struct WriteFailure { + error: std::io::Error, + cleanup: Option, +} + +/// Replace the whole content of `file` and return what it held before. +/// +/// It takes a handle, not a path, so the content it returns and the content +/// it replaces belong to the same file whatever happens to the path meanwhile. +/// +/// The new content is written over the old and the file is cut to length +/// afterwards, with no truncation first. The old bytes are then still in +/// place if the write fails, and putting them back never grows the file. On +/// a filesystem that overwrites in place, that needs none of the space or +/// quota the failed write ran out of. It is not a guarantee: a copy-on-write +/// filesystem can need new space even for an overwrite, and an I/O error can +/// fail any write, which is why a failed restore is reported. +fn replace_through( + file: &mut fs::File, + content: &[u8], +) -> std::result::Result, WriteFailure> { + use std::io::{Read, Seek, SeekFrom}; + let mut original = Vec::new(); + file.seek(SeekFrom::Start(0)) + .and_then(|_| file.read_to_end(&mut original)) + .map_err(|error| WriteFailure { + error, + cleanup: None, + })?; let written = file - .write_all(content) + .seek(SeekFrom::Start(0)) + .and_then(|_| file.write_all(content)) .and_then(|()| file.set_len(content.len() as u64)); - if let Err(e) = written { - // Only the bytes before the current position can have changed. - let restored = file.stream_position().and_then(|position| { - let changed = original.len().min(position as usize); - file.seek(SeekFrom::Start(0))?; - file.write_all(&original[..changed])?; - file.set_len(original.len() as u64) - }); - let error = anyhow::Error::new(e).context(format!("Redo {} failed", op_type)); - return Err(match restored { - Ok(()) => error, - Err(restore_error) => error.context(format!( - "{} could not be restored after a failed redo and may be damaged: {}", - path, restore_error - )), - }); + match written { + Ok(()) => Ok(original), + Err(error) => { + // Only the bytes before the current position can have changed. + let restored = file.stream_position().and_then(|position| { + let changed = original.len().min(position as usize); + file.seek(SeekFrom::Start(0))?; + file.write_all(&original[..changed])?; + file.set_len(original.len() as u64) + }); + Err(WriteFailure { + error, + cleanup: restored.err(), + }) + } } +} - Ok(Operation::new(op_type, path.to_string(), None).with_undo_data(original)) +/// Append to `file`, which must be open for appending, and return the size +/// it had before. The size comes from the handle, so it is the size of the +/// file that was appended to. A partial append is cut off again. +fn append_through(file: &mut fs::File, appended: &[u8]) -> std::result::Result { + let original_size = file + .metadata() + .map_err(|error| WriteFailure { + error, + cleanup: None, + })? + .len(); + match file.write_all(appended) { + Ok(()) => Ok(original_size), + Err(error) => Err(WriteFailure { + error, + cleanup: file.set_len(original_size).err(), + }), + } } -/// Append bytes to a file and describe it, without recording it. The returned -/// `FileAppended` operation carries the size before the append as its undo -/// data, as the `>>` redirection that first recorded it did. fn apply_append(state: &ShellState, path: &str, appended: &[u8]) -> Result { let full_path = state.resolve_path(path); if !full_path.is_file() { anyhow::bail!("Cannot redo append: {} is not a file", path); } - let original_size = fs::metadata(&full_path) - .with_context(|| format!("Failed to stat {} before redo", path))? - .len(); let mut file = fs::OpenOptions::new() .append(true) .open(&full_path) .context("Redo append failed")?; - if let Err(e) = file.write_all(appended) { - // Cut off whatever part of the append landed. - let error = anyhow::Error::new(e).context("Redo append failed"); - return Err(match file.set_len(original_size) { - Ok(()) => error, - Err(cleanup_error) => error.context(format!( + let original_size = append_through(&mut file, appended).map_err(|failure| { + let error = anyhow::Error::new(failure.error).context("Redo append failed"); + match failure.cleanup { + None => error, + Some(cleanup_error) => error.context(format!( "the partial append to {} could not be cut off: {}", path, cleanup_error )), - }); - } + } + })?; Ok( Operation::new(OperationType::FileAppended, path.to_string(), None) @@ -3201,6 +3245,56 @@ mod tests { )); } + /// `replace_through` and `append_through` take an open file, so what they + /// read, measure and change is that file, whatever the path names by + /// then. Here another file is renamed over the path after the open. + #[test] + fn test_redo_writes_go_to_the_file_that_was_opened() { + let temp = tempfile::TempDir::new().unwrap(); + let path = temp.path().join("f"); + let other = temp.path().join("other"); + + fs::write(&path, b"opened").unwrap(); + let mut file = crate::fs_pure::open_pure_read_write(&path).unwrap(); + fs::write(&other, b"renamed over the path").unwrap(); + fs::rename(&other, &path).unwrap(); + let original = replace_through(&mut file, b"new").ok().unwrap(); + assert_eq!(original, b"opened"); + assert_eq!(fs::read(&path).unwrap(), b"renamed over the path"); + + fs::write(&path, b"12345").unwrap(); + let mut file = fs::OpenOptions::new().append(true).open(&path).unwrap(); + fs::write(&other, b"renamed over the path").unwrap(); + fs::rename(&other, &path).unwrap(); + let original_size = append_through(&mut file, b"678").ok().unwrap(); + assert_eq!(original_size, 5); + assert_eq!(fs::read(&path).unwrap(), b"renamed over the path"); + } + + /// Without a rename in the way: the content and size returned are the + /// ones before the write, and the file holds the new content. + #[test] + fn test_replace_through_and_append_through() { + let temp = tempfile::TempDir::new().unwrap(); + let path = temp.path().join("f"); + + fs::write(&path, b"a longer original").unwrap(); + let mut file = crate::fs_pure::open_pure_read_write(&path).unwrap(); + assert_eq!( + replace_through(&mut file, b"short").ok().unwrap(), + b"a longer original" + ); + assert_eq!(fs::read(&path).unwrap(), b"short"); + // A second call on the same handle starts from the beginning again. + assert_eq!(replace_through(&mut file, b"").ok().unwrap(), b"short"); + assert_eq!(fs::read(&path).unwrap(), b""); + + fs::write(&path, b"12345").unwrap(); + let mut file = fs::OpenOptions::new().append(true).open(&path).unwrap(); + assert_eq!(append_through(&mut file, b"678").ok().unwrap(), 5); + assert_eq!(fs::read(&path).unwrap(), b"12345678"); + } + #[test] fn test_read_within_limit() { let read = |bytes: &[u8], limit| read_within_limit(bytes, limit).unwrap(); diff --git a/impl/rust-cli/src/fs_pure.rs b/impl/rust-cli/src/fs_pure.rs index d5672609..0efc61ee 100644 --- a/impl/rust-cli/src/fs_pure.rs +++ b/impl/rust-cli/src/fs_pure.rs @@ -65,20 +65,32 @@ use std::path::Path; /// /// On non-Linux targets this is equivalent to [`File::open`]. pub fn open_pure(path: &Path) -> io::Result { + open_noatime(File::options().read(true), path) +} + +/// Open an existing file for reading and writing through one handle, with the +/// same `O_NOATIME` handling as [`open_pure`] for the reads. +/// +/// For callers that read a file and then change it: one handle means what +/// was read and what is changed are the same file, even if the path is +/// renamed or replaced in between. The file is not created or truncated. +pub fn open_pure_read_write(path: &Path) -> io::Result { + open_noatime(File::options().read(true).write(true), path) +} + +/// Open with `O_NOATIME` on Linux, without it if the kernel refuses the flag. +fn open_noatime(options: &mut std::fs::OpenOptions, path: &Path) -> io::Result { #[cfg(target_os = "linux")] { use std::os::unix::fs::OpenOptionsExt; - let mut opts = File::options(); - opts.read(true).custom_flags(libc::O_NOATIME); - match opts.open(path) { - Ok(f) => Ok(f), - Err(e) if e.raw_os_error() == Some(libc::EPERM) => File::open(path), - Err(e) => Err(e), + match options.clone().custom_flags(libc::O_NOATIME).open(path) { + Err(e) if e.raw_os_error() == Some(libc::EPERM) => options.open(path), + result => result, } } #[cfg(not(target_os = "linux"))] { - File::open(path) + options.open(path) } } diff --git a/impl/rust-cli/src/state.rs b/impl/rust-cli/src/state.rs index 1602ca8b..8e35d37f 100644 --- a/impl/rust-cli/src/state.rs +++ b/impl/rust-cli/src/state.rs @@ -182,9 +182,10 @@ pub enum RedoData { /// The ownership undo replaced (`Chown`). Owner { uid: u32, gid: u32 }, /// The value undo replaced, and whether the variable was exported - /// (`SetVariable`). + /// (`SetVariable`). `None` is a variable that was not set when it was + /// undone (removed outside the history, as when a function returns). Variable { - value: VariableValue, + value: Option, exported: bool, }, /// The content was larger than the capture limit, so undo did not keep it @@ -683,12 +684,30 @@ impl ShellState { /// absence). Shared by [`Self::set_variable_tracked`] and redo, so both /// capture the same undo data. pub fn apply_set_variable(&mut self, name: String, value: VariableValue) -> Operation { + self.apply_variable_state(name, Some(value)) + } + + /// Put a variable into the given state (`None` is not set) and return a + /// `SetVariable` operation whose undo data is the state it replaced. + /// Redo uses the `None` case for an assignment whose variable was no + /// longer set when it was undone. + pub fn apply_variable_state( + &mut self, + name: String, + value: Option, + ) -> Operation { // Capture previous state for undo let previous = self.variables.get(&name).cloned(); let undo_data = serde_json::to_vec(&previous).unwrap_or_default(); - // Perform the set - self.variables.insert(name.clone(), value); + match value { + Some(value) => { + self.variables.insert(name.clone(), value); + } + None => { + self.variables.remove(&name); + } + } Operation::new(OperationType::SetVariable, name, None).with_undo_data(undo_data) } diff --git a/impl/rust-cli/tests/redo_roundtrip_tests.rs b/impl/rust-cli/tests/redo_roundtrip_tests.rs index 1662b5f2..2afa0d25 100644 --- a/impl/rust-cli/tests/redo_roundtrip_tests.rs +++ b/impl/rust-cli/tests/redo_roundtrip_tests.rs @@ -599,6 +599,29 @@ fn assignment_redo_restores_not_exported() { ); } +/// A variable that is no longer set when its assignment is undone is a state +/// redo must restore, not a failed capture. Shell functions do this: an +/// assignment inside a function is recorded, and returning from the function +/// removes the variable without a record. +#[test] +fn assignment_redo_restores_a_variable_that_was_not_set() { + use vsh::executable::ExecutableCommand; + use vsh::parser::parse_command; + + let (_temp, mut state) = setup(); + parse_command("X=1").unwrap().execute(&mut state).unwrap(); + parse_command("X=2").unwrap().execute(&mut state).unwrap(); + // Outside the history: this call records nothing. + state.unset_variable("X"); + + for cycle in 0..3 { + undo(&mut state); + assert_eq!(state.get_variable("X"), Some("1"), "cycle {cycle}: undo"); + redo(&mut state); + assert_eq!(state.get_variable("X"), None, "cycle {cycle}: redo"); + } +} + // --------------------------------------------------------------------------- // The mode of a file that undo deletes // --------------------------------------------------------------------------- From e3fd805cca1b24cc7690c0f5912faa9a0d4bca3b Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 15:22:35 +0000 Subject: [PATCH 15/15] fix(undo, redo): do not grow a shortened file; redo unset of an absent variable Undo of an append set the file's length to its earlier size. A file cut shorter than that outside the history was grown with zero bytes that were never in it, and redo then left it grown. Undo (and transaction rollback) now only cut a file that is longer than the recorded size. Redo of unset failed when the variable was already not set (removed outside the history after the undo restored it), and the entry stayed on the redo stack. That is the state the redo is for: it now succeeds and records an unset with nothing to restore. Changelog: note that undo and redo identify a file by path only, so a file replaced outside the history between an operation and its undo is overwritten or cut by that undo (confirmed with a probe; not fixed here). Tests: one for each fix; both failed before the change. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDiC97Fg2LvDpuFCz95 --- CHANGELOG.adoc | 9 +++- impl/rust-cli/src/commands.rs | 22 +++++++-- impl/rust-cli/src/state.rs | 11 +++++ impl/rust-cli/tests/redo_roundtrip_tests.rs | 54 +++++++++++++++++++++ 4 files changed, 90 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 7ef09b37..7987101e 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -100,6 +100,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Redo of an assignment whose variable was no longer set when it was undone (a function's local, removed when the function returned) failed with "not captured". It now restores the variable as not set. +- Undo of a `>>` redirection no longer grows a file that was cut shorter + outside the history in the meantime. It used to extend the file to its + earlier size with zero bytes. +- Redo of `unset` succeeds when the variable is already not set, where it + failed and stayed on the redo stack. - Idris2 model: `redoUndoIdentity`, `undoRedoUndoIdentity`, `redoUndoComposition` added to `Filesystem.Composition`. Their status is the proof ledger's row for the undo/redo stack laws @@ -108,7 +113,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `impl/rust-cli/tests/redo_roundtrip_tests.rs`): undo of `rm`/`rmdir` does not restore the mode; undo of `unset` does not restore the exported flag; `rm` and redirections follow symbolic links and their undo does not account - for it. + for it. Undo and redo identify a file by its path only: a file replaced + outside the history between an operation and its undo is overwritten or cut + by that undo. === Planned - Glob expansion (*.txt, ?.rs, [a-z]*) diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index f7602f9b..8c95999e 100644 --- a/impl/rust-cli/src/commands.rs +++ b/impl/rust-cli/src/commands.rs @@ -1043,8 +1043,7 @@ pub fn undo(state: &mut ShellState, count: usize, verbose: bool) -> Result<()> { .write(true) .open(&path) .context("Failed to open file for truncation")?; - file.set_len(original_size) - .context("Undo append (truncate) failed")?; + cut_back_to(&file, original_size).context("Undo append (truncate) failed")?; } OperationType::Move => { // Undo move = move it back (dst -> src) @@ -1308,6 +1307,16 @@ fn redo_capture_limit(state: &ShellState) -> Option { parse_redo_capture_limit(setting.as_deref()) } +/// Cut `file` back to `size` bytes to reverse an append. A file that is +/// already that short or shorter is left alone: `set_len` would grow it with +/// zero bytes that were never in it. +fn cut_back_to(file: &fs::File, size: u64) -> std::io::Result<()> { + if file.metadata()?.len() > size { + file.set_len(size)?; + } + Ok(()) +} + /// Read `reader` to its end, or return `None` once it has given more than /// `limit` bytes. At most `limit + 1` bytes are held, however long the stream /// turns out to be; with no limit, everything is read. @@ -1539,9 +1548,12 @@ fn reapply(state: &mut ShellState, op: &Operation) -> Result { } Ok(new_op) } - OperationType::UnsetVariable => state + // A variable that is already not set (removed outside the history + // since the undo) is the state this redo is for. The record then + // says there was nothing to restore, so undo of it changes nothing. + OperationType::UnsetVariable => Ok(state .apply_unset_variable(&op.path) - .with_context(|| format!("Cannot redo unset {}: it is not set", op.path)), + .unwrap_or_else(|| state.apply_variable_absent(&op.path))), OperationType::Chmod => { let Some(RedoData::Mode(mode)) = op.redo_data else { return Err(missing()); @@ -1929,7 +1941,7 @@ pub fn rollback_transaction(state: &mut ShellState) -> Result<()> { OpenOptions::new() .write(true) .open(&path) - .and_then(|file| file.set_len(original_size)) + .and_then(|file| cut_back_to(&file, original_size)) .context("Failed to truncate file") } else { Err(anyhow::anyhow!("Invalid size data in undo_data")) diff --git a/impl/rust-cli/src/state.rs b/impl/rust-cli/src/state.rs index 8e35d37f..694c8b8c 100644 --- a/impl/rust-cli/src/state.rs +++ b/impl/rust-cli/src/state.rs @@ -739,6 +739,17 @@ impl ShellState { ) } + /// The record of an `unset` that found the variable already not set: + /// an `UnsetVariable` operation whose undo data says there is nothing to + /// restore. The `unset` command records nothing in that case; redo needs + /// a record, because it replaces the entry it took off the redo stack. + pub fn apply_variable_absent(&mut self, name: &str) -> Operation { + self.exported_vars.remove(name); + let undo_data = serde_json::to_vec(&None::).unwrap_or_default(); + Operation::new(OperationType::UnsetVariable, name.to_string(), None) + .with_undo_data(undo_data) + } + /// Get a shell variable value (scalar only) /// Returns None if variable doesn't exist or is an array pub fn get_variable(&self, name: &str) -> Option<&str> { diff --git a/impl/rust-cli/tests/redo_roundtrip_tests.rs b/impl/rust-cli/tests/redo_roundtrip_tests.rs index 2afa0d25..117a0d5b 100644 --- a/impl/rust-cli/tests/redo_roundtrip_tests.rs +++ b/impl/rust-cli/tests/redo_roundtrip_tests.rs @@ -622,6 +622,60 @@ fn assignment_redo_restores_a_variable_that_was_not_set() { } } +/// Redo of `unset` when the variable is already not set (removed outside the +/// history after the undo restored it) is the state redo is asked for. It +/// succeeds and does not block the redo stack. +#[test] +fn unset_redo_succeeds_when_the_variable_is_already_not_set() { + use vsh::executable::ExecutableCommand; + use vsh::parser::parse_command; + + let (_temp, mut state) = setup(); + parse_command("X=1").unwrap().execute(&mut state).unwrap(); + parse_command("unset X") + .unwrap() + .execute(&mut state) + .unwrap(); + undo(&mut state); + assert_eq!(state.get_variable("X"), Some("1")); + // Outside the history: this call records nothing. + state.unset_variable("X"); + + redo(&mut state); + assert_eq!(state.get_variable("X"), None); + assert!(state.get_redo_stack().is_empty()); + // Undo of that redo has nothing to restore: the variable was not set. + undo(&mut state); + assert_eq!(state.get_variable("X"), None); +} + +/// Undo of an append cuts the file back to its earlier size. If the file is +/// already shorter than that (cut outside the history), undo must not grow +/// it with zero bytes, and redo must leave it as undo found it. +#[test] +fn append_undo_never_grows_a_file_that_was_cut_shorter() { + use vsh::executable::ExecutableCommand; + use vsh::parser::parse_command; + + let (temp, mut state) = setup(); + let f = temp.path().join("f"); + fs::write(&f, b"0123456789").unwrap(); + parse_command("/bin/echo hi >> f") + .unwrap() + .execute(&mut state) + .unwrap(); + assert_eq!(read(&f), b"0123456789hi\n"); + // Outside the history. + fs::write(&f, b"012").unwrap(); + + undo(&mut state); + assert_eq!(read(&f), b"012", "undo must not extend the file"); + redo(&mut state); + assert_eq!(read(&f), b"012", "redo restores what undo found"); + undo(&mut state); + assert_eq!(read(&f), b"012"); +} + // --------------------------------------------------------------------------- // The mode of a file that undo deletes // ---------------------------------------------------------------------------