Skip to content

Commit 0e1be59

Browse files
committed
util(cksum): Avoid collecting a file vector preemptively
1 parent 909da50 commit 0e1be59

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/uu/cksum/src/cksum.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
267267
return Err(ChecksumError::AlgorithmNotSupportedWithCheck.into());
268268
}
269269

270+
let files = matches.get_many::<OsString>(options::FILE).map_or_else(
271+
// No files given, read from stdin.
272+
|| Box::new(iter::once(OsStr::new("-"))) as Box<dyn Iterator<Item = &OsStr>>,
273+
// At least one file given, read from them.
274+
|files| Box::new(files.map(OsStr::new)) as Box<dyn Iterator<Item = &OsStr>>,
275+
);
276+
270277
if check {
271278
let text_flag = matches.get_flag(options::TEXT);
272279
let binary_flag = matches.get_flag(options::BINARY);
@@ -290,11 +297,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
290297

291298
// Execute the checksum validation based on the presence of files or the use of stdin
292299

293-
let files = matches.get_many::<OsString>(options::FILE).map_or_else(
294-
|| iter::once(OsStr::new("-")).collect::<Vec<_>>(),
295-
|files| files.map(OsStr::new).collect::<Vec<_>>(),
296-
);
297-
298300
let verbose = ChecksumVerbose::new(status, quiet, warn);
299301
let opts = ChecksumOptions {
300302
binary: binary_flag,
@@ -303,7 +305,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
303305
verbose,
304306
};
305307

306-
return perform_checksum_validation(files.iter().copied(), algo_option, length, opts);
308+
return perform_checksum_validation(files, algo_option, length, opts);
307309
}
308310

309311
let (tag, asterisk) = handle_tag_text_binary_flags(std::env::args_os())?;
@@ -330,10 +332,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
330332
line_ending,
331333
};
332334

333-
match matches.get_many::<OsString>(options::FILE) {
334-
Some(files) => cksum(opts, files.map(OsStr::new))?,
335-
None => cksum(opts, iter::once(OsStr::new("-")))?,
336-
}
335+
cksum(opts, files)?;
337336

338337
Ok(())
339338
}

0 commit comments

Comments
 (0)