diff options
| author | bors <bors@rust-lang.org> | 2016-02-05 01:00:31 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2016-02-05 01:00:31 +0000 |
| commit | 7bcced73b77ba56834c3b5da0c4f82f80aa74db8 (patch) | |
| tree | 8c206d0720ab3138fd604ec031b04c6046f6082a /src/libstd/sys | |
| parent | 9d8e3a024aec4985927687b19cbb1077a09022c2 (diff) | |
| parent | d1681bbde563dff88ad8f32592e7d373f62a038d (diff) | |
| download | rust-7bcced73b77ba56834c3b5da0c4f82f80aa74db8.tar.gz rust-7bcced73b77ba56834c3b5da0c4f82f80aa74db8.zip | |
Auto merge of #30865 - alexcrichton:mtime-system-time, r=aturon
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`.
Diffstat (limited to 'src/libstd/sys')
| -rw-r--r-- | src/libstd/sys/unix/fs.rs | 62 | ||||
| -rw-r--r-- | src/libstd/sys/unix/time.rs | 12 | ||||
| -rw-r--r-- | src/libstd/sys/windows/ext/fs.rs | 6 | ||||
| -rw-r--r-- | src/libstd/sys/windows/fs.rs | 31 | ||||
| -rw-r--r-- | src/libstd/sys/windows/time.rs | 6 |
5 files changed, 109 insertions, 8 deletions
diff --git a/src/libstd/sys/unix/fs.rs b/src/libstd/sys/unix/fs.rs index 2527c6774ff..e672d9f1586 100644 --- a/src/libstd/sys/unix/fs.rs +++ b/src/libstd/sys/unix/fs.rs @@ -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 } } diff --git a/src/libstd/sys/unix/time.rs b/src/libstd/sys/unix/time.rs index a07c30d9648..dd248416f84 100644 --- a/src/libstd/sys/unix/time.rs +++ b/src/libstd/sys/unix/time.rs @@ -146,6 +146,12 @@ mod inner { } } + impl From<libc::timeval> for SystemTime { + fn from(t: libc::timeval) -> SystemTime { + SystemTime { t: t } + } + } + impl PartialEq for SystemTime { fn eq(&self, other: &SystemTime) -> bool { self.t.tv_sec == other.t.tv_sec && self.t.tv_usec == other.t.tv_usec @@ -282,6 +288,12 @@ mod inner { } } + impl From<libc::timespec> for SystemTime { + fn from(t: libc::timespec) -> SystemTime { + SystemTime { t: Timespec { t: t } } + } + } + impl fmt::Debug for SystemTime { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("SystemTime") diff --git a/src/libstd/sys/windows/ext/fs.rs b/src/libstd/sys/windows/ext/fs.rs index d060c902fba..d378a6853f3 100644 --- a/src/libstd/sys/windows/ext/fs.rs +++ b/src/libstd/sys/windows/ext/fs.rs @@ -196,9 +196,9 @@ pub trait MetadataExt { #[stable(feature = "metadata_ext", since = "1.1.0")] impl MetadataExt for Metadata { fn file_attributes(&self) -> u32 { self.as_inner().attrs() } - fn creation_time(&self) -> u64 { self.as_inner().created() } - fn last_access_time(&self) -> u64 { self.as_inner().accessed() } - fn last_write_time(&self) -> u64 { self.as_inner().modified() } + fn creation_time(&self) -> u64 { self.as_inner().created_u64() } + fn last_access_time(&self) -> u64 { self.as_inner().accessed_u64() } + fn last_write_time(&self) -> u64 { self.as_inner().modified_u64() } fn file_size(&self) -> u64 { self.as_inner().size() } } diff --git a/src/libstd/sys/windows/fs.rs b/src/libstd/sys/windows/fs.rs index 8d921146653..16d337fcc70 100644 --- a/src/libstd/sys/windows/fs.rs +++ b/src/libstd/sys/windows/fs.rs @@ -20,6 +20,7 @@ use ptr; use slice; use sync::Arc; use sys::handle::Handle; +use sys::time::SystemTime; use sys::{c, cvt}; use sys_common::FromInner; @@ -421,12 +422,28 @@ impl FileAttr { FileType::new(self.data.dwFileAttributes, self.reparse_tag) } - pub fn created(&self) -> u64 { self.to_u64(&self.data.ftCreationTime) } - pub fn accessed(&self) -> u64 { self.to_u64(&self.data.ftLastAccessTime) } - pub fn modified(&self) -> u64 { self.to_u64(&self.data.ftLastWriteTime) } + pub fn modified(&self) -> io::Result<SystemTime> { + Ok(SystemTime::from(self.data.ftLastWriteTime)) + } + + pub fn accessed(&self) -> io::Result<SystemTime> { + Ok(SystemTime::from(self.data.ftLastAccessTime)) + } - fn to_u64(&self, ft: &c::FILETIME) -> u64 { - (ft.dwLowDateTime as u64) | ((ft.dwHighDateTime as u64) << 32) + pub fn created(&self) -> io::Result<SystemTime> { + Ok(SystemTime::from(self.data.ftCreationTime)) + } + + pub fn modified_u64(&self) -> u64 { + to_u64(&self.data.ftLastWriteTime) + } + + pub fn accessed_u64(&self) -> u64 { + to_u64(&self.data.ftLastAccessTime) + } + + pub fn created_u64(&self) -> u64 { + to_u64(&self.data.ftCreationTime) } fn is_reparse_point(&self) -> bool { @@ -434,6 +451,10 @@ impl FileAttr { } } +fn to_u64(ft: &c::FILETIME) -> u64 { + (ft.dwLowDateTime as u64) | ((ft.dwHighDateTime as u64) << 32) +} + impl FilePermissions { pub fn readonly(&self) -> bool { self.attrs & c::FILE_ATTRIBUTE_READONLY != 0 diff --git a/src/libstd/sys/windows/time.rs b/src/libstd/sys/windows/time.rs index 058587b11dc..ef8ed606526 100644 --- a/src/libstd/sys/windows/time.rs +++ b/src/libstd/sys/windows/time.rs @@ -166,6 +166,12 @@ impl fmt::Debug for SystemTime { } } +impl From<c::FILETIME> for SystemTime { + fn from(t: c::FILETIME) -> SystemTime { + SystemTime { t: t } + } +} + fn dur2intervals(d: &Duration) -> i64 { d.as_secs().checked_mul(INTERVALS_PER_SEC).and_then(|i| { i.checked_add(d.subsec_nanos() as u64 / 100) |
