use super::procfs::set_times_through_proc_self_fd;
use crate::fs::{open, OpenOptions, SystemTimeSpec};
use fs_set_times::SetTimes;
use std::{fs, io, path::Path};
pub(crate) fn set_times_impl(
start: &fs::File,
path: &Path,
atime: Option<SystemTimeSpec>,
mtime: Option<SystemTimeSpec>,
) -> io::Result<()> {
match open(start, path, OpenOptions::new().write(true)) {
Ok(file) => {
return file.set_times(
atime.map(SystemTimeSpec::into_std),
mtime.map(SystemTimeSpec::into_std),
)
}
Err(err) => match posish::io::Error::from_io_error(&err) {
Some(posish::io::Error::ACCES) | Some(posish::io::Error::ISDIR) => (),
_ => return Err(err),
},
}
match open(start, path, OpenOptions::new().read(true)) {
Ok(file) => {
return file.set_times(
atime.map(SystemTimeSpec::into_std),
mtime.map(SystemTimeSpec::into_std),
)
}
Err(err) => match posish::io::Error::from_io_error(&err) {
Some(posish::io::Error::ACCES) => (),
_ => return Err(err),
},
}
set_times_through_proc_self_fd(start, path, atime, mtime)
}