Skip to content

Commit 3321c33

Browse files
committed
rm: add the --progress option like with cp & mv
1 parent 0c3eb12 commit 3321c33

File tree

8 files changed

+243
-22
lines changed

8 files changed

+243
-22
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/src/extensions.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ packages.
4343

4444
`mv` can display a progress bar when the `-g`/`--progress` flag is set.
4545

46+
## `rm`
47+
48+
`rm` can display a progress bar when the `-g`/`--progress` flag is set.
49+
4650
## `hashsum`
4751

4852
This utility does not exist in GNU coreutils. `hashsum` is a utility that

src/uu/rm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ thiserror = { workspace = true }
2222
clap = { workspace = true }
2323
uucore = { workspace = true, features = ["fs", "parser", "safe-traversal"] }
2424
fluent = { workspace = true }
25+
indicatif = { workspace = true }
2526

2627
[target.'cfg(unix)'.dependencies]
2728
libc = { workspace = true }

src/uu/rm/locales/en-US.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ rm-help-preserve-root = do not remove '/' (default)
2828
rm-help-recursive = remove directories and their contents recursively
2929
rm-help-dir = remove empty directories
3030
rm-help-verbose = explain what is being done
31+
rm-help-progress = display a progress bar. Note: this feature is not supported by GNU coreutils.
32+
33+
# Progress messages
34+
rm-progress-removing = Removing
3135
3236
# Error messages
3337
rm-error-missing-operand = missing operand

src/uu/rm/locales/fr-FR.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ rm-help-preserve-root = ne pas supprimer '/' (par défaut)
2828
rm-help-recursive = supprimer les répertoires et leur contenu récursivement
2929
rm-help-dir = supprimer les répertoires vides
3030
rm-help-verbose = expliquer ce qui est fait
31+
rm-help-progress = afficher une barre de progression. Note : cette fonctionnalité n'est pas supportée par GNU coreutils.
32+
33+
# Messages de progression
34+
rm-progress-removing = Suppression
3135
3236
# Messages d'erreur
3337
rm-error-missing-operand = opérande manquant

src/uu/rm/src/platform/linux.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
// spell-checker:ignore fstatat unlinkat
99

10+
use indicatif::ProgressBar;
1011
use std::ffi::OsStr;
1112
use std::fs;
1213
use std::path::Path;
@@ -28,14 +29,22 @@ pub fn is_readable(path: &Path) -> bool {
2829
}
2930

3031
/// Remove a single file using safe traversal
31-
pub fn safe_remove_file(path: &Path, options: &Options) -> Option<bool> {
32+
pub fn safe_remove_file(
33+
path: &Path,
34+
options: &Options,
35+
progress_bar: Option<&ProgressBar>,
36+
) -> Option<bool> {
3237
let parent = path.parent()?;
3338
let file_name = path.file_name()?;
3439

3540
let dir_fd = DirFd::open(parent).ok()?;
3641

3742
match dir_fd.unlink_at(file_name, false) {
3843
Ok(_) => {
44+
// Update progress bar for file removal
45+
if let Some(pb) = progress_bar {
46+
pb.inc(1);
47+
}
3948
verbose_removed_file(path, options);
4049
Some(false)
4150
}
@@ -51,14 +60,22 @@ pub fn safe_remove_file(path: &Path, options: &Options) -> Option<bool> {
5160
}
5261

5362
/// Remove an empty directory using safe traversal
54-
pub fn safe_remove_empty_dir(path: &Path, options: &Options) -> Option<bool> {
63+
pub fn safe_remove_empty_dir(
64+
path: &Path,
65+
options: &Options,
66+
progress_bar: Option<&ProgressBar>,
67+
) -> Option<bool> {
5568
let parent = path.parent()?;
5669
let dir_name = path.file_name()?;
5770

5871
let dir_fd = DirFd::open(parent).ok()?;
5972

6073
match dir_fd.unlink_at(dir_name, true) {
6174
Ok(_) => {
75+
// Update progress bar for directory removal
76+
if let Some(pb) = progress_bar {
77+
pb.inc(1);
78+
}
6279
verbose_removed_directory(path, options);
6380
Some(false)
6481
}
@@ -172,12 +189,16 @@ pub fn remove_dir_with_special_cases(path: &Path, options: &Options, error_occur
172189
}
173190
}
174191

175-
pub fn safe_remove_dir_recursive(path: &Path, options: &Options) -> bool {
192+
pub fn safe_remove_dir_recursive(
193+
path: &Path,
194+
options: &Options,
195+
progress_bar: Option<&ProgressBar>,
196+
) -> bool {
176197
// Base case 1: this is a file or a symbolic link.
177198
// Use lstat to avoid race condition between check and use
178199
match fs::symlink_metadata(path) {
179200
Ok(metadata) if !metadata.is_dir() => {
180-
return remove_file(path, options);
201+
return remove_file(path, options, progress_bar);
181202
}
182203
Ok(_) => {}
183204
Err(e) => {

0 commit comments

Comments
 (0)