Skip to content

Commit 1d32fed

Browse files
committed
Return an error if trying to set a file timestamp to 0 on Windows
This would otherwise silently ignore the attempt, since 0 serves as a flag to not set a timestamp.
1 parent e2b5729 commit 1d32fed

File tree

1 file changed

+12
-5
lines changed
  • library/std/src/sys/windows

1 file changed

+12
-5
lines changed

library/std/src/sys/windows/fs.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ pub struct FilePermissions {
8484

8585
#[derive(Copy, Clone, Debug, Default)]
8686
pub struct FileTimes {
87-
accessed: c::FILETIME,
88-
modified: c::FILETIME,
87+
accessed: Option<c::FILETIME>,
88+
modified: Option<c::FILETIME>,
8989
}
9090

9191
#[derive(Debug)]
@@ -558,8 +558,15 @@ impl File {
558558
}
559559

560560
pub fn set_times(&self, times: FileTimes) -> io::Result<()> {
561+
let is_zero = |t: c::FILETIME| t.dwLowDateTime == 0 && t.dwHighDateTime == 0;
562+
if times.accessed.map_or(false, is_zero) || times.modified.map_or(false, is_zero) {
563+
return Err(io::const_io_error!(
564+
io::ErrorKind::InvalidInput,
565+
"Cannot set file timestamp to 0",
566+
));
567+
}
561568
cvt(unsafe {
562-
c::SetFileTime(self.as_handle(), None, Some(&times.accessed), Some(&times.modified))
569+
c::SetFileTime(self.as_handle(), None, times.accessed.as_ref(), times.modified.as_ref())
563570
})?;
564571
Ok(())
565572
}
@@ -911,11 +918,11 @@ impl FilePermissions {
911918

912919
impl FileTimes {
913920
pub fn set_accessed(&mut self, t: SystemTime) {
914-
self.accessed = t.into_inner();
921+
self.accessed = Some(t.into_inner());
915922
}
916923

917924
pub fn set_modified(&mut self, t: SystemTime) {
918-
self.modified = t.into_inner();
925+
self.modified = Some(t.into_inner());
919926
}
920927
}
921928

0 commit comments

Comments
 (0)