diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index de48c3d..7987101 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -47,6 +47,76 @@ 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. 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, + 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. +- `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 + 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 + 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. +- 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. +- 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 + (`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; + `rm` and redirections follow symbolic links and their undo does not account + 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]*) - Quote processing (proper escaping, single/double quotes) diff --git a/EXPLAINME.adoc b/EXPLAINME.adoc index 7f0fc4d..43fab3c 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/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 | tested-only | `--total`, plus `scripts/check-abi-layout.sh` diff --git a/docs/LEAN4_RUST_CORRESPONDENCE.adoc b/docs/LEAN4_RUST_CORRESPONDENCE.adoc index 9aae04c..df0494b 100644 --- a/docs/LEAN4_RUST_CORRESPONDENCE.adoc +++ b/docs/LEAN4_RUST_CORRESPONDENCE.adoc @@ -478,6 +478,75 @@ 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: 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 +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 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 +`+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 diff --git a/impl/rust-cli/src/commands.rs b/impl/rust-cli/src/commands.rs index 4f91625..8c95999 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, &[], None)?; let op_id = op.id; state.record_operation(op); @@ -243,6 +241,71 @@ 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], + mode: Option, +) -> 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 options = fs::OpenOptions::new(); + options.write(true).create_new(true); + // A mode to restore is also the creation mode, so the file is never + // wider than it, even before the exact mode is set below. + #[cfg(unix)] + if let Some(mode) = mode { + use std::os::unix::fs::OpenOptionsExt; + options.mode(mode & 0o777); + } + let mut file = options.open(&full_path).context("touch failed")?; + let written = file.write_all(content).and_then(|()| { + // After the write: the creation mode is cut down by the umask, and + // writing can clear the set-user-ID and set-group-ID bits. + #[cfg(unix)] + if let Some(mode) = mode { + use std::os::unix::fs::PermissionsExt; + file.set_permissions(fs::Permissions::from_mode(mode & 0o7777))?; + } + Ok(()) + }); + if let Err(e) = written { + // A partial file must not outlive the failure: it was not there + // before, and it would block a retry. + drop(file); + let error = anyhow::Error::new(e).context("touch failed"); + return Err(match fs::remove_file(&full_path) { + Ok(()) => error, + Err(cleanup_error) => error.context(format!( + "the partial file {} could not be removed: {}", + path, cleanup_error + )), + }); + } + + 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,26 +333,7 @@ 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 full_path = state.resolve_path(path); - - // Runtime check of the Lean 4 precondition (see verification.rs) - verification::verify_delete_file(state.root(), path)?; - - if !full_path.exists() { - anyhow::bail!("Path does not exist (ENOENT)"); - } - if full_path.is_dir() { - anyhow::bail!("Path is a directory - use rmdir (EISDIR)"); - } - - // 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(); - - fs::remove_file(&full_path).context("rm failed")?; - - let op = - Operation::new(OperationType::DeleteFile, path.to_string(), None).with_undo_data(content); + let op = apply_rm(state, path)?; let op_id = op.id; state.record_operation(op); @@ -308,6 +352,38 @@ pub fn rm(state: &mut ShellState, path: &str, verbose: bool) -> Result<()> { 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) + verification::verify_delete_file(state.root(), path)?; + + if !full_path.exists() { + anyhow::bail!("Path does not exist (ENOENT)"); + } + if full_path.is_dir() { + anyhow::bail!("Path is a directory - use rmdir (EISDIR)"); + } + + // Store content for undo (pure read of the soon-to-be-deleted file; + // 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")?; + + Ok(Operation::new(OperationType::DeleteFile, path.to_string(), None).with_undo_data(content)) +} + /// Copy a file from source to destination. /// /// This operation is reversible via [`undo`] (deletes the copy) and corresponds @@ -337,6 +413,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); @@ -358,31 +458,41 @@ pub fn cp(state: &mut ShellState, src: &str, dst: &str, verbose: bool) -> Result anyhow::bail!("Parent of destination does not exist (ENOENT)"); } - // Execute operation - fs::copy(&src_path, &dst_path).context("cp failed")?; - - // Record in history: path = dst, undo_data = src path for reference + // 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) => 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 - 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 +524,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 +575,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 +608,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 +653,61 @@ 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) + .with_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<()> { + Ok(()) +} + +/// 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() { @@ -555,16 +718,15 @@ pub fn chmod(state: &mut ShellState, mode_str: &str, path: &str, verbose: bool) #[cfg(unix)] let old_mode = { use std::os::unix::fs::PermissionsExt; - let metadata = - fs::symlink_metadata(&file_path).context("chmod: failed to read metadata")?; + // Through a symbolic link, as `set_permissions` below goes: the mode + // to put back is the target's, not the link's own. + let metadata = fs::metadata(&file_path).context("chmod: failed to read metadata")?; metadata.permissions().mode() }; #[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 +738,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 +841,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 +870,45 @@ 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; + // Through a symbolic link, as `chown` below goes: the owner to put back + // is the target's, not the link's own. + let metadata = fs::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 +1007,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 => { @@ -881,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) @@ -993,6 +1154,29 @@ 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(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; + } + } println!( "{} {} {} {} {}", @@ -1017,15 +1201,33 @@ 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 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 @@ -1047,112 +1249,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) => { + // 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); } - } - - 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 +1279,460 @@ 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()) +} + +/// 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. +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 +/// 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 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 { + // 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()))) + } + OperationType::FileAppended => { + use std::io::{Seek, SeekFrom}; + let original_size = + u64::from_le_bytes(op.undo_data.as_ref()?.get(..8)?.try_into().ok()?); + let capture = || -> std::io::Result { + let mut file = crate::fs_pure::open_pure(&path)?; + file.seek(SeekFrom::Start(original_size))?; + Ok(match read(file, original_size)? { + RedoData::Content(appended) => RedoData::Appended(appended), + other => other, + }) + }; + Some(capture().unwrap_or_else(|e| RedoData::NotCaptured(e.to_string()))) + } + OperationType::Chmod => { + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + // Through a symbolic link, as `chmod` itself goes. + let mode = fs::metadata(&path).ok()?.permissions().mode(); + Some(RedoData::Mode(mode)) + } + #[cfg(not(unix))] + None + } + OperationType::Chown => { + #[cfg(unix)] + { + use std::os::unix::fs::MetadataExt; + // Through a symbolic link, as `chown` itself goes. + let metadata = fs::metadata(&path).ok()?; + Some(RedoData::Owner { + uid: metadata.uid(), + gid: metadata.gid(), + }) + } + #[cfg(not(unix))] + None + } + OperationType::SetVariable => { + // 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 + | 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 + ), + 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, + 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. + // Anything but captured content is refused: recreating the file + // empty would silently lose what the redirection wrote. + let Some(RedoData::Created { content, mode }) = &op.redo_data else { + return Err(missing()); + }; + apply_create_file(state, &op.path, content, *mode) + } + 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, exported }) = &op.redo_data else { + return Err(missing()); + }; + 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. + if *exported { + state.export_variable(op.path.clone()); + } else { + state.exported_vars.remove(&op.path); + } + Ok(new_op) + } + // 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) + .unwrap_or_else(|| state.apply_variable_absent(&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 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 + .seek(SeekFrom::Start(0)) + .and_then(|_| file.write_all(content)) + .and_then(|()| file.set_len(content.len() as u64)); + 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(), + }) + } + } +} + +/// 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(), + }), + } +} + +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 mut file = fs::OpenOptions::new() + .append(true) + .open(&full_path) + .context("Redo append failed")?; + 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) + .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 @@ -1380,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")) @@ -2668,6 +3229,148 @@ 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(_)) + )); + } + + /// `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(); + 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. + 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/fs_pure.rs b/impl/rust-cli/src/fs_pure.rs index d567260..0efc61e 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/redirection.rs b/impl/rust-cli/src/redirection.rs index 8c8c155..bc0bc76 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 7f75389..694c8b8 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,45 @@ 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 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`). + Mode(u32), + /// The ownership undo replaced (`Chown`). + Owner { uid: u32, gid: u32 }, + /// The value undo replaced, and whether the variable was exported + /// (`SetVariable`). `None` is a variable that was not set when it was + /// undone (removed outside the history, as when a function returns). + Variable { + value: Option, + 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. /// /// Each operation is assigned a unique UUID and records all data needed for reversal. @@ -195,6 +234,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 +252,7 @@ impl Operation { undo_data: None, undone: false, undone_by: None, + redo_data: None, } } @@ -630,40 +675,79 @@ 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 { + 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(), VariableValue::Scalar(value)); + match value { + Some(value) => { + self.variables.insert(name.clone(), value); + } + None => { + self.variables.remove(&name); + } + } - // 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), + ) + } + + /// 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) diff --git a/impl/rust-cli/tests/correspondence_tests.rs b/impl/rust-cli/tests/correspondence_tests.rs index 771ef53..bc2d996 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 0000000..117a0d5 --- /dev/null +++ b/impl/rust-cli/tests/redo_roundtrip_tests.rs @@ -0,0 +1,1206 @@ +// 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()); +} + +// --------------------------------------------------------------------------- +// 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::Created { .. }))); + 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"); +} + +/// `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" + ); +} + +/// 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"); + } +} + +/// 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 +// --------------------------------------------------------------------------- + +/// 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 +// --------------------------------------------------------------------------- + +/// 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") + }, + // 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", + |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 { + // `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, + ..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` 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()), + Just("export 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 + ); + } + } + } +} diff --git a/impl/rust-cli/tests/redo_write_failure_tests.rs b/impl/rust-cli/tests/redo_write_failure_tests.rs new file mode 100644 index 0000000..ff6cc39 --- /dev/null +++ b/impl/rust-cli/tests/redo_write_failure_tests.rs @@ -0,0 +1,199 @@ +// SPDX-License-Identifier: MPL-2.0 +// Copyright (c) Jonathan D.A. Jewell +//! 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 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 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"); + 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); +} 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 0000000..3d44b8d --- /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"); +} diff --git a/proofs/idris2/README.adoc b/proofs/idris2/README.adoc index a39d376..9f0d4f0 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 3dfc6d9..c05680b 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))