1
Fork 0

std: Expose SystemTime accessors on fs::Metadata

These accessors are used to get at the last modification, last access, and
creation time of the underlying file. Currently not all platforms provide the
creation time, so that currently returns `Option`.
This commit is contained in:
Alex Crichton 2016-01-12 17:24:16 -08:00
parent d0ef740266
commit d1681bbde5
7 changed files with 183 additions and 8 deletions

View file

@ -22,6 +22,7 @@ use ptr;
use sync::Arc;
use sys::fd::FileDesc;
use sys::platform::raw;
use sys::time::SystemTime;
use sys::{cvt, cvt_r};
use sys_common::{AsInner, FromInner};
@ -86,6 +87,67 @@ impl FileAttr {
}
}
#[cfg(any(target_os = "ios", target_os = "macos"))]
// FIXME: update SystemTime to store a timespec and don't lose precision
impl FileAttr {
pub fn modified(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timeval {
tv_sec: self.stat.st_mtime,
tv_usec: (self.stat.st_mtime_nsec / 1000) as libc::suseconds_t,
}))
}
pub fn accessed(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timeval {
tv_sec: self.stat.st_atime,
tv_usec: (self.stat.st_atime_nsec / 1000) as libc::suseconds_t,
}))
}
pub fn created(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timeval {
tv_sec: self.stat.st_birthtime,
tv_usec: (self.stat.st_birthtime_nsec / 1000) as libc::suseconds_t,
}))
}
}
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
impl FileAttr {
pub fn modified(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_mtime,
tv_nsec: self.stat.st_mtime_nsec as libc::c_long,
}))
}
pub fn accessed(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_atime,
tv_nsec: self.stat.st_atime_nsec as libc::c_long,
}))
}
#[cfg(any(target_os = "bitrig",
target_os = "freebsd",
target_os = "openbsd"))]
pub fn created(&self) -> io::Result<SystemTime> {
Ok(SystemTime::from(libc::timespec {
tv_sec: self.stat.st_birthtime,
tv_nsec: self.stat.st_birthtime_nsec as libc::c_long,
}))
}
#[cfg(not(any(target_os = "bitrig",
target_os = "freebsd",
target_os = "openbsd")))]
pub fn created(&self) -> io::Result<SystemTime> {
Err(io::Error::new(io::ErrorKind::Other,
"creation time is not available on this platform \
currently"))
}
}
impl AsInner<raw::stat> for FileAttr {
fn as_inner(&self) -> &raw::stat { &self.stat }
}