asmov_common_tooling/
uu.rs

1use std::path::{Path, PathBuf};
2
3use chrono::{DateTime, Utc};
4use filetime::FileTime;
5
6/// Performs a coreutils touch on a file, creating it if necessary, and setting
7/// its modified time if provided.
8pub fn touch_file(path: &Path, datetime: Option<DateTime<Utc>>) {
9    let source = match datetime {
10        Some(dt) => {
11            uu_touch::Source::Timestamp(FileTime::from_unix_time(dt.timestamp(), dt.timestamp_subsec_nanos()))
12        },
13        None => uu_touch::Source::Now
14    };
15
16    uu_touch::touch(
17        &[uu_touch::InputFile::Path(PathBuf::from(path))],
18        &uu_touch::Options {
19            no_create: false,
20            no_deref: false,
21            source,
22            date: None,
23            change_times: uu_touch::ChangeTimes::Both,
24            strict: true,
25        }
26    ).unwrap();
27}