aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/fs.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-15 17:13:24 +0100
committersalmonik <salmonik@tuta.io>2026-11-15 17:14:22 +0100
commitb7b01494926a134f39daf8102281889702ddf62b (patch)
tree813aeefd68352968b363aa7328f81eec796b8287 /src/tools/fs.rs
parent0.5.2: lazy-wrap redraw, and reasoning ends its own line (diff)
downloadagent686-b7b01494926a134f39daf8102281889702ddf62b.tar.gz
agent686-b7b01494926a134f39daf8102281889702ddf62b.zip
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
Diffstat (limited to 'src/tools/fs.rs')
-rw-r--r--src/tools/fs.rs17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/tools/fs.rs b/src/tools/fs.rs
index 947649f..ac0a475 100644
--- a/src/tools/fs.rs
+++ b/src/tools/fs.rs
@@ -10,6 +10,9 @@ const MAX_LINE_BYTES: usize = 4096;
10const MAX_READ_LINES: usize = 2000; 10const MAX_READ_LINES: usize = 2000;
11const MAX_MATCHES: usize = 200; 11const MAX_MATCHES: usize = 200;
12const MAX_SCAN_BYTES: u64 = 4 * 1024 * 1024; 12const MAX_SCAN_BYTES: u64 = 4 * 1024 * 1024;
13/// Edit rewrites the whole file, so it has to fit comfortably in the 32-bit
14/// address space together with its own copy.
15const MAX_EDIT_BYTES: u64 = 64 * 1024 * 1024;
13const PREVIEW_BUDGET: usize = 60; 16const PREVIEW_BUDGET: usize = 60;
14 17
15const SKIP_DIRS: &[&str] = &[ 18const SKIP_DIRS: &[&str] = &[
@@ -239,7 +242,19 @@ pub fn plan_edit(args: &Value) -> Result<Change, String> {
239 if old.is_empty() { 242 if old.is_empty() {
240 return Err("`old` must not be empty; use write to create a file".into()); 243 return Err("`old` must not be empty; use write to create a file".into());
241 } 244 }
242 let text = fs::read_to_string(&path).map_err(|e| format!("{path}: {e}"))?; 245 let bytes = fs::read(&path).map_err(|e| format!("{path}: {e}"))?;
246 if bytes.len() as u64 > MAX_EDIT_BYTES {
247 return Err(format!(
248 "{path} is {} MiB, too large to edit safely; use bash (sed) instead",
249 bytes.len() / (1024 * 1024)
250 ));
251 }
252 let text = String::from_utf8(bytes).map_err(|_| {
253 format!(
254 "{path} is not valid UTF-8, so edit cannot round-trip it without \
255 corrupting the file; use bash (sed, perl) instead"
256 )
257 })?;
243 // Fail before the approval prompt if the match is unusable. 258 // Fail before the approval prompt if the match is unusable.
244 locate(&text, &old)?; 259 locate(&text, &old)?;
245 Ok(Change::Edit { path, old, new }) 260 Ok(Change::Edit { path, old, new })