@@ -1195,57 +1195,6 @@ impl fmt::Debug for OpenOptions {
11951195 }
11961196}
11971197
1198- #[ cfg( not( any(
1199- target_os = "redox" ,
1200- target_os = "espidf" ,
1201- target_os = "horizon" ,
1202- target_os = "nuttx" ,
1203- ) ) ) ]
1204- #[ inline( always) ]
1205- fn to_timespec ( time : Option < SystemTime > ) -> io:: Result < libc:: timespec > {
1206- match time {
1207- Some ( time) if let Some ( ts) = time. t . to_timespec ( ) => Ok ( ts) ,
1208- Some ( time) if time > crate :: sys:: time:: UNIX_EPOCH => Err ( io:: const_error!(
1209- io:: ErrorKind :: InvalidInput ,
1210- "timestamp is too large to set as a file time" ,
1211- ) ) ,
1212- Some ( _) => Err ( io:: const_error!(
1213- io:: ErrorKind :: InvalidInput ,
1214- "timestamp is too small to set as a file time" ,
1215- ) ) ,
1216- None => Ok ( libc:: timespec { tv_sec : 0 , tv_nsec : libc:: UTIME_OMIT as _ } ) ,
1217- }
1218- }
1219-
1220- #[ cfg( target_vendor = "apple" ) ]
1221- #[ inline( always) ]
1222- fn set_attrlist_with_times (
1223- times : & FileTimes ,
1224- ) -> io:: Result < ( libc:: attrlist , [ mem:: MaybeUninit < libc:: timespec > ; 3 ] , usize ) > {
1225- let mut buf = [ mem:: MaybeUninit :: < libc:: timespec > :: uninit ( ) ; 3 ] ;
1226- let mut num_times = 0 ;
1227- let mut attrlist: libc:: attrlist = unsafe { mem:: zeroed ( ) } ;
1228- attrlist. bitmapcount = libc:: ATTR_BIT_MAP_COUNT ;
1229-
1230- if times. created . is_some ( ) {
1231- buf[ num_times] . write ( to_timespec ( times. created ) ?) ;
1232- num_times += 1 ;
1233- attrlist. commonattr |= libc:: ATTR_CMN_CRTIME ;
1234- }
1235- if times. modified . is_some ( ) {
1236- buf[ num_times] . write ( to_timespec ( times. modified ) ?) ;
1237- num_times += 1 ;
1238- attrlist. commonattr |= libc:: ATTR_CMN_MODTIME ;
1239- }
1240- if times. accessed . is_some ( ) {
1241- buf[ num_times] . write ( to_timespec ( times. accessed ) ?) ;
1242- num_times += 1 ;
1243- attrlist. commonattr |= libc:: ATTR_CMN_ACCTIME ;
1244- }
1245-
1246- Ok ( ( attrlist, buf, num_times) )
1247- }
1248-
12491198impl File {
12501199 pub fn open ( path : & Path , opts : & OpenOptions ) -> io:: Result < File > {
12511200 run_path_with_cstr ( path, & |path| File :: open_c ( path, opts) )
@@ -1760,18 +1709,19 @@ impl TimesAttrlist {
17601709 if times. created . is_some ( ) {
17611710 this. buf [ this. num_times ] . write ( file_time_to_timespec ( times. created ) ?) ;
17621711 this. num_times += 1 ;
1763- attrlist. commonattr |= libc:: ATTR_CMN_CRTIME ;
1712+ this . attrlist . commonattr |= libc:: ATTR_CMN_CRTIME ;
17641713 }
17651714 if times. modified . is_some ( ) {
17661715 this. buf [ this. num_times ] . write ( file_time_to_timespec ( times. modified ) ?) ;
17671716 this. num_times += 1 ;
1768- attrlist. commonattr |= libc:: ATTR_CMN_MODTIME ;
1717+ this . attrlist . commonattr |= libc:: ATTR_CMN_MODTIME ;
17691718 }
17701719 if times. accessed . is_some ( ) {
17711720 this. buf [ this. num_times ] . write ( file_time_to_timespec ( times. accessed ) ?) ;
17721721 this. num_times += 1 ;
1773- attrlist. commonattr |= libc:: ATTR_CMN_ACCTIME ;
1722+ this . attrlist . commonattr |= libc:: ATTR_CMN_ACCTIME ;
17741723 }
1724+ Ok ( this)
17751725 }
17761726
17771727 fn attrlist ( & self ) -> * mut libc:: c_void {
@@ -2174,7 +2124,8 @@ fn set_times_impl(p: &CStr, times: FileTimes, flags: c_int) -> io::Result<()> {
21742124 }
21752125 target_vendor = "apple" => {
21762126 // Apple platforms use setattrlist which supports setting times on symlinks
2177- let ( attrlist, buf, num_times) = set_attrlist_with_times( & times) ?;
2127+ //let (attrlist, buf, num_times) = set_attrlist_with_times(×)?;
2128+ let ta = TimesAttrlist :: from_times( & times) ?;
21782129 let options = if flags == libc:: AT_SYMLINK_NOFOLLOW {
21792130 libc:: FSOPT_NOFOLLOW
21802131 } else {
@@ -2183,15 +2134,15 @@ fn set_times_impl(p: &CStr, times: FileTimes, flags: c_int) -> io::Result<()> {
21832134
21842135 cvt( unsafe { libc:: setattrlist(
21852136 p. as_ptr( ) ,
2186- ( & raw const attrlist ) . cast :: <libc :: c_void> ( ) . cast_mut ( ) ,
2187- buf . as_ptr ( ) . cast :: <libc :: c_void> ( ) . cast_mut ( ) ,
2188- num_times * size_of :: <libc :: timespec> ( ) ,
2137+ ta . attrlist ( ) ,
2138+ ta . times_buf ( ) ,
2139+ ta . times_buf_size ( ) ,
21892140 options as u32
21902141 ) } ) ?;
21912142 Ok ( ( ) )
21922143 }
21932144 target_os = "android" => {
2194- let times = [ to_timespec ( times. accessed) ?, to_timespec ( times. modified) ?] ;
2145+ let times = [ file_time_to_timespec ( times. accessed) ?, file_time_to_timespec ( times. modified) ?] ;
21952146 // utimensat requires Android API level 19
21962147 cvt( unsafe {
21972148 weak!(
@@ -2225,18 +2176,20 @@ fn set_times_impl(p: &CStr, times: FileTimes, flags: c_int) -> io::Result<()> {
22252176 return Ok ( ( ) ) ;
22262177 }
22272178 }
2228- let times = [ to_timespec ( times. accessed) ?, to_timespec ( times. modified) ?] ;
2179+ let times = [ file_time_to_timespec ( times. accessed) ?, file_time_to_timespec ( times. modified) ?] ;
22292180 cvt( unsafe { libc:: utimensat( libc:: AT_FDCWD , p. as_ptr( ) , times. as_ptr( ) , flags) } ) ?;
22302181 Ok ( ( ) )
22312182 }
22322183 }
22332184}
22342185
2186+ #[ inline( always) ]
22352187pub fn set_times ( p : & CStr , times : FileTimes ) -> io:: Result < ( ) > {
22362188 // flags = 0 means follow symlinks
22372189 set_times_impl ( p, times, 0 )
22382190}
22392191
2192+ #[ inline( always) ]
22402193pub fn set_times_nofollow ( p : & CStr , times : FileTimes ) -> io:: Result < ( ) > {
22412194 set_times_impl ( p, times, libc:: AT_SYMLINK_NOFOLLOW )
22422195}
0 commit comments