From b7b01494926a134f39daf8102281889702ddf62b Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 17:13:24 +0100 Subject: 0.5.3: session locking, UTF-8 and size guards for edit, correct character widths - exclusive flock on session files - edit refuses non-UTF-8 and >64 MiB files with actionable messages - editor uses unicode-width, expands tabs, handles resize --- src/session.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/session.rs') diff --git a/src/session.rs b/src/session.rs index 6e3c1c1..6416b56 100644 --- a/src/session.rs +++ b/src/session.rs @@ -2,6 +2,7 @@ use std::fs::{File, OpenOptions}; use std::hash::{Hash, Hasher}; use std::io::{BufRead, BufReader, Read, Write}; use std::os::unix::fs::PermissionsExt; +use std::os::unix::io::AsRawFd; use std::path::Path; use std::path::PathBuf; use std::time::{SystemTime, UNIX_EPOCH}; @@ -48,6 +49,18 @@ fn restrict(path: &Path, mode: u32) { let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)); } +/// Two processes appending to one transcript interleave messages and corrupt it +/// permanently, because history is never rewritten. Refuse the second one. +fn lock_exclusive(file: &File, id: &str) -> Result<(), String> { + let locked = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) }; + if locked != 0 { + return Err(format!( + "session {id} is already open in another process" + )); + } + Ok(()) +} + pub struct Session { pub id: String, pub model: String, @@ -84,6 +97,7 @@ impl Session { .open(&path) .map_err(|e| format!("{}: {e}", path.display()))?; restrict(&path, 0o600); + lock_exclusive(&file, &id)?; let cwd = std::env::current_dir() .map(|path| path.display().to_string()) @@ -164,6 +178,7 @@ impl Session { .map_err(|e| format!("{}: {e}", path.display()))?; // Older builds created these 0644. restrict(&path, 0o600); + lock_exclusive(&file, &session_id)?; Ok(( Session { -- cgit v1.2.3