@@ -184,6 +184,11 @@ pub struct DirEntry(fs_imp::DirEntry);
184
184
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
185
185
pub struct OpenOptions ( fs_imp:: OpenOptions ) ;
186
186
187
+ /// Representation of the various timestamps on a file.
188
+ #[ derive( Copy , Clone , Debug , Default ) ]
189
+ #[ unstable( feature = "file_set_times" , issue = "98245" ) ]
190
+ pub struct FileTimes ( fs_imp:: FileTimes ) ;
191
+
187
192
/// Representation of the various permissions on a file.
188
193
///
189
194
/// This module only currently provides one bit of information,
@@ -590,6 +595,58 @@ impl File {
590
595
pub fn set_permissions ( & self , perm : Permissions ) -> io:: Result < ( ) > {
591
596
self . inner . set_permissions ( perm. 0 )
592
597
}
598
+
599
+ /// Changes the timestamps of the underlying file.
600
+ ///
601
+ /// # Platform-specific behavior
602
+ ///
603
+ /// This function currently corresponds to the `futimens` function on Unix (falling back to
604
+ /// `futimes` on macOS before 10.13) and the `SetFileTime` function on Windows. Note that this
605
+ /// [may change in the future][changes].
606
+ ///
607
+ /// [changes]: io#platform-specific-behavior
608
+ ///
609
+ /// # Errors
610
+ ///
611
+ /// This function will return an error if the user lacks permission to change timestamps on the
612
+ /// underlying file. It may also return an error in other os-specific unspecified cases.
613
+ ///
614
+ /// This function may return an error if the operating system lacks support to change one or
615
+ /// more of the timestamps set in the `FileTimes` structure.
616
+ ///
617
+ /// # Examples
618
+ ///
619
+ /// ```no_run
620
+ /// #![feature(file_set_times)]
621
+ ///
622
+ /// fn main() -> std::io::Result<()> {
623
+ /// use std::fs::{self, File, FileTimes};
624
+ ///
625
+ /// let src = fs::metadata("src")?;
626
+ /// let dest = File::options().write(true).open("dest")?;
627
+ /// let times = FileTimes::new()
628
+ /// .set_accessed(src.accessed()?)
629
+ /// .set_modified(src.modified()?);
630
+ /// dest.set_times(times)?;
631
+ /// Ok(())
632
+ /// }
633
+ /// ```
634
+ #[ unstable( feature = "file_set_times" , issue = "98245" ) ]
635
+ #[ doc( alias = "futimens" ) ]
636
+ #[ doc( alias = "futimes" ) ]
637
+ #[ doc( alias = "SetFileTime" ) ]
638
+ pub fn set_times ( & self , times : FileTimes ) -> io:: Result < ( ) > {
639
+ self . inner . set_times ( times. 0 )
640
+ }
641
+
642
+ /// Changes the modification time of the underlying file.
643
+ ///
644
+ /// This is an alias for `set_times(FileTimes::new().set_modified(time))`.
645
+ #[ unstable( feature = "file_set_times" , issue = "98245" ) ]
646
+ #[ inline]
647
+ pub fn set_modified ( & self , time : SystemTime ) -> io:: Result < ( ) > {
648
+ self . set_times ( FileTimes :: new ( ) . set_modified ( time) )
649
+ }
593
650
}
594
651
595
652
// In addition to the `impl`s here, `File` also has `impl`s for
@@ -1246,6 +1303,30 @@ impl FromInner<fs_imp::FileAttr> for Metadata {
1246
1303
}
1247
1304
}
1248
1305
1306
+ impl FileTimes {
1307
+ /// Create a new `FileTimes` with no times set.
1308
+ ///
1309
+ /// Using the resulting `FileTimes` in [`File::set_times`] will not modify any timestamps.
1310
+ #[ unstable( feature = "file_set_times" , issue = "98245" ) ]
1311
+ pub fn new ( ) -> Self {
1312
+ Self :: default ( )
1313
+ }
1314
+
1315
+ /// Set the last access time of a file.
1316
+ #[ unstable( feature = "file_set_times" , issue = "98245" ) ]
1317
+ pub fn set_accessed ( mut self , t : SystemTime ) -> Self {
1318
+ self . 0 . set_accessed ( t. into_inner ( ) ) ;
1319
+ self
1320
+ }
1321
+
1322
+ /// Set the last modified time of a file.
1323
+ #[ unstable( feature = "file_set_times" , issue = "98245" ) ]
1324
+ pub fn set_modified ( mut self , t : SystemTime ) -> Self {
1325
+ self . 0 . set_modified ( t. into_inner ( ) ) ;
1326
+ self
1327
+ }
1328
+ }
1329
+
1249
1330
impl Permissions {
1250
1331
/// Returns `true` if these permissions describe a readonly (unwritable) file.
1251
1332
///
0 commit comments