Skip to content

Commit db8c6e4

Browse files
committed
Merge branch 'ariasuni-fix-handling-maybe-unsupported-time-metadata'
2 parents df0d30e + 490d968 commit db8c6e4

File tree

5 files changed

+19
-84
lines changed

5 files changed

+19
-84
lines changed

src/fs/file.rs

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,9 @@ impl<'dir> File<'dir> {
331331
/// This file’s last modified timestamp.
332332
/// If the file's time is invalid, assume it was modified today
333333
pub fn modified_time(&self) -> Duration {
334-
if self.metadata.modified().unwrap() < UNIX_EPOCH {
335-
return SystemTime::now().duration_since(UNIX_EPOCH).unwrap()
336-
} else {
337-
return self.metadata.modified().unwrap().duration_since(UNIX_EPOCH).unwrap()
334+
match self.metadata.modified() {
335+
Ok(system_time) => system_time.duration_since(UNIX_EPOCH).unwrap(),
336+
Err(_) => Duration::new(0, 0),
338337
}
339338
}
340339

@@ -346,20 +345,18 @@ impl<'dir> File<'dir> {
346345
/// This file’s last accessed timestamp.
347346
/// If the file's time is invalid, assume it was accessed today
348347
pub fn accessed_time(&self) -> Duration {
349-
if self.metadata.accessed().unwrap() < UNIX_EPOCH{
350-
return SystemTime::now().duration_since(UNIX_EPOCH).unwrap()
351-
} else {
352-
return self.metadata.accessed().unwrap().duration_since(UNIX_EPOCH).unwrap()
348+
match self.metadata.accessed() {
349+
Ok(system_time) => system_time.duration_since(UNIX_EPOCH).unwrap(),
350+
Err(_) => Duration::new(0, 0),
353351
}
354352
}
355353

356354
/// This file’s created timestamp.
357355
/// If the file's time is invalid, assume it was created today
358356
pub fn created_time(&self) -> Duration {
359-
if self.metadata.created().unwrap() < UNIX_EPOCH {
360-
return SystemTime::now().duration_since(UNIX_EPOCH).unwrap()
361-
} else {
362-
return self.metadata.created().unwrap().duration_since(UNIX_EPOCH).unwrap()
357+
match self.metadata.created() {
358+
Ok(system_time) => system_time.duration_since(UNIX_EPOCH).unwrap(),
359+
Err(_) => Duration::new(0, 0),
363360
}
364361
}
365362

@@ -477,41 +474,6 @@ impl<'dir> FileTarget<'dir> {
477474
}
478475

479476

480-
pub enum PlatformMetadata {
481-
ModifiedTime,
482-
ChangedTime,
483-
AccessedTime,
484-
CreatedTime,
485-
}
486-
487-
impl PlatformMetadata {
488-
pub fn check_supported(&self) -> Result<(), Misfire> {
489-
use std::env::temp_dir;
490-
let result = match self {
491-
// Call the functions that return a Result to see if it works
492-
PlatformMetadata::AccessedTime => metadata(temp_dir()).unwrap().accessed(),
493-
PlatformMetadata::ModifiedTime => metadata(temp_dir()).unwrap().modified(),
494-
PlatformMetadata::CreatedTime => metadata(temp_dir()).unwrap().created(),
495-
// We use the Unix API so we know it’s not available elsewhere
496-
PlatformMetadata::ChangedTime => {
497-
if cfg!(target_family = "unix") {
498-
return Ok(())
499-
} else {
500-
return Err(Misfire::Unsupported(
501-
// for consistency, this error message similar to the one Rust
502-
// use when created time is not available
503-
"status modified time is not available on this platform currently".to_string()));
504-
}
505-
},
506-
};
507-
match result {
508-
Ok(_) => Ok(()),
509-
Err(err) => Err(Misfire::Unsupported(err.to_string()))
510-
}
511-
}
512-
}
513-
514-
515477
/// More readable aliases for the permission bits exposed by libc.
516478
#[allow(trivial_numeric_casts)]
517479
mod modes {

src/fs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod dir;
22
pub use self::dir::{Dir, DotFilter};
33

44
mod file;
5-
pub use self::file::{File, FileTarget, PlatformMetadata};
5+
pub use self::file::{File, FileTarget};
66

77
pub mod feature;
88
pub mod fields;

src/options/filter.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Parsing the options for `FileFilter`.
22
3-
use crate::fs::{DotFilter, PlatformMetadata};
3+
use crate::fs::DotFilter;
44
use crate::fs::filter::{FileFilter, SortField, SortCase, IgnorePatterns, GitIgnore};
55

66
use crate::options::{flags, Misfire};
@@ -67,23 +67,7 @@ impl SortField {
6767
_ => return Err(Misfire::BadArgument(&flags::SORT, word.into()))
6868
};
6969

70-
match SortField::to_platform_metadata(field) {
71-
Some(m) => match m.check_supported() {
72-
Ok(_) => Ok(field),
73-
Err(misfire) => Err(misfire),
74-
},
75-
None => Ok(field),
76-
}
77-
}
78-
79-
fn to_platform_metadata(field: Self) -> Option<PlatformMetadata> {
80-
match field {
81-
SortField::ModifiedDate => Some(PlatformMetadata::ModifiedTime),
82-
SortField::ChangedDate => Some(PlatformMetadata::ChangedTime),
83-
SortField::AccessedDate => Some(PlatformMetadata::AccessedTime),
84-
SortField::CreatedDate => Some(PlatformMetadata::CreatedTime),
85-
_ => None
86-
}
70+
Ok(field)
8771
}
8872
}
8973

src/options/view.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::output::grid_details::{self, RowThreshold};
77
use crate::output::table::{TimeTypes, Environment, SizeFormat, Columns, Options as TableOptions};
88
use crate::output::time::TimeFormat;
99

10-
use crate::fs::PlatformMetadata;
1110
use crate::fs::feature::xattr;
1211

1312

@@ -356,17 +355,6 @@ impl TimeTypes {
356355
TimeTypes::default()
357356
};
358357

359-
let mut fields = vec![];
360-
if time_types.modified { fields.push(PlatformMetadata::ModifiedTime); }
361-
if time_types.changed { fields.push(PlatformMetadata::ChangedTime); }
362-
if time_types.accessed { fields.push(PlatformMetadata::AccessedTime); }
363-
if time_types.created { fields.push(PlatformMetadata::CreatedTime); }
364-
365-
for field in fields {
366-
if let Err(misfire) = field.check_supported() {
367-
return Err(misfire);
368-
}
369-
}
370358
Ok(time_types)
371359
}
372360
}
@@ -554,15 +542,9 @@ mod test {
554542
test!(time_a: TimeTypes <- ["-t", "acc"]; Both => Ok(TimeTypes { modified: false, changed: false, accessed: true, created: false }));
555543

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

568550
// Multiples

src/output/time.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ impl DefaultFormat {
147147

148148
#[allow(trivial_numeric_casts)]
149149
fn format_local(&self, time: Duration) -> String {
150+
if time.as_nanos() == 0 {
151+
return "-".to_string();
152+
}
150153
let date = LocalDateTime::at(time.as_secs() as i64);
151154

152155
if self.is_recent(date) {
@@ -161,6 +164,10 @@ impl DefaultFormat {
161164

162165
#[allow(trivial_numeric_casts)]
163166
fn format_zoned(&self, time: Duration, zone: &TimeZone) -> String {
167+
if time.as_nanos() == 0 {
168+
return "-".to_string();
169+
}
170+
164171
let date = zone.to_zoned(LocalDateTime::at(time.as_secs() as i64));
165172

166173
if self.is_recent(date) {

0 commit comments

Comments
 (0)