Merge pull request #557 from BrennanMcDonald/master

Added checks for modified/accessed/created metadata
This commit is contained in:
Benjamin Sago 2020-01-19 00:39:40 +00:00 committed by GitHub
commit c5a1e8e103
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,7 +5,7 @@ use std::io::Error as IOError;
use std::io::Result as IOResult;
use std::os::unix::fs::{MetadataExt, PermissionsExt, FileTypeExt};
use std::path::{Path, PathBuf};
use std::time::{UNIX_EPOCH, Duration};
use std::time::{SystemTime, UNIX_EPOCH, Duration};
use log::{debug, error};
@ -329,8 +329,13 @@ impl<'dir> File<'dir> {
}
/// This files last modified timestamp.
/// If the file's time is invalid, assume it was modified today
pub fn modified_time(&self) -> Duration {
self.metadata.modified().unwrap().duration_since(UNIX_EPOCH).unwrap()
if self.metadata.modified().unwrap() < UNIX_EPOCH {
return SystemTime::now().duration_since(UNIX_EPOCH).unwrap()
} else {
return self.metadata.modified().unwrap().duration_since(UNIX_EPOCH).unwrap()
}
}
/// This files last changed timestamp.
@ -339,13 +344,23 @@ impl<'dir> File<'dir> {
}
/// This files last accessed timestamp.
/// If the file's time is invalid, assume it was accessed today
pub fn accessed_time(&self) -> Duration {
self.metadata.accessed().unwrap().duration_since(UNIX_EPOCH).unwrap()
if self.metadata.accessed().unwrap() < UNIX_EPOCH{
return SystemTime::now().duration_since(UNIX_EPOCH).unwrap()
} else {
return self.metadata.accessed().unwrap().duration_since(UNIX_EPOCH).unwrap()
}
}
/// This files created timestamp.
/// If the file's time is invalid, assume it was created today
pub fn created_time(&self) -> Duration {
self.metadata.created().unwrap().duration_since(UNIX_EPOCH).unwrap()
if self.metadata.created().unwrap() < UNIX_EPOCH {
return SystemTime::now().duration_since(UNIX_EPOCH).unwrap()
} else {
return self.metadata.created().unwrap().duration_since(UNIX_EPOCH).unwrap()
}
}
/// This files type.