18
18
19
19
const TEMPFILE_ATTEMPTS : u32 = 100 ;
20
20
21
- use libc;
22
- use nix;
23
- use openat;
24
21
use rand:: Rng ;
25
22
use std:: ffi:: OsStr ;
26
23
use std:: fs:: File ;
@@ -140,7 +137,7 @@ pub trait OpenatDirExt {
140
137
141
138
/// Create a `FileWriter` which provides a `std::io::BufWriter` and then atomically creates
142
139
/// the file at the destination, renaming it over an existing one if necessary.
143
- fn new_file_writer < ' a > ( & ' a self , mode : libc:: mode_t ) -> io:: Result < FileWriter > ;
140
+ fn new_file_writer ( & self , mode : libc:: mode_t ) -> io:: Result < FileWriter > ;
144
141
145
142
/// Atomically create or replace the destination file, calling the provided
146
143
/// function to generate the contents. Note that the contents of the
@@ -220,7 +217,7 @@ impl OpenatDirExt for openat::Dir {
220
217
}
221
218
222
219
fn remove_file_optional < P : openat:: AsPath > ( & self , p : P ) -> io:: Result < bool > {
223
- Ok ( impl_remove_file_optional ( self , p) ? )
220
+ impl_remove_file_optional ( self , p)
224
221
}
225
222
226
223
fn remove_dir_optional < P : openat:: AsPath > ( & self , p : P ) -> io:: Result < bool > {
@@ -414,8 +411,8 @@ impl OpenatDirExt for openat::Dir {
414
411
Ok ( ( ) )
415
412
}
416
413
417
- fn new_file_writer < ' a > ( & ' a self , mode : libc:: mode_t ) -> io:: Result < FileWriter > {
418
- let ( tmpf, name) = if let Some ( tmpf) = self . new_unnamed_file ( mode) . ok ( ) {
414
+ fn new_file_writer ( & self , mode : libc:: mode_t ) -> io:: Result < FileWriter > {
415
+ let ( tmpf, name) = if let Ok ( tmpf) = self . new_unnamed_file ( mode) {
419
416
( tmpf, None )
420
417
} else {
421
418
// FIXME allow this to be configurable
@@ -433,10 +430,7 @@ fn impl_read_to_string(mut f: File) -> io::Result<String> {
433
430
}
434
431
435
432
fn map_nix_error ( e : nix:: Error ) -> io:: Error {
436
- match e. as_errno ( ) {
437
- Some ( os_err) => io:: Error :: from_raw_os_error ( os_err as i32 ) ,
438
- _ => io:: Error :: new ( io:: ErrorKind :: Other , e) ,
439
- }
433
+ io:: Error :: from_raw_os_error ( e as i32 )
440
434
}
441
435
442
436
fn copy_regfile_inner (
@@ -501,7 +495,7 @@ pub(crate) fn tempfile_in(
501
495
match d. new_file ( tmpname. as_str ( ) , mode) {
502
496
Ok ( f) => return Ok ( ( f, tmpname) ) ,
503
497
Err ( ref e) if e. kind ( ) == io:: ErrorKind :: AlreadyExists => continue ,
504
- Err ( e) => Err ( e) ? ,
498
+ Err ( e) => return Err ( e) ,
505
499
}
506
500
}
507
501
Err ( io:: Error :: new (
@@ -518,7 +512,7 @@ pub(crate) fn tempfile_in(
518
512
/// optimal case where all components exist above.
519
513
pub ( crate ) fn impl_ensure_dir_all ( d : & openat:: Dir , p : & Path , mode : libc:: mode_t ) -> io:: Result < ( ) > {
520
514
if let Some ( parent) = p. parent ( ) {
521
- if parent. as_os_str ( ) . len ( ) > 0 {
515
+ if ! parent. as_os_str ( ) . is_empty ( ) {
522
516
impl_ensure_dir_all ( d, parent, mode) ?;
523
517
}
524
518
}
@@ -734,22 +728,22 @@ impl FileExt for File {
734
728
let copy_result =
735
729
copy_file_range ( self . as_raw_fd ( ) , None , to. as_raw_fd ( ) , None , bytes_to_copy) ;
736
730
if let Err ( ref copy_err) = copy_result {
737
- match copy_err. as_errno ( ) {
738
- Some ( Errno :: ENOSYS ) | Some ( Errno :: EPERM ) => {
731
+ match copy_err {
732
+ Errno :: ENOSYS | Errno :: EPERM => {
739
733
HAS_COPY_FILE_RANGE . store ( false , Ordering :: Relaxed ) ;
740
734
}
741
735
_ => { }
742
736
}
743
737
}
744
738
copy_result
745
739
} else {
746
- Err ( nix :: Error :: from_errno ( Errno :: ENOSYS ) )
740
+ Err ( Errno :: ENOSYS )
747
741
} ;
748
742
match copy_result {
749
743
Ok ( ret) => written += ret as u64 ,
750
744
Err ( err) => {
751
- match err. as_errno ( ) {
752
- Some ( os_err)
745
+ match err {
746
+ os_err
753
747
if os_err == Errno :: ENOSYS
754
748
|| os_err == Errno :: EXDEV
755
749
|| os_err == Errno :: EINVAL
@@ -763,8 +757,7 @@ impl FileExt for File {
763
757
assert_eq ! ( written, 0 ) ;
764
758
return fallback_file_copy ( self , to) ;
765
759
}
766
- Some ( os_err) => return Err ( io:: Error :: from_raw_os_error ( os_err as i32 ) ) ,
767
- _ => return Err ( io:: Error :: new ( io:: ErrorKind :: Other , err) ) ,
760
+ os_err => return Err ( map_nix_error ( os_err) ) ,
768
761
}
769
762
}
770
763
}
@@ -785,7 +778,7 @@ impl FileExt for File {
785
778
fn pread_exact ( & self , buf : & mut [ u8 ] , start_pos : usize ) -> io:: Result < ( ) > {
786
779
use nix:: sys:: uio:: pread;
787
780
788
- if buf. len ( ) == 0 {
781
+ if buf. is_empty ( ) {
789
782
return Err ( io:: Error :: new (
790
783
io:: ErrorKind :: InvalidInput ,
791
784
"zero-sized buffer in input" ,
0 commit comments