@@ -1195,6 +1195,27 @@ impl fmt::Debug for OpenOptions {
1195
1195
}
1196
1196
}
1197
1197
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
+
1198
1219
impl File {
1199
1220
pub fn open ( path : & Path , opts : & OpenOptions ) -> io:: Result < File > {
1200
1221
run_path_with_cstr ( path, & |path| File :: open_c ( path, opts) )
@@ -1604,24 +1625,6 @@ impl File {
1604
1625
}
1605
1626
1606
1627
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
- } ;
1625
1628
cfg_select ! {
1626
1629
any( target_os = "redox" , target_os = "espidf" , target_os = "horizon" , target_os = "nuttx" ) => {
1627
1630
// Redox doesn't appear to support `UTIME_OMIT`.
@@ -1970,6 +1973,54 @@ pub fn set_perm(p: &CStr, perm: FilePermissions) -> io::Result<()> {
1970
1973
cvt_r ( || unsafe { libc:: chmod ( p. as_ptr ( ) , perm. mode ) } ) . map ( |_| ( ) )
1971
1974
}
1972
1975
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
+ // flags = 0 means follow symlinks
2017
+ set_times_impl ( p, times, 0 )
2018
+ }
2019
+
2020
+ pub fn set_times_nofollow ( p : & CStr , times : FileTimes ) -> io:: Result < ( ) > {
2021
+ set_times_impl ( p, times, libc:: AT_SYMLINK_NOFOLLOW )
2022
+ }
2023
+
1973
2024
pub fn rmdir ( p : & CStr ) -> io:: Result < ( ) > {
1974
2025
cvt ( unsafe { libc:: rmdir ( p. as_ptr ( ) ) } ) . map ( |_| ( ) )
1975
2026
}
0 commit comments