cargo-reclaim 0.2.1

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
use std::fs::{self, Metadata};
use std::path::{Component, Path, PathBuf};
use std::time::SystemTime;

use crate::persistence::{PersistedTimestamp, PlanId, PlanPersistenceResult, fingerprint_path};

use super::classify::classify_cargo_home_relative_path;
use super::model::{CargoHomeClass, CargoHomePathKind};
use super::persistence::{
    PersistedCargoHomePlan, PersistedCargoHomePlanEntry, class_label,
    ensure_cargo_home_plan_usable, path_kind_label,
};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CargoHomeApplyReport {
    pub plan_id: PlanId,
    pub dry_run: bool,
    pub validation_only: bool,
    pub entries: Vec<CargoHomeApplyEntryResult>,
    pub totals: CargoHomeApplyTotals,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CargoHomeApplyTotals {
    pub entry_count: usize,
    pub delete_candidate_count: usize,
    pub would_delete_count: usize,
    pub would_delete_bytes: u64,
    pub applied_count: usize,
    pub applied_bytes: u64,
    pub failed_count: usize,
    pub skipped_count: usize,
    pub stale_skip_count: usize,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CargoHomeApplyEntryResult {
    pub path: String,
    pub planned_action: String,
    pub status: CargoHomeApplyEntryStatus,
    pub size_bytes: u64,
    pub reason: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CargoHomeApplyEntryStatus {
    WouldDelete,
    Deleted,
    NotPlannedForDeletion,
    SkipStalePlan,
    DeleteFailed,
}

pub fn validate_cargo_home_plan_for_apply(
    document: &PersistedCargoHomePlan,
    now: SystemTime,
) -> PlanPersistenceResult<CargoHomeApplyReport> {
    ensure_cargo_home_plan_usable(document, now)?;
    let entries = collect_revalidated_entries(document);

    Ok(CargoHomeApplyReport {
        plan_id: document.id.clone(),
        dry_run: true,
        validation_only: true,
        totals: CargoHomeApplyTotals::from_entries(&entries),
        entries,
    })
}

pub fn execute_cargo_home_plan_apply(
    document: &PersistedCargoHomePlan,
    now: SystemTime,
) -> PlanPersistenceResult<CargoHomeApplyReport> {
    ensure_cargo_home_plan_usable(document, now)?;
    let entries = collect_deleted_entries(document);

    Ok(CargoHomeApplyReport {
        plan_id: document.id.clone(),
        dry_run: false,
        validation_only: false,
        totals: CargoHomeApplyTotals::from_entries(&entries),
        entries,
    })
}

impl CargoHomeApplyTotals {
    fn from_entries(entries: &[CargoHomeApplyEntryResult]) -> Self {
        let mut totals = Self {
            entry_count: entries.len(),
            ..Self::default()
        };

        for entry in entries {
            if entry.planned_action == "delete_candidate" {
                totals.delete_candidate_count += 1;
            }

            match entry.status {
                CargoHomeApplyEntryStatus::WouldDelete => {
                    totals.would_delete_count += 1;
                    totals.would_delete_bytes =
                        totals.would_delete_bytes.saturating_add(entry.size_bytes);
                }
                CargoHomeApplyEntryStatus::Deleted => {
                    totals.applied_count += 1;
                    totals.applied_bytes = totals.applied_bytes.saturating_add(entry.size_bytes);
                }
                CargoHomeApplyEntryStatus::NotPlannedForDeletion => totals.skipped_count += 1,
                CargoHomeApplyEntryStatus::SkipStalePlan => {
                    totals.skipped_count += 1;
                    totals.stale_skip_count += 1;
                }
                CargoHomeApplyEntryStatus::DeleteFailed => totals.failed_count += 1,
            }
        }

        totals
    }
}

fn collect_revalidated_entries(
    document: &PersistedCargoHomePlan,
) -> Vec<CargoHomeApplyEntryResult> {
    document
        .body
        .plan
        .entries
        .iter()
        .map(|entry| revalidate_entry(document, entry))
        .collect()
}

fn collect_deleted_entries(document: &PersistedCargoHomePlan) -> Vec<CargoHomeApplyEntryResult> {
    document
        .body
        .plan
        .entries
        .iter()
        .map(|entry| revalidate_entry(document, entry))
        .map(delete_revalidated_entry)
        .collect()
}

fn revalidate_entry(
    document: &PersistedCargoHomePlan,
    entry: &PersistedCargoHomePlanEntry,
) -> CargoHomeApplyEntryResult {
    if entry.action != "delete_candidate" {
        return result(
            entry,
            CargoHomeApplyEntryStatus::NotPlannedForDeletion,
            "entry is not planned for deletion",
        );
    }

    match revalidate_delete_candidate(document, entry) {
        Ok(()) => result(
            entry,
            CargoHomeApplyEntryStatus::WouldDelete,
            "delete candidate revalidated; no files were deleted",
        ),
        Err(reason) => result(entry, CargoHomeApplyEntryStatus::SkipStalePlan, reason),
    }
}

fn delete_revalidated_entry(entry: CargoHomeApplyEntryResult) -> CargoHomeApplyEntryResult {
    if entry.status != CargoHomeApplyEntryStatus::WouldDelete {
        return entry;
    }

    match remove_path(Path::new(&entry.path)) {
        Ok(()) => CargoHomeApplyEntryResult {
            status: CargoHomeApplyEntryStatus::Deleted,
            reason: "deleted revalidated path".to_string(),
            ..entry
        },
        Err(reason) => CargoHomeApplyEntryResult {
            status: CargoHomeApplyEntryStatus::DeleteFailed,
            reason,
            ..entry
        },
    }
}

fn revalidate_delete_candidate(
    document: &PersistedCargoHomePlan,
    entry: &PersistedCargoHomePlanEntry,
) -> Result<(), String> {
    let root = Path::new(&document.body.plan.input.root);
    let path = Path::new(&entry.path);
    let relative_path = Path::new(&entry.relative_path);
    let joined = root.join(relative_path);

    if !is_plain_relative_path(relative_path) {
        return Err(
            "skip_stale_plan: persisted relative path must stay inside Cargo home".to_string(),
        );
    }

    if path != joined {
        return Err("skip_stale_plan: path no longer matches root and relative path".to_string());
    }

    let root_metadata = fs::symlink_metadata(root).map_err(|error| {
        format!(
            "skip_stale_plan: failed to read persisted Cargo home root {}: {error}",
            root.display()
        )
    })?;
    if root_metadata.file_type().is_symlink() || !root_metadata.is_dir() {
        return Err(
            "skip_stale_plan: persisted Cargo home root is not a real directory".to_string(),
        );
    }

    let metadata = fs::symlink_metadata(path).map_err(|error| {
        format!(
            "skip_stale_plan: failed to read path metadata for {}: {error}",
            entry.path
        )
    })?;

    if metadata.file_type().is_symlink() {
        return Err("skip_stale_plan: path is now a symlink".to_string());
    }

    let canonical_root = root.canonicalize().map_err(|error| {
        format!(
            "skip_stale_plan: failed to resolve persisted Cargo home root {}: {error}",
            root.display()
        )
    })?;
    let canonical_path = path.canonicalize().map_err(|error| {
        format!(
            "skip_stale_plan: failed to resolve persisted path {}: {error}",
            entry.path
        )
    })?;
    if !canonical_path.starts_with(&canonical_root) {
        return Err("skip_stale_plan: path is outside persisted Cargo home root".to_string());
    }

    let current_kind = current_path_kind(&metadata);
    if current_kind != entry.path_kind {
        return Err(format!(
            "skip_stale_plan: path kind changed from {} to {current_kind}",
            entry.path_kind
        ));
    }

    let current_size = measure_size(path, &metadata)?;
    if current_size != entry.size_bytes {
        return Err(format!(
            "skip_stale_plan: size changed from {} to {current_size}",
            entry.size_bytes
        ));
    }

    let current_fingerprint = fingerprint_path(path, &metadata)
        .map_err(|error| format!("skip_stale_plan: failed to fingerprint path: {error}"))?;
    let Some(expected_fingerprint) = &entry.content_fingerprint else {
        return Err("skip_stale_plan: persisted content fingerprint is missing".to_string());
    };
    if current_fingerprint != *expected_fingerprint {
        return Err("skip_stale_plan: content fingerprint changed".to_string());
    }

    if let Some(expected_modified) = entry.modified {
        let current_modified = metadata.modified().ok().and_then(system_time_to_timestamp);
        if current_modified != Some(expected_modified) {
            return Err("skip_stale_plan: modification time changed".to_string());
        }
    }

    let Some(persisted_class) = class_from_label(&entry.class) else {
        return Err("skip_stale_plan: persisted class is not recognized".to_string());
    };
    let current_class = classify_cargo_home_relative_path(relative_path);
    if current_class != persisted_class {
        return Err(format!(
            "skip_stale_plan: class changed from {} to {}",
            entry.class,
            class_label(current_class)
        ));
    }

    if !known_cache_class_selected_by_policy(persisted_class, &document.body.policy) {
        return Err(
            "skip_stale_plan: persisted class is not selected by persisted policy".to_string(),
        );
    }

    Ok(())
}

fn remove_path(path: &Path) -> Result<(), String> {
    let metadata = fs::symlink_metadata(path)
        .map_err(|error| format!("delete_failed: failed to read path metadata: {error}"))?;

    if metadata.file_type().is_symlink() {
        return Err("delete_failed: path is now a symlink".to_string());
    }

    if metadata.is_file() {
        return fs::remove_file(path)
            .map_err(|error| format!("delete_failed: failed to remove file: {error}"));
    }

    if metadata.is_dir() {
        return fs::remove_dir_all(path)
            .map_err(|error| format!("delete_failed: failed to remove directory: {error}"));
    }

    Err("delete_failed: path kind is not removable".to_string())
}

fn is_plain_relative_path(path: &Path) -> bool {
    let mut has_component = false;
    for component in path.components() {
        match component {
            Component::Normal(_) => has_component = true,
            Component::CurDir
            | Component::ParentDir
            | Component::RootDir
            | Component::Prefix(_) => return false,
        }
    }
    has_component
}

fn result(
    entry: &PersistedCargoHomePlanEntry,
    status: CargoHomeApplyEntryStatus,
    reason: impl Into<String>,
) -> CargoHomeApplyEntryResult {
    CargoHomeApplyEntryResult {
        path: entry.path.clone(),
        planned_action: entry.action.clone(),
        status,
        size_bytes: entry.size_bytes,
        reason: reason.into(),
    }
}

fn class_from_label(value: &str) -> Option<CargoHomeClass> {
    match value {
        "registry_index" => Some(CargoHomeClass::RegistryIndex),
        "registry_cache" => Some(CargoHomeClass::RegistryCache),
        "registry_source" => Some(CargoHomeClass::RegistrySource),
        "git_database" => Some(CargoHomeClass::GitDatabase),
        "git_checkouts" => Some(CargoHomeClass::GitCheckouts),
        "config" => Some(CargoHomeClass::Config),
        "credentials" => Some(CargoHomeClass::Credentials),
        "installed_binaries" => Some(CargoHomeClass::InstalledBinaries),
        "install_metadata" => Some(CargoHomeClass::InstallMetadata),
        "unknown_user_authored" => Some(CargoHomeClass::UnknownUserAuthored),
        _ => None,
    }
}

fn known_cache_class_selected_by_policy(class: CargoHomeClass, policy: &str) -> bool {
    match policy {
        "conservative" => matches!(class, CargoHomeClass::RegistryCache),
        "balanced" => matches!(
            class,
            CargoHomeClass::RegistryCache | CargoHomeClass::RegistrySource
        ),
        "aggressive" => matches!(
            class,
            CargoHomeClass::RegistryIndex
                | CargoHomeClass::RegistryCache
                | CargoHomeClass::RegistrySource
                | CargoHomeClass::GitDatabase
                | CargoHomeClass::GitCheckouts
        ),
        _ => false,
    }
}

fn current_path_kind(metadata: &Metadata) -> &'static str {
    if metadata.is_file() {
        path_kind_label(CargoHomePathKind::File)
    } else if metadata.is_dir() {
        path_kind_label(CargoHomePathKind::Directory)
    } else {
        path_kind_label(CargoHomePathKind::Other)
    }
}

fn measure_size(path: &Path, metadata: &Metadata) -> Result<u64, String> {
    if metadata.is_file() {
        return Ok(metadata.len());
    }

    if metadata.is_dir() {
        let mut total = 0_u64;
        for entry in fs::read_dir(path).map_err(|error| {
            format!(
                "skip_stale_plan: failed to read directory {}: {error}",
                path.display()
            )
        })? {
            let entry = entry.map_err(|error| {
                format!(
                    "skip_stale_plan: failed to read directory entry in {}: {error}",
                    path.display()
                )
            })?;
            let child_path: PathBuf = entry.path();
            let metadata = fs::symlink_metadata(&child_path).map_err(|error| {
                format!(
                    "skip_stale_plan: failed to read child metadata under {}: {error}",
                    path.display()
                )
            })?;
            if metadata.file_type().is_symlink() {
                return Err("skip_stale_plan: directory now contains a symlink".to_string());
            }
            total = total.saturating_add(measure_size(&child_path, &metadata)?);
        }
        return Ok(total);
    }

    Ok(metadata.len())
}

fn system_time_to_timestamp(time: std::time::SystemTime) -> Option<PersistedTimestamp> {
    PersistedTimestamp::from_system_time(time).ok()
}