Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions library/std/src/fs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2301,6 +2301,57 @@ fn test_fs_set_times() {
}
}

#[test]
fn test_fs_set_times_on_dir() {
#[cfg(target_vendor = "apple")]
use crate::os::darwin::fs::FileTimesExt;
#[cfg(windows)]
use crate::os::windows::fs::FileTimesExt;

let tmp = tmpdir();
let dir_path = tmp.join("testdir");
fs::create_dir(&dir_path).unwrap();

let mut times = FileTimes::new();
let accessed = SystemTime::UNIX_EPOCH + Duration::from_secs(12345);
let modified = SystemTime::UNIX_EPOCH + Duration::from_secs(54321);
times = times.set_accessed(accessed).set_modified(modified);

#[cfg(any(windows, target_vendor = "apple"))]
let created = SystemTime::UNIX_EPOCH + Duration::from_secs(32123);
#[cfg(any(windows, target_vendor = "apple"))]
{
times = times.set_created(created);
}

match fs::set_times(&dir_path, times) {
// Allow unsupported errors on platforms which don't support setting times.
#[cfg(not(any(
windows,
all(
unix,
not(any(
target_os = "android",
target_os = "redox",
target_os = "espidf",
target_os = "horizon"
))
)
)))]
Err(e) if e.kind() == ErrorKind::Unsupported => return,
Err(e) => panic!("error setting directory times: {e:?}"),
Ok(_) => {}
}

let metadata = fs::metadata(&dir_path).unwrap();
assert_eq!(metadata.accessed().unwrap(), accessed);
assert_eq!(metadata.modified().unwrap(), modified);
#[cfg(any(windows, target_vendor = "apple"))]
{
assert_eq!(metadata.created().unwrap(), created);
}
}

#[test]
fn test_fs_set_times_follows_symlink() {
#[cfg(target_vendor = "apple")]
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/sys/fs/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1556,15 +1556,15 @@ pub fn set_perm(p: &WCStr, perm: FilePermissions) -> io::Result<()> {

pub fn set_times(p: &WCStr, times: FileTimes) -> io::Result<()> {
let mut opts = OpenOptions::new();
opts.write(true);
opts.access_mode(c::FILE_WRITE_ATTRIBUTES);
opts.custom_flags(c::FILE_FLAG_BACKUP_SEMANTICS);
let file = File::open_native(p, &opts)?;
file.set_times(times)
}

pub fn set_times_nofollow(p: &WCStr, times: FileTimes) -> io::Result<()> {
let mut opts = OpenOptions::new();
opts.write(true);
opts.access_mode(c::FILE_WRITE_ATTRIBUTES);
// `FILE_FLAG_OPEN_REPARSE_POINT` for no_follow behavior
opts.custom_flags(c::FILE_FLAG_BACKUP_SEMANTICS | c::FILE_FLAG_OPEN_REPARSE_POINT);
let file = File::open_native(p, &opts)?;
Expand Down
Loading