stout 0.2.1

A fast, Rust-based Homebrew-compatible package manager
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//! Cleanup command for removing old files and cache
//!
//! Compatible with `brew cleanup` - removes stale lock files and outdated
//! downloads, and removes old versions of installed packages.

use anyhow::{Context, Result};
use clap::Args as ClapArgs;
use console::style;
use std::path::Path;
use stout_cask::InstalledCasks;
use stout_fetch::DownloadCache;
use stout_state::{InstalledPackages, Paths};

/// Default cleanup age in days (same as Homebrew's HOMEBREW_CLEANUP_MAX_AGE_DAYS)
const DEFAULT_CLEANUP_DAYS: u64 = 120;

#[derive(ClapArgs)]
pub struct Args {
    /// Specific formulas/casks to clean up (default: all)
    pub formulas: Vec<String>,

    /// Remove all cache files older than specified days (use 0 for all)
    #[arg(long, value_name = "DAYS")]
    pub prune: Option<u64>,

    /// Scrub cache, removing downloads even for latest versions
    /// (still keeps downloads for installed packages)
    #[arg(long, short = 's')]
    pub scrub: bool,

    /// Only show what would be removed without actually removing
    #[arg(long, short = 'n')]
    pub dry_run: bool,

    /// Only prune symlinks and directories from prefix
    #[arg(long)]
    pub prune_prefix: bool,
}

pub async fn run(args: Args) -> Result<()> {
    let paths = Paths::default();
    let installed = InstalledPackages::load(&paths)?;
    let mut total_freed: u64 = 0;
    let mut items_removed: usize = 0;

    println!("\n{}...", style("Cleaning up").cyan());

    // Handle --prune-prefix separately
    if args.prune_prefix {
        let freed = prune_prefix_only(&paths, args.dry_run)?;
        total_freed += freed.0;
        items_removed += freed.1;
    } else {
        // Determine max age for cleanup
        let max_age_days = args.prune.unwrap_or(DEFAULT_CLEANUP_DAYS);
        let max_age_secs = max_age_days * 24 * 60 * 60;

        // Clean download cache
        let cache = DownloadCache::new(&paths.stout_dir);

        if max_age_days == 0 || args.scrub {
            // Remove all downloads (--prune=0 or --scrub)
            let freed = clean_all_downloads(&paths, &installed, args.scrub, args.dry_run)?;
            total_freed += freed.0;
            items_removed += freed.1;
        } else {
            // Remove old downloads based on age
            if args.dry_run {
                let (size, count) = preview_old_downloads(&paths, max_age_secs)?;
                total_freed += size;
                items_removed += count;
            } else {
                let freed = cache
                    .clean(max_age_secs)
                    .context("Failed to clean download cache")?;
                if freed > 0 {
                    println!(
                        "  {} Removed stale downloads: {}",
                        style("").green(),
                        format_bytes(freed)
                    );
                    total_freed += freed;
                }
            }
        }

        // Clean cask artifact cache
        let cask_state_path = paths.stout_dir.join("casks.json");
        let installed_casks = InstalledCasks::load(&cask_state_path).unwrap_or_default();
        let cask_cache_dir = paths.stout_dir.join("cache").join("casks");

        if max_age_days == 0 || args.scrub {
            let freed = clean_all_cask_artifacts(
                &cask_cache_dir,
                &installed_casks,
                args.scrub,
                args.dry_run,
            )?;
            total_freed += freed.0;
            items_removed += freed.1;
        } else if args.dry_run {
            let (size, count) = preview_old_cask_artifacts(&cask_cache_dir, max_age_secs)?;
            total_freed += size;
            items_removed += count;
        } else {
            let freed = clean_old_cask_artifacts(&cask_cache_dir, max_age_secs)?;
            if freed > 0 {
                println!(
                    "  {} Removed stale cask artifacts: {}",
                    style("").green(),
                    format_bytes(freed)
                );
                total_freed += freed;
            }
        }

        // Remove old versions from Cellar (always done by brew cleanup)
        let freed = scrub_old_versions(&paths, &args.formulas, args.dry_run)?;
        total_freed += freed.0;
        items_removed += freed.1;

        // Clean formula/cask JSON cache if scrubbing
        if args.scrub {
            let freed = clean_json_cache(&paths, args.dry_run)?;
            total_freed += freed.0;
            items_removed += freed.1;
        }
    }

    // Summary
    if args.dry_run {
        if total_freed > 0 || items_removed > 0 {
            println!(
                "\n{} Would free {} ({} items)",
                style("Dry run:").yellow(),
                format_bytes(total_freed),
                items_removed
            );
        } else {
            println!("\n{}", style("Nothing to clean up.").dim());
        }
    } else if total_freed > 0 {
        println!(
            "\n{} Freed {}",
            style("Cleaned up").green().bold(),
            format_bytes(total_freed)
        );
    } else {
        println!("\n{}", style("Nothing to clean up.").dim());
    }

    Ok(())
}

/// Clean all downloaded bottles
/// If scrub is false, keeps downloads for installed packages
fn clean_all_downloads(
    paths: &Paths,
    installed: &InstalledPackages,
    scrub: bool,
    dry_run: bool,
) -> Result<(u64, usize)> {
    let downloads_dir = paths.stout_dir.join("downloads");
    if !downloads_dir.exists() {
        return Ok((0, 0));
    }

    let mut total_size = 0u64;
    let mut count = 0usize;

    for entry in std::fs::read_dir(&downloads_dir)? {
        let entry = entry?;
        if entry.file_type()?.is_file() {
            let filename = entry.file_name().to_string_lossy().to_string();

            // If not scrubbing, skip downloads for installed packages
            if !scrub {
                // Parse package name from filename (format: name-version-platform.tar.gz)
                if let Some(pkg_name) = filename.split('-').next() {
                    if installed.is_installed(pkg_name) {
                        continue;
                    }
                }
            }

            let size = entry.metadata()?.len();
            total_size += size;
            count += 1;

            if dry_run {
                println!(
                    "  {} {} ({})",
                    style("Would remove:").yellow(),
                    filename,
                    format_bytes(size)
                );
            } else {
                std::fs::remove_file(entry.path())?;
                println!(
                    "  {} Removed {} ({})",
                    style("").green(),
                    filename,
                    format_bytes(size)
                );
            }
        }
    }

    Ok((total_size, count))
}

/// Preview what old downloads would be removed
fn preview_old_downloads(paths: &Paths, max_age_secs: u64) -> Result<(u64, usize)> {
    let downloads_dir = paths.stout_dir.join("downloads");
    if !downloads_dir.exists() {
        return Ok((0, 0));
    }

    let now = std::time::SystemTime::now();
    let mut total_size = 0u64;
    let mut count = 0usize;

    for entry in std::fs::read_dir(&downloads_dir)? {
        let entry = entry?;
        let metadata = entry.metadata()?;

        if metadata.is_file() {
            if let Ok(modified) = metadata.modified() {
                if let Ok(age) = now.duration_since(modified) {
                    if age.as_secs() > max_age_secs {
                        let size = metadata.len();
                        total_size += size;
                        count += 1;
                        println!(
                            "  {} {} ({}, {} days old)",
                            style("Would remove:").yellow(),
                            entry.file_name().to_string_lossy(),
                            format_bytes(size),
                            age.as_secs() / 86400
                        );
                    }
                }
            }
        }
    }

    Ok((total_size, count))
}

/// Clean all cask artifact downloads
/// If scrub is false, keeps artifacts for installed casks
fn clean_all_cask_artifacts(
    cache_dir: &Path,
    installed_casks: &InstalledCasks,
    scrub: bool,
    dry_run: bool,
) -> Result<(u64, usize)> {
    if !cache_dir.exists() {
        return Ok((0, 0));
    }

    let mut total_size = 0u64;
    let mut count = 0usize;

    for entry in std::fs::read_dir(cache_dir)? {
        let entry = entry?;
        if !entry.file_type()?.is_file() {
            continue;
        }

        let filename = entry.file_name().to_string_lossy().to_string();

        // If not scrubbing, skip artifacts for installed casks
        if !scrub {
            // Filename format: <token>.<ext> (e.g. firefox.dmg, slack.zip)
            if let Some(token) = filename.split('.').next() {
                if installed_casks.is_installed(token) {
                    continue;
                }
            }
        }

        let size = entry.metadata()?.len();
        total_size += size;
        count += 1;

        if dry_run {
            println!(
                "  {} {} ({})",
                style("Would remove:").yellow(),
                filename,
                format_bytes(size)
            );
        } else {
            std::fs::remove_file(entry.path())?;
            println!(
                "  {} Removed {} ({})",
                style("").green(),
                filename,
                format_bytes(size)
            );
        }
    }

    Ok((total_size, count))
}

/// Preview what old cask artifacts would be removed
fn preview_old_cask_artifacts(cache_dir: &Path, max_age_secs: u64) -> Result<(u64, usize)> {
    if !cache_dir.exists() {
        return Ok((0, 0));
    }

    let now = std::time::SystemTime::now();
    let mut total_size = 0u64;
    let mut count = 0usize;

    for entry in std::fs::read_dir(cache_dir)? {
        let entry = entry?;
        let metadata = entry.metadata()?;

        if !metadata.is_file() {
            continue;
        }

        if let Ok(modified) = metadata.modified() {
            if let Ok(age) = now.duration_since(modified) {
                if age.as_secs() > max_age_secs {
                    let size = metadata.len();
                    total_size += size;
                    count += 1;
                    println!(
                        "  {} {} ({}, {} days old)",
                        style("Would remove:").yellow(),
                        entry.file_name().to_string_lossy(),
                        format_bytes(size),
                        age.as_secs() / 86400
                    );
                }
            }
        }
    }

    Ok((total_size, count))
}

/// Remove old cask artifacts based on age
fn clean_old_cask_artifacts(cache_dir: &Path, max_age_secs: u64) -> Result<u64> {
    if !cache_dir.exists() {
        return Ok(0);
    }

    let now = std::time::SystemTime::now();
    let mut freed = 0u64;

    for entry in std::fs::read_dir(cache_dir)? {
        let entry = entry?;
        let metadata = entry.metadata()?;

        if !metadata.is_file() {
            continue;
        }

        if let Ok(modified) = metadata.modified() {
            if let Ok(age) = now.duration_since(modified) {
                if age.as_secs() > max_age_secs {
                    freed += metadata.len();
                    std::fs::remove_file(entry.path())?;
                }
            }
        }
    }

    Ok(freed)
}

/// Clean formula/cask JSON cache
fn clean_json_cache(paths: &Paths, dry_run: bool) -> Result<(u64, usize)> {
    let mut total_size = 0u64;
    let mut count = 0usize;

    for subdir in &["formulas", "casks"] {
        let cache_dir = paths.stout_dir.join(subdir);
        if !cache_dir.exists() {
            continue;
        }

        let result = clean_directory(&cache_dir, dry_run)?;
        total_size += result.0;
        count += result.1;
    }

    if count > 0 && !dry_run {
        println!(
            "  {} Cleared JSON cache: {}",
            style("").green(),
            format_bytes(total_size)
        );
    }

    Ok((total_size, count))
}

/// Scrub old versions from Cellar (keep only latest)
/// If formulas is non-empty, only clean those specific packages
fn scrub_old_versions(paths: &Paths, formulas: &[String], dry_run: bool) -> Result<(u64, usize)> {
    let installed = InstalledPackages::load(paths)?;
    let mut total_size = 0u64;
    let mut count = 0usize;

    if !paths.cellar.exists() {
        return Ok((0, 0));
    }

    for entry in std::fs::read_dir(&paths.cellar)? {
        let entry = entry?;
        if !entry.file_type()?.is_dir() {
            continue;
        }

        let pkg_name = entry.file_name().to_string_lossy().to_string();

        // If specific formulas requested, skip others
        if !formulas.is_empty() && !formulas.contains(&pkg_name) {
            continue;
        }

        // Get the current installed version
        let current_version = installed.get(&pkg_name).map(|p| p.version.clone());

        // List all versions in cellar
        let pkg_dir = entry.path();
        for version_entry in std::fs::read_dir(&pkg_dir)? {
            let version_entry = version_entry?;
            if !version_entry.file_type()?.is_dir() {
                continue;
            }

            let version = version_entry.file_name().to_string_lossy().to_string();

            // Skip current version
            if current_version.as_ref() == Some(&version) {
                continue;
            }

            // This is an old version, remove it
            let size = dir_size(&version_entry.path())?;
            total_size += size;
            count += 1;

            if dry_run {
                println!(
                    "  {} {}/{} ({})",
                    style("Would remove:").yellow(),
                    pkg_name,
                    version,
                    format_bytes(size)
                );
            } else {
                std::fs::remove_dir_all(version_entry.path())?;
                println!(
                    "  {} Removed old version: {}/{} ({})",
                    style("").green(),
                    pkg_name,
                    version,
                    format_bytes(size)
                );
            }
        }

        // Remove package directory if empty (no versions left)
        if !dry_run && pkg_dir.read_dir()?.next().is_none() {
            std::fs::remove_dir(&pkg_dir)?;
        }
    }

    Ok((total_size, count))
}

/// Prune only symlinks and directories from prefix
fn prune_prefix_only(paths: &Paths, dry_run: bool) -> Result<(u64, usize)> {
    let mut total_size = 0u64;
    let mut count = 0usize;

    // Check for broken symlinks in prefix directories
    for subdir in &["bin", "sbin", "lib", "include", "share", "etc", "opt"] {
        let dir = paths.prefix.join(subdir);
        if !dir.exists() {
            continue;
        }

        let result = prune_broken_symlinks(&dir, dry_run)?;
        total_size += result.0;
        count += result.1;
    }

    if count > 0 && !dry_run {
        println!("  {} Pruned {} broken symlinks", style("").green(), count);
    }

    Ok((total_size, count))
}

/// Remove broken symlinks from a directory
fn prune_broken_symlinks(dir: &Path, dry_run: bool) -> Result<(u64, usize)> {
    let mut total_size = 0u64;
    let mut count = 0usize;

    for entry in std::fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_symlink() {
            // Check if symlink target exists
            if !path.exists() {
                count += 1;
                if dry_run {
                    println!(
                        "  {} {} (broken symlink)",
                        style("Would remove:").yellow(),
                        path.display()
                    );
                } else {
                    std::fs::remove_file(&path)?;
                }
            }
        } else if path.is_dir() {
            let result = prune_broken_symlinks(&path, dry_run)?;
            total_size += result.0;
            count += result.1;

            // Remove empty directories
            if !dry_run {
                if let Ok(mut entries) = std::fs::read_dir(&path) {
                    if entries.next().is_none() {
                        let _ = std::fs::remove_dir(&path);
                    }
                }
            }
        }
    }

    Ok((total_size, count))
}

/// Clean all files in a directory
fn clean_directory(dir: &Path, dry_run: bool) -> Result<(u64, usize)> {
    let mut total_size = 0u64;
    let mut count = 0usize;

    for entry in std::fs::read_dir(dir)? {
        let entry = entry?;
        let metadata = entry.metadata()?;

        if metadata.is_file() {
            let size = metadata.len();
            total_size += size;
            count += 1;

            if !dry_run {
                std::fs::remove_file(entry.path())?;
            }
        } else if metadata.is_dir() {
            let result = clean_directory(&entry.path(), dry_run)?;
            total_size += result.0;
            count += result.1;

            if !dry_run {
                let _ = std::fs::remove_dir(entry.path());
            }
        }
    }

    Ok((total_size, count))
}

/// Calculate total size of a directory
fn dir_size(path: &Path) -> Result<u64> {
    let mut total = 0u64;

    if path.is_file() {
        return Ok(std::fs::metadata(path)?.len());
    }

    for entry in std::fs::read_dir(path)? {
        let entry = entry?;
        let metadata = entry.metadata()?;

        if metadata.is_file() {
            total += metadata.len();
        } else if metadata.is_dir() {
            total += dir_size(&entry.path())?;
        }
    }

    Ok(total)
}

/// Format bytes as human-readable string
fn format_bytes(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;

    if bytes >= GB {
        format!("{:.2} GB", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.2} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.2} KB", bytes as f64 / KB as f64)
    } else {
        format!("{} bytes", bytes)
    }
}