-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
241 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ include = [ | |
] | ||
|
||
[dependencies] | ||
filetime = { version = "0.2", optional = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use crate::error::*; | ||
use std::path::Path; | ||
|
||
use filetime::{set_file_atime, set_file_mtime, FileTime}; | ||
|
||
/// Flags which can be used to configure how file and directory times will be | ||
/// assigned. | ||
#[derive(Clone)] | ||
pub struct TimeOptions { | ||
/// Keep the same modification time. | ||
pub retain_modification_time: bool, | ||
/// Keep the same access time. | ||
pub retain_access_time: bool, | ||
} | ||
|
||
impl TimeOptions { | ||
/// Initialize struct TimeOptions with default value. | ||
pub fn new() -> Self { | ||
TimeOptions { | ||
retain_modification_time: false, | ||
retain_access_time: false, | ||
} | ||
} | ||
} | ||
|
||
/// Assign time attributes for `to` same as in `from`. | ||
pub fn copy_time<P, Q>(from: P, to: Q, options: &TimeOptions) -> Result<()> | ||
where | ||
P: AsRef<Path>, | ||
Q: AsRef<Path>, | ||
{ | ||
if options.retain_modification_time || options.retain_access_time { | ||
match from.as_ref().metadata() { | ||
Ok(metadata) => { | ||
let mtime = FileTime::from_last_modification_time(&metadata); | ||
let atime = FileTime::from_last_access_time(&metadata); | ||
if options.retain_modification_time { | ||
set_file_mtime(to.as_ref(), mtime); | ||
} | ||
if options.retain_access_time { | ||
set_file_atime(to.as_ref(), atime); | ||
} | ||
} | ||
Err(_) => { | ||
err!("Could not read metadata", ErrorKind::Other); | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} |
Oops, something went wrong.