about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorJosh Triplett <josh@joshtriplett.org>2022-06-25 12:04:13 -0700
committerJosh Triplett <josh@joshtriplett.org>2022-07-15 02:54:06 -0700
commit828b637c2a37c85f8d3734d57ad459868ea31584 (patch)
tree7226ba9db8f88baff794df02570e015573073d4d /library/std/src
parent1ac8d5ece31dae9441f56a6915115633e30eeb69 (diff)
downloadrust-828b637c2a37c85f8d3734d57ad459868ea31584.tar.gz
rust-828b637c2a37c85f8d3734d57ad459868ea31584.zip
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.
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/sys/windows/fs.rs17
1 files changed, 12 insertions, 5 deletions
diff --git a/library/std/src/sys/windows/fs.rs b/library/std/src/sys/windows/fs.rs
index e3809f3579d..39aabadcf15 100644
--- a/library/std/src/sys/windows/fs.rs
+++ b/library/std/src/sys/windows/fs.rs
@@ -84,8 +84,8 @@ pub struct FilePermissions {
 
 #[derive(Copy, Clone, Debug, Default)]
 pub struct FileTimes {
-    accessed: c::FILETIME,
-    modified: c::FILETIME,
+    accessed: Option<c::FILETIME>,
+    modified: Option<c::FILETIME>,
 }
 
 #[derive(Debug)]
@@ -558,8 +558,15 @@ impl File {
     }
 
     pub fn set_times(&self, times: FileTimes) -> io::Result<()> {
+        let is_zero = |t: c::FILETIME| t.dwLowDateTime == 0 && t.dwHighDateTime == 0;
+        if times.accessed.map_or(false, is_zero) || times.modified.map_or(false, is_zero) {
+            return Err(io::const_io_error!(
+                io::ErrorKind::InvalidInput,
+                "Cannot set file timestamp to 0",
+            ));
+        }
         cvt(unsafe {
-            c::SetFileTime(self.as_handle(), None, Some(&times.accessed), Some(&times.modified))
+            c::SetFileTime(self.as_handle(), None, times.accessed.as_ref(), times.modified.as_ref())
         })?;
         Ok(())
     }
@@ -911,11 +918,11 @@ impl FilePermissions {
 
 impl FileTimes {
     pub fn set_accessed(&mut self, t: SystemTime) {
-        self.accessed = t.into_inner();
+        self.accessed = Some(t.into_inner());
     }
 
     pub fn set_modified(&mut self, t: SystemTime) {
-        self.modified = t.into_inner();
+        self.modified = Some(t.into_inner());
     }
 }