use std::fmt;
use std::fs;
use std::io;
use std::path::Path;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
cfg_if::cfg_if! {
if #[cfg(target_os = "redox")] {
#[path = "redox.rs"]
mod imp;
} else if #[cfg(windows)] {
#[path = "windows.rs"]
mod imp;
} else if #[cfg(all(target_family = "wasm", not(target_os = "emscripten")))] {
#[path = "wasm.rs"]
mod imp;
} else {
#[path = "unix/mod.rs"]
mod imp;
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Copy, Clone, Hash)]
pub struct FileTime {
seconds: i64,
nanos: u32,
}
impl FileTime {
pub const fn zero() -> FileTime {
FileTime {
seconds: 0,
nanos: 0,
}
}
const fn emulate_second_only_system(self) -> FileTime {
if cfg!(emulate_second_only_system) {
FileTime {
seconds: self.seconds,
nanos: 0,
}
} else {
self
}
}
pub fn now() -> FileTime {
FileTime::from_system_time(SystemTime::now())
}
pub const fn from_unix_time(seconds: i64, nanos: u32) -> FileTime {
FileTime {
seconds: seconds + if cfg!(windows) { 11644473600 } else { 0 },
nanos,
}
.emulate_second_only_system()
}
pub fn from_last_modification_time(meta: &fs::Metadata) -> FileTime {
imp::from_last_modification_time(meta).emulate_second_only_system()
}
pub fn from_last_access_time(meta: &fs::Metadata) -> FileTime {
imp::from_last_access_time(meta).emulate_second_only_system()
}
pub fn from_creation_time(meta: &fs::Metadata) -> Option<FileTime> {
imp::from_creation_time(meta).map(|x| x.emulate_second_only_system())
}
pub fn from_system_time(time: SystemTime) -> FileTime {
let epoch = if cfg!(windows) {
UNIX_EPOCH - Duration::from_secs(11644473600)
} else {
UNIX_EPOCH
};
time.duration_since(epoch)
.map(|d| FileTime {
seconds: d.as_secs() as i64,
nanos: d.subsec_nanos(),
})
.unwrap_or_else(|e| {
let until_epoch = e.duration();
let (sec_offset, nanos) = if until_epoch.subsec_nanos() == 0 {
(0, 0)
} else {
(-1, 1_000_000_000 - until_epoch.subsec_nanos())
};
FileTime {
seconds: -1 * until_epoch.as_secs() as i64 + sec_offset,
nanos,
}
})
.emulate_second_only_system()
}
pub const fn seconds(&self) -> i64 {
self.seconds
}
pub const fn unix_seconds(&self) -> i64 {
self.seconds - if cfg!(windows) { 11644473600 } else { 0 }
}
pub const fn nanoseconds(&self) -> u32 {
self.nanos
}
}
impl fmt::Display for FileTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}.{:09}s", self.seconds, self.nanos)
}
}
impl From<SystemTime> for FileTime {
fn from(time: SystemTime) -> FileTime {
FileTime::from_system_time(time)
}
}
pub fn set_file_times<P>(p: P, atime: FileTime, mtime: FileTime) -> io::Result<()>
where
P: AsRef<Path>,
{
imp::set_file_times(p.as_ref(), atime, mtime)
}
pub fn set_file_handle_times(
f: &fs::File,
atime: Option<FileTime>,
mtime: Option<FileTime>,
) -> io::Result<()> {
imp::set_file_handle_times(f, atime, mtime)
}
pub fn set_symlink_file_times<P>(p: P, atime: FileTime, mtime: FileTime) -> io::Result<()>
where
P: AsRef<Path>,
{
imp::set_symlink_file_times(p.as_ref(), atime, mtime)
}
pub fn set_file_mtime<P>(p: P, mtime: FileTime) -> io::Result<()>
where
P: AsRef<Path>,
{
imp::set_file_mtime(p.as_ref(), mtime)
}
pub fn set_file_atime<P>(p: P, atime: FileTime) -> io::Result<()>
where
P: AsRef<Path>,
{
imp::set_file_atime(p.as_ref(), atime)
}
#[cfg(test)]
mod tests {
use super::{
set_file_atime, set_file_handle_times, set_file_mtime, set_file_times,
set_symlink_file_times, FileTime,
};
use std::fs::{self, File};
use std::io;
use std::path::Path;
use std::time::{Duration, UNIX_EPOCH};
use tempfile::Builder;
#[cfg(unix)]
fn make_symlink_file<P, Q>(src: P, dst: Q) -> io::Result<()>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
use std::os::unix::fs::symlink;
symlink(src, dst)
}
#[cfg(windows)]
fn make_symlink_file<P, Q>(src: P, dst: Q) -> io::Result<()>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
use std::os::windows::fs::symlink_file;
symlink_file(src, dst)
}
#[cfg(unix)]
fn make_symlink_dir<P, Q>(src: P, dst: Q) -> io::Result<()>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
use std::os::unix::fs::symlink;
symlink(src, dst)
}
#[cfg(windows)]
fn make_symlink_dir<P, Q>(src: P, dst: Q) -> io::Result<()>
where
P: AsRef<Path>,
Q: AsRef<Path>,
{
use std::os::windows::fs::symlink_dir;
symlink_dir(src, dst)
}
#[test]
#[cfg(windows)]
fn from_unix_time_test() {
let time = FileTime::from_unix_time(10, 100_000_000);
assert_eq!(11644473610, time.seconds);
assert_eq!(100_000_000, time.nanos);
let time = FileTime::from_unix_time(-10, 100_000_000);
assert_eq!(11644473590, time.seconds);
assert_eq!(100_000_000, time.nanos);
let time = FileTime::from_unix_time(-12_000_000_000, 0);
assert_eq!(-355526400, time.seconds);
assert_eq!(0, time.nanos);
}
#[test]
#[cfg(not(windows))]
fn from_unix_time_test() {
let time = FileTime::from_unix_time(10, 100_000_000);
assert_eq!(10, time.seconds);
assert_eq!(100_000_000, time.nanos);
let time = FileTime::from_unix_time(-10, 100_000_000);
assert_eq!(-10, time.seconds);
assert_eq!(100_000_000, time.nanos);
let time = FileTime::from_unix_time(-12_000_000_000, 0);
assert_eq!(-12_000_000_000, time.seconds);
assert_eq!(0, time.nanos);
}
#[test]
#[cfg(windows)]
fn from_system_time_test() {
let time = FileTime::from_system_time(UNIX_EPOCH + Duration::from_secs(10));
assert_eq!(11644473610, time.seconds);
assert_eq!(0, time.nanos);
let time = FileTime::from_system_time(UNIX_EPOCH - Duration::from_secs(10));
assert_eq!(11644473590, time.seconds);
assert_eq!(0, time.nanos);
let time = FileTime::from_system_time(UNIX_EPOCH - Duration::from_millis(1100));
assert_eq!(11644473598, time.seconds);
assert_eq!(900_000_000, time.nanos);
let time = FileTime::from_system_time(UNIX_EPOCH - Duration::from_secs(12_000_000_000));
assert_eq!(-355526400, time.seconds);
assert_eq!(0, time.nanos);
}
#[test]
#[cfg(not(windows))]
fn from_system_time_test() {
let time = FileTime::from_system_time(UNIX_EPOCH + Duration::from_secs(10));
assert_eq!(10, time.seconds);
assert_eq!(0, time.nanos);
let time = FileTime::from_system_time(UNIX_EPOCH - Duration::from_secs(10));
assert_eq!(-10, time.seconds);
assert_eq!(0, time.nanos);
let time = FileTime::from_system_time(UNIX_EPOCH - Duration::from_millis(1100));
assert_eq!(-2, time.seconds);
assert_eq!(900_000_000, time.nanos);
let time = FileTime::from_system_time(UNIX_EPOCH - Duration::from_secs(12_000_000));
assert_eq!(-12_000_000, time.seconds);
assert_eq!(0, time.nanos);
}
#[test]
fn set_file_times_test() -> io::Result<()> {
let td = Builder::new().prefix("filetime").tempdir()?;
let path = td.path().join("foo.txt");
let mut f = File::create(&path)?;
let metadata = fs::metadata(&path)?;
let mtime = FileTime::from_last_modification_time(&metadata);
let atime = FileTime::from_last_access_time(&metadata);
set_file_times(&path, atime, mtime)?;
let new_mtime = FileTime::from_unix_time(10_000, 0);
set_file_times(&path, atime, new_mtime)?;
let metadata = fs::metadata(&path)?;
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime, "modification should be updated");
let new_mtime = FileTime::from_unix_time(20_000, 0);
set_file_handle_times(&mut f, None, Some(new_mtime))?;
let metadata = f.metadata()?;
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime, "modification time should be updated");
let new_atime = FileTime::from_last_access_time(&metadata);
assert_eq!(atime, new_atime, "accessed time should not be updated");
let new_atime = FileTime::from_unix_time(30_000, 0);
set_file_handle_times(&mut f, Some(new_atime), None)?;
let metadata = f.metadata()?;
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime, "modification time should not be updated");
let atime = FileTime::from_last_access_time(&metadata);
assert_eq!(atime, new_atime, "accessed time should be updated");
let spath = td.path().join("bar.txt");
make_symlink_file(&path, &spath)?;
let metadata = fs::symlink_metadata(&spath)?;
let smtime = FileTime::from_last_modification_time(&metadata);
set_file_times(&spath, atime, mtime)?;
let metadata = fs::metadata(&path)?;
let cur_mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, cur_mtime);
let metadata = fs::symlink_metadata(&spath)?;
let cur_mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(smtime, cur_mtime);
set_file_times(&spath, atime, new_mtime)?;
let metadata = fs::metadata(&path)?;
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime);
let metadata = fs::symlink_metadata(&spath)?;
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, smtime);
Ok(())
}
#[test]
fn set_dir_times_test() -> io::Result<()> {
let td = Builder::new().prefix("filetime").tempdir()?;
let path = td.path().join("foo");
fs::create_dir(&path)?;
let metadata = fs::metadata(&path)?;
let mtime = FileTime::from_last_modification_time(&metadata);
let atime = FileTime::from_last_access_time(&metadata);
set_file_times(&path, atime, mtime)?;
let new_mtime = FileTime::from_unix_time(10_000, 0);
set_file_times(&path, atime, new_mtime)?;
let metadata = fs::metadata(&path)?;
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime, "modification should be updated");
let new_mtime = FileTime::from_unix_time(20_000, 0);
set_file_mtime(&path, new_mtime)?;
let metadata = fs::metadata(&path)?;
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime, "modification time should be updated");
let new_atime = FileTime::from_last_access_time(&metadata);
assert_eq!(atime, new_atime, "accessed time should not be updated");
let new_atime = FileTime::from_unix_time(30_000, 0);
set_file_atime(&path, new_atime)?;
let metadata = fs::metadata(&path)?;
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime, "modification time should not be updated");
let atime = FileTime::from_last_access_time(&metadata);
assert_eq!(atime, new_atime, "accessed time should be updated");
let spath = td.path().join("bar");
make_symlink_dir(&path, &spath)?;
let metadata = fs::symlink_metadata(&spath)?;
let smtime = FileTime::from_last_modification_time(&metadata);
set_file_times(&spath, atime, mtime)?;
let metadata = fs::metadata(&path)?;
let cur_mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, cur_mtime);
let metadata = fs::symlink_metadata(&spath)?;
let cur_mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(smtime, cur_mtime);
set_file_times(&spath, atime, new_mtime)?;
let metadata = fs::metadata(&path)?;
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime);
let metadata = fs::symlink_metadata(&spath)?;
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, smtime);
Ok(())
}
#[test]
fn set_file_times_pre_unix_epoch_test() {
let td = Builder::new().prefix("filetime").tempdir().unwrap();
let path = td.path().join("foo.txt");
File::create(&path).unwrap();
let metadata = fs::metadata(&path).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
let atime = FileTime::from_last_access_time(&metadata);
set_file_times(&path, atime, mtime).unwrap();
let new_mtime = FileTime::from_unix_time(-10_000, 0);
if cfg!(target_os = "aix") {
let result = set_file_times(&path, atime, new_mtime);
assert!(result.is_err());
assert!(result.err().unwrap().kind() == std::io::ErrorKind::InvalidInput);
} else {
set_file_times(&path, atime, new_mtime).unwrap();
let metadata = fs::metadata(&path).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime);
}
}
#[test]
#[cfg(windows)]
fn set_file_times_pre_windows_epoch_test() {
let td = Builder::new().prefix("filetime").tempdir().unwrap();
let path = td.path().join("foo.txt");
File::create(&path).unwrap();
let metadata = fs::metadata(&path).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
let atime = FileTime::from_last_access_time(&metadata);
set_file_times(&path, atime, mtime).unwrap();
let new_mtime = FileTime::from_unix_time(-12_000_000_000, 0);
assert!(set_file_times(&path, atime, new_mtime).is_err());
}
#[test]
fn set_symlink_file_times_test() {
let td = Builder::new().prefix("filetime").tempdir().unwrap();
let path = td.path().join("foo.txt");
File::create(&path).unwrap();
let metadata = fs::metadata(&path).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
let atime = FileTime::from_last_access_time(&metadata);
set_symlink_file_times(&path, atime, mtime).unwrap();
let new_mtime = FileTime::from_unix_time(10_000, 0);
set_symlink_file_times(&path, atime, new_mtime).unwrap();
let metadata = fs::metadata(&path).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime);
let spath = td.path().join("bar.txt");
make_symlink_file(&path, &spath).unwrap();
let metadata = fs::symlink_metadata(&spath).unwrap();
let smtime = FileTime::from_last_modification_time(&metadata);
let satime = FileTime::from_last_access_time(&metadata);
set_symlink_file_times(&spath, smtime, satime).unwrap();
let metadata = fs::metadata(&path).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime);
let new_smtime = FileTime::from_unix_time(20_000, 0);
set_symlink_file_times(&spath, atime, new_smtime).unwrap();
let metadata = fs::metadata(&spath).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime);
let metadata = fs::symlink_metadata(&spath).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_smtime);
}
#[test]
fn set_symlink_dir_times_test() {
let td = Builder::new().prefix("filetime").tempdir().unwrap();
let path = td.path().join("foo");
fs::create_dir(&path).unwrap();
let metadata = fs::metadata(&path).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
let atime = FileTime::from_last_access_time(&metadata);
set_symlink_file_times(&path, atime, mtime).unwrap();
let new_mtime = FileTime::from_unix_time(10_000, 0);
set_symlink_file_times(&path, atime, new_mtime).unwrap();
let metadata = fs::metadata(&path).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime);
let spath = td.path().join("bar");
make_symlink_dir(&path, &spath).unwrap();
let metadata = fs::symlink_metadata(&spath).unwrap();
let smtime = FileTime::from_last_modification_time(&metadata);
let satime = FileTime::from_last_access_time(&metadata);
set_symlink_file_times(&spath, smtime, satime).unwrap();
let metadata = fs::metadata(&path).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime);
let new_smtime = FileTime::from_unix_time(20_000, 0);
set_symlink_file_times(&spath, atime, new_smtime).unwrap();
let metadata = fs::metadata(&spath).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime);
let metadata = fs::symlink_metadata(&spath).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_smtime);
}
#[test]
fn set_single_time_test() {
use super::{set_file_atime, set_file_mtime};
let td = Builder::new().prefix("filetime").tempdir().unwrap();
let path = td.path().join("foo.txt");
File::create(&path).unwrap();
let metadata = fs::metadata(&path).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
let atime = FileTime::from_last_access_time(&metadata);
set_file_times(&path, atime, mtime).unwrap();
let new_mtime = FileTime::from_unix_time(10_000, 0);
set_file_mtime(&path, new_mtime).unwrap();
let metadata = fs::metadata(&path).unwrap();
let mtime = FileTime::from_last_modification_time(&metadata);
assert_eq!(mtime, new_mtime, "modification time should be updated");
assert_eq!(
atime,
FileTime::from_last_access_time(&metadata),
"access time should not be updated",
);
let new_atime = FileTime::from_unix_time(20_000, 0);
set_file_atime(&path, new_atime).unwrap();
let metadata = fs::metadata(&path).unwrap();
let atime = FileTime::from_last_access_time(&metadata);
assert_eq!(atime, new_atime, "access time should be updated");
assert_eq!(
mtime,
FileTime::from_last_modification_time(&metadata),
"modification time should not be updated"
);
}
}