@@ -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,49 @@ 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
+ }
593
641
}
594
642
595
643
// In addition to the `impl`s here, `File` also has `impl`s for
@@ -1246,6 +1294,30 @@ impl FromInner<fs_imp::FileAttr> for Metadata {
1246
1294
}
1247
1295
}
1248
1296
1297
+ impl FileTimes {
1298
+ /// Create a new `FileTimes` with no times set.
1299
+ ///
1300
+ /// Using the resulting `FileTimes` in [`File::set_times`] will not modify any timestamps.
1301
+ #[ unstable( feature = "file_set_times" , issue = "98245" ) ]
1302
+ pub fn new ( ) -> Self {
1303
+ Self :: default ( )
1304
+ }
1305
+
1306
+ /// Set the last access time of a file.
1307
+ #[ unstable( feature = "file_set_times" , issue = "98245" ) ]
1308
+ pub fn set_accessed ( mut self , t : SystemTime ) -> Self {
1309
+ self . 0 . set_accessed ( t. into_inner ( ) ) ;
1310
+ self
1311
+ }
1312
+
1313
+ /// Set the last modified time of a file.
1314
+ #[ unstable( feature = "file_set_times" , issue = "98245" ) ]
1315
+ pub fn set_modified ( mut self , t : SystemTime ) -> Self {
1316
+ self . 0 . set_modified ( t. into_inner ( ) ) ;
1317
+ self
1318
+ }
1319
+ }
1320
+
1249
1321
impl Permissions {
1250
1322
/// Returns `true` if these permissions describe a readonly (unwritable) file.
1251
1323
///
0 commit comments