summaryrefslogtreecommitdiffstats
path: root/src/tools/fs.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-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 })