Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 13 additions & 47 deletions src/fs/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Files, and methods and fields to access their metadata.

use std::fs::{self, metadata};
use std::fs;
use std::io::Error as IOError;
use std::io::Result as IOResult;
use std::os::unix::fs::{MetadataExt, PermissionsExt, FileTypeExt};
Expand All @@ -9,8 +9,6 @@ use std::time::{UNIX_EPOCH, Duration};

use fs::dir::Dir;
use fs::fields as f;
use options::Misfire;


/// A **File** is a wrapper around one of Rust's Path objects, along with
/// associated data about the file.
Expand Down Expand Up @@ -327,23 +325,27 @@ impl<'dir> File<'dir> {
}

/// This file’s last modified timestamp.
pub fn modified_time(&self) -> Duration {
self.metadata.modified().unwrap().duration_since(UNIX_EPOCH).unwrap()
pub fn modified_time(&self) -> Option<Duration> {
self.metadata.modified().ok().map(|d| d.duration_since(UNIX_EPOCH).unwrap())
}

/// This file’s last changed timestamp.
pub fn changed_time(&self) -> Duration {
Duration::new(self.metadata.ctime() as u64, self.metadata.ctime_nsec() as u32)
pub fn changed_time(&self) -> Option<Duration> {
if cfg!(target_family = "unix") {
Some(Duration::new(self.metadata.ctime() as u64, self.metadata.ctime_nsec() as u32))
} else {
None
}
}

/// This file’s last accessed timestamp.
pub fn accessed_time(&self) -> Duration {
self.metadata.accessed().unwrap().duration_since(UNIX_EPOCH).unwrap()
pub fn accessed_time(&self) -> Option<Duration> {
self.metadata.accessed().ok().map(|d| d.duration_since(UNIX_EPOCH).unwrap())
}

/// This file’s created timestamp.
pub fn created_time(&self) -> Duration {
self.metadata.created().unwrap().duration_since(UNIX_EPOCH).unwrap()
pub fn created_time(&self) -> Option<Duration> {
self.metadata.created().ok().map(|d| d.duration_since(UNIX_EPOCH).unwrap())
}

/// This file’s ‘type’.
Expand Down Expand Up @@ -459,42 +461,6 @@ impl<'dir> FileTarget<'dir> {
}
}


pub enum PlatformMetadata {
ModifiedTime,
ChangedTime,
AccessedTime,
CreatedTime,
}

impl PlatformMetadata {
pub fn check_supported(&self) -> Result<(), Misfire> {
use std::env::temp_dir;
let result = match self {
// Call the functions that return a Result to see if it works
PlatformMetadata::AccessedTime => metadata(temp_dir()).unwrap().accessed(),
PlatformMetadata::ModifiedTime => metadata(temp_dir()).unwrap().modified(),
PlatformMetadata::CreatedTime => metadata(temp_dir()).unwrap().created(),
// We use the Unix API so we know it’s not available elsewhere
PlatformMetadata::ChangedTime => {
if cfg!(target_family = "unix") {
return Ok(())
} else {
return Err(Misfire::Unsupported(
// for consistency, this error message similar to the one Rust
// use when created time is not available
"status modified time is not available on this platform currently".to_string()));
}
},
};
match result {
Ok(_) => Ok(()),
Err(err) => Err(Misfire::Unsupported(err.to_string()))
}
}
}


/// More readable aliases for the permission bits exposed by libc.
#[allow(trivial_numeric_casts)]
mod modes {
Expand Down
2 changes: 1 addition & 1 deletion src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod dir;
pub use self::dir::{Dir, DotFilter};

mod file;
pub use self::file::{File, FileTarget, PlatformMetadata};
pub use self::file::{File, FileTarget};

pub mod feature;
pub mod fields;
Expand Down
20 changes: 2 additions & 18 deletions src/options/filter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parsing the options for `FileFilter`.

use fs::{DotFilter, PlatformMetadata};
use fs::DotFilter;
use fs::filter::{FileFilter, SortField, SortCase, IgnorePatterns, GitIgnore};

use options::{flags, Misfire};
Expand Down Expand Up @@ -67,23 +67,7 @@ impl SortField {
_ => return Err(Misfire::BadArgument(&flags::SORT, word.into()))
};

match SortField::to_platform_metadata(field) {
Some(m) => match m.check_supported() {
Ok(_) => Ok(field),
Err(misfire) => Err(misfire),
},
None => Ok(field),
}
}

fn to_platform_metadata(field: Self) -> Option<PlatformMetadata> {
match field {
SortField::ModifiedDate => Some(PlatformMetadata::ModifiedTime),
SortField::ChangedDate => Some(PlatformMetadata::ChangedTime),
SortField::AccessedDate => Some(PlatformMetadata::AccessedTime),
SortField::CreatedDate => Some(PlatformMetadata::CreatedTime),
_ => None
}
Ok(field)
}
}

Expand Down
18 changes: 0 additions & 18 deletions src/options/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use output::time::TimeFormat;
use options::{flags, Misfire, Vars};
use options::parser::MatchedFlags;

use fs::PlatformMetadata;
use fs::feature::xattr;


Expand Down Expand Up @@ -344,17 +343,6 @@ impl TimeTypes {
TimeTypes::default()
};

let mut fields = vec![];
if time_types.modified { fields.push(PlatformMetadata::ModifiedTime); }
if time_types.changed { fields.push(PlatformMetadata::ChangedTime); }
if time_types.accessed { fields.push(PlatformMetadata::AccessedTime); }
if time_types.created { fields.push(PlatformMetadata::CreatedTime); }

for field in fields {
if let Err(misfire) = field.check_supported() {
return Err(misfire);
}
}
Ok(time_types)
}
}
Expand Down Expand Up @@ -542,15 +530,9 @@ mod test {
test!(time_a: TimeTypes <- ["-t", "acc"]; Both => Ok(TimeTypes { modified: false, changed: false, accessed: true, created: false }));

// Created
#[cfg(not(target_os = "linux"))]
test!(cr: TimeTypes <- ["--created"]; Both => Ok(TimeTypes { modified: false, changed: false, accessed: false, created: true }));
#[cfg(target_os = "linux")]
test!(cr: TimeTypes <- ["--created"]; Both => err Misfire::Unsupported("creation time is not available on this platform currently".to_string()));
#[cfg(not(target_os = "linux"))]
test!(c: TimeTypes <- ["-U"]; Both => Ok(TimeTypes { modified: false, changed: false, accessed: false, created: true }));
#[cfg(not(target_os = "linux"))]
test!(time_cr: TimeTypes <- ["--time=created"]; Both => Ok(TimeTypes { modified: false, changed: false, accessed: false, created: true }));
#[cfg(not(target_os = "linux"))]
test!(t_cr: TimeTypes <- ["-tcr"]; Both => Ok(TimeTypes { modified: false, changed: false, accessed: false, created: true }));

// Multiples
Expand Down
18 changes: 11 additions & 7 deletions src/output/render/times.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ pub trait Render {
format: &TimeFormat) -> TextCell;
}

impl Render for std::time::Duration {
impl Render for Option<std::time::Duration> {
fn render(self, style: Style,
tz: &Option<TimeZone>,
format: &TimeFormat) -> TextCell {

if let Some(ref tz) = *tz {
let datestamp = format.format_zoned(self, tz);
TextCell::paint(style, datestamp)
if let Some(duration) = self {
if let Some(ref tz) = *tz {
let datestamp = format.format_zoned(duration, tz);
TextCell::paint(style, datestamp)
}
else {
let datestamp = format.format_local(duration);
TextCell::paint(style, datestamp)
}
}
else {
let datestamp = format.format_local(self);
TextCell::paint(style, datestamp)
TextCell::paint(style, String::from("-"))
}
}
}