bigtext 0.1.1

Fast text editor for huge files (1-50+ GB): opens instantly, never freezes, streaming search/replace and atomic save
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! Crash-safe saving: stream the document to a temp file in the target's directory, fsync, then
//! atomically replace the target. The original is never touched until the final rename, and any
//! failure leaves it intact.
//!
//! On Windows the replacement uses `ReplaceFileW`, which keeps the original's attributes and
//! ACLs and handles the optional backup. The caller must release every mapping/handle of the
//! target first (Windows refuses to replace a memory-mapped file).

use std::fs::{self, File};
use std::io::{self, BufWriter, Write};
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use crate::encoding::{Codec, LineEnding, NlByte};
use crate::jobs::{CancelToken, Progress};
use crate::replace::{self, ReplaceStats, Template};
use crate::search::Matcher;
use crate::text::TextBuf;

const WRITE_BUF: usize = 8 * 1024 * 1024;

pub enum Transform<'a> {
    Identity,
    /// Convert line breaks to the given style.
    Eol(LineEnding),
    /// Replace every match in the range.
    Replace { matcher: &'a Matcher, template: &'a Template, range: std::ops::Range<u64> },
}

#[derive(Debug)]
pub enum SaveError {
    Cancelled,
    Io(String),
}

impl std::fmt::Display for SaveError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SaveError::Cancelled => write!(f, "cancelled"),
            SaveError::Io(s) => write!(f, "{s}"),
        }
    }
}

/// Temp file next to `target` (same volume, so the final rename is atomic).
pub fn temp_path_for(target: &Path) -> PathBuf {
    let dir = target.parent().map(Path::to_path_buf).unwrap_or_else(|| PathBuf::from("."));
    let name = target.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| "file".into());
    let nanos = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0);
    dir.join(format!(".{name}.{}-{nanos:x}.bigtext.tmp", std::process::id()))
}

pub fn backup_path_for(target: &Path) -> PathBuf {
    let mut s = target.as_os_str().to_os_string();
    s.push(".bak");
    PathBuf::from(s)
}

/// Human-readable message for an I/O error while saving.
pub fn describe(e: &io::Error, path: &Path) -> String {
    let p = crate::file::display_path(path);
    match e.raw_os_error() {
        Some(32) | Some(33) => format!("“{p}” is in use by another process. Close it there and try again."),
        Some(5) => format!("Access denied writing “{p}”. The file may be read-only or protected."),
        Some(112) | Some(39) => format!("Not enough disk space to save “{p}”."),
        Some(1224) => format!("“{p}” is still mapped by another program."),
        _ => match e.kind() {
            io::ErrorKind::PermissionDenied => format!("Access denied writing “{p}”."),
            io::ErrorKind::NotFound => format!("Folder not found for “{p}”."),
            _ => format!("Could not save “{p}”: {e}"),
        },
    }
}

/// Line-ending converter that streams with one unit of look-behind.
struct EolWriter<'w, W: Write> {
    out: &'w mut W,
    codec: Codec,
    target: Vec<u8>,
    pending_cr: bool,
    written: u64,
}

impl<W: Write> EolWriter<'_, W> {
    fn emit(&mut self, b: &[u8]) -> io::Result<()> {
        self.written += b.len() as u64;
        self.out.write_all(b)
    }

    fn write(&mut self, chunk: &[u8]) -> io::Result<()> {
        let unit = self.codec.unit();
        let cr = self.codec.cr_unit().to_vec();
        let lf: Vec<u8> = match self.codec.enc {
            crate::encoding::Encoding::Utf16Le => b"\n\0".to_vec(),
            crate::encoding::Encoding::Utf16Be => b"\0\n".to_vec(),
            _ => b"\n".to_vec(),
        };
        let target = self.target.clone();
        if unit == 1 {
            let mut i = 0;
            while i < chunk.len() {
                if self.pending_cr {
                    self.pending_cr = false;
                    if chunk[i] == b'\n' {
                        self.emit(&target)?;
                        i += 1;
                        continue;
                    } else if self.codec.nl == NlByte::Lf {
                        self.emit(b"\r")?;
                    } else {
                        self.emit(&target)?;
                    }
                }
                match memchr::memchr2(b'\r', b'\n', &chunk[i..]) {
                    Some(j) => {
                        self.emit(&chunk[i..i + j])?;
                        let c = chunk[i + j];
                        if c == b'\r' {
                            self.pending_cr = true;
                        } else if self.codec.nl == NlByte::Lf {
                            self.emit(&target)?;
                        } else {
                            self.emit(b"\n")?;
                        }
                        i += j + 1;
                    }
                    None => {
                        self.emit(&chunk[i..])?;
                        break;
                    }
                }
            }
        } else {
            for u in chunk.chunks(2) {
                if self.pending_cr {
                    self.pending_cr = false;
                    if u == lf.as_slice() {
                        self.emit(&target)?;
                        continue;
                    } else if self.codec.nl == NlByte::Lf {
                        self.emit(&cr)?;
                    } else {
                        self.emit(&target)?;
                    }
                }
                if u == cr.as_slice() {
                    self.pending_cr = true;
                } else if u == lf.as_slice() && self.codec.nl == NlByte::Lf {
                    self.emit(&target)?;
                } else {
                    self.emit(u)?;
                }
            }
        }
        Ok(())
    }

    fn finish(&mut self) -> io::Result<()> {
        if self.pending_cr {
            self.pending_cr = false;
            if self.codec.nl == NlByte::Lf {
                let cr = self.codec.cr_unit().to_vec();
                self.emit(&cr)?;
            } else {
                let t = self.target.clone();
                self.emit(&t)?;
            }
        }
        Ok(())
    }
}

/// Streams `buf` (a snapshot) through `transform` into a new temp file next to `target`, and
/// fsyncs it. On any error or cancellation the temp file is removed.
pub fn write_temp(buf: &TextBuf, target: &Path, transform: Transform, cancel: &CancelToken, progress: &Progress) -> Result<(PathBuf, ReplaceStats), SaveError> {
    let temp = temp_path_for(target);
    let file = File::options().write(true).create_new(true).open(&temp).map_err(|e| SaveError::Io(describe(&e, target)))?;
    let result = (|| -> Result<ReplaceStats, SaveError> {
        let mut w = BufWriter::with_capacity(WRITE_BUF, &file);
        let len = buf.len();
        progress.set_total(len);
        let io = |e: io::Error| SaveError::Io(describe(&e, target));
        let stats = match transform {
            Transform::Replace { matcher, template, range } => {
                match replace::replace_all_to(buf, matcher, template, range, &mut w, cancel, Some(progress)).map_err(io)? {
                    Some(s) => s,
                    None => return Err(SaveError::Cancelled),
                }
            }
            Transform::Identity | Transform::Eol(_) => {
                let mut sink = match &transform {
                    Transform::Eol(style) => Sink::Eol(EolWriter { out: &mut w, codec: buf.codec, target: buf.codec.line_break(*style), pending_cr: false, written: 0 }),
                    _ => Sink::Plain(&mut w, 0),
                };
                let mut err = None;
                let mut done = 0u64;
                // Read in bounded pieces so cancellation and progress stay responsive.
                const STEP: u64 = 16 * 1024 * 1024;
                let mut pos = 0;
                while pos < len {
                    if cancel.is_cancelled() {
                        return Err(SaveError::Cancelled);
                    }
                    let end = (pos + STEP).min(len);
                    let r = buf.for_each_chunk(pos..end, |_, b| {
                        if let Err(e) = sink.write(b) {
                            err = Some(e);
                            return false;
                        }
                        done += b.len() as u64;
                        true
                    });
                    if let Some(e) = err.take() {
                        return Err(io(e));
                    }
                    if r.is_none() {
                        return Err(SaveError::Io("Could not read the document while saving.".into()));
                    }
                    progress.set_done(done);
                    pos = end;
                }
                sink.finish().map_err(io)?;
                ReplaceStats { count: 0, bytes_in: len, bytes_out: sink.written() }
            }
        };
        w.flush().map_err(io)?;
        drop(w);
        file.sync_all().map_err(io)?;
        Ok(stats)
    })();
    drop(file);
    match result {
        Ok(s) => Ok((temp, s)),
        Err(e) => {
            let _ = fs::remove_file(&temp);
            Err(e)
        }
    }
}

enum Sink<'w, W: Write> {
    Plain(&'w mut W, u64),
    Eol(EolWriter<'w, W>),
}

impl<W: Write> Sink<'_, W> {
    fn write(&mut self, b: &[u8]) -> io::Result<()> {
        match self {
            Sink::Plain(w, n) => {
                *n += b.len() as u64;
                w.write_all(b)
            }
            Sink::Eol(e) => e.write(b),
        }
    }
    fn finish(&mut self) -> io::Result<()> {
        match self {
            Sink::Plain(..) => Ok(()),
            Sink::Eol(e) => e.finish(),
        }
    }
    fn written(&self) -> u64 {
        match self {
            Sink::Plain(_, n) => *n,
            Sink::Eol(e) => e.written,
        }
    }
}

/// Atomically moves `temp` over `target`, optionally keeping `target.bak`. Retries briefly on
/// transient sharing violations (virus scanners, indexers). On failure the temp file is removed
/// and the original is left as it was.
pub fn finalize(temp: &Path, target: &Path, backup: bool) -> Result<(), String> {
    let r = finalize_impl(temp, target, backup);
    if r.is_err() {
        let _ = fs::remove_file(temp);
    }
    r
}

#[cfg(windows)]
fn finalize_impl(temp: &Path, target: &Path, backup: bool) -> Result<(), String> {
    use std::os::windows::ffi::OsStrExt;
    #[link(name = "kernel32")]
    extern "system" {
        fn ReplaceFileW(replaced: *const u16, replacement: *const u16, backup: *const u16, flags: u32, exclude: *mut std::ffi::c_void, reserved: *mut std::ffi::c_void) -> i32;
        fn GetLastError() -> u32;
    }
    fn wide(p: &Path) -> Vec<u16> {
        let s = p.as_os_str();
        let st = s.to_string_lossy();
        let mut v: Vec<u16> = if p.is_absolute() && !st.starts_with(r"\\") {
            r"\\?\".encode_utf16().chain(s.encode_wide()).collect()
        } else if let Some(unc) = st.strip_prefix(r"\\").filter(|r| !r.starts_with(r"?\")) {
            format!(r"\\?\UNC\{unc}").encode_utf16().collect()
        } else {
            s.encode_wide().collect()
        };
        v.push(0);
        v
    }
    if !target.exists() {
        return rename_retry(temp, target).map_err(|e| describe(&e, target));
    }
    // ReplaceFileW always gets a backup name so a half-finished replace can be undone.
    let user_bak = backup_path_for(target);
    let internal_bak = temp.with_extension("bak.tmp");
    let bak = if backup { user_bak.clone() } else { internal_bak.clone() };
    if backup && bak.exists() {
        let _ = fs::remove_file(&bak);
    }
    let (wt, wr, wb) = (wide(target), wide(temp), wide(&bak));
    let mut last = 0u32;
    for attempt in 0..40 {
        // REPLACEFILE_IGNORE_MERGE_ERRORS = 2
        let ok = unsafe { ReplaceFileW(wt.as_ptr(), wr.as_ptr(), wb.as_ptr(), 2, std::ptr::null_mut(), std::ptr::null_mut()) };
        if ok != 0 {
            if !backup {
                let _ = fs::remove_file(&internal_bak);
            }
            return Ok(());
        }
        last = unsafe { GetLastError() };
        match last {
            // ERROR_UNABLE_TO_MOVE_REPLACEMENT_2: original was moved to the backup name. Restore it.
            1177 => {
                let _ = fs::rename(&bak, target);
                break;
            }
            // Sharing violation, access denied (scanners), unable to remove replaced, mapped file.
            32 | 5 | 1175 | 1224 if attempt < 39 => std::thread::sleep(Duration::from_millis(50)),
            _ => break,
        }
    }
    Err(describe(&io::Error::from_raw_os_error(last as i32), target))
}

#[cfg(windows)]
fn rename_retry(from: &Path, to: &Path) -> io::Result<()> {
    let mut last = None;
    for _ in 0..40 {
        match fs::rename(from, to) {
            Ok(()) => return Ok(()),
            Err(e) if matches!(e.raw_os_error(), Some(32) | Some(5)) => {
                last = Some(e);
                std::thread::sleep(Duration::from_millis(50));
            }
            Err(e) => return Err(e),
        }
    }
    Err(last.unwrap_or_else(|| io::Error::other("rename failed")))
}

#[cfg(not(windows))]
fn finalize_impl(temp: &Path, target: &Path, backup: bool) -> Result<(), String> {
    if let Ok(meta) = fs::metadata(target) {
        let _ = fs::set_permissions(temp, meta.permissions());
        if backup {
            let bak = backup_path_for(target);
            let _ = fs::remove_file(&bak);
            if fs::hard_link(target, &bak).is_err() {
                fs::copy(target, &bak).map_err(|e| describe(&e, &bak))?;
            }
        }
    }
    fs::rename(temp, target).map_err(|e| describe(&e, target))?;
    if let Some(dir) = target.parent() {
        if let Ok(d) = File::open(dir) {
            let _ = d.sync_all();
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::encoding::{Encoding, LineEnding};
    use crate::file::{OpenMode, Source};
    use crate::index;
    use crate::search::Query;
    use std::sync::Arc;

    fn open(path: &Path, codec: Codec) -> TextBuf {
        let src = Arc::new(Source::open(path, OpenMode::Auto).unwrap());
        let idx = index::build_blocking(&src, codec);
        let mut t = TextBuf::new(src, idx, codec);
        t.blocking = true;
        t
    }

    #[test]
    fn save_roundtrip_with_edits_and_backup() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("a.txt");
        fs::write(&path, b"hello world\n").unwrap();
        let mut t = open(&path, Codec::utf8());
        t.replace(0..5, b"HELLO");
        let (temp, _) = write_temp(&t, &path, Transform::Identity, &CancelToken::new(), &Progress::default()).unwrap();
        drop(t); // release the mapping before replacing (required on Windows)
        finalize(&temp, &path, true).unwrap();
        assert_eq!(fs::read(&path).unwrap(), b"HELLO world\n");
        assert_eq!(fs::read(backup_path_for(&path)).unwrap(), b"hello world\n");
        assert!(!temp.exists());
    }

    #[test]
    fn cancelled_save_leaves_original() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("b.txt");
        fs::write(&path, b"original").unwrap();
        let t = open(&path, Codec::utf8());
        let c = CancelToken::new();
        c.cancel();
        assert!(matches!(write_temp(&t, &path, Transform::Identity, &c, &Progress::default()), Err(SaveError::Cancelled)));
        drop(t);
        assert_eq!(fs::read(&path).unwrap(), b"original");
        assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 1, "temp file left behind");
    }

    #[test]
    fn eol_conversion() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("c.txt");
        fs::write(&path, b"a\r\nb\nc\rd\r\n").unwrap();
        let t = open(&path, Codec::utf8());
        let (temp, _) = write_temp(&t, &path, Transform::Eol(LineEnding::Lf), &CancelToken::new(), &Progress::default()).unwrap();
        assert_eq!(fs::read(&temp).unwrap(), b"a\nb\nc\rd\n");
        let (temp2, _) = write_temp(&t, &path, Transform::Eol(LineEnding::CrLf), &CancelToken::new(), &Progress::default()).unwrap();
        assert_eq!(fs::read(&temp2).unwrap(), b"a\r\nb\r\nc\rd\r\n");
        let u16c = Codec::new(Encoding::Utf16Le, crate::encoding::NlByte::Lf, 0);
        let p2 = dir.path().join("d.txt");
        fs::write(&p2, u16c.encode("x\r\ny\nz")).unwrap();
        let t2 = open(&p2, u16c);
        let (temp3, _) = write_temp(&t2, &p2, Transform::Eol(LineEnding::CrLf), &CancelToken::new(), &Progress::default()).unwrap();
        assert_eq!(fs::read(&temp3).unwrap(), u16c.encode("x\r\ny\r\nz"));
    }

    #[test]
    fn streaming_replace_to_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("e.txt");
        fs::write(&path, b"one two one two").unwrap();
        let t = open(&path, Codec::utf8());
        let q = Query { pattern: "one".into(), case_sensitive: true, ..Default::default() };
        let m = Matcher::new(&q, Codec::utf8()).unwrap();
        let tpl = Template::new("1", false);
        let (temp, stats) = write_temp(&t, &path, Transform::Replace { matcher: &m, template: &tpl, range: 0..t.len() }, &CancelToken::new(), &Progress::default()).unwrap();
        assert_eq!(stats.count, 2);
        drop(t);
        finalize(&temp, &path, false).unwrap();
        assert_eq!(fs::read(&path).unwrap(), b"1 two 1 two");
        assert!(!backup_path_for(&path).exists());
    }

    #[test]
    fn save_as_new_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("src.txt");
        fs::write(&path, b"data").unwrap();
        let t = open(&path, Codec::utf8());
        let target = dir.path().join("новый файл.txt");
        let (temp, _) = write_temp(&t, &target, Transform::Identity, &CancelToken::new(), &Progress::default()).unwrap();
        finalize(&temp, &target, false).unwrap();
        assert_eq!(fs::read(&target).unwrap(), b"data");
        assert_eq!(fs::read(&path).unwrap(), b"data");
    }
}