atomwrite 0.1.34

Atomic file operations CLI for LLM agents — read, write, edit, search, replace with NDJSON output
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
// SPDX-License-Identifier: MIT OR Apache-2.0

//! File deletion with optional backup before removal.
//!
//! Workload: I/O-bound (stat + optional hash + unlink + fsync).
//! Parallelism: **all** roots (multi-path files + recursive dirs) expand into
//! one path list via `collect_files_parallel`, then a single `rayon::par_iter`.
//! Empty-dir cleanup discovers candidates via WalkParallel (bottom-up remove
//! stays ordered).
//! (hash/backup/unlink independent per file). NDJSON emits in path-sorted
//! order after the join. Single-file stays sequential (coordination cost >
//! work). Bound: `--threads` / `--max-concurrency` (WalkParallel + rayon).

use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant, SystemTime};

use anyhow::{Context, Result};
use rayon::prelude::*;

use crate::checksum;
use crate::cli::{DeleteArgs, GlobalArgs};
use crate::commands::resolve_backup;
use crate::concurrency::should_parallelize;
use crate::error::AtomwriteError;
use crate::ndjson_types::{DeleteOutput, DryRunPlan, Summary};
use crate::output::NdjsonWriter;
use crate::platform;

fn parse_human_duration(s: &str) -> std::result::Result<Duration, AtomwriteError> {
    let s = s.trim();
    if s.is_empty() {
        return Err(AtomwriteError::InvalidInput {
            reason: "empty duration string".into(),
        });
    }
    let mut total_secs: u64 = 0;
    let mut num_buf = String::new();
    for ch in s.chars() {
        if ch.is_ascii_digit() {
            num_buf.push(ch);
        } else {
            let n: u64 = num_buf.parse().map_err(|_| AtomwriteError::InvalidInput {
                reason: format!("invalid number in duration: {s:?}"),
            })?;
            num_buf.clear();
            let multiplier = match ch {
                's' => 1,
                'm' => 60,
                'h' => 3600,
                'd' => 86400,
                'w' => 604800,
                _ => {
                    return Err(AtomwriteError::InvalidInput {
                        reason: format!("unknown duration suffix '{ch}' in {s:?}; use s/m/h/d/w"),
                    });
                }
            };
            total_secs += n * multiplier;
        }
    }
    if !num_buf.is_empty() {
        let n: u64 = num_buf.parse().map_err(|_| AtomwriteError::InvalidInput {
            reason: format!("invalid number in duration: {s:?}"),
        })?;
        total_secs += n;
    }
    if total_secs == 0 {
        return Err(AtomwriteError::InvalidInput {
            reason: format!("duration must be > 0: {s:?}"),
        });
    }
    Ok(Duration::from_secs(total_secs))
}

/// Outcome of processing one delete candidate (for ordered NDJSON emit).
enum DeleteItem {
    SkippedAge,
    Plan {
        path: String,
        size: u64,
    },
    Deleted {
        path: String,
        size: u64,
        hash: String,
    },
    Err(anyhow::Error),
}

/// Delete files with optional backup and dry-run support.
///
/// # Errors
///
/// Returns `AtomwriteError::NotFound` if the target file does not exist.
/// Returns `AtomwriteError::WorkspaceJail` if the path escapes the workspace.
/// Returns `AtomwriteError::Io` if deleting the file fails.
#[tracing::instrument(skip_all, fields(command = "delete"))]
pub fn cmd_delete(
    args: &DeleteArgs,
    global: &GlobalArgs,
    writer: &mut NdjsonWriter<impl Write>,
    defaults: &crate::config::DefaultsSection,
) -> Result<()> {
    let start = Instant::now();
    let workspace = global.resolve_workspace()?;
    let resolved = resolve_backup(&args.backup_opts, defaults);
    // v0.1.28 GAP-CLI-SURFACE-DRIFT: `--keep-backup` is a silent no-op for
    // delete (deletion backups are never auto-removed); surface it instead
    // of discarding the flag silently.
    let warnings: Vec<String> = if args.backup_opts.keep_backup {
        vec![
            "--keep-backup is redundant for delete: deletion backups are always preserved"
                .to_owned(),
        ]
    } else {
        Vec::new()
    };
    let mut deleted = 0u64;
    let mut _bytes_freed = 0u64;
    let mut skipped = 0u64;

    let age_threshold = match &args.older_than {
        Some(dur_str) => Some(parse_human_duration(dur_str)?),
        None => None,
    };

    let mut visited = 0u64;
    let max_size = global.effective_max_filesize();
    let dry_or_confirm = args.dry_run || args.confirm;
    let do_backup = resolved.backup;
    let retention = resolved.retention;

    // Phase 1 — expand every root into one file list (fail-fast NotFound).
    // Multi-root dirs use one WalkBuilder + `.add` (docs.rs), then one par_iter.
    let mut files_to_delete: Vec<PathBuf> = Vec::new();
    let mut dir_roots: Vec<PathBuf> = Vec::new();

    for path in &args.paths {
        if crate::signal::is_global_shutdown() {
            break;
        }

        let path = crate::path_safety::validate_path(path, &workspace)?;

        if !path.exists() {
            return Err(AtomwriteError::NotFound { path }.into());
        }

        if path.is_dir() && !args.recursive {
            return Err(AtomwriteError::InvalidInput {
                reason: format!("{} is a directory, use --recursive", path.display()),
            }
            .into());
        }

        if path.is_dir() {
            dir_roots.push(path);
        } else {
            files_to_delete.push(path);
        }
    }

    if !dir_roots.is_empty() && !crate::signal::is_global_shutdown() {
        let mut walker_builder = ignore::WalkBuilder::new(&dir_roots[0]);
        for d in dir_roots.iter().skip(1) {
            walker_builder.add(d);
        }
        walker_builder
            .hidden(!global.hidden)
            .git_ignore(!global.no_gitignore)
            .follow_links(global.follow_symlinks);
        crate::concurrency::apply_walk_threads(&mut walker_builder, global.threads);

        if !args.include.is_empty() || !args.exclude.is_empty() {
            let mut overrides = ignore::overrides::OverrideBuilder::new(&dir_roots[0]);
            for pat in &args.include {
                overrides.add(pat)?;
            }
            for pat in &args.exclude {
                overrides.add(&format!("!{pat}"))?;
            }
            walker_builder.overrides(overrides.build()?);
        }

        files_to_delete.extend(crate::concurrency::collect_files_parallel(&walker_builder));
    }

    crate::concurrency::sort_paths_parallel(&mut files_to_delete);
    files_to_delete.dedup();

    if crate::signal::is_global_shutdown() {
        return Ok(());
    }

    // Phase 2 — fan-out mutations (or sequential when a single file).
    let items: Vec<DeleteItem> = if should_parallelize(files_to_delete.len()) {
        files_to_delete
            .par_iter()
            .map(|file_path| {
                process_one_delete(
                    file_path,
                    age_threshold,
                    max_size,
                    dry_or_confirm,
                    do_backup,
                    retention,
                )
            })
            .collect()
    } else {
        files_to_delete
            .iter()
            .map(|file_path| {
                process_one_delete(
                    file_path,
                    age_threshold,
                    max_size,
                    dry_or_confirm,
                    do_backup,
                    retention,
                )
            })
            .collect()
    };

    for item in items {
        if crate::signal::is_global_shutdown() {
            tracing::info!(visited, deleted, "delete interrupted by signal");
            break;
        }
        match item {
            DeleteItem::SkippedAge => {
                visited += 1;
                skipped += 1;
            }
            DeleteItem::Plan {
                path: path_str,
                size,
            } => {
                visited += 1;
                writer.write_event(&DryRunPlan {
                    r#type: "plan",
                    operation: "delete".into(),
                    path: path_str,
                    would_modify: true,
                    details: Some(format!("{size} bytes")),
                })?;
                deleted += 1;
                _bytes_freed += size;
            }
            DeleteItem::Deleted {
                path: path_str,
                size,
                hash,
            } => {
                visited += 1;
                deleted += 1;
                _bytes_freed += size;
                writer.write_event(&DeleteOutput {
                    r#type: "deleted",
                    path: path_str,
                    bytes: size,
                    checksum_before: hash,
                    elapsed_ms: start.elapsed().as_millis() as u64,
                    warnings: warnings.clone(),
                })?;
            }
            DeleteItem::Err(e) => return Err(e),
        }
    }

    // Phase 3 — remove emptied directory trees (independent roots fan out).
    if !args.dry_run && !crate::signal::is_global_shutdown() {
        let cleanup: Vec<Result<(), anyhow::Error>> = if should_parallelize(dir_roots.len()) {
            dir_roots
                .par_iter()
                .map(|path| cleanup_dir_root(path, &files_to_delete))
                .collect()
        } else {
            dir_roots
                .iter()
                .map(|path| cleanup_dir_root(path, &files_to_delete))
                .collect()
        };
        for r in cleanup {
            r?;
        }
    }

    // On cooperative cancel, skip summary so main can emit the shutdown
    // banner + signal exit code (same contract as search/list/count).
    if crate::signal::is_global_shutdown() {
        return Ok(());
    }

    writer.write_event(&Summary {
        r#type: "summary",
        files_visited: visited,
        files_matched: deleted,
        files_modified: Some(deleted),
        files_skipped: Some(skipped + visited.saturating_sub(deleted + skipped)),
        total_matches: None,
        total_replacements: None,
        elapsed_ms: start.elapsed().as_millis() as u64,
    })?;

    Ok(())
}

fn cleanup_dir_root(path: &Path, files_to_delete: &[PathBuf]) -> Result<()> {
    let files_under = files_to_delete.iter().any(|f| f.starts_with(path));
    if !files_under {
        std::fs::remove_dir_all(path)
            .with_context(|| format!("cannot remove directory {}", path.display()))?;
    } else {
        // Parallel discovery of empty-candidate dirs; bottom-up remove stays
        // sequential (parent depends on children being gone first).
        let mut builder = ignore::WalkBuilder::new(path);
        builder.hidden(true).git_ignore(false);
        crate::concurrency::apply_walk_threads(&mut builder, None);
        let mut dirs_to_remove: Vec<PathBuf> =
            crate::concurrency::collect_mapped_parallel(&builder, |entry| {
                if entry.file_type().is_some_and(|ft| ft.is_dir()) {
                    Some(entry.path().to_path_buf())
                } else {
                    None
                }
            });
        // Deepest paths first ≈ contents_first semantics for remove_dir.
        dirs_to_remove.sort_by(|a, b| {
            let da = a.components().count();
            let db = b.components().count();
            db.cmp(&da).then_with(|| b.cmp(a))
        });
        for dir in &dirs_to_remove {
            let _ = std::fs::remove_dir(dir);
        }
    }
    Ok(())
}

fn process_one_delete(
    file_path: &Path,
    age_threshold: Option<Duration>,
    max_size: u64,
    dry_or_confirm: bool,
    do_backup: bool,
    retention: u8,
) -> DeleteItem {
    if crate::signal::is_global_shutdown() {
        return DeleteItem::Err(crate::signal::cancelled_error("delete cancelled by signal").into());
    }

    let path_str = file_path.display().to_string();
    let meta = match std::fs::metadata(file_path) {
        Ok(m) => m,
        Err(e) => {
            return DeleteItem::Err(
                anyhow::Error::new(e).context(format!("cannot stat {path_str}")),
            );
        }
    };

    if let Some(threshold) = age_threshold {
        let modified = meta.modified().unwrap_or(SystemTime::UNIX_EPOCH);
        let age = SystemTime::now()
            .duration_since(modified)
            .unwrap_or(Duration::ZERO);
        if age < threshold {
            return DeleteItem::SkippedAge;
        }
    }

    let hash = match checksum::hash_file(file_path, max_size) {
        Ok(h) => h,
        Err(e) => return DeleteItem::Err(e),
    };
    let size = meta.len();

    if dry_or_confirm {
        return DeleteItem::Plan {
            path: path_str,
            size,
        };
    }

    if do_backup {
        if let Err(e) = crate::atomic::create_backup(file_path, retention) {
            return DeleteItem::Err(e);
        }
    }

    if let Err(e) = std::fs::remove_file(file_path) {
        return DeleteItem::Err(
            anyhow::Error::new(e).context(format!("cannot delete {}", file_path.display())),
        );
    }

    if let Some(parent) = file_path.parent() {
        if let Err(e) = platform::fsync_dir(parent) {
            tracing::warn!(
                path = %parent.display(),
                error = %e,
                "fsync_dir after delete failed"
            );
        }
    }

    DeleteItem::Deleted {
        path: path_str,
        size,
        hash,
    }
}