cargo-reclaim 0.2.2

Safe Cargo cleanup for target directories, stale artifacts, and Cargo home caches
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
use std::fs;
use std::path::Path;
use std::time::SystemTime;

use rayon::prelude::*;
use serde::{Deserialize, Serialize};

use crate::inventory::InventoryOptions;
use crate::model::{
    ArtifactClass, PLAN_SCHEMA_VERSION, PathKind, PathSnapshot, Plan, PlanAction, PlanEntry,
    PlanInput, PlanSkip, PlanSkipReason, PlanTotals, TargetEvidence,
};
use crate::planner::{PlannerOptions, WholeTargetMode};
use crate::policy::PolicyKind;
use crate::scanner::ScannerOptions;

use super::PERSISTED_PLAN_SCHEMA_VERSION;
use super::error::{PlanPersistenceError, PlanPersistenceResult};
use super::fingerprint_path;
use super::id::PlanId;
use super::time::PersistedTimestamp;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PersistedPlan {
    pub schema_version: u16,
    pub id: PlanId,
    #[serde(flatten)]
    pub body: PersistedPlanBody,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PersistedPlanBody {
    pub created_at: PersistedTimestamp,
    pub expires_at: PersistedTimestamp,
    pub interactive_selection_modified: bool,
    pub invocation: PlanInvocation,
    pub plan: PersistedPlanSnapshot,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SavePlanOptions {
    pub created_at: SystemTime,
    pub expires_at: SystemTime,
    pub interactive_selection_modified: bool,
    pub invocation: PlanInvocation,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PlanInvocation {
    pub command: PlanCommandKind,
    pub policy: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub config_path: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub config_version: Option<u16>,
    pub scanner_options: PersistedScannerOptions,
    pub inventory_options: PersistedInventoryOptions,
    #[serde(default)]
    pub planner_options: PersistedPlannerOptions,
}

impl PlanInvocation {
    pub fn new(
        command: PlanCommandKind,
        policy: PolicyKind,
        scanner_options: &ScannerOptions,
        inventory_options: &InventoryOptions,
        planner_options: &PlannerOptions,
    ) -> Self {
        Self {
            command,
            policy: policy_label(policy).to_string(),
            config_path: None,
            config_version: None,
            scanner_options: PersistedScannerOptions::from_options(scanner_options),
            inventory_options: PersistedInventoryOptions::from_options(inventory_options),
            planner_options: PersistedPlannerOptions::from_options(planner_options),
        }
    }

    pub fn with_config(mut self, path: &Path, version: u16) -> Self {
        self.config_path = Some(path_string(path));
        self.config_version = Some(version);
        self
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PlanCommandKind {
    Plan,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PersistedScannerOptions {
    pub follow_symlinks: bool,
    pub allow_name_only_targets: bool,
    pub cross_filesystems: bool,
    pub ignored_paths: Vec<String>,
    pub skipped_paths: Vec<String>,
}

impl PersistedScannerOptions {
    fn from_options(options: &ScannerOptions) -> Self {
        Self {
            follow_symlinks: options.follow_symlinks,
            allow_name_only_targets: options.allow_name_only_targets,
            cross_filesystems: options.cross_filesystems,
            ignored_paths: options.ignored_paths.iter().map(path_string).collect(),
            skipped_paths: options.skipped_paths.iter().map(path_string).collect(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PersistedInventoryOptions {
    pub follow_symlinks: bool,
    #[serde(default, skip_serializing_if = "is_false")]
    pub deep_target_scan: bool,
    #[serde(default, skip_serializing_if = "is_false")]
    pub deep_directory_measurement: bool,
}

impl PersistedInventoryOptions {
    fn from_options(options: &InventoryOptions) -> Self {
        Self {
            follow_symlinks: options.follow_symlinks,
            deep_target_scan: options.deep_target_scan,
            deep_directory_measurement: options.deep_directory_measurement,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct PersistedPlannerOptions {
    pub recent_write_keep_window_seconds: Option<u64>,
    pub keep_size_bytes: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target_size_goal_bytes: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target_free_disk_bytes: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub minimum_reclaim_bytes: Option<u64>,
    #[serde(default)]
    pub keep_rustc_hashes: Vec<u64>,
    #[serde(default, skip_serializing_if = "is_false")]
    pub keep_installed_toolchains: bool,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub keep_toolchains: Vec<String>,
    #[serde(default, skip_serializing_if = "is_default_whole_target_mode")]
    pub whole_target_mode: PersistedWholeTargetMode,
}

impl PersistedPlannerOptions {
    fn from_options(options: &PlannerOptions) -> Self {
        Self {
            recent_write_keep_window_seconds: options
                .recent_write_keep_window
                .map(|duration| duration.as_secs()),
            keep_size_bytes: options.keep_size_bytes,
            target_size_goal_bytes: options.target_size_goal_bytes,
            target_free_disk_bytes: options.target_free_disk_bytes,
            minimum_reclaim_bytes: options.minimum_reclaim_bytes,
            keep_rustc_hashes: options.keep_rustc_hashes.clone(),
            keep_installed_toolchains: options.keep_installed_toolchains,
            keep_toolchains: options.keep_toolchains.clone(),
            whole_target_mode: PersistedWholeTargetMode::from_mode(options.whole_target_mode),
        }
    }
}

fn is_false(value: &bool) -> bool {
    !*value
}

fn is_zero(value: &usize) -> bool {
    *value == 0
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PersistedWholeTargetMode {
    #[default]
    Off,
    Confirm,
    DeleteConfirmed,
}

impl PersistedWholeTargetMode {
    fn from_mode(mode: WholeTargetMode) -> Self {
        match mode {
            WholeTargetMode::Off => Self::Off,
            WholeTargetMode::Confirm => Self::Confirm,
            WholeTargetMode::DeleteConfirmed => Self::DeleteConfirmed,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PersistedPlanSnapshot {
    pub schema_version: u16,
    pub input: PersistedPlanInput,
    pub entries: Vec<PersistedPlanEntry>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub skipped_paths: Vec<PersistedPlanSkip>,
    pub totals: PersistedPlanTotals,
}

impl PersistedPlanSnapshot {
    fn from_plan(plan: &Plan) -> PlanPersistenceResult<Self> {
        Ok(Self {
            schema_version: plan.schema_version,
            input: PersistedPlanInput::from_input(&plan.input),
            entries: plan
                .entries
                .par_iter()
                .map(PersistedPlanEntry::from_entry)
                .collect::<PlanPersistenceResult<Vec<_>>>()?,
            skipped_paths: plan
                .skipped_paths
                .iter()
                .map(PersistedPlanSkip::from_skip)
                .collect(),
            totals: PersistedPlanTotals::from_totals(plan.totals),
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PersistedPlanInput {
    pub roots: Vec<String>,
}

impl PersistedPlanInput {
    fn from_input(input: &PlanInput) -> Self {
        Self {
            roots: input.roots.iter().map(path_string).collect(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PersistedPlanSkip {
    pub path: String,
    pub reason: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

impl PersistedPlanSkip {
    fn from_skip(skip: &PlanSkip) -> Self {
        Self {
            path: path_string(&skip.path),
            reason: skip_reason_label(skip.reason).to_string(),
            message: skip.message.clone(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PersistedPlanEntry {
    pub snapshot: PersistedPathSnapshot,
    pub artifact_class: String,
    pub evidence: PersistedEvidence,
    pub action: String,
    pub policy_reason: String,
    pub requires_confirmation: bool,
}

impl PersistedPlanEntry {
    fn from_entry(entry: &PlanEntry) -> PlanPersistenceResult<Self> {
        Ok(Self {
            snapshot: PersistedPathSnapshot::from_snapshot(
                &entry.snapshot,
                content_fingerprint_for_entry(entry)?,
            ),
            artifact_class: artifact_label(entry.artifact_class).to_string(),
            evidence: PersistedEvidence::from_evidence(&entry.evidence),
            action: action_label(&entry.action).to_string(),
            policy_reason: entry.policy_reason.clone(),
            requires_confirmation: entry.requires_confirmation,
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PersistedPathSnapshot {
    pub path: String,
    pub size_bytes: u64,
    pub path_kind: String,
    pub modified: Option<PersistedTimestamp>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub content_fingerprint: Option<String>,
}

impl PersistedPathSnapshot {
    fn from_snapshot(snapshot: &PathSnapshot, content_fingerprint: Option<String>) -> Self {
        Self {
            path: path_string(&snapshot.path),
            size_bytes: snapshot.size_bytes,
            path_kind: path_kind_label(snapshot.path_kind).to_string(),
            modified: snapshot
                .modified
                .and_then(|modified| PersistedTimestamp::from_system_time(modified).ok()),
            content_fingerprint,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PersistedPlanTotals {
    pub entry_count: usize,
    pub total_bytes: u64,
    pub preserved_count: usize,
    pub delete_candidate_count: usize,
    #[serde(default, skip_serializing_if = "is_zero")]
    pub skipped_path_count: usize,
}

impl PersistedPlanTotals {
    fn from_totals(totals: PlanTotals) -> Self {
        Self {
            entry_count: totals.entry_count,
            total_bytes: totals.total_bytes,
            preserved_count: totals.preserved_count,
            delete_candidate_count: totals.delete_candidate_count,
            skipped_path_count: totals.skipped_path_count,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PersistedEvidence {
    pub kind: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub marker: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub project_manifest: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub matched_name: Option<String>,
}

impl PersistedEvidence {
    fn from_evidence(evidence: &TargetEvidence) -> Self {
        match evidence {
            TargetEvidence::StrongMarker { marker } => Self {
                kind: "strong_marker".to_string(),
                marker: Some(marker.clone()),
                source: None,
                project_manifest: None,
                matched_name: None,
            },
            TargetEvidence::ConfiguredPath { source } => Self {
                kind: "configured_path".to_string(),
                marker: None,
                source: Some(source.clone()),
                project_manifest: None,
                matched_name: None,
            },
            TargetEvidence::ProjectContext { project_manifest } => Self {
                kind: "project_context".to_string(),
                marker: None,
                source: None,
                project_manifest: Some(path_string(project_manifest)),
                matched_name: None,
            },
            TargetEvidence::WeakNameOnly { matched_name } => Self {
                kind: "weak_name_only".to_string(),
                marker: None,
                source: None,
                project_manifest: None,
                matched_name: Some(matched_name.clone()),
            },
        }
    }
}

pub fn persist_plan(plan: &Plan, options: SavePlanOptions) -> PlanPersistenceResult<PersistedPlan> {
    if options.expires_at <= options.created_at {
        return Err(PlanPersistenceError::InvalidTimeRange);
    }

    let body = PersistedPlanBody {
        created_at: PersistedTimestamp::from_system_time(options.created_at)?,
        expires_at: PersistedTimestamp::from_system_time(options.expires_at)?,
        interactive_selection_modified: options.interactive_selection_modified,
        invocation: options.invocation,
        plan: PersistedPlanSnapshot::from_plan(plan)?,
    };
    let id = PlanId::from_body(&body)?;

    Ok(PersistedPlan {
        schema_version: PERSISTED_PLAN_SCHEMA_VERSION,
        id,
        body,
    })
}

fn content_fingerprint_for_entry(entry: &PlanEntry) -> PlanPersistenceResult<Option<String>> {
    if !requires_content_fingerprint(entry) {
        return Ok(None);
    }

    let path = &entry.snapshot.path;
    let metadata = fs::symlink_metadata(path).map_err(|error| PlanPersistenceError::Io {
        path: path.clone(),
        message: error.to_string(),
    })?;
    Ok(Some(fingerprint_path(path, &metadata)?))
}

fn requires_content_fingerprint(entry: &PlanEntry) -> bool {
    matches!(
        entry.action,
        PlanAction::Delete | PlanAction::RequiresConfirmation
    ) && entry.artifact_class != ArtifactClass::WholeTarget
        && entry.artifact_class != ArtifactClass::StaleDeps
        && entry.artifact_class != ArtifactClass::StaleIncremental
        && entry.artifact_class != ArtifactClass::DepsOutput
        && entry.snapshot.path_kind == PathKind::File
}

pub fn ensure_plan_usable(document: &PersistedPlan, now: SystemTime) -> PlanPersistenceResult<()> {
    if document.schema_version != PERSISTED_PLAN_SCHEMA_VERSION {
        return Err(PlanPersistenceError::PersistenceSchemaMismatch {
            found: document.schema_version,
            expected: PERSISTED_PLAN_SCHEMA_VERSION,
        });
    }

    if document.body.plan.schema_version != PLAN_SCHEMA_VERSION {
        return Err(PlanPersistenceError::PlanSchemaMismatch {
            found: document.body.plan.schema_version,
            expected: PLAN_SCHEMA_VERSION,
        });
    }

    let expected_id = PlanId::from_body(&document.body)?;
    if expected_id != document.id {
        return Err(PlanPersistenceError::PlanIdMismatch {
            expected: expected_id.0,
            found: document.id.0.clone(),
        });
    }

    if now >= document.body.expires_at.to_system_time() {
        return Err(PlanPersistenceError::PlanExpired);
    }

    Ok(())
}

fn policy_label(policy: PolicyKind) -> &'static str {
    match policy {
        PolicyKind::Observe => "observe",
        PolicyKind::Conservative => "conservative",
        PolicyKind::Balanced => "balanced",
        PolicyKind::Aggressive => "aggressive",
        PolicyKind::Custom => "custom",
    }
}

fn action_label(action: &PlanAction) -> &'static str {
    match action {
        PlanAction::Delete => "delete",
        PlanAction::Preserve => "preserve",
        PlanAction::SkipActive => "skip_active",
        PlanAction::SkipLocked => "skip_locked",
        PlanAction::Unknown => "unknown",
        PlanAction::RequiresConfirmation => "requires_confirmation",
    }
}

fn artifact_label(artifact_class: ArtifactClass) -> &'static str {
    artifact_class.label()
}

fn skip_reason_label(reason: PlanSkipReason) -> &'static str {
    reason.label()
}

fn is_default_whole_target_mode(mode: &PersistedWholeTargetMode) -> bool {
    *mode == PersistedWholeTargetMode::Off
}

fn path_kind_label(path_kind: PathKind) -> &'static str {
    match path_kind {
        PathKind::File => "file",
        PathKind::Directory => "directory",
        PathKind::Symlink => "symlink",
        PathKind::Unknown => "unknown",
    }
}

fn path_string(path: impl AsRef<Path>) -> String {
    path.as_ref().display().to_string()
}