33// For the full copyright and license information, please view the LICENSE
44// file that was distributed with this source code.
55
6- // spell-checker:ignore (words) wipesync prefill
6+ // spell-checker:ignore (words) wipesync prefill couldnt
77
88use clap:: { Arg , ArgAction , Command } ;
99#[ cfg( unix) ]
@@ -20,7 +20,8 @@ use uucore::parser::parse_size::parse_size_u64;
2020use uucore:: parser:: shortcut_value_parser:: ShortcutValueParser ;
2121use uucore:: { format_usage, show_error, show_if_err} ;
2222
23- use uucore:: locale:: get_message;
23+ use std:: collections:: HashMap ;
24+ use uucore:: locale:: { get_message, get_message_with_args} ;
2425
2526pub mod options {
2627 pub const FORCE : & str = "force" ;
@@ -242,7 +243,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
242243 let matches = uu_app ( ) . try_get_matches_from ( args) ?;
243244
244245 if !matches. contains_id ( options:: FILE ) {
245- return Err ( UUsageError :: new ( 1 , "missing file operand" ) ) ;
246+ return Err ( UUsageError :: new (
247+ 1 ,
248+ get_message ( "shred-missing-file-operand" ) ,
249+ ) ) ;
246250 }
247251
248252 let iterations = match matches. get_one :: < String > ( options:: ITERATIONS ) {
@@ -251,7 +255,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
251255 Err ( _) => {
252256 return Err ( USimpleError :: new (
253257 1 ,
254- format ! ( "invalid number of passes: {}" , s. quote( ) ) ,
258+ get_message_with_args (
259+ "shred-invalid-number-of-passes" ,
260+ HashMap :: from ( [ ( "passes" . to_string ( ) , s. quote ( ) . to_string ( ) ) ] ) ,
261+ ) ,
255262 ) ) ;
256263 }
257264 } ,
@@ -262,7 +269,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
262269 Some ( filepath) => RandomSource :: Read ( File :: open ( filepath) . map_err ( |_| {
263270 USimpleError :: new (
264271 1 ,
265- format ! ( "cannot open random source: {}" , filepath. quote( ) ) ,
272+ get_message_with_args (
273+ "shred-cannot-open-random-source" ,
274+ HashMap :: from ( [ ( "source" . to_string ( ) , filepath. quote ( ) . to_string ( ) ) ] ) ,
275+ ) ,
266276 )
267277 } ) ?) ,
268278 None => RandomSource :: System ,
@@ -321,14 +331,14 @@ pub fn uu_app() -> Command {
321331 Arg :: new ( options:: FORCE )
322332 . long ( options:: FORCE )
323333 . short ( 'f' )
324- . help ( "change permissions to allow writing if necessary" )
334+ . help ( get_message ( "shred-force-help" ) )
325335 . action ( ArgAction :: SetTrue ) ,
326336 )
327337 . arg (
328338 Arg :: new ( options:: ITERATIONS )
329339 . long ( options:: ITERATIONS )
330340 . short ( 'n' )
331- . help ( "overwrite N times instead of the default (3)" )
341+ . help ( get_message ( "shred-iterations-help" ) )
332342 . value_name ( "NUMBER" )
333343 . default_value ( "3" ) ,
334344 )
@@ -337,12 +347,12 @@ pub fn uu_app() -> Command {
337347 . long ( options:: SIZE )
338348 . short ( 's' )
339349 . value_name ( "N" )
340- . help ( "shred this many bytes (suffixes like K, M, G accepted)" ) ,
350+ . help ( get_message ( "shred-size-help" ) ) ,
341351 )
342352 . arg (
343353 Arg :: new ( options:: WIPESYNC )
344354 . short ( 'u' )
345- . help ( " deallocate and remove file after overwriting" )
355+ . help ( get_message ( "shred- deallocate-help" ) )
346356 . action ( ArgAction :: SetTrue ) ,
347357 )
348358 . arg (
@@ -357,37 +367,34 @@ pub fn uu_app() -> Command {
357367 . num_args ( 0 ..=1 )
358368 . require_equals ( true )
359369 . default_missing_value ( options:: remove:: WIPESYNC )
360- . help ( "like -u but give control on HOW to delete; See below" )
370+ . help ( get_message ( "shred-remove-help" ) )
361371 . action ( ArgAction :: Set ) ,
362372 )
363373 . arg (
364374 Arg :: new ( options:: VERBOSE )
365375 . long ( options:: VERBOSE )
366376 . short ( 'v' )
367- . help ( "show progress" )
377+ . help ( get_message ( "shred-verbose-help" ) )
368378 . action ( ArgAction :: SetTrue ) ,
369379 )
370380 . arg (
371381 Arg :: new ( options:: EXACT )
372382 . long ( options:: EXACT )
373383 . short ( 'x' )
374- . help (
375- "do not round file sizes up to the next full block;\n \
376- this is the default for non-regular files",
377- )
384+ . help ( get_message ( "shred-exact-help" ) )
378385 . action ( ArgAction :: SetTrue ) ,
379386 )
380387 . arg (
381388 Arg :: new ( options:: ZERO )
382389 . long ( options:: ZERO )
383390 . short ( 'z' )
384- . help ( "add a final overwrite with zeros to hide shredding" )
391+ . help ( get_message ( "shred-zero-help" ) )
385392 . action ( ArgAction :: SetTrue ) ,
386393 )
387394 . arg (
388395 Arg :: new ( options:: RANDOM_SOURCE )
389396 . long ( options:: RANDOM_SOURCE )
390- . help ( "take random bytes from FILE" )
397+ . help ( get_message ( "shred- random-source-help" ) )
391398 . value_hint ( clap:: ValueHint :: FilePath )
392399 . action ( ArgAction :: Set ) ,
393400 )
@@ -405,7 +412,13 @@ fn get_size(size_str_opt: Option<String>) -> Option<u64> {
405412 . and_then ( |size| parse_size_u64 ( size. as_str ( ) ) . ok ( ) )
406413 . or_else ( || {
407414 if let Some ( size) = size_str_opt {
408- show_error ! ( "invalid file size: {}" , size. quote( ) ) ;
415+ show_error ! (
416+ "{}" ,
417+ get_message_with_args(
418+ "shred-invalid-file-size" ,
419+ HashMap :: from( [ ( "size" . to_string( ) , size. quote( ) . to_string( ) ) ] )
420+ )
421+ ) ;
409422 // TODO: replace with our error management
410423 std:: process:: exit ( 1 ) ;
411424 }
@@ -439,13 +452,19 @@ fn wipe_file(
439452 if !path. exists ( ) {
440453 return Err ( USimpleError :: new (
441454 1 ,
442- format ! ( "{}: No such file or directory" , path. maybe_quote( ) ) ,
455+ get_message_with_args (
456+ "shred-no-such-file-or-directory" ,
457+ HashMap :: from ( [ ( "file" . to_string ( ) , path. maybe_quote ( ) . to_string ( ) ) ] ) ,
458+ ) ,
443459 ) ) ;
444460 }
445461 if !path. is_file ( ) {
446462 return Err ( USimpleError :: new (
447463 1 ,
448- format ! ( "{}: Not a file" , path. maybe_quote( ) ) ,
464+ get_message_with_args (
465+ "shred-not-a-file" ,
466+ HashMap :: from ( [ ( "file" . to_string ( ) , path. maybe_quote ( ) . to_string ( ) ) ] ) ,
467+ ) ,
449468 ) ) ;
450469 }
451470
@@ -518,7 +537,12 @@ fn wipe_file(
518537 . write ( true )
519538 . truncate ( false )
520539 . open ( path)
521- . map_err_context ( || format ! ( "{}: failed to open for writing" , path. maybe_quote( ) ) ) ?;
540+ . map_err_context ( || {
541+ get_message_with_args (
542+ "shred-failed-to-open-for-writing" ,
543+ HashMap :: from ( [ ( "file" . to_string ( ) , path. maybe_quote ( ) . to_string ( ) ) ] ) ,
544+ )
545+ } ) ?;
522546
523547 let size = match size {
524548 Some ( size) => size,
@@ -528,23 +552,35 @@ fn wipe_file(
528552 for ( i, pass_type) in pass_sequence. into_iter ( ) . enumerate ( ) {
529553 if verbose {
530554 let pass_name = pass_name ( & pass_type) ;
555+ let msg = get_message_with_args (
556+ "shred-pass-progress" ,
557+ HashMap :: from ( [ ( "file" . to_string ( ) , path. maybe_quote ( ) . to_string ( ) ) ] ) ,
558+ ) ;
531559 show_error ! (
532- "{}: pass {}/{total_passes} ({pass_name})..." ,
533- path . maybe_quote ( ) ,
534- i + 1 ,
560+ "{} {}/{total_passes} ({pass_name})..." ,
561+ msg ,
562+ ( i + 1 ) . to_string ( )
535563 ) ;
536564 }
537565 // size is an optional argument for exactly how many bytes we want to shred
538566 // Ignore failed writes; just keep trying
539567 show_if_err ! (
540- do_pass( & mut file, & pass_type, exact, random_source, size)
541- . map_err_context( || format!( "{}: File write pass failed" , path. maybe_quote( ) ) )
568+ do_pass( & mut file, & pass_type, exact, random_source, size) . map_err_context( || {
569+ get_message_with_args(
570+ "shred-file-write-pass-failed" ,
571+ HashMap :: from( [ ( "file" . to_string( ) , path. maybe_quote( ) . to_string( ) ) ] ) ,
572+ )
573+ } )
542574 ) ;
543575 }
544576
545577 if remove_method != RemoveMethod :: None {
546- do_remove ( path, path_str, verbose, remove_method)
547- . map_err_context ( || format ! ( "{}: failed to remove file" , path. maybe_quote( ) ) ) ?;
578+ do_remove ( path, path_str, verbose, remove_method) . map_err_context ( || {
579+ get_message_with_args (
580+ "shred-failed-to-remove-file" ,
581+ HashMap :: from ( [ ( "file" . to_string ( ) , path. maybe_quote ( ) . to_string ( ) ) ] ) ,
582+ )
583+ } ) ?;
548584 }
549585 Ok ( ( ) )
550586}
@@ -615,9 +651,10 @@ fn wipe_name(orig_path: &Path, verbose: bool, remove_method: RemoveMethod) -> Op
615651 Ok ( ( ) ) => {
616652 if verbose {
617653 show_error ! (
618- "{}: renamed to {}" ,
619- last_path. maybe_quote( ) ,
620- new_path. display( )
654+ "{}: {} {}" ,
655+ last_path. maybe_quote( ) . to_string( ) ,
656+ get_message( "shred-renamed-to" , ) ,
657+ new_path. display( ) . to_string( )
621658 ) ;
622659 }
623660
@@ -634,11 +671,15 @@ fn wipe_name(orig_path: &Path, verbose: bool, remove_method: RemoveMethod) -> Op
634671 break ;
635672 }
636673 Err ( e) => {
637- show_error ! (
638- "{}: Couldn't rename to {}: {e}" ,
639- last_path. maybe_quote( ) ,
640- new_path. quote( ) ,
674+ let msg = get_message_with_args (
675+ "shred-couldnt-rename" ,
676+ HashMap :: from ( [
677+ ( "file" . to_string ( ) , last_path. maybe_quote ( ) . to_string ( ) ) ,
678+ ( "new_name" . to_string ( ) , new_path. quote ( ) . to_string ( ) ) ,
679+ ( "error" . to_string ( ) , e. to_string ( ) ) ,
680+ ] ) ,
641681 ) ;
682+ show_error ! ( "{}" , msg) ;
642683 // TODO: replace with our error management
643684 std:: process:: exit ( 1 ) ;
644685 }
@@ -656,7 +697,13 @@ fn do_remove(
656697 remove_method : RemoveMethod ,
657698) -> Result < ( ) , io:: Error > {
658699 if verbose {
659- show_error ! ( "{}: removing" , orig_filename. maybe_quote( ) ) ;
700+ show_error ! (
701+ "{}" ,
702+ get_message_with_args(
703+ "shred-removing" ,
704+ HashMap :: from( [ ( "file" . to_string( ) , orig_filename. maybe_quote( ) . to_string( ) ) ] )
705+ )
706+ ) ;
660707 }
661708
662709 let remove_path = if remove_method == RemoveMethod :: Unlink {
@@ -670,7 +717,13 @@ fn do_remove(
670717 }
671718
672719 if verbose {
673- show_error ! ( "{}: removed" , orig_filename. maybe_quote( ) ) ;
720+ show_error ! (
721+ "{}" ,
722+ get_message_with_args(
723+ "shred-removed" ,
724+ HashMap :: from( [ ( "file" . to_string( ) , orig_filename. maybe_quote( ) . to_string( ) ) ] )
725+ )
726+ ) ;
674727 }
675728
676729 Ok ( ( ) )
0 commit comments