@@ -1195,6 +1195,27 @@ 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+ fn to_timespec ( time : Option < SystemTime > ) -> io:: Result < libc:: timespec > {
1205+ match time {
1206+ Some ( time) if let Some ( ts) = time. t . to_timespec ( ) => Ok ( ts) ,
1207+ Some ( time) if time > crate :: sys:: time:: UNIX_EPOCH => Err ( io:: const_error!(
1208+ io:: ErrorKind :: InvalidInput ,
1209+ "timestamp is too large to set as a file time" ,
1210+ ) ) ,
1211+ Some ( _) => Err ( io:: const_error!(
1212+ io:: ErrorKind :: InvalidInput ,
1213+ "timestamp is too small to set as a file time" ,
1214+ ) ) ,
1215+ None => Ok ( libc:: timespec { tv_sec : 0 , tv_nsec : libc:: UTIME_OMIT as _ } ) ,
1216+ }
1217+ }
1218+
11981219impl File {
11991220 pub fn open ( path : & Path , opts : & OpenOptions ) -> io:: Result < File > {
12001221 run_path_with_cstr ( path, & |path| File :: open_c ( path, opts) )
@@ -1604,24 +1625,6 @@ impl File {
16041625 }
16051626
16061627 pub fn set_times ( & self , times : FileTimes ) -> io:: Result < ( ) > {
1607- #[ cfg( not( any(
1608- target_os = "redox" ,
1609- target_os = "espidf" ,
1610- target_os = "horizon" ,
1611- target_os = "nuttx" ,
1612- ) ) ) ]
1613- let to_timespec = |time : Option < SystemTime > | match time {
1614- Some ( time) if let Some ( ts) = time. t . to_timespec ( ) => Ok ( ts) ,
1615- Some ( time) if time > crate :: sys:: time:: UNIX_EPOCH => Err ( io:: const_error!(
1616- io:: ErrorKind :: InvalidInput ,
1617- "timestamp is too large to set as a file time" ,
1618- ) ) ,
1619- Some ( _) => Err ( io:: const_error!(
1620- io:: ErrorKind :: InvalidInput ,
1621- "timestamp is too small to set as a file time" ,
1622- ) ) ,
1623- None => Ok ( libc:: timespec { tv_sec : 0 , tv_nsec : libc:: UTIME_OMIT as _ } ) ,
1624- } ;
16251628 cfg_select ! {
16261629 any( target_os = "redox" , target_os = "espidf" , target_os = "horizon" , target_os = "nuttx" ) => {
16271630 // Redox doesn't appear to support `UTIME_OMIT`.
@@ -1970,6 +1973,53 @@ pub fn set_perm(p: &CStr, perm: FilePermissions) -> io::Result<()> {
19701973 cvt_r ( || unsafe { libc:: chmod ( p. as_ptr ( ) , perm. mode ) } ) . map ( |_| ( ) )
19711974}
19721975
1976+ fn set_times_impl ( p : & CStr , times : FileTimes , flags : c_int ) -> io:: Result < ( ) > {
1977+ #[ cfg( not( any(
1978+ target_os = "redox" ,
1979+ target_os = "espidf" ,
1980+ target_os = "horizon" ,
1981+ target_os = "nuttx" ,
1982+ ) ) ) ]
1983+ let times = [ to_timespec ( times. accessed ) ?, to_timespec ( times. modified ) ?] ;
1984+ cfg_select ! {
1985+ any( target_os = "redox" , target_os = "espidf" , target_os = "horizon" , target_os = "nuttx" ) => {
1986+ let _ = ( p, times, flags) ;
1987+ Err ( io:: const_error!(
1988+ io:: ErrorKind :: Unsupported ,
1989+ "setting file times not supported" ,
1990+ ) )
1991+ }
1992+ target_vendor = "apple" => {
1993+ // FIXME: need to implement
1994+ let _ = ( p, times, flags) ;
1995+ Err ( io:: const_error!(
1996+ io:: ErrorKind :: Unsupported ,
1997+ "setting file times not supported" ,
1998+ ) )
1999+ }
2000+ target_os = "android" => {
2001+ // FIXME: need to implement
2002+ let _ = ( p, times, flags) ;
2003+ Err ( io:: const_error!(
2004+ io:: ErrorKind :: Unsupported ,
2005+ "setting file times not supported" ,
2006+ ) )
2007+ }
2008+ _ => {
2009+ cvt( unsafe { libc:: utimensat( libc:: AT_FDCWD , p. as_ptr( ) , times. as_ptr( ) , flags) } ) ?;
2010+ Ok ( ( ) )
2011+ }
2012+ }
2013+ }
2014+
2015+ pub fn set_times ( p : & CStr , times : FileTimes ) -> io:: Result < ( ) > {
2016+ set_times_impl ( p, times, libc:: AT_SYMLINK_FOLLOW )
2017+ }
2018+
2019+ pub fn set_times_nofollow ( p : & CStr , times : FileTimes ) -> io:: Result < ( ) > {
2020+ set_times_impl ( p, times, libc:: AT_SYMLINK_NOFOLLOW )
2021+ }
2022+
19732023pub fn rmdir ( p : & CStr ) -> io:: Result < ( ) > {
19742024 cvt ( unsafe { libc:: rmdir ( p. as_ptr ( ) ) } ) . map ( |_| ( ) )
19752025}
0 commit comments