3
3
// For the full copyright and license information, please view the LICENSE
4
4
// file that was distributed with this source code.
5
5
6
- // spell-checker:ignore (words) wipesync prefill
341A
code>
6
+ // spell-checker:ignore (words) wipesync prefill couldnt
7
7
8
8
use clap:: { Arg , ArgAction , Command } ;
9
9
#[ cfg( unix) ]
@@ -20,7 +20,8 @@ use uucore::parser::parse_size::parse_size_u64;
20
20
use uucore:: parser:: shortcut_value_parser:: ShortcutValueParser ;
21
21
use uucore:: { format_usage, show_error, show_if_err} ;
22
22
23
- use uucore:: locale:: get_message;
23
+ use std:: collections:: HashMap ;
24
+ use uucore:: locale:: { get_message, get_message_with_args} ;
24
25
25
26
pub mod options {
26
27
pub const FORCE : & str = "force" ;
@@ -242,7 +243,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
242
243
let matches = uu_app ( ) . try_get_matches_from ( args) ?;
243
244
244
245
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
+ ) ) ;
246
250
}
247
251
248
252
let iterations = match matches. get_one :: < String > ( options:: ITERATIONS ) {
@@ -251,7 +255,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
251
255
Err ( _) => {
252
256
return Err ( USimpleError :: new (
253
257
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
+ ) ,
255
262
) ) ;
256
263
}
257
264
} ,
@@ -262,7 +269,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
262
269
Some ( filepath) => RandomSource :: Read ( File :: open ( filepath) . map_err ( |_| {
263
270
USimpleError :: new (
264
271
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
+ ) ,
266
276
)
267
277
} ) ?) ,
268
278
None => RandomSource :: System ,
@@ -321,14 +331,14 @@ pub fn uu_app() -> Command {
321
331
Arg :: new ( options:: FORCE )
322
332
. long ( options:: FORCE )
323
333
. short ( 'f' )
324
- . help ( "change permissions to allow writing if necessary" )
334
+ . help ( get_message ( "shred-force-help" ) )
325
335
. action ( ArgAction :: SetTrue ) ,
326
336
)
327
337
. arg (
328
338
Arg :: new ( options:: ITERATIONS )
329
339
. long ( options:: ITERATIONS )
330
340
. short ( 'n' )
331
- . help ( "overwrite N times instead of the default (3)" )
341
+ . help ( get_message ( "shred-iterations-help" ) )
332
342
. value_name ( "NUMBER" )
333
343
. default_value ( "3" ) ,
334
344
)
@@ -337,12 +347,12 @@ pub fn uu_app() -> Command {
337
347
. long ( options:: SIZE )
338
348
. short ( 's' )
339
349
. value_name ( "N" )
340
- . help ( "shred this many bytes (suffixes like K, M, G accepted)" ) ,
350
+ . help ( get_message ( "shred-size-help" ) ) ,
341
351
)
342
352
. arg (
343
353
Arg :: new ( options:: WIPESYNC )
344
354
. short ( 'u' )
345
- . help ( " deallocate and remove file after overwriting" )
355
+ . help ( get_message ( "shred- deallocate-help" ) )
346
356
. action ( ArgAction :: SetTrue ) ,
347
357
)
348
358
. arg (
@@ -357,37 +367,34 @@ pub fn uu_app() -> Command {
357
367
. num_args ( 0 ..=1 )
358
368
. require_equals ( true )
359
369
. 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" ) )
361
371
. action ( ArgAction :: Set ) ,
362
372
)
363
373
. arg (
364
374
Arg :: new ( options:: VERBOSE )
365
375
. long ( options:: VERBOSE )
366
376
. short ( 'v' )
367
- . help ( "show progress" )
377
+ . help ( get_message ( "shred-verbose-help" ) )
368
378
. action ( ArgAction :: SetTrue ) ,
369
379
)
370
380
. arg (
371
381
Arg :: new ( options:: EXACT )
372
382
. long ( options:: EXACT )
373
383
. 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" ) )
378
385
. action ( ArgAction :: SetTrue ) ,
379
386
)
380
387
. arg (
381
388
Arg :: new ( options:: ZERO )
382
389
. long ( options:: ZERO )
383
390
. short ( 'z' )
384
- . help ( "add a final overwrite with zeros to hide shredding" )
391
+ . help ( get_message ( "shred-zero-help" ) )
385
392
. action ( ArgAction :: SetTrue ) ,
386
393
)
387
394
. arg (
388
395
Arg :: new ( options:: RANDOM_SOURCE )
389
396
. long ( options:: RANDOM_SOURCE )
390
- . help ( "take random bytes from FILE" )
397
+ . help ( get_message ( "shred- random-source-help" ) )
391
398
. value_hint ( clap:: ValueHint :: FilePath )
392
399
. action ( ArgAction :: Set ) ,
393
400
)
@@ -405,7 +412,13 @@ fn get_size(size_str_opt: Option<String>) -> Option<u64> {
405
412
. and_then ( |size| parse_size_u64 ( size. as_str ( ) ) . ok ( ) )
406
413
. or_else ( || {
407
414
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
+ ) ;
409
422
// TODO: replace with our error management
410
423
std:: process:: exit ( 1 ) ;
411
424
}
@@ -439,13 +452,19 @@ fn wipe_file(
439
452
if !path. exists ( ) {
440
453
return Err ( USimpleError :: new (
441
454
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
+ ) ,
443
459
) ) ;
444
460
}
445
461
if !path. is_file ( ) {
446
462
return Err ( USimpleError :: new (
447
463
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
+ ) ,
449
468
) ) ;
450
469
}
451
470
@@ -518,7 +537,12 @@ fn wipe_file(
518
537
. write ( true )
519
538
. truncate ( false )
520
539
. 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
+ } ) ?;
522
546
523
547
let size = match size {
524
548
Some ( size) => size,
@@ -528,23 +552,35 @@ fn wipe_file(
528
552
for ( i, pass_type) in pass_sequence. into_iter ( ) . enumerate ( ) {
529
553
if verbose {
530
554
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
+ ) ;
531
559
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 ( )
535
563
) ;
536
564
}
537
565
// size is an optional argument for exactly how many bytes we want to shred
538
566
// Ignore failed writes; just keep trying
539
567
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
+ } )
542
574
) ;
543
575
}
544
576
545
577
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
+ } ) ?;
548
584
}
549
585
Ok ( ( ) )
550
586
}
@@ -615,9 +651,10 @@ fn wipe_name(orig_path: &Path, verbose: bool, remove_method: RemoveMethod) -> Op
615
651
Ok ( ( ) ) => {
616
652
if verbose {
617
653
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( )
621
658
) ;
622
659
}
623
660
@@ -634,11 +671,15 @@ fn wipe_name(orig_path: &Path, verbose: bool, remove_method: RemoveMethod) -> Op
634
671
break ;
635
672
}
636
673
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
+ ] ) ,
641
681
) ;
682
+ show_error ! ( "{}" , msg) ;
642
683
// TODO: replace with our error management
643
684
std:: process:: exit ( 1 ) ;
644
685
}
@@ -656,7 +697,13 @@ fn do_remove(
656
697
remove_method : RemoveMethod ,
657
698
) -> Result < ( ) , io:: Error > {
658
699
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
+ ) ;
660
707
}
661
708
662
709
let remove_path = if remove_method == RemoveMethod :: Unlink {
@@ -670,7 +717,13 @@ fn do_remove(
670
717
}
671
718
672
719
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
+ ) ;
674
727
}
675
728
676
729
Ok ( ( ) )
0 commit comments