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
use anyhow::{anyhow, Result};
use clap::Parser;
use tracing::instrument;
#[derive(Parser, Debug, Clone)]
#[command(
name = "rrm",
version,
about = "Remove large filesets efficiently - equivalent to `rm -rf`",
long_about = "`rrm` is a tool for removing large number of files efficiently.
EXAMPLE:
# Remove a path recursively with progress
rrm /path/to/remove --progress --summary
Note: Like `rm -rf`, this is a destructive operation. Use with caution."
)]
struct Args {
// Removal options
/// Exit on first error
#[arg(short = 'e', long = "fail-early", help_heading = "Removal options")]
fail_early: bool,
// Filtering options
/// Glob pattern for files to include (can be specified multiple times)
///
/// Only files matching at least one include pattern will be removed. Patterns use glob
/// syntax: * matches anything except /, ** matches anything including /, ? matches single
/// char, [...] for character classes. Leading / anchors to source root, trailing / matches
/// only directories. Simple patterns (like *.txt) apply to the source root itself;
/// anchored patterns (like /src/**) match paths inside the source.
#[arg(long, value_name = "PATTERN", action = clap::ArgAction::Append, help_heading = "Filtering")]
include: Vec<String>,
/// Glob pattern for files to exclude (can be specified multiple times)
///
/// Files matching any exclude pattern will be skipped. Excludes are checked before includes.
/// Simple patterns (like *.log) can exclude the source root itself; anchored patterns
/// (like /build/) only match paths inside the source.
#[arg(long, value_name = "PATTERN", action = clap::ArgAction::Append, help_heading = "Filtering")]
exclude: Vec<String>,
/// Read filter patterns from file
#[arg(long, value_name = "PATH", conflicts_with_all = ["include", "exclude"], help_heading = "Filtering")]
filter_file: Option<std::path::PathBuf>,
/// Preview mode - show what would be removed without actually removing
///
/// --progress and --summary are suppressed in dry-run mode (use -v to
/// still see summary output).
#[arg(long, value_name = "MODE", help_heading = "Filtering")]
dry_run: Option<common::DryRunMode>,
// Progress & output
/// Show progress
#[arg(long, help_heading = "Progress & output")]
progress: bool,
/// Toggles the type of progress to show
///
/// If specified, --progress flag is implied.
///
/// Options are: `ProgressBar` (animated progress bar), `TextUpdates` (appropriate for logging), Auto (default, will
/// choose between `ProgressBar` or `TextUpdates` depending on the type of terminal attached to stderr)
#[arg(long, value_name = "TYPE", help_heading = "Progress & output")]
progress_type: Option<common::ProgressType>,
/// Sets the delay between progress updates
///
/// - For the interactive (--progress-type=ProgressBar), the default is 200ms.
/// - For the non-interactive (--progress-type=TextUpdates), the default is 10s.
///
/// If specified, --progress flag is implied.
///
/// This option accepts a human readable duration, e.g. "200ms", "10s", "5min" etc.
#[arg(long, value_name = "DELAY", help_heading = "Progress & output")]
progress_delay: Option<String>,
/// Verbose level (implies "summary"): -v INFO / -vv DEBUG / -vvv TRACE (default: ERROR)
#[arg(short = 'v', long = "verbose", action = clap::ArgAction::Count, help_heading = "Progress & output")]
verbose: u8,
/// Print summary at the end
#[arg(long, help_heading = "Progress & output")]
summary: bool,
/// Quiet mode, don't report errors
#[arg(short = 'q', long = "quiet", help_heading = "Progress & output")]
quiet: bool,
// Performance & throttling
/// Maximum number of open files, 0 means no limit, leaving unspecified means using 80% of max open files system limit
#[arg(long, value_name = "N", help_heading = "Performance & throttling")]
max_open_files: Option<usize>,
/// Throttle the number of operations per second, 0 means no throttle
#[arg(
long,
default_value = "0",
value_name = "N",
help_heading = "Performance & throttling"
)]
ops_throttle: usize,
/// Throttle the number of I/O operations per second, 0 means no throttle
///
/// I/O is calculated based on provided chunk size -- number of I/O operations for a file is calculated as:
/// ((file size - 1) / chunk size) + 1
#[arg(
long,
default_value = "0",
value_name = "N",
help_heading = "Performance & throttling"
)]
iops_throttle: usize,
/// Chunk size used to calculate number of I/O per file
///
/// Modifying this setting to a value > 0 is REQUIRED when using --iops-throttle.
#[arg(
long,
default_value = "0",
value_name = "SIZE",
help_heading = "Performance & throttling"
)]
chunk_size: u64,
// Advanced settings
/// Number of worker threads, 0 means number of cores
#[arg(
long,
default_value = "0",
value_name = "N",
help_heading = "Advanced settings"
)]
max_workers: usize,
/// Number of blocking worker threads, 0 means Tokio runtime default (512)
#[arg(
long,
default_value = "0",
value_name = "N",
help_heading = "Advanced settings"
)]
max_blocking_threads: usize,
// ARGUMENTS
/// Path(s) to remove
#[arg()]
paths: Vec<std::path::PathBuf>,
}
#[instrument]
async fn async_main(args: Args) -> Result<common::rm::Summary> {
// build filter settings once before the loop
let filter = if let Some(ref path) = args.filter_file {
Some(common::filter::FilterSettings::from_file(path)?)
} else if !args.include.is_empty() || !args.exclude.is_empty() {
let mut filter_settings = common::filter::FilterSettings::new();
for p in &args.include {
filter_settings.add_include(p)?;
}
for p in &args.exclude {
filter_settings.add_exclude(p)?;
}
Some(filter_settings)
} else {
None
};
let mut join_set = tokio::task::JoinSet::new();
for path in args.paths {
let settings = common::rm::Settings {
fail_early: args.fail_early,
filter: filter.clone(),
dry_run: args.dry_run,
};
let do_rm = || async move { common::rm(&path, &settings).await };
join_set.spawn(do_rm());
}
let error_collector = common::error_collector::ErrorCollector::default();
let mut rm_summary = common::rm::Summary::default();
while let Some(res) = join_set.join_next().await {
match res? {
Ok(summary) => rm_summary = rm_summary + summary,
Err(error) => {
tracing::error!("{:#}", &error);
rm_summary = rm_summary + error.summary;
if args.fail_early {
if args.summary {
return Err(anyhow!("{}\n\n{}", error, &rm_summary));
}
return Err(anyhow!("{}", error));
}
error_collector.push(error.source);
}
}
}
if let Some(err) = error_collector.into_error() {
if args.summary {
return Err(anyhow!("{:#}\n\n{}", err, &rm_summary));
}
return Err(err);
}
Ok(rm_summary)
}
fn main() -> Result<()> {
let args = Args::parse();
let dry_run_warnings = args.dry_run.map(|_| {
common::DryRunWarnings::new(
args.progress || args.progress_type.is_some() || args.progress_delay.is_some(),
args.summary,
args.verbose,
false, // rrm has no --overwrite
!args.include.is_empty() || !args.exclude.is_empty() || args.filter_file.is_some(),
false, // rrm has no destination
false, // rrm has no --ignore-existing
)
});
let is_dry_run = dry_run_warnings.is_some();
let func = {
let args = args.clone();
|| async_main(args)
};
let output = common::OutputConfig {
quiet: args.quiet,
verbose: args.verbose,
print_summary: if is_dry_run { false } else { args.summary },
..Default::default()
};
let runtime = common::RuntimeConfig {
max_workers: args.max_workers,
max_blocking_threads: args.max_blocking_threads,
};
let throttle = common::ThrottleConfig {
max_open_files: args.max_open_files,
ops_throttle: args.ops_throttle,
iops_throttle: args.iops_throttle,
chunk_size: args.chunk_size,
};
let tracing = common::TracingConfig {
remote_layer: None,
debug_log_file: None,
chrome_trace_prefix: None,
flamegraph_prefix: None,
trace_identifier: "rrm".to_string(),
profile_level: None,
tokio_console: false,
tokio_console_port: None,
};
let res = common::run(
if !is_dry_run
&& (args.progress || args.progress_type.is_some() || args.progress_delay.is_some())
{
Some(common::ProgressSettings {
progress_type: common::GeneralProgressType::User(
args.progress_type.unwrap_or_default(),
),
progress_delay: args.progress_delay,
})
} else {
None
},
output,
runtime,
throttle,
tracing,
func,
);
if let Some(warnings) = dry_run_warnings {
warnings.print();
}
if res.is_none() {
std::process::exit(1);
}
Ok(())
}