77
88use clap:: builder:: ValueParser ;
99use clap:: { Arg , ArgAction , Command } ;
10- use std:: ffi:: OsString ;
11- use uucore:: checksum:: compute:: {
12- ChecksumComputeOptions , OutputFormat , perform_checksum_computation,
13- } ;
14- use uucore:: checksum:: validate:: {
15- ChecksumValidateOptions , ChecksumVerbose , perform_checksum_validation,
16- } ;
10+
11+ use uu_checksum_common:: { checksum_main, options} ;
12+
13+ use uucore:: checksum:: compute:: OutputFormat ;
1714use uucore:: checksum:: {
18- AlgoKind , ChecksumError , SUPPORTED_ALGORITHMS , SizedAlgoKind , calculate_blake2b_length_str,
15+ AlgoKind , ChecksumError , SUPPORTED_ALGORITHMS , calculate_blake2b_length_str,
1916 sanitize_sha2_sha3_length_str,
2017} ;
2118use uucore:: error:: UResult ;
2219use uucore:: hardware:: { HasHardwareFeatures as _, SimdPolicy } ;
23- use uucore:: line_ending:: LineEnding ;
2420use uucore:: { format_usage, show_error, translate} ;
2521
2622/// Print CPU hardware capability detection information to stderr
@@ -47,26 +43,6 @@ fn print_cpu_debug_info() {
4743 }
4844}
4945
50- mod options {
51- pub const ALGORITHM : & str = "algorithm" ;
52- pub const FILE : & str = "file" ;
53- pub const UNTAGGED : & str = "untagged" ;
54- pub const TAG : & str = "tag" ;
55- pub const LENGTH : & str = "length" ;
56- pub const RAW : & str = "raw" ;
57- pub const BASE64 : & str = "base64" ;
58- pub const CHECK : & str = "check" ;
59- pub const STRICT : & str = "strict" ;
60- pub const TEXT : & str = "text" ;
61- pub const BINARY : & str = "binary" ;
62- pub const STATUS : & str = "status" ;
63- pub const WARN : & str = "warn" ;
64- pub const IGNORE_MISSING : & str = "ignore-missing" ;
65- pub const QUIET : & str = "quiet" ;
66- pub const ZERO : & str = "zero" ;
67- pub const DEBUG : & str = "debug" ;
68- }
69-
7046/// cksum has a bunch of legacy behavior. We handle this in this function to
7147/// make sure they are self contained and "easier" to understand.
7248///
@@ -101,14 +77,6 @@ fn maybe_sanitize_length(
10177pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
10278 let matches = uucore:: clap_localization:: handle_clap_result ( uu_app ( ) , args) ?;
10379
104- let check = matches. get_flag ( options:: CHECK ) ;
105-
106- let ignore_missing = matches. get_flag ( options:: IGNORE_MISSING ) ;
107- let warn = matches. get_flag ( options:: WARN ) ;
108- let quiet = matches. get_flag ( options:: QUIET ) ;
109- let strict = matches. get_flag ( options:: STRICT ) ;
110- let status = matches. get_flag ( options:: STATUS ) ;
111-
11280 let algo_cli = matches
11381 . get_one :: < String > ( options:: ALGORITHM )
11482 . map ( AlgoKind :: from_cksum)
@@ -120,68 +88,22 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
12088
12189 let length = maybe_sanitize_length ( algo_cli, input_length) ?;
12290
123- // clap provides the default value -. So we unwrap() safety.
124- let files = matches
125- . get_many :: < OsString > ( options:: FILE )
126- . unwrap ( )
127- . map ( |s| s. as_os_str ( ) ) ;
128-
129- if check {
130- // cksum does not support '--check'ing legacy algorithms
131- if algo_cli. is_some_and ( AlgoKind :: is_legacy) {
132- return Err ( ChecksumError :: AlgorithmNotSupportedWithCheck . into ( ) ) ;
133- }
134-
135- let text_flag = matches. get_flag ( options:: TEXT ) ;
136- let binary_flag = matches. get_flag ( options:: BINARY ) ;
137- let tag = matches. get_flag ( options:: TAG ) ;
138-
139- if tag || binary_flag || text_flag {
140- return Err ( ChecksumError :: BinaryTextConflict . into ( ) ) ;
141- }
142-
143- // Execute the checksum validation based on the presence of files or the use of stdin
144-
145- let verbose = ChecksumVerbose :: new ( status, quiet, warn) ;
146- let opts = ChecksumValidateOptions {
147- ignore_missing,
148- strict,
149- verbose,
150- } ;
151-
152- return perform_checksum_validation ( files, algo_cli, length, opts) ;
153- }
154-
155- // Not --check
91+ let output_format = OutputFormat :: from_cksum (
92+ algo_cli. unwrap_or ( AlgoKind :: Crc ) ,
93+ // Making TAG default at clap blocks --untagged
94+ /* tag */
95+ !matches. get_flag ( options:: UNTAGGED ) ,
96+ /* binary */ matches. get_flag ( options:: BINARY ) ,
97+ /* raw */ matches. get_flag ( options:: RAW ) ,
98+ /* base64 */ matches. get_flag ( options:: BASE64 ) ,
99+ ) ;
156100
157101 // Print hardware debug info if requested
158102 if matches. get_flag ( options:: DEBUG ) {
159103 print_cpu_debug_info ( ) ;
160104 }
161105
162- // Set the default algorithm to CRC when not '--check'ing.
163- let algo_kind = algo_cli. unwrap_or ( AlgoKind :: Crc ) ;
164-
165- let algo = SizedAlgoKind :: from_unsized ( algo_kind, length) ?;
166- let line_ending = LineEnding :: from_zero_flag ( matches. get_flag ( options:: ZERO ) ) ;
167-
168- let opts = ChecksumComputeOptions {
169- algo_kind : algo,
170- output_format : OutputFormat :: from_cksum (
171- algo_kind,
172- // Making TAG default at clap blocks --untagged
173- /* tag */
174- !matches. get_flag ( options:: UNTAGGED ) ,
175- /* binary */ matches. get_flag ( options:: BINARY ) ,
176- /* raw */ matches. get_flag ( options:: RAW ) ,
177- /* base64 */ matches. get_flag ( options:: BASE64 ) ,
178- ) ,
179- line_ending,
180- } ;
181-
182- perform_checksum_computation ( opts, files) ?;
183-
184- Ok ( ( ) )
106+ checksum_main ( algo_cli, length, matches, output_format)
185107}
186108
187109pub fn uu_app ( ) -> Command {
0 commit comments