devclean-cli 0.5.0

Audit and safely remove rebuildable development artifacts
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
469
//! Persistent safety holds for artifacts that should remain restorable before deletion.

use std::ffi::OsStr;
use std::fs::{self, File, OpenOptions};
use std::io::Write as _;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::{Context, Result, bail};
use directories::BaseDirs;
use fs4::FileExt;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::model::Category;

const STORE_VERSION: u32 = 1;
const QUARANTINE_PREFIX: &str = ".devclean-quarantine-";

/// One restorable artifact held beside its original location.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuarantineEntry {
    /// Stable identifier used by list, restore, and purge commands.
    pub id: String,
    /// Original artifact location.
    pub original_path: PathBuf,
    /// Hidden adjacent location used during the safety hold.
    pub quarantine_path: PathBuf,
    /// Scanner category validated immediately before the move.
    pub category: Category,
    /// Scan-time allocated bytes retained by the hold.
    pub bytes: u64,
    /// Creation time as seconds since the Unix epoch.
    pub created_at_unix: u64,
    /// Time after which automatic purge is allowed.
    pub expires_at_unix: u64,
}

/// Outcome of purging expired or explicitly selected safety holds.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PurgeReport {
    /// Holds deleted successfully.
    pub purged: Vec<QuarantineEntry>,
    /// Hold-specific failures. Other valid holds are still processed.
    pub failures: Vec<String>,
    /// Allocated bytes represented by successful purges.
    pub purged_bytes: u64,
}

#[derive(Debug, Default, Serialize, Deserialize)]
struct QuarantineStore {
    version: u32,
    entries: Vec<QuarantineEntry>,
}

/// Returns the platform-local registry used to track safety holds.
///
/// # Errors
///
/// Returns an error when the operating system data directory is unavailable.
pub fn default_registry_path() -> Result<PathBuf> {
    let base = BaseDirs::new().context("platform data directory is unavailable")?;
    Ok(base.data_local_dir().join("devclean/quarantine.json"))
}

/// Moves a validated artifact into a hidden adjacent safety hold and records it.
///
/// The move does not reclaim disk space until the hold is purged.
///
/// # Errors
///
/// Returns an error if the path is not a real directory, cannot be moved atomically, or the
/// registry cannot be updated. A failed registry update attempts to restore the original path.
pub fn hold(
    path: &Path,
    category: Category,
    bytes: u64,
    retention: Duration,
    registry_path: Option<&Path>,
) -> Result<QuarantineEntry> {
    if retention.is_zero() {
        bail!("quarantine retention must be greater than zero");
    }
    let metadata = fs::symlink_metadata(path)
        .with_context(|| format!("failed to inspect candidate {}", path.display()))?;
    if !metadata.is_dir() || metadata.file_type().is_symlink() {
        bail!("candidate is not a real directory");
    }
    let parent = path.parent().context("candidate has no parent directory")?;
    let now = unix_time(SystemTime::now());
    let id = next_id();
    let quarantine_path = parent.join(format!("{QUARANTINE_PREFIX}{id}"));
    if quarantine_path.exists() {
        bail!("unique quarantine path unexpectedly exists");
    }

    fs::rename(path, &quarantine_path).with_context(|| {
        format!(
            "failed to move {} into an adjacent safety hold",
            path.display()
        )
    })?;
    let entry = QuarantineEntry {
        id,
        original_path: path.to_path_buf(),
        quarantine_path: quarantine_path.clone(),
        category,
        bytes,
        created_at_unix: now,
        expires_at_unix: now.saturating_add(retention.as_secs()),
    };

    let registry =
        registry_path.map_or_else(default_registry_path, |value| Ok(value.to_path_buf()));
    if let Err(error) = registry.and_then(|registry| {
        update_store(&registry, |store| {
            store.entries.push(entry.clone());
            Ok(())
        })
    }) {
        let restored = fs::rename(&quarantine_path, path).is_ok();
        bail!("failed to record safety hold: {error:#}; restored original path: {restored}");
    }
    Ok(entry)
}

/// Lists all recorded safety holds after pruning registry entries whose paths disappeared.
///
/// # Errors
///
/// Returns an error when the registry is unreadable or invalid.
pub fn list(registry_path: Option<&Path>) -> Result<Vec<QuarantineEntry>> {
    let registry =
        registry_path.map_or_else(default_registry_path, |value| Ok(value.to_path_buf()))?;
    let mut entries = Vec::new();
    update_store(&registry, |store| {
        store.entries.retain(|entry| entry.quarantine_path.exists());
        entries.clone_from(&store.entries);
        Ok(())
    })?;
    entries.sort_by_key(|entry| entry.expires_at_unix);
    Ok(entries)
}

/// Restores one safety hold to its original location.
///
/// # Errors
///
/// Returns an error for an unknown identifier, an unsafe registry entry, an occupied original
/// path, or a failed filesystem move.
pub fn restore(id: &str, registry_path: Option<&Path>) -> Result<QuarantineEntry> {
    let registry =
        registry_path.map_or_else(default_registry_path, |value| Ok(value.to_path_buf()))?;
    let mut restored = None;
    update_store(&registry, |store| {
        let index = store
            .entries
            .iter()
            .position(|entry| entry.id == id)
            .with_context(|| format!("unknown quarantine id `{id}`"))?;
        let entry = store.entries[index].clone();
        validate_entry(&entry)?;
        if entry.original_path.exists() {
            bail!(
                "cannot restore because original path exists: {}",
                entry.original_path.display()
            );
        }
        fs::rename(&entry.quarantine_path, &entry.original_path)
            .with_context(|| format!("failed to restore {}", entry.original_path.display()))?;
        store.entries.remove(index);
        restored = Some(entry);
        Ok(())
    })?;
    restored.context("quarantine restore completed without an entry")
}

/// Purges holds whose expiration time is at or before `now_unix`.
///
/// # Errors
///
/// Returns an error when the registry cannot be loaded or saved. Individual deletion failures
/// are returned in [`PurgeReport::failures`].
pub fn purge_expired(now_unix: u64, registry_path: Option<&Path>) -> Result<PurgeReport> {
    let registry =
        registry_path.map_or_else(default_registry_path, |value| Ok(value.to_path_buf()))?;
    let mut report = PurgeReport::default();
    update_store(&registry, |store| {
        let mut retained = Vec::new();
        for entry in store.entries.drain(..) {
            if entry.expires_at_unix > now_unix {
                retained.push(entry);
                continue;
            }
            if !entry.quarantine_path.exists() {
                continue;
            }
            if let Err(error) = validate_entry(&entry)
                .and_then(|()| fs::remove_dir_all(&entry.quarantine_path).map_err(Into::into))
            {
                report
                    .failures
                    .push(format!("{}: {error:#}", entry.quarantine_path.display()));
                retained.push(entry);
                continue;
            }
            report.purged_bytes = report.purged_bytes.saturating_add(entry.bytes);
            report.purged.push(entry);
        }
        store.entries = retained;
        Ok(())
    })?;
    Ok(report)
}

/// Permanently deletes one explicitly selected safety hold before or after expiry.
///
/// # Errors
///
/// Returns an error when the identifier is unknown or the registry cannot be loaded or saved.
/// A validation or filesystem deletion failure is returned in [`PurgeReport::failures`].
pub fn purge_selected(id: &str, registry_path: Option<&Path>) -> Result<PurgeReport> {
    let registry =
        registry_path.map_or_else(default_registry_path, |value| Ok(value.to_path_buf()))?;
    let mut report = PurgeReport::default();
    update_store(&registry, |store| {
        let index = store
            .entries
            .iter()
            .position(|entry| entry.id == id)
            .with_context(|| format!("unknown quarantine id `{id}`"))?;
        let entry = store.entries[index].clone();
        if !entry.quarantine_path.exists() {
            store.entries.remove(index);
            return Ok(());
        }
        if let Err(error) = validate_entry(&entry)
            .and_then(|()| fs::remove_dir_all(&entry.quarantine_path).map_err(Into::into))
        {
            report
                .failures
                .push(format!("{}: {error:#}", entry.quarantine_path.display()));
            return Ok(());
        }
        store.entries.remove(index);
        report.purged_bytes = entry.bytes;
        report.purged.push(entry);
        Ok(())
    })?;
    Ok(report)
}

fn validate_entry(entry: &QuarantineEntry) -> Result<()> {
    let expected_parent = entry
        .original_path
        .parent()
        .context("recorded original path has no parent")?;
    let expected_name = format!("{QUARANTINE_PREFIX}{}", entry.id);
    if entry.quarantine_path.parent() != Some(expected_parent)
        || entry.quarantine_path.file_name() != Some(OsStr::new(&expected_name))
    {
        bail!("registry entry does not point to an adjacent devclean quarantine");
    }
    let metadata = fs::symlink_metadata(&entry.quarantine_path)?;
    if !metadata.is_dir() || metadata.file_type().is_symlink() {
        bail!("quarantine path is not a real directory");
    }
    Ok(())
}

fn update_store(
    registry_path: &Path,
    operation: impl FnOnce(&mut QuarantineStore) -> Result<()>,
) -> Result<()> {
    let parent = registry_path
        .parent()
        .context("quarantine registry has no parent directory")?;
    fs::create_dir_all(parent)?;
    let lock_path = registry_path.with_extension("lock");
    let lock = open_private(&lock_path)?;
    FileExt::lock(&lock)?;

    let mut store = if registry_path.is_file() {
        let content = fs::read(registry_path)?;
        serde_json::from_slice(&content).context("invalid quarantine registry")?
    } else {
        QuarantineStore {
            version: STORE_VERSION,
            entries: Vec::new(),
        }
    };
    if store.version != STORE_VERSION {
        bail!("unsupported quarantine registry version {}", store.version);
    }
    operation(&mut store)?;
    save_store(registry_path, &store)?;
    FileExt::unlock(&lock)?;
    Ok(())
}

fn save_store(path: &Path, store: &QuarantineStore) -> Result<()> {
    let temporary = path.with_extension(format!("tmp-{}", std::process::id()));
    let mut options = OpenOptions::new();
    options.create(true).write(true).truncate(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt as _;
        options.mode(0o600);
    }
    let mut file = options
        .open(&temporary)
        .with_context(|| format!("failed to open {}", temporary.display()))?;
    serde_json::to_writer_pretty(&mut file, store)?;
    file.write_all(b"\n")?;
    file.sync_all()?;
    #[cfg(windows)]
    if path.exists() {
        fs::remove_file(path)?;
    }
    fs::rename(&temporary, path)?;
    Ok(())
}

fn open_private(path: &Path) -> Result<File> {
    let mut options = OpenOptions::new();
    options.create(true).read(true).write(true).truncate(false);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt as _;
        options.mode(0o600);
    }
    options
        .open(path)
        .with_context(|| format!("failed to open {}", path.display()))
}

fn next_id() -> String {
    Uuid::new_v4().to_string()
}

fn unix_time(value: SystemTime) -> u64 {
    value
        .duration_since(UNIX_EPOCH)
        .map_or(0, |duration| duration.as_secs())
}

#[cfg(test)]
mod tests {
    use tempfile::tempdir;

    use super::*;

    #[test]
    fn hold_and_restore_should_round_trip_directory() -> Result<()> {
        let temporary = tempdir()?;
        let original = temporary.path().join("node_modules");
        fs::create_dir_all(&original)?;
        let registry = temporary.path().join("state/quarantine.json");

        let entry = hold(
            &original,
            Category::NodeModules,
            42,
            Duration::from_secs(60),
            Some(&registry),
        )?;
        let restored = restore(&entry.id, Some(&registry))?;

        assert_eq!(restored.original_path, original);
        assert!(original.is_dir());
        Ok(())
    }

    #[test]
    fn purge_should_delete_expired_hold() -> Result<()> {
        let temporary = tempdir()?;
        let original = temporary.path().join("target");
        fs::create_dir_all(&original)?;
        let registry = temporary.path().join("state/quarantine.json");
        let entry = hold(
            &original,
            Category::RustTarget,
            99,
            Duration::from_secs(1),
            Some(&registry),
        )?;

        let report = purge_expired(u64::MAX, Some(&registry))?;

        assert_eq!(report.purged_bytes, 99);
        assert!(!entry.quarantine_path.exists());
        Ok(())
    }

    #[test]
    fn purge_selected_should_delete_only_requested_hold() -> Result<()> {
        let temporary = tempdir()?;
        let first = temporary.path().join("first/target");
        let second = temporary.path().join("second/target");
        fs::create_dir_all(&first)?;
        fs::create_dir_all(&second)?;
        let registry = temporary.path().join("state/quarantine.json");
        let first_entry = hold(
            &first,
            Category::RustTarget,
            40,
            Duration::from_secs(60),
            Some(&registry),
        )?;
        let second_entry = hold(
            &second,
            Category::RustTarget,
            60,
            Duration::from_secs(60),
            Some(&registry),
        )?;

        let report = purge_selected(&first_entry.id, Some(&registry))?;

        assert_eq!(report.purged_bytes, 40);
        assert!(!first_entry.quarantine_path.exists());
        assert!(second_entry.quarantine_path.exists());
        assert_eq!(list(Some(&registry))?.len(), 1);
        Ok(())
    }

    #[test]
    fn purge_selected_should_refuse_unknown_id_without_deleting_holds() -> Result<()> {
        let temporary = tempdir()?;
        let original = temporary.path().join("target");
        fs::create_dir_all(&original)?;
        let registry = temporary.path().join("state/quarantine.json");
        let entry = hold(
            &original,
            Category::RustTarget,
            99,
            Duration::from_secs(60),
            Some(&registry),
        )?;

        let result = purge_selected("missing", Some(&registry));

        assert!(result.is_err());
        assert!(entry.quarantine_path.exists());
        assert_eq!(list(Some(&registry))?.len(), 1);
        Ok(())
    }

    #[test]
    fn restore_should_refuse_occupied_original_path() -> Result<()> {
        let temporary = tempdir()?;
        let original = temporary.path().join("node_modules");
        fs::create_dir_all(&original)?;
        let registry = temporary.path().join("state/quarantine.json");
        let entry = hold(
            &original,
            Category::NodeModules,
            42,
            Duration::from_secs(60),
            Some(&registry),
        )?;
        fs::create_dir_all(&original)?;

        let result = restore(&entry.id, Some(&registry));

        assert!(result.is_err());
        Ok(())
    }
}