Skip to content

Commit c5a1e8e

Browse files
authored
Merge pull request ogham#557 from BrennanMcDonald/master
Added checks for modified/accessed/created metadata
2 parents 565a323 + 215b779 commit c5a1e8e

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/fs/file.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io::Error as IOError;
55
use std::io::Result as IOResult;
66
use std::os::unix::fs::{MetadataExt, PermissionsExt, FileTypeExt};
77
use std::path::{Path, PathBuf};
8-
use std::time::{UNIX_EPOCH, Duration};
8+
use std::time::{SystemTime, UNIX_EPOCH, Duration};
99

1010
use log::{debug, error};
1111

@@ -329,8 +329,13 @@ impl<'dir> File<'dir> {
329329
}
330330

331331
/// This file’s last modified timestamp.
332+
/// If the file's time is invalid, assume it was modified today
332333
pub fn modified_time(&self) -> Duration {
333-
self.metadata.modified().unwrap().duration_since(UNIX_EPOCH).unwrap()
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()
338+
}
334339
}
335340

336341
/// This file’s last changed timestamp.
@@ -339,13 +344,23 @@ impl<'dir> File<'dir> {
339344
}
340345

341346
/// This file’s last accessed timestamp.
347+
/// If the file's time is invalid, assume it was accessed today
342348
pub fn accessed_time(&self) -> Duration {
343-
self.metadata.accessed().unwrap().duration_since(UNIX_EPOCH).unwrap()
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()
353+
}
344354
}
345355

346356
/// This file’s created timestamp.
357+
/// If the file's time is invalid, assume it was created today
347358
pub fn created_time(&self) -> Duration {
348-
self.metadata.created().unwrap().duration_since(UNIX_EPOCH).unwrap()
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()
363+
}
349364
}
350365

351366
/// This file’s ‘type’.

0 commit comments

Comments
 (0)