Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

packaging: keep a precompressed sibling valid through the copy

RoversXa10c90cdeacb
diff --git a/ci/deploy.sh b/ci/deploy.sh
index 62fca89a..3ea99a2b 100755
--- a/ci/deploy.sh
+++ b/ci/deploy.sh
@@ -51,12 +51,15 @@ case $OSTYPE in
# inside the bundle finds it at <exe>/../Resources/web.
if [[ -f thinkterm-web/www/pkg/thinkterm_web.js ]] ; then
mkdir -p $zipdir/ThinkTerm.app/Contents/Resources/web
- cp thinkterm-web/www/index.html thinkterm-web/www/app.js $zipdir/ThinkTerm.app/Contents/Resources/web/
+ # -p throughout: the server decides whether a .gz still stands for
+ # its file by comparing their modification times, and a copy that
+ # rewrites them can make a stale sibling look current.
+ cp -p thinkterm-web/www/index.html thinkterm-web/www/app.js $zipdir/ThinkTerm.app/Contents/Resources/web/
# The precompressed siblings, when ci/build-web.sh made them.
for gz in thinkterm-web/www/*.gz ; do
- [[ -f "$gz" ]] && cp "$gz" $zipdir/ThinkTerm.app/Contents/Resources/web/
+ [[ -f "$gz" ]] && cp -p "$gz" $zipdir/ThinkTerm.app/Contents/Resources/web/
done
- cp -r thinkterm-web/www/pkg thinkterm-web/www/fonts $zipdir/ThinkTerm.app/Contents/Resources/web/
+ cp -Rp thinkterm-web/www/pkg thinkterm-web/www/fonts $zipdir/ThinkTerm.app/Contents/Resources/web/
fi
tic -xe wezterm -o $zipdir/ThinkTerm.app/Contents/Resources/terminfo termwiz/data/wezterm.terminfo
@@ -572,12 +575,13 @@ EOF
# The browser client, when ci/build-web.sh ran before this: the
# server finds it at <exe>/../share/thinkterm/web.
if [[ -f thinkterm-web/www/pkg/thinkterm_web.js ]] ; then
- install -Dm644 -t "$tardir/share/thinkterm/web" thinkterm-web/www/index.html thinkterm-web/www/app.js
+ # -p keeps the modification times the .gz freshness check reads.
+ install -Dpm644 -t "$tardir/share/thinkterm/web" thinkterm-web/www/index.html thinkterm-web/www/app.js
for gz in thinkterm-web/www/*.gz ; do
- [[ -f "$gz" ]] && install -Dm644 -t "$tardir/share/thinkterm/web" "$gz"
+ [[ -f "$gz" ]] && install -Dpm644 -t "$tardir/share/thinkterm/web" "$gz"
done
- install -Dm644 -t "$tardir/share/thinkterm/web/pkg" thinkterm-web/www/pkg/*
- install -Dm644 -t "$tardir/share/thinkterm/web/fonts" thinkterm-web/www/fonts/*
+ install -Dpm644 -t "$tardir/share/thinkterm/web/pkg" thinkterm-web/www/pkg/*
+ install -Dpm644 -t "$tardir/share/thinkterm/web/fonts" thinkterm-web/www/fonts/*
fi
if [[ "$variant" == thinkterm ]] ; then
install -Dm755 -t "$tardir/bin" assets/open-thinkterm-here assets/open-wezterm-here
diff --git a/install.sh b/install.sh
index 16769fb9..af470521 100755
--- a/install.sh
+++ b/install.sh
@@ -560,7 +560,9 @@ done
# A tarball without one leaves an installed bundle alone.
if [ -d "$root/share/thinkterm/web" ]; then
rm -rf "$sharedir/thinkterm/web"
- cp -R "$root/share/thinkterm/web" "$sharedir/thinkterm/web"
+ # -p: the server compares a .gz against its file by modification
+ # time, so an install that rewrites them can mislead it.
+ cp -Rp "$root/share/thinkterm/web" "$sharedir/thinkterm/web"
fi
# Written last, so it only ever describes a completed install.
diff --git a/wezterm-mux-server-impl/src/web_http.rs b/wezterm-mux-server-impl/src/web_http.rs
index efada07c..8223f39e 100644
--- a/wezterm-mux-server-impl/src/web_http.rs
+++ b/wezterm-mux-server-impl/src/web_http.rs
@@ -13,8 +13,9 @@ use crate::web_auth::WEB_TOKENS;
use crate::web_stream::{Prefixed, WebStream};
use base64::Engine;
use config::{default_port, is_loopback_host, split_authority, Origin};
-use futures::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
+use futures::io::{AsyncRead, AsyncReadExt, AsyncSeekExt, AsyncWrite, AsyncWriteExt};
use sha1::{Digest, Sha1};
+use std::io::SeekFrom;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
@@ -511,13 +512,32 @@ fn already_held(header: Option<&str>, tag: &str) -> bool {
})
}
+/// The uncompressed length gzip records in its last four bytes.
+///
+/// This is a length, not a digest: it is taken modulo 2^32 and says
+/// nothing about the content, so an edit that keeps the size passes it.
+/// It answers the question modification times cannot -- whether a copy
+/// that rewrote the timestamps left a sibling standing for other bytes --
+/// and the time comparison answers the one this cannot. Both are cheap,
+/// and each catches what the other misses.
+async fn gzip_uncompressed_len(path: &Path) -> Option<u64> {
+ let mut file = smol::fs::File::open(path).await.ok()?;
+ file.seek(SeekFrom::End(-4)).await.ok()?;
+ let mut trailer = [0u8; 4];
+ file.read_exact(&mut trailer).await.ok()?;
+ Some(u32::from_le_bytes(trailer) as u64)
+}
+
/// The `<name>.gz` beside a bundle file, when it is no older than the file
/// it stands for. `ci/build-web.sh` writes these, so serving one costs the
/// same as serving any other file: a chunk at a time, nothing held.
///
-/// A sibling older than its original is ignored. That happens when someone
-/// rebuilds the wasm without rerunning the script, and serving the previous
-/// build compressed is worse than serving this one plainly.
+/// A sibling is used only when two things agree that it stands for the
+/// file beside it: it is no older, and the length in its trailer is the
+/// length of that file. Either alone can be fooled -- a copy that rewrites
+/// timestamps defeats the first, an edit that keeps the size defeats the
+/// second -- and serving the previous build compressed is worse than
+/// serving this one plainly, so a sibling that fails either is ignored.
async fn precompressed(path: &Path) -> Option<PathBuf> {
let mut name = path.as_os_str().to_os_string();
name.push(".gz");
@@ -527,7 +547,13 @@ async fn precompressed(path: &Path) -> Option<PathBuf> {
return None;
}
let plain = smol::fs::metadata(path).await.ok()?;
- (meta.modified().ok()? >= plain.modified().ok()?).then_some(packed)
+ if meta.modified().ok()? < plain.modified().ok()? {
+ return None;
+ }
+ // The trailer counts modulo 2^32, so compare the file's length the
+ // same way rather than refusing everything past 4 GiB.
+ let wrapped = plain.len() % (1u64 << 32);
+ (gzip_uncompressed_len(&packed).await? == wrapped).then_some(packed)
}
/// Send a file from the bundle without holding more than one piece of it:
@@ -1289,6 +1315,93 @@ mod end_to_end {
});
}
+ /// Copy a file the way the packaging scripts do, timestamps and all.
+ fn copy_preserving_times(from: &Path, to: &Path) {
+ let status = std::process::Command::new("cp")
+ .arg("-p")
+ .arg(from)
+ .arg(to)
+ .status()
+ .unwrap();
+ assert!(status.success(), "cp -p failed");
+ }
+
+ #[test]
+ fn a_sibling_survives_the_copy_the_packaging_scripts_do() {
+ let build = tempfile::tempdir().unwrap();
+ write_sibling(build.path(), "app.js", b"the build\n");
+ // ci/deploy.sh copies the plain file first and the sibling after,
+ // which without -p would leave the sibling looking newer than it is
+ // -- and the other order would leave it looking stale.
+ let shipped = tempfile::tempdir().unwrap();
+ copy_preserving_times(&build.path().join("app.js"), &shipped.path().join("app.js"));
+ copy_preserving_times(
+ &build.path().join("app.js.gz"),
+ &shipped.path().join("app.js.gz"),
+ );
+ smol::block_on(async {
+ assert!(
+ precompressed(&shipped.path().join("app.js")).await.is_some(),
+ "a packaged sibling was refused"
+ );
+ });
+ }
+
+ #[test]
+ fn a_rebuild_that_kept_the_length_still_rejects_the_old_sibling() {
+ let dir = tempfile::tempdir().unwrap();
+ write_sibling(dir.path(), "app.js", b"version = \"1\"\n");
+ // The same number of bytes, so the trailer still matches: only the
+ // modification time can tell that the sibling is a build behind.
+ std::thread::sleep(std::time::Duration::from_millis(20));
+ std::fs::write(dir.path().join("app.js"), b"version = \"2\"\n").unwrap();
+ let path = dir.path().join("app.js");
+ smol::block_on(async {
+ // The trailer still matches, so this proves the time comparison
+ // is what rejects the sibling -- the length cannot.
+ assert_eq!(
+ gzip_uncompressed_len(&dir.path().join("app.js.gz"))
+ .await
+ .unwrap(),
+ std::fs::metadata(&path).unwrap().len(),
+ "this test only means something while the length agrees"
+ );
+ assert!(
+ precompressed(&path).await.is_none(),
+ "a same-length rebuild was served from the old sibling"
+ );
+ });
+ }
+
+ #[test]
+ fn a_sibling_that_stands_for_other_bytes_is_refused_however_new_it_looks() {
+ let dir = tempfile::tempdir().unwrap();
+ // The sibling holds a previous, shorter build; the packaging copy
+ // then gave it the newer timestamp, so only its trailer gives it
+ // away.
+ std::fs::write(dir.path().join("app.js"), b"the second build, longer\n").unwrap();
+ let packed = std::process::Command::new("gzip")
+ .args(["-9", "-n", "-c"])
+ .stdin(std::process::Stdio::piped())
+ .stdout(std::process::Stdio::piped())
+ .spawn()
+ .and_then(|mut child| {
+ use std::io::Write;
+ child.stdin.take().unwrap().write_all(b"the first build\n")?;
+ child.wait_with_output()
+ })
+ .unwrap();
+ std::thread::sleep(std::time::Duration::from_millis(20));
+ std::fs::write(dir.path().join("app.js.gz"), packed.stdout).unwrap();
+ let path = dir.path().join("app.js");
+ smol::block_on(async {
+ assert!(
+ precompressed(&path).await.is_none(),
+ "a sibling for other bytes was trusted because it looked newer"
+ );
+ });
+ }
+
#[test]
fn a_sibling_older_than_its_file_is_ignored() {
let dir = tempfile::tempdir().unwrap();