Skip to content

Commit 466e5c1

Browse files
Rollup merge of rust-lang#109773 - beetrees:set-file-time-improvements, r=Amanieu
Add creation time support to `FileTimes` on apple and windows Adds support for setting file creation times on platforms which support changing it directly (currently only Apple and Windows). Based on top of rust-lang#110093 (which was split from this PR). ACP: rust-lang/libs-team#199 (currently still in progress) Tracking issue: rust-lang#98245 `@rustbot` label +T-libs-api -T-libs
2 parents 5c579a4 + f31c34c commit 466e5c1

File tree

8 files changed

+182
-29
lines changed

8 files changed

+182
-29
lines changed

library/std/src/fs.rs

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::ffi::OsString;
1515
use crate::fmt;
1616
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
1717
use crate::path::{Path, PathBuf};
18+
use crate::sealed::Sealed;
1819
use crate::sys::fs as fs_imp;
1920
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
2021
use crate::time::SystemTime;
@@ -1383,6 +1384,16 @@ impl FileTimes {
13831384
}
13841385
}
13851386

1387+
impl AsInnerMut<fs_imp::FileTimes> for FileTimes {
1388+
fn as_inner_mut(&mut self) -> &mut fs_imp::FileTimes {
1389+
&mut self.0
1390+
}
1391+
}
1392+
1393+
// For implementing OS extension traits in `std::os`
1394+
#[unstable(feature = "file_set_times", issue = "98245")]
1395+
impl Sealed for FileTimes {}
1396+
13861397
impl Permissions {
13871398
/// Returns `true` if these permissions describe a readonly (unwritable) file.
13881399
///

library/std/src/fs/tests.rs

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use crate::io::prelude::*;
22

33
use crate::env;
4-
use crate::fs::{self, File, OpenOptions};
4+
use crate::fs::{self, File, FileTimes, OpenOptions};
55
use crate::io::{BorrowedBuf, ErrorKind, SeekFrom};
66
use crate::mem::MaybeUninit;
77
use crate::path::Path;
88
use crate::str;
99
use crate::sync::Arc;
1010
use crate::sys_common::io::test::{tmpdir, TempDir};
1111
use crate::thread;
12-
use crate::time::{Duration, Instant};
12+
use crate::time::{Duration, Instant, SystemTime};
1313

1414
use rand::RngCore;
1515

@@ -1629,3 +1629,36 @@ fn rename_directory() {
16291629
assert!(new_path.join("newdir").is_dir());
16301630
assert!(new_path.join("newdir/temp.txt").exists());
16311631
}
1632+
1633+
#[test]
1634+
fn test_file_times() {
1635+
#[cfg(target_os = "ios")]
1636+
use crate::os::ios::fs::FileTimesExt;
1637+
#[cfg(target_os = "macos")]
1638+
use crate::os::macos::fs::FileTimesExt;
1639+
#[cfg(target_os = "watchos")]
1640+
use crate::os::watchos::fs::FileTimesExt;
1641+
#[cfg(windows)]
1642+
use crate::os::windows::fs::FileTimesExt;
1643+
1644+
let tmp = tmpdir();
1645+
let file = File::create(tmp.join("foo")).unwrap();
1646+
let mut times = FileTimes::new();
1647+
let accessed = SystemTime::UNIX_EPOCH + Duration::from_secs(12345);
1648+
let modified = SystemTime::UNIX_EPOCH + Duration::from_secs(54321);
1649+
times = times.set_accessed(accessed).set_modified(modified);
1650+
#[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))]
1651+
let created = SystemTime::UNIX_EPOCH + Duration::from_secs(32123);
1652+
#[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))]
1653+
{
1654+
times = times.set_created(created);
1655+
}
1656+
file.set_times(times).unwrap();
1657+
let metadata = file.metadata().unwrap();
1658+
assert_eq!(metadata.accessed().unwrap(), accessed);
1659+
assert_eq!(metadata.modified().unwrap(), modified);
1660+
#[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))]
1661+
{
1662+
assert_eq!(metadata.created().unwrap(), created);
1663+
}
1664+
}

library/std/src/os/ios/fs.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#![stable(feature = "metadata_ext", since = "1.1.0")]
22

3-
use crate::fs::Metadata;
4-
use crate::sys_common::AsInner;
3+
use crate::fs::{self, Metadata};
4+
use crate::sealed::Sealed;
5+
use crate::sys_common::{AsInner, AsInnerMut, IntoInner};
6+
use crate::time::SystemTime;
57

68
#[allow(deprecated)]
79
use crate::os::ios::raw;
@@ -140,3 +142,19 @@ impl MetadataExt for Metadata {
140142
self.as_inner().as_inner().st_lspare as u32
141143
}
142144
}
145+
146+
/// OS-specific extensions to [`fs::FileTimes`].
147+
#[unstable(feature = "file_set_times", issue = "98245")]
148+
pub trait FileTimesExt: Sealed {
149+
/// Set the creation time of a file.
150+
#[unstable(feature = "file_set_times", issue = "98245")]
151+
fn set_created(self, t: SystemTime) -> Self;
152+
}
153+
154+
#[unstable(feature = "file_set_times", issue = "98245")]
155+
impl FileTimesExt for fs::FileTimes {
156+
fn set_created(mut self, t: SystemTime) -> Self {
157+
self.as_inner_mut().set_created(t.into_inner());
158+
self
159+
}
160+
}

library/std/src/os/macos/fs.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#![stable(feature = "metadata_ext", since = "1.1.0")]
22

3-
use crate::fs::Metadata;
4-
use crate::sys_common::AsInner;
3+
use crate::fs::{self, Metadata};
4+
use crate::sealed::Sealed;
5+
use crate::sys_common::{AsInner, AsInnerMut, IntoInner};
6+
use crate::time::SystemTime;
57

68
#[allow(deprecated)]
79
use crate::os::macos::raw;
@@ -146,3 +148,19 @@ impl MetadataExt for Metadata {
146148
[qspare[0] as u64, qspare[1] as u64]
147149
}
148150
}
151+
152+
/// OS-specific extensions to [`fs::FileTimes`].
153+
#[unstable(feature = "file_set_times", issue = "98245")]
154+
pub trait FileTimesExt: Sealed {
155+
/// Set the creation time of a file.
156+
#[unstable(feature = "file_set_times", issue = "98245")]
157+
fn set_created(self, t: SystemTime) -> Self;
158+
}
159+
160+
#[unstable(feature = "file_set_times", issue = "98245")]
161+
impl FileTimesExt for fs::FileTimes {
162+
fn set_created(mut self, t: SystemTime) -> Self {
163+
self.as_inner_mut().set_created(t.into_inner());
164+
self
165+
}
166+
}

library/std/src/os/watchos/fs.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#![stable(feature = "metadata_ext", since = "1.1.0")]
22

3-
use crate::fs::Metadata;
4-
use crate::sys_common::AsInner;
3+
use crate::fs::{self, Metadata};
4+
use crate::sealed::Sealed;
5+
use crate::sys_common::{AsInner, AsInnerMut, IntoInner};
6+
use crate::time::SystemTime;
57

68
#[allow(deprecated)]
79
use crate::os::watchos::raw;
@@ -140,3 +142,19 @@ impl MetadataExt for Metadata {
140142
self.as_inner().as_inner().st_lspare as u32
141143
}
142144
}
145+
146+
/// OS-specific extensions to [`fs::FileTimes`].
147+
#[unstable(feature = "file_set_times", issue = "98245")]
148+
pub trait FileTimesExt: Sealed {
149+
/// Set the creation time of a file.
150+
#[unstable(feature = "file_set_times", issue = "98245")]
151+
fn set_created(self, t: SystemTime) -> Self;
152+
}
153+
154+
#[unstable(feature = "file_set_times", issue = "98245")]
155+
impl FileTimesExt for fs::FileTimes {
156+
fn set_created(mut self, t: SystemTime) -> Self {
157+
self.as_inner_mut().set_created(t.into_inner());
158+
self
159+
}
160+
}

library/std/src/os/windows/fs.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use crate::io;
99
use crate::path::Path;
1010
use crate::sealed::Sealed;
1111
use crate::sys;
12-
use crate::sys_common::{AsInner, AsInnerMut};
12+
use crate::sys_common::{AsInner, AsInnerMut, IntoInner};
13+
use crate::time::SystemTime;
1314

1415
/// Windows-specific extensions to [`fs::File`].
1516
#[stable(feature = "file_offset", since = "1.15.0")]
@@ -526,6 +527,22 @@ impl FileTypeExt for fs::FileType {
526527
}
527528
}
528529

530+
/// Windows-specific extensions to [`fs::FileTimes`].
531+
#[unstable(feature = "file_set_times", issue = "98245")]
532+
pub trait FileTimesExt: Sealed {
533+
/// Set the creation time of a file.
534+
#[unstable(feature = "file_set_times", issue = "98245")]
535+
fn set_created(self, t: SystemTime) -> Self;
536+
}
537+
538+
#[unstable(feature = "file_set_times", issue = "98245")]
539+
impl FileTimesExt for fs::FileTimes {
540+
fn set_created(mut self, t: SystemTime) -> Self {
541+
self.as_inner_mut().set_created(t.into_inner());
542+
self
543+
}
544+
}
545+
529546
/// Creates a new symlink to a non-directory file on the filesystem.
530547
///
531548
/// The `link` path will be a file symbolic link pointing to the `original`

library/std/src/sys/unix/fs.rs

+39-17
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ pub struct FilePermissions {
348348
pub struct FileTimes {
349349
accessed: Option<SystemTime>,
350350
modified: Option<SystemTime>,
351+
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))]
352+
created: Option<SystemTime>,
351353
}
352354

353355
#[derive(Copy, Clone, Eq, Debug)]
@@ -579,6 +581,11 @@ impl FileTimes {
579581
pub fn set_modified(&mut self, t: SystemTime) {
580582
self.modified = Some(t);
581583
}
584+
585+
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))]
586+
pub fn set_created(&mut self, t: SystemTime) {
587+
self.created = Some(t);
588+
}
582589
}
583590

584591
impl FileType {
@@ -1192,8 +1199,6 @@ impl File {
11921199
None => Ok(libc::timespec { tv_sec: 0, tv_nsec: libc::UTIME_OMIT as _ }),
11931200
}
11941201
};
1195-
#[cfg(not(any(target_os = "redox", target_os = "espidf", target_os = "horizon")))]
1196-
let times = [to_timespec(times.accessed)?, to_timespec(times.modified)?];
11971202
cfg_if::cfg_if! {
11981203
if #[cfg(any(target_os = "redox", target_os = "espidf", target_os = "horizon"))] {
11991204
// Redox doesn't appear to support `UTIME_OMIT`.
@@ -1204,25 +1209,41 @@ impl File {
12041209
io::ErrorKind::Unsupported,
12051210
"setting file times not supported",
12061211
))
1207-
} else if #[cfg(any(target_os = "android", target_os = "macos"))] {
1208-
// futimens requires macOS 10.13, and Android API level 19
1212+
} else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "watchos"))] {
1213+
let mut buf = [mem::MaybeUninit::<libc::timespec>::uninit(); 3];
1214+
let mut num_times = 0;
1215+
let mut attrlist: libc::attrlist = unsafe { mem::zeroed() };
1216+
attrlist.bitmapcount = libc::ATTR_BIT_MAP_COUNT;
1217+
if times.created.is_some() {
1218+
buf[num_times].write(to_timespec(times.created)?);
1219+
num_times += 1;
1220+
attrlist.commonattr |= libc::ATTR_CMN_CRTIME;
1221+
}
1222+
if times.modified.is_some() {
1223+
buf[num_times].write(to_timespec(times.modified)?);
1224+
num_times += 1;
1225+
attrlist.commonattr |= libc::ATTR_CMN_MODTIME;
1226+
}
1227+
if times.accessed.is_some() {
1228+
buf[num_times].write(to_timespec(times.accessed)?);
1229+
num_times += 1;
1230+
attrlist.commonattr |= libc::ATTR_CMN_ACCTIME;
1231+
}
1232+
cvt(unsafe { libc::fsetattrlist(
1233+
self.as_raw_fd(),
1234+
(&attrlist as *const libc::attrlist).cast::<libc::c_void>().cast_mut(),
1235+
buf.as_ptr().cast::<libc::c_void>().cast_mut(),
1236+
num_times * mem::size_of::<libc::timespec>(),
1237+
0
1238+
) })?;
1239+
Ok(())
1240+
} else if #[cfg(target_os = "android")] {
1241+
let times = [to_timespec(times.accessed)?, to_timespec(times.modified)?];
1242+
// futimens requires Android API level 19
12091243
cvt(unsafe {
12101244
weak!(fn futimens(c_int, *const libc::timespec) -> c_int);
12111245
match futimens.get() {
12121246
Some(futimens) => futimens(self.as_raw_fd(), times.as_ptr()),
1213-
#[cfg(target_os = "macos")]
1214-
None => {
1215-
fn ts_to_tv(ts: &libc::timespec) -> libc::timeval {
1216-
libc::timeval {
1217-
tv_sec: ts.tv_sec,
1218-
tv_usec: (ts.tv_nsec / 1000) as _
1219-
}
1220-
}
1221-
let timevals = [ts_to_tv(&times[0]), ts_to_tv(&times[1])];
1222-
libc::futimes(self.as_raw_fd(), timevals.as_ptr())
1223-
}
1224-
// futimes requires even newer Android.
1225-
#[cfg(target_os = "android")]
12261247
None => return Err(io::const_io_error!(
12271248
io::ErrorKind::Unsupported,
12281249
"setting file times requires Android API level >= 19",
@@ -1231,6 +1252,7 @@ impl File {
12311252
})?;
12321253
Ok(())
12331254
} else {
1255+
let times = [to_timespec(times.accessed)?, to_timespec(times.modified)?];
12341256
cvt(unsafe { libc::futimens(self.as_raw_fd(), times.as_ptr()) })?;
12351257
Ok(())
12361258
}

library/std/src/sys/windows/fs.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub struct FilePermissions {
8888
pub struct FileTimes {
8989
accessed: Option<c::FILETIME>,
9090
modified: Option<c::FILETIME>,
91+
created: Option<c::FILETIME>,
9192
}
9293

9394
#[derive(Debug)]
@@ -567,22 +568,33 @@ impl File {
567568

568569
pub fn set_times(&self, times: FileTimes) -> io::Result<()> {
569570
let is_zero = |t: c::FILETIME| t.dwLowDateTime == 0 && t.dwHighDateTime == 0;
570-
if times.accessed.map_or(false, is_zero) || times.modified.map_or(false, is_zero) {
571+
if times.accessed.map_or(false, is_zero)
572+
|| times.modified.map_or(false, is_zero)
573+
|| times.created.map_or(false, is_zero)
574+
{
571575
return Err(io::const_io_error!(
572576
io::ErrorKind::InvalidInput,
573577
"Cannot set file timestamp to 0",
574578
));
575579
}
576580
let is_max =
577581
|t: c::FILETIME| t.dwLowDateTime == c::DWORD::MAX && t.dwHighDateTime == c::DWORD::MAX;
578-
if times.accessed.map_or(false, is_max) || times.modified.map_or(false, is_max) {
582+
if times.accessed.map_or(false, is_max)
583+
|| times.modified.map_or(false, is_max)
584+
|| times.created.map_or(false, is_max)
585+
{
579586
return Err(io::const_io_error!(
580587
io::ErrorKind::InvalidInput,
581588
"Cannot set file timestamp to 0xFFFF_FFFF_FFFF_FFFF",
582589
));
583590
}
584591
cvt(unsafe {
585-
c::SetFileTime(self.as_handle(), None, times.accessed.as_ref(), times.modified.as_ref())
592+
c::SetFileTime(
593+
self.as_handle(),
594+
times.created.as_ref(),
595+
times.accessed.as_ref(),
596+
times.modified.as_ref(),
597+
)
586598
})?;
587599
Ok(())
588600
}
@@ -985,6 +997,10 @@ impl FileTimes {
985997
pub fn set_modified(&mut self, t: SystemTime) {
986998
self.modified = Some(t.into_inner());
987999
}
1000+
1001+
pub fn set_created(&mut self, t: SystemTime) {
1002+
self.created = Some(t.into_inner());
1003+
}
9881004
}
9891005

9901006
impl FileType {

0 commit comments

Comments
 (0)