Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions cmux-tui/crates/cmux-tui-core/src/output_store/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,16 +331,9 @@ fn write_pass_at(mux: &Mux, store: &OutputStore, policy: PersistencePolicy, quit
Ok(()) => {
wrote = true;
log.state.lock().unwrap_or_else(PoisonError::into_inner).store_succeeded();
snapshot_written(
directory,
&log,
tail,
digest,
size,
&generation,
stream_end,
gaps,
);
let written =
Written { digest, size, generation: &generation, stream_end, gaps };
snapshot_written(directory, &log, tail, written);
}
Err(error) => {
report_store_error(&log, "output snapshot", &error);
Expand Down Expand Up @@ -532,18 +525,21 @@ enum Capture {
},
}

/// The snapshot at `stream_end` is in place: record it, and in snapshot mode
/// drop the tail it covers (the file, then the unwritten bytes before it).
fn snapshot_written(
directory: &Path,
log: &TerminalLog,
tail: bool,
/// A snapshot file just written: what it shows and where in the log's stream it ends.
struct Written<'a> {
/// The screen digest of the capture.
digest: u64,
size: (u16, u16),
generation: &Arc<str>,
generation: &'a Arc<str>,
stream_end: u64,
/// The log's stream-gap count at the capture.
gaps: u64,
) {
}

/// The snapshot at `stream_end` is in place: record it, and in snapshot mode
/// drop the tail it covers (the file, then the unwritten bytes before it).
fn snapshot_written(directory: &Path, log: &TerminalLog, tail: bool, written: Written<'_>) {
let Written { digest, size, generation, stream_end, gaps } = written;
let mut state = log.state.lock().unwrap_or_else(PoisonError::into_inner);
state.snapshot_digest = Some(digest);
state.snapshot_at = Some(Instant::now());
Expand Down
15 changes: 11 additions & 4 deletions cmux-tui/crates/cmux-tui-core/src/output_store/snapshot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::sync::{Arc, Mutex};

use super::super::{LogState, OutputStore, TerminalLog};
use super::{Plan, Seen, flush_tail, snapshot_written};
use super::{Plan, Seen, Written, flush_tail, snapshot_written};
use crate::resource::TerminalPublicId;

fn directory(name: &str) -> std::path::PathBuf {
Expand Down Expand Up @@ -209,7 +209,10 @@ fn a_snapshot_in_full_mode_removes_a_tail_left_from_snapshot_mode() {
let directory = directory("full-mode");
let log = log(b"");
super::super::tail::append(&directory, &log.terminal_id, "g", 0, 0, b"old", false).unwrap();
snapshot_written(&directory, &log, false, 9, (80, 24), &Arc::from("g"), 3, 0);
let generation: Arc<str> = Arc::from("g");
let written =
Written { digest: 9, size: (80, 24), generation: &generation, stream_end: 3, gaps: 0 };
snapshot_written(&directory, &log, false, written);
let left = super::super::tail::path(&directory, &log.terminal_id).exists();
let _ = std::fs::remove_dir_all(&directory);
assert!(!left, "the tail of snapshot mode outlived a full-mode snapshot");
Expand Down Expand Up @@ -382,7 +385,9 @@ fn a_drop_during_a_snapshot_write_still_asks_for_a_snapshot() {
}
// The snapshot's capture ended at 6; then a flood crossed the cap.
store.append(&id, &generation, &vec![b'x'; super::super::TAIL_MAX_UNWRITTEN_BYTES + 1], None);
snapshot_written(&directory, &log, true, 9, (80, 24), &generation, 6, 0);
let written =
Written { digest: 9, size: (80, 24), generation: &generation, stream_end: 6, gaps: 0 };
snapshot_written(&directory, &log, true, written);
let retry = log.state.lock().unwrap().snapshot_retry_in();
let _ = std::fs::remove_dir_all(&directory);
assert!(retry.is_some(), "the dropped bytes are in no snapshot, and none is asked for");
Expand All @@ -404,7 +409,9 @@ fn a_reconnect_during_a_snapshot_write_still_asks_for_a_snapshot() {
// The pass captured the old screen at offset 10; the reconnect replaces
// the terminal before the pass records its snapshot.
store.stream_gap(&id);
snapshot_written(&directory, &log, true, 9, (80, 24), &generation, 10, 0);
let written =
Written { digest: 9, size: (80, 24), generation: &generation, stream_end: 10, gaps: 0 };
snapshot_written(&directory, &log, true, written);
let retry = log.state.lock().unwrap().snapshot_retry_in();
let _ = std::fs::remove_dir_all(&directory);
assert!(retry.is_some(), "the snapshot of the old screen cleared the reconnect's request");
Expand Down
Loading