in file_utils/src/file_metadata.rs [106:146]
fn test_set_metadata_timestamps() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("test_file");
let file = File::create(&file_path).unwrap();
// Create a file with specific timestamps to copy from
let src_file_path = dir.path().join("src_file");
let src_file = File::create(&src_file_path).unwrap();
let src_metadata = src_file.metadata().unwrap();
let atime = SystemTime::now() - Duration::from_secs(24 * 3600);
let mtime = SystemTime::now() - Duration::from_secs(48 * 3600);
let times = [
libc::timespec {
tv_sec: atime.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as libc::time_t,
tv_nsec: atime.duration_since(SystemTime::UNIX_EPOCH).unwrap().subsec_nanos() as libc::c_long,
},
libc::timespec {
tv_sec: mtime.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as libc::time_t,
tv_nsec: mtime.duration_since(SystemTime::UNIX_EPOCH).unwrap().subsec_nanos() as libc::c_long,
},
];
unsafe {
libc::utimensat(
libc::AT_FDCWD,
src_file_path.to_str().unwrap().as_bytes().as_ptr() as *const libc::c_char,
times.as_ptr(),
0,
);
}
// Apply set_metadata
set_file_metadata(&file_path, &src_metadata, false).unwrap();
// Check that timestamps have been updated
let updated_metadata = file.metadata().unwrap();
assert_eq!(updated_metadata.modified().unwrap(), src_metadata.modified().unwrap());
}