From f4fe781b29313f630735d25d036deb02739d71fa Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Fri, 29 Aug 2025 18:38:38 +0200 Subject: [PATCH 1/2] cksum: earlier continue if path is dir --- src/uu/cksum/src/cksum.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index 01e3da55f..fba4adaba 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -9,7 +9,7 @@ use clap::builder::ValueParser; use clap::{Arg, ArgAction, Command, value_parser}; use std::ffi::{OsStr, OsString}; use std::fs::File; -use std::io::{self, BufReader, Read, Write, stdin, stdout}; +use std::io::{BufReader, Read, Write, stdin, stdout}; use std::iter; use std::path::Path; use uucore::checksum::{ @@ -70,12 +70,18 @@ where let file_buf; let not_file = filename == OsStr::new("-"); + if filename.is_dir() { + show!(USimpleError::new( + 1, + translate!("cksum-error-is-directory", "file" => filename.display()) + )); + continue; + } + // Handle the file input let mut file = BufReader::new(if not_file { stdin_buf = stdin(); Box::new(stdin_buf) as Box - } else if filename.is_dir() { - Box::new(BufReader::new(io::empty())) as Box } else { file_buf = match File::open(filename) { Ok(file) => file, @@ -87,14 +93,6 @@ where Box::new(file_buf) as Box }); - if filename.is_dir() { - show!(USimpleError::new( - 1, - translate!("cksum-error-is-directory", "file" => filename.display()) - )); - continue; - } - let (sum_hex, sz) = digest_reader(&mut options.digest, &mut file, false, options.output_bits) .map_err_context(|| translate!("cksum-error-failed-to-read-input"))?; From 40b474c0ea1aaa7b0ac190311781fcc907b4c2e5 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Fri, 29 Aug 2025 18:44:01 +0200 Subject: [PATCH 2/2] cksum: rename "not_file" to avoid double negation --- src/uu/cksum/src/cksum.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index fba4adaba..4174a6bd1 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -68,7 +68,7 @@ where let filename = Path::new(filename); let stdin_buf; let file_buf; - let not_file = filename == OsStr::new("-"); + let is_stdin = filename == OsStr::new("-"); if filename.is_dir() { show!(USimpleError::new( @@ -79,7 +79,7 @@ where } // Handle the file input - let mut file = BufReader::new(if not_file { + let mut file = BufReader::new(if is_stdin { stdin_buf = stdin(); Box::new(stdin_buf) as Box } else { @@ -128,9 +128,9 @@ where "{} {}{}", sum.parse::().unwrap(), sz.div_ceil(options.output_bits), - if not_file { "" } else { " " } + if is_stdin { "" } else { " " } ), - !not_file, + !is_stdin, String::new(), ), ALGORITHM_OPTIONS_BSD => ( @@ -138,14 +138,14 @@ where "{:0bsd_width$} {:bsd_width$}{}", sum.parse::().unwrap(), sz.div_ceil(options.output_bits), - if not_file { "" } else { " " } + if is_stdin { "" } else { " " } ), - !not_file, + !is_stdin, String::new(), ), ALGORITHM_OPTIONS_CRC | ALGORITHM_OPTIONS_CRC32B => ( - format!("{sum} {sz}{}", if not_file { "" } else { " " }), - !not_file, + format!("{sum} {sz}{}", if is_stdin { "" } else { " " }), + !is_stdin, String::new(), ), ALGORITHM_OPTIONS_BLAKE2B if options.tag => {