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:
@@ -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() }
|
||||
}
|
||||
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
fn to_u64(&self, ft: &c::FILETIME) -> u64 {
|
||||
(ft.dwLowDateTime as u64) | ((ft.dwHighDateTime as u64) << 32)
|
||||
pub fn accessed(&self) -> io::Result<SystemTime> {
|
||||
Ok(SystemTime::from(self.data.ftLastAccessTime))
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user