|
1 | 1 | use crate::io::prelude::*;
|
2 | 2 |
|
3 | 3 | use crate::env;
|
4 |
| -use crate::fs::{self, File, OpenOptions}; |
| 4 | +use crate::fs::{self, File, FileTimes, OpenOptions}; |
5 | 5 | use crate::io::{BorrowedBuf, ErrorKind, SeekFrom};
|
6 | 6 | use crate::mem::MaybeUninit;
|
7 | 7 | use crate::path::Path;
|
8 | 8 | use crate::str;
|
9 | 9 | use crate::sync::Arc;
|
10 | 10 | use crate::sys_common::io::test::{tmpdir, TempDir};
|
11 | 11 | use crate::thread;
|
12 |
| -use crate::time::{Duration, Instant}; |
| 12 | +use crate::time::{Duration, Instant, SystemTime}; |
13 | 13 |
|
14 | 14 | use rand::RngCore;
|
15 | 15 |
|
@@ -1633,3 +1633,53 @@ fn rename_directory() {
|
1633 | 1633 | assert!(new_path.join("newdir").is_dir());
|
1634 | 1634 | assert!(new_path.join("newdir/temp.txt").exists());
|
1635 | 1635 | }
|
| 1636 | + |
| 1637 | +#[test] |
| 1638 | +fn test_file_times() { |
| 1639 | + #[cfg(target_os = "ios")] |
| 1640 | + use crate::os::ios::fs::FileTimesExt; |
| 1641 | + #[cfg(target_os = "macos")] |
| 1642 | + use crate::os::macos::fs::FileTimesExt; |
| 1643 | + #[cfg(target_os = "watchos")] |
| 1644 | + use crate::os::watchos::fs::FileTimesExt; |
| 1645 | + #[cfg(windows)] |
| 1646 | + use crate::os::windows::fs::FileTimesExt; |
| 1647 | + |
| 1648 | + let tmp = tmpdir(); |
| 1649 | + let file = File::create(tmp.join("foo")).unwrap(); |
| 1650 | + let mut times = FileTimes::new(); |
| 1651 | + let accessed = SystemTime::UNIX_EPOCH + Duration::from_secs(12345); |
| 1652 | + let modified = SystemTime::UNIX_EPOCH + Duration::from_secs(54321); |
| 1653 | + times = times.set_accessed(accessed).set_modified(modified); |
| 1654 | + #[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))] |
| 1655 | + let created = SystemTime::UNIX_EPOCH + Duration::from_secs(32123); |
| 1656 | + #[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))] |
| 1657 | + { |
| 1658 | + times = times.set_created(created); |
| 1659 | + } |
| 1660 | + match file.set_times(times) { |
| 1661 | + // Allow unsupported errors on platforms which don't support setting times. |
| 1662 | + #[cfg(not(any( |
| 1663 | + windows, |
| 1664 | + all( |
| 1665 | + unix, |
| 1666 | + not(any( |
| 1667 | + target_os = "android", |
| 1668 | + target_os = "redox", |
| 1669 | + target_os = "espidf", |
| 1670 | + target_os = "horizon" |
| 1671 | + )) |
| 1672 | + ) |
| 1673 | + )))] |
| 1674 | + Err(e) if e.kind() == ErrorKind::Unsupported => return, |
| 1675 | + Err(e) => panic!("error setting file times: {e:?}"), |
| 1676 | + Ok(_) => {} |
| 1677 | + } |
| 1678 | + let metadata = file.metadata().unwrap(); |
| 1679 | + assert_eq!(metadata.accessed().unwrap(), accessed); |
| 1680 | + assert_eq!(metadata.modified().unwrap(), modified); |
| 1681 | + #[cfg(any(windows, target_os = "macos", target_os = "ios", target_os = "watchos"))] |
| 1682 | + { |
| 1683 | + assert_eq!(metadata.created().unwrap(), created); |
| 1684 | + } |
| 1685 | +} |
0 commit comments