Skip to main content

cargo/ops/
cargo_clean.rs

1use crate::compiler::trim_paths;
2use crate::compiler::{CompileKind, CompileMode, Layout, RustcTargetData};
3use crate::ops;
4use crate::util::HumanBytes;
5use crate::util::data_structures::{IndexMap, IndexSet};
6use crate::util::edit_distance;
7use crate::util::errors::CargoResult;
8use crate::util::interning::InternedString;
9use crate::util::{GlobalContext, Progress, ProgressStyle};
10use crate::workspace::profiles::Profiles;
11use crate::workspace::{PackageIdSpec, PackageIdSpecQuery, TargetKind, Workspace};
12use anyhow::bail;
13use cargo_util::paths;
14use cargo_util_terminal::report::Level;
15
16use std::ffi::OsString;
17use std::io::Read;
18use std::path::{Path, PathBuf};
19use std::rc::Rc;
20use std::{fs, io};
21
22pub struct CleanOptions<'gctx> {
23    pub gctx: &'gctx GlobalContext,
24    /// A list of packages to clean. If empty, everything is cleaned.
25    pub spec: IndexSet<String>,
26    /// The target arch triple to clean, or None for the host arch
27    pub targets: Vec<String>,
28    /// Whether to clean the release directory
29    pub profile_specified: bool,
30    /// Whether to clean the directory of a certain build profile
31    pub requested_profile: InternedString,
32    /// Whether to just clean the doc directory
33    pub doc: bool,
34    /// If set, doesn't delete anything.
35    pub dry_run: bool,
36    /// true if target-dir was was explicitly specified via --target-dir
37    pub explicit_target_dir_arg: bool,
38}
39
40pub struct CleanContext<'gctx> {
41    pub gctx: &'gctx GlobalContext,
42    progress: Box<dyn CleaningProgressBar + 'gctx>,
43    pub dry_run: bool,
44    num_files_removed: u64,
45    num_dirs_removed: u64,
46    total_bytes_removed: u64,
47}
48
49/// Cleans various caches.
50pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
51    let mut target_dir = ws.target_dir();
52    let mut build_dir = ws.build_dir();
53    let gctx = opts.gctx;
54    let mut clean_ctx = CleanContext::new(gctx);
55    clean_ctx.dry_run = opts.dry_run;
56
57    const CLEAN_ABORT_NOTE: &str =
58        "cleaning has been aborted to prevent accidental deletion of unrelated files";
59
60    // make sure target_dir is a directory if it exists so that we don't delete files
61    if let Ok(meta) = fs::symlink_metadata(target_dir.as_path_unlocked()) {
62        // do not error if target_dir is symlink; let cargo delete it
63        if !meta.is_symlink() && !meta.is_dir() {
64            let title = format!("cannot clean `{}`: not a directory", target_dir.display());
65            let report = [Level::ERROR
66                .primary_title(title)
67                .element(Level::NOTE.message(CLEAN_ABORT_NOTE))];
68            gctx.shell().print_report(&report, false)?;
69            return Err(crate::AlreadyPrintedError::new(anyhow::anyhow!("")).into());
70        }
71    }
72
73    // do some validation on target_dir if it was specified via --target-dir
74    if opts.explicit_target_dir_arg {
75        let target_dir_path = target_dir.as_path_unlocked();
76
77        // perform validation on target_dir only if it exists and check if the target directory has a valid CACHEDIR.TAG
78        if target_dir_path.exists()
79            && let Err(err) = validate_target_dir_tag(target_dir_path)
80        {
81            // if target_dir was passed explicitly via --target-dir, then hard error if validation fails
82            let title = format!("cannot clean `{}`: {err}", target_dir_path.display());
83            let report = [Level::ERROR
84                .primary_title(title)
85                .element(Level::NOTE.message(CLEAN_ABORT_NOTE))];
86            gctx.shell().print_report(&report, false)?;
87            return Err(crate::AlreadyPrintedError::new(anyhow::anyhow!("")).into());
88        }
89    }
90
91    if opts.doc {
92        if !opts.spec.is_empty() {
93            // FIXME: https://github.com/rust-lang/cargo/issues/8790
94            // This should support the ability to clean specific packages
95            // within the doc directory. It's a little tricky since it
96            // needs to find all documentable targets, but also consider
97            // the fact that target names might overlap with dependency
98            // names and such.
99            bail!("--doc cannot be used with -p");
100        }
101        // If the doc option is set, we just want to delete the doc directory.
102        let doc_dirs = CompileKind::from_requested_targets(gctx, &opts.targets)?
103            .into_iter()
104            .map(|kind| {
105                let target_dir = match kind {
106                    CompileKind::Host => target_dir.clone(),
107                    CompileKind::Target(target) => target_dir.join(target.short_name()),
108                };
109                target_dir.join("doc").into_path_unlocked()
110            })
111            .collect::<Vec<_>>();
112        clean_ctx.remove_paths(&doc_dirs)?;
113    } else {
114        let profiles = Profiles::new(&ws, opts.requested_profile)?;
115
116        if opts.profile_specified {
117            // After parsing profiles we know the dir-name of the profile, if a profile
118            // was passed from the command line. If so, delete only the directory of
119            // that profile.
120            let dir_name = profiles.get_dir_name();
121            target_dir = target_dir.join(dir_name);
122            build_dir = build_dir.join(dir_name);
123        }
124
125        // If we have a spec, then we need to delete some packages, otherwise, just
126        // remove the whole target directory and be done with it!
127        //
128        // Note that we don't bother grabbing a lock here as we're just going to
129        // blow it all away anyway.
130        if opts.spec.is_empty() {
131            let paths: &[PathBuf] = if build_dir != target_dir {
132                &[
133                    target_dir.into_path_unlocked(),
134                    build_dir.into_path_unlocked(),
135                ]
136            } else {
137                &[target_dir.into_path_unlocked()]
138            };
139            clean_ctx.remove_paths(paths)?;
140        } else {
141            clean_specs(
142                &mut clean_ctx,
143                &ws,
144                &profiles,
145                &opts.targets,
146                &opts.spec,
147                opts.dry_run,
148            )?;
149        }
150    }
151
152    clean_ctx.display_summary()?;
153    Ok(())
154}
155
156fn validate_target_dir_tag(target_dir_path: &Path) -> CargoResult<()> {
157    const TAG_SIGNATURE: &[u8] = b"Signature: 8a477f597d28d172789f06886806bc55";
158
159    let tag_path = target_dir_path.join("CACHEDIR.TAG");
160
161    // per https://bford.info/cachedir the tag file must not be a symlink
162    if tag_path.is_symlink() {
163        bail!("expect `CACHEDIR.TAG` to be a regular file, got a symlink");
164    }
165
166    if !tag_path.is_file() {
167        bail!("missing or invalid `CACHEDIR.TAG` file");
168    }
169
170    let mut file = fs::File::open(&tag_path)
171        .map_err(|err| anyhow::anyhow!("failed to open `{}`: {}", tag_path.display(), err))?;
172
173    let mut buf = [0u8; TAG_SIGNATURE.len()];
174    match file.read_exact(&mut buf) {
175        Ok(()) if &buf[..] == TAG_SIGNATURE => {}
176        Err(e) if e.kind() != io::ErrorKind::UnexpectedEof => {
177            bail!("failed to read `{}`: {e}", tag_path.display());
178        }
179        _ => {
180            bail!("invalid signature in `CACHEDIR.TAG` file");
181        }
182    }
183
184    Ok(())
185}
186
187fn clean_specs(
188    clean_ctx: &mut CleanContext<'_>,
189    ws: &Workspace<'_>,
190    profiles: &Profiles,
191    targets: &[String],
192    spec: &IndexSet<String>,
193    dry_run: bool,
194) -> CargoResult<()> {
195    // Clean specific packages.
196    let requested_kinds = CompileKind::from_requested_targets(clean_ctx.gctx, targets)?;
197    let target_data = RustcTargetData::new(ws, &requested_kinds)?;
198    let (pkg_set, resolve) = ops::resolve_ws(ws, dry_run)?;
199    let prof_dir_name = profiles.get_dir_name();
200    let host_layout = Layout::new(ws, None, &prof_dir_name, true, true)?;
201    // Convert requested kinds to a Vec of layouts.
202    let target_layouts: Vec<(CompileKind, Layout)> = requested_kinds
203        .into_iter()
204        .filter_map(|kind| match kind {
205            CompileKind::Target(target) => {
206                match Layout::new(ws, Some(target), &prof_dir_name, true, true) {
207                    Ok(layout) => Some(Ok((kind, layout))),
208                    Err(e) => Some(Err(e)),
209                }
210            }
211            CompileKind::Host => None,
212        })
213        .collect::<CargoResult<_>>()?;
214    // A Vec of layouts. This is a little convoluted because there can only be
215    // one host_layout.
216    let layouts = if target_layouts.is_empty() {
217        vec![(CompileKind::Host, &host_layout)]
218    } else {
219        target_layouts
220            .iter()
221            .map(|(kind, layout)| (*kind, layout))
222            .collect()
223    };
224    // Create a Vec that also includes the host for things that need to clean both.
225    let layouts_with_host: Vec<(CompileKind, &Layout)> =
226        std::iter::once((CompileKind::Host, &host_layout))
227            .chain(layouts.iter().map(|(k, l)| (*k, *l)))
228            .collect();
229
230    // Cleaning individual rustdoc crates is currently not supported.
231    // For example, the search index would need to be rebuilt to fully
232    // remove it (otherwise you're left with lots of broken links).
233    // Doc tests produce no output.
234
235    // Get Packages for the specified specs.
236    let mut pkg_ids = Vec::new();
237    for spec_str in spec.iter() {
238        // Translate the spec to a Package.
239        let spec = PackageIdSpec::parse(spec_str)?;
240        if spec.partial_version().is_some() {
241            clean_ctx.gctx.shell().warn(&format!(
242                "version qualifier in `-p {}` is ignored, \
243                cleaning all versions of `{}` found",
244                spec_str,
245                spec.name()
246            ))?;
247        }
248        if spec.url().is_some() {
249            clean_ctx.gctx.shell().warn(&format!(
250                "url qualifier in `-p {}` ignored, \
251                cleaning all versions of `{}` found",
252                spec_str,
253                spec.name()
254            ))?;
255        }
256        let matches: Vec<_> = resolve.iter().filter(|id| spec.matches(*id)).collect();
257        if matches.is_empty() {
258            let mut suggestion = String::new();
259            suggestion.push_str(&edit_distance::closest_msg(
260                &spec.name(),
261                resolve.iter(),
262                |id| id.name().as_str(),
263                "package",
264            ));
265            anyhow::bail!(
266                "package ID specification `{}` did not match any packages{}",
267                spec,
268                suggestion
269            );
270        }
271        pkg_ids.extend(matches);
272    }
273    let packages = pkg_set.get_many(pkg_ids)?;
274
275    clean_ctx.progress = Box::new(CleaningPackagesBar::new(clean_ctx.gctx, packages.len()));
276    let mut dirs_to_clean = DirectoriesToClean::default();
277
278    if clean_ctx.gctx.cli_unstable().build_dir_new_layout {
279        for pkg in packages {
280            clean_ctx.progress.on_cleaning_package(&pkg.name())?;
281
282            // Remove intermediate artifacts
283            for (_compile_kind, layout) in &layouts_with_host {
284                let dir = layout.build_dir().build_unit(&pkg.name());
285                clean_ctx.rm_rf(&dir)?;
286            }
287
288            // Remove the uplifted copy.
289            for target in pkg.targets() {
290                if target.is_custom_build() {
291                    continue;
292                }
293                let crate_name: Rc<str> = target.crate_name().into();
294                for &mode in &[
295                    CompileMode::Build,
296                    CompileMode::Test,
297                    CompileMode::Check { test: false },
298                ] {
299                    for (compile_kind, layout) in &layouts {
300                        let triple = target_data.short_name(compile_kind);
301                        let (file_types, _unsupported) = target_data
302                            .info(*compile_kind)
303                            .rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?;
304                        let artifact_dir = layout
305                            .artifact_dir()
306                            .expect("artifact-dir was not locked during clean");
307                        let uplift_dir = match target.kind() {
308                            TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {
309                                Some(artifact_dir.examples())
310                            }
311                            // Tests/benchmarks are never uplifted.
312                            TargetKind::Test | TargetKind::Bench => None,
313                            _ => Some(artifact_dir.dest()),
314                        };
315                        if let Some(uplift_dir) = uplift_dir {
316                            for file_type in file_types {
317                                let uplifted_filename = file_type.uplift_filename(target);
318
319                                // Dep-info generated by Cargo itself.
320                                let dep_info = Path::new(&uplifted_filename)
321                                    .with_extension("d")
322                                    .to_string_lossy()
323                                    .into_owned();
324
325                                dirs_to_clean.mark_utf(uplift_dir, |filename| {
326                                    filename == uplifted_filename || filename == dep_info
327                                });
328                            }
329                        }
330                        let path_dash = format!("{}-", crate_name);
331
332                        dirs_to_clean.mark_utf(layout.build_dir().incremental(), |filename| {
333                            filename.starts_with(&path_dash)
334                        });
335                    }
336                }
337            }
338        }
339    } else {
340        for pkg in packages {
341            clean_ctx.progress.on_cleaning_package(&pkg.name())?;
342
343            // Clean fingerprints.
344            for (_, layout) in &layouts_with_host {
345                dirs_to_clean.mark_utf(layout.build_dir().legacy_fingerprint(), |filename| {
346                    let Some((pkg_name, _)) = filename.rsplit_once('-') else {
347                        return false;
348                    };
349
350                    pkg_name == pkg.name().as_str()
351                });
352            }
353
354            for target in pkg.targets() {
355                if target.is_custom_build() {
356                    // Get both the build_script_build and the output directory.
357                    for (_, layout) in &layouts_with_host {
358                        dirs_to_clean.mark_utf(layout.build_dir().build(), |filename| {
359                            let Some((current_name, _)) = filename.rsplit_once('-') else {
360                                return false;
361                            };
362
363                            current_name == pkg.name().as_str()
364                        });
365                    }
366                    continue;
367                }
368                let crate_name: Rc<str> = target.crate_name().into();
369                let path_dot: &str = &format!("{crate_name}.");
370                let path_dash: &str = &format!("{crate_name}-");
371                for &mode in &[
372                    CompileMode::Build,
373                    CompileMode::Test,
374                    CompileMode::Check { test: false },
375                ] {
376                    for (compile_kind, layout) in &layouts {
377                        let triple = target_data.short_name(compile_kind);
378                        let (file_types, _unsupported) = target_data
379                            .info(*compile_kind)
380                            .rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?;
381                        let artifact_dir = layout
382                            .artifact_dir()
383                            .expect("artifact-dir was not locked during clean");
384                        let (dir, uplift_dir) = match target.kind() {
385                            TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {
386                                (layout.build_dir().examples(), Some(artifact_dir.examples()))
387                            }
388                            // Tests/benchmarks are never uplifted.
389                            TargetKind::Test | TargetKind::Bench => {
390                                (layout.build_dir().legacy_deps(), None)
391                            }
392                            _ => (layout.build_dir().legacy_deps(), Some(artifact_dir.dest())),
393                        };
394
395                        for file_type in file_types {
396                            // Some files include a hash in the filename, some don't.
397                            let (prefix, suffix) = file_type.output_prefix_suffix(target);
398                            let unhashed_name = file_type.output_filename(target, None);
399                            // Handle uplifted or unhashed output (e.g. on MSVC executables)
400                            let unhashed_unremap =
401                                format!("{unhashed_name}{}", trim_paths::UNREMAP_SUFFIX);
402                            dirs_to_clean.mark_utf(&dir, |filename| {
403                                (filename.starts_with(&prefix) && filename.ends_with(&suffix))
404                                    || unhashed_name == filename
405                                    || unhashed_unremap == filename
406                            });
407
408                            // Remove the uplifted copy.
409                            if let Some(uplift_dir) = uplift_dir {
410                                let uplifted_path =
411                                    uplift_dir.join(file_type.uplift_filename(target));
412                                clean_ctx.rm_rf(&uplifted_path)?;
413                                // Dep-info generated by Cargo itself.
414                                let dep_info = uplifted_path.with_extension("d");
415                                clean_ctx.rm_rf(&dep_info)?;
416                                // Unremap file emitted for `-Ztrim-paths`.
417                                let unremap = trim_paths::append_unremap_suffix(&uplifted_path);
418                                clean_ctx.rm_rf(&unremap)?;
419                            }
420                        }
421                        let unhashed_dep_info = format!("{}.d", crate_name);
422                        dirs_to_clean.mark_utf(dir, |filename| filename == unhashed_dep_info);
423
424                        dirs_to_clean.mark_utf(dir, |filename| {
425                            if filename.starts_with(&path_dash) {
426                                // Remove dep-info file generated by rustc. It is not tracked in
427                                // file_types. It does not have a prefix.
428                                filename.ends_with(".d")
429                                    // Unremap file emitted for `-Ztrim-paths`.
430                                    || filename.ends_with(trim_paths::UNREMAP_SUFFIX)
431                            } else if filename.starts_with(&path_dot) {
432                                // Remove split-debuginfo files generated by rustc.
433                                [".o", ".dwo", ".dwp"]
434                                    .iter()
435                                    .any(|suffix| filename.ends_with(suffix))
436                            } else {
437                                false
438                            }
439                        });
440
441                        // TODO: what to do about build_script_build?
442                        dirs_to_clean.mark_utf(layout.build_dir().incremental(), |filename| {
443                            filename.starts_with(path_dash)
444                        });
445                    }
446                }
447            }
448        }
449    }
450    clean_ctx.rm_rf_all(dirs_to_clean)?;
451
452    Ok(())
453}
454
455#[derive(Default)]
456struct DirectoriesToClean {
457    dir_contents: IndexMap<PathBuf, IndexSet<OsString>>,
458    to_remove: IndexSet<PathBuf>,
459}
460
461impl DirectoriesToClean {
462    fn mark(&mut self, directory: &Path, mut should_remove_entry: impl FnMut(&OsString) -> bool) {
463        let entry = match self.dir_contents.entry(directory.to_owned()) {
464            indexmap::map::Entry::Occupied(occupied_entry) => occupied_entry.into_mut(),
465            indexmap::map::Entry::Vacant(vacant_entry) => {
466                let Ok(dir_entries) = std::fs::read_dir(directory.to_owned()) else {
467                    return;
468                };
469                vacant_entry.insert(
470                    dir_entries
471                        .into_iter()
472                        .flatten()
473                        .map(|entry| entry.file_name())
474                        .collect::<IndexSet<_>>(),
475                )
476            }
477        };
478
479        entry.retain(|path| {
480            let should_remove = should_remove_entry(path);
481            if should_remove {
482                self.to_remove.insert(directory.join(path));
483            }
484            !should_remove
485        });
486    }
487
488    fn mark_utf(&mut self, directory: &Path, mut should_remove_entry: impl FnMut(&str) -> bool) {
489        self.mark(directory, move |filename| {
490            let Some(as_utf) = filename.to_str() else {
491                return false;
492            };
493            should_remove_entry(as_utf)
494        });
495    }
496}
497
498impl<'gctx> CleanContext<'gctx> {
499    pub fn new(gctx: &'gctx GlobalContext) -> Self {
500        // This progress bar will get replaced, this is just here to avoid needing
501        // an Option until the actual bar is created.
502        let progress = CleaningFolderBar::new(gctx, 0);
503        CleanContext {
504            gctx,
505            progress: Box::new(progress),
506            dry_run: false,
507            num_files_removed: 0,
508            num_dirs_removed: 0,
509            total_bytes_removed: 0,
510        }
511    }
512
513    fn rm_rf_all(&mut self, dirs: DirectoriesToClean) -> CargoResult<()> {
514        for path in dirs.to_remove {
515            self.rm_rf(&path)?;
516        }
517        Ok(())
518    }
519
520    pub fn rm_rf(&mut self, path: &Path) -> CargoResult<()> {
521        let meta = match fs::symlink_metadata(path) {
522            Ok(meta) => meta,
523            Err(e) => {
524                if e.kind() != std::io::ErrorKind::NotFound {
525                    self.gctx
526                        .shell()
527                        .warn(&format!("cannot access {}: {e}", path.display()))?;
528                }
529                return Ok(());
530            }
531        };
532
533        // dry-run displays paths while walking, so don't print here.
534        if !self.dry_run {
535            self.gctx
536                .shell()
537                .verbose(|shell| shell.status("Removing", path.display()))?;
538        }
539        self.progress.display_now()?;
540
541        let mut rm_file = |path: &Path, meta: Result<std::fs::Metadata, _>| {
542            if let Ok(meta) = meta {
543                // Note: This can over-count bytes removed for hard-linked
544                // files. It also under-counts since it only counts the exact
545                // byte sizes and not the block sizes.
546                self.total_bytes_removed += meta.len();
547            }
548            self.num_files_removed += 1;
549            if !self.dry_run {
550                paths::remove_file(path)?;
551            }
552            Ok(())
553        };
554
555        if !meta.is_dir() {
556            return rm_file(path, Ok(meta));
557        }
558
559        for entry in walkdir::WalkDir::new(path).contents_first(true) {
560            let entry = entry?;
561            self.progress.on_clean()?;
562            if self.dry_run {
563                // This prints the path without the "Removing" status since I feel
564                // like it can be surprising or even frightening if cargo says it
565                // is removing something without actually removing it. And I can't
566                // come up with a different verb to use as the status.
567                self.gctx
568                    .shell()
569                    .verbose(|shell| Ok(writeln!(shell.out(), "{}", entry.path().display())?))?;
570            }
571            if entry.file_type().is_dir() {
572                self.num_dirs_removed += 1;
573                // The contents should have been removed by now, but sometimes a race condition is hit
574                // where other files have been added by the OS. `paths::remove_dir_all` also falls back
575                // to `std::fs::remove_dir_all`, which may be more reliable than a simple walk in
576                // platform-specific edge cases.
577                if !self.dry_run {
578                    paths::remove_dir_all(entry.path())?;
579                }
580            } else {
581                rm_file(entry.path(), entry.metadata())?;
582            }
583        }
584
585        Ok(())
586    }
587
588    pub fn display_summary(&self) -> CargoResult<()> {
589        let status = if self.dry_run { "Summary" } else { "Removed" };
590        let byte_count = if self.total_bytes_removed == 0 {
591            String::new()
592        } else {
593            let bytes = HumanBytes(self.total_bytes_removed);
594            format!(", {bytes:.1} total")
595        };
596        // I think displaying the number of directories removed isn't
597        // particularly interesting to the user. However, if there are 0
598        // files, and a nonzero number of directories, cargo should indicate
599        // that it did *something*, so directory counts are only shown in that
600        // case.
601        let file_count = match (self.num_files_removed, self.num_dirs_removed) {
602            (0, 0) => format!("0 files"),
603            (0, 1) => format!("1 directory"),
604            (0, 2..) => format!("{} directories", self.num_dirs_removed),
605            (1, _) => format!("1 file"),
606            (2.., _) => format!("{} files", self.num_files_removed),
607        };
608        self.gctx
609            .shell()
610            .status(status, format!("{file_count}{byte_count}"))?;
611        if self.dry_run {
612            self.gctx
613                .shell()
614                .warn("no files deleted due to --dry-run")?;
615        }
616        Ok(())
617    }
618
619    /// Deletes all of the given paths, showing a progress bar as it proceeds.
620    ///
621    /// If any path does not exist, or is not accessible, this will not
622    /// generate an error. This only generates an error for other issues, like
623    /// not being able to write to the console.
624    pub fn remove_paths(&mut self, paths: &[PathBuf]) -> CargoResult<()> {
625        let num_paths = paths
626            .iter()
627            .map(|path| walkdir::WalkDir::new(path).into_iter().count())
628            .sum();
629        self.progress = Box::new(CleaningFolderBar::new(self.gctx, num_paths));
630        for path in paths {
631            self.rm_rf(path)?;
632        }
633        Ok(())
634    }
635}
636
637trait CleaningProgressBar {
638    fn display_now(&mut self) -> CargoResult<()>;
639    fn on_clean(&mut self) -> CargoResult<()>;
640    fn on_cleaning_package(&mut self, _package: &str) -> CargoResult<()> {
641        Ok(())
642    }
643}
644
645struct CleaningFolderBar<'gctx> {
646    bar: Progress<'gctx>,
647    max: usize,
648    cur: usize,
649}
650
651impl<'gctx> CleaningFolderBar<'gctx> {
652    fn new(gctx: &'gctx GlobalContext, max: usize) -> Self {
653        Self {
654            bar: Progress::with_style("Cleaning", ProgressStyle::Percentage, gctx),
655            max,
656            cur: 0,
657        }
658    }
659
660    fn cur_progress(&self) -> usize {
661        std::cmp::min(self.cur, self.max)
662    }
663}
664
665impl<'gctx> CleaningProgressBar for CleaningFolderBar<'gctx> {
666    fn display_now(&mut self) -> CargoResult<()> {
667        self.bar.tick_now(self.cur_progress(), self.max, "")
668    }
669
670    fn on_clean(&mut self) -> CargoResult<()> {
671        self.cur += 1;
672        self.bar.tick(self.cur_progress(), self.max, "")
673    }
674}
675
676struct CleaningPackagesBar<'gctx> {
677    bar: Progress<'gctx>,
678    max: usize,
679    cur: usize,
680    num_files_folders_cleaned: usize,
681    package_being_cleaned: String,
682}
683
684impl<'gctx> CleaningPackagesBar<'gctx> {
685    fn new(gctx: &'gctx GlobalContext, max: usize) -> Self {
686        Self {
687            bar: Progress::with_style("Cleaning", ProgressStyle::Ratio, gctx),
688            max,
689            cur: 0,
690            num_files_folders_cleaned: 0,
691            package_being_cleaned: String::new(),
692        }
693    }
694
695    fn cur_progress(&self) -> usize {
696        std::cmp::min(self.cur, self.max)
697    }
698
699    fn format_message(&self) -> String {
700        format!(
701            ": {}, {} files/folders cleaned",
702            self.package_being_cleaned, self.num_files_folders_cleaned
703        )
704    }
705}
706
707impl<'gctx> CleaningProgressBar for CleaningPackagesBar<'gctx> {
708    fn display_now(&mut self) -> CargoResult<()> {
709        self.bar
710            .tick_now(self.cur_progress(), self.max, &self.format_message())
711    }
712
713    fn on_clean(&mut self) -> CargoResult<()> {
714        self.bar
715            .tick(self.cur_progress(), self.max, &self.format_message())?;
716        self.num_files_folders_cleaned += 1;
717        Ok(())
718    }
719
720    fn on_cleaning_package(&mut self, package: &str) -> CargoResult<()> {
721        self.cur += 1;
722        self.package_being_cleaned = String::from(package);
723        self.bar
724            .tick(self.cur_progress(), self.max, &self.format_message())
725    }
726}