Skip to content

Commit 1e6b444

Browse files
committed
library: fs: Factor out a file_time_to_timespec function in preparation for reusing it
1 parent fed46ff commit 1e6b444

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,24 +1604,6 @@ impl File {
16041604
}
16051605

16061606
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-
};
16251607
cfg_select! {
16261608
any(target_os = "redox", target_os = "espidf", target_os = "horizon", target_os = "nuttx") => {
16271609
// Redox doesn't appear to support `UTIME_OMIT`.
@@ -1639,17 +1621,17 @@ impl File {
16391621
let mut attrlist: libc::attrlist = unsafe { mem::zeroed() };
16401622
attrlist.bitmapcount = libc::ATTR_BIT_MAP_COUNT;
16411623
if times.created.is_some() {
1642-
buf[num_times].write(to_timespec(times.created)?);
1624+
buf[num_times].write(file_time_to_timespec(times.created)?);
16431625
num_times += 1;
16441626
attrlist.commonattr |= libc::ATTR_CMN_CRTIME;
16451627
}
16461628
if times.modified.is_some() {
1647-
buf[num_times].write(to_timespec(times.modified)?);
1629+
buf[num_times].write(file_time_to_timespec(times.modified)?);
16481630
num_times += 1;
16491631
attrlist.commonattr |= libc::ATTR_CMN_MODTIME;
16501632
}
16511633
if times.accessed.is_some() {
1652-
buf[num_times].write(to_timespec(times.accessed)?);
1634+
buf[num_times].write(file_time_to_timespec(times.accessed)?);
16531635
num_times += 1;
16541636
attrlist.commonattr |= libc::ATTR_CMN_ACCTIME;
16551637
}
@@ -1663,7 +1645,7 @@ impl File {
16631645
Ok(())
16641646
}
16651647
target_os = "android" => {
1666-
let times = [to_timespec(times.accessed)?, to_timespec(times.modified)?];
1648+
let times = [file_time_to_timespec(times.accessed)?, file_time_to_timespec(times.modified)?];
16671649
// futimens requires Android API level 19
16681650
cvt(unsafe {
16691651
weak!(
@@ -1697,14 +1679,35 @@ impl File {
16971679
return Ok(());
16981680
}
16991681
}
1700-
let times = [to_timespec(times.accessed)?, to_timespec(times.modified)?];
1682+
let times = [file_time_to_timespec(times.accessed)?, file_time_to_timespec(times.modified)?];
17011683
cvt(unsafe { libc::futimens(self.as_raw_fd(), times.as_ptr()) })?;
17021684
Ok(())
17031685
}
17041686
}
17051687
}
17061688
}
17071689

1690+
#[cfg(not(any(
1691+
target_os = "redox",
1692+
target_os = "espidf",
1693+
target_os = "horizon",
1694+
target_os = "nuttx",
1695+
)))]
1696+
fn file_time_to_timespec(time: Option<SystemTime>) -> io::Result<libc::timespec> {
1697+
match time {
1698+
Some(time) if let Some(ts) = time.t.to_timespec() => Ok(ts),
1699+
Some(time) if time > crate::sys::time::UNIX_EPOCH => Err(io::const_error!(
1700+
io::ErrorKind::InvalidInput,
1701+
"timestamp is too large to set as a file time",
1702+
)),
1703+
Some(_) => Err(io::const_error!(
1704+
io::ErrorKind::InvalidInput,
1705+
"timestamp is too small to set as a file time",
1706+
)),
1707+
None => Ok(libc::timespec { tv_sec: 0, tv_nsec: libc::UTIME_OMIT as _ }),
1708+
}
1709+
};
1710+
17081711
impl DirBuilder {
17091712
pub fn new() -> DirBuilder {
17101713
DirBuilder { mode: 0o777 }

0 commit comments

Comments
 (0)