use std::io::Result;
pub use super::sys::filetime::*;
pub(crate) trait FileTimeExt {
fn seconds_(&self) -> Result<libc::time_t>;
fn nanoseconds_(&self) -> libc::c_long;
}
impl FileTimeExt for filetime::FileTime {
fn seconds_(&self) -> Result<libc::time_t> {
use std::convert::TryInto;
use std::io::Error;
let sec = match self.seconds().try_into() {
Ok(sec) => sec,
Err(_) => {
tracing::debug!("filetime_to_timespec failed converting seconds to required width");
return Err(Error::from_raw_os_error(libc::EOVERFLOW));
}
};
Ok(sec)
}
fn nanoseconds_(&self) -> libc::c_long {
use std::convert::TryInto;
self.nanoseconds().try_into().unwrap()
}
}
#[derive(Debug, Copy, Clone)]
pub enum FileTime {
Now,
Omit,
FileTime(filetime::FileTime),
}
pub(crate) fn to_timespec(ft: &FileTime) -> Result<libc::timespec> {
let ts = match ft {
FileTime::Now => libc::timespec {
tv_sec: 0,
tv_nsec: libc::UTIME_NOW,
},
FileTime::Omit => libc::timespec {
tv_sec: 0,
tv_nsec: libc::UTIME_OMIT,
},
FileTime::FileTime(ft) => libc::timespec {
tv_sec: ft.seconds_()?,
tv_nsec: ft.nanoseconds_(),
},
};
Ok(ts)
}