]> git.lizzy.rs Git - rust.git/commitdiff
Fix fsync shim for Windows hosts with RO files
authorDavid Cook <divergentdave@gmail.com>
Tue, 26 May 2020 00:50:06 +0000 (19:50 -0500)
committerDavid Cook <divergentdave@gmail.com>
Tue, 26 May 2020 02:45:29 +0000 (21:45 -0500)
src/shims/fs.rs

index 66e191e3ed4a11bb2cd5fb214878711e0cef2194..7f1c5caaf642747c5434dc58b70f41fb2f5a1177 100644 (file)
@@ -1125,9 +1125,14 @@ fn fsync(&mut self, fd_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
         this.check_no_isolation("fsync")?;
 
         let fd = this.read_scalar(fd_op)?.to_i32()?;
-        if let Some(FileHandle { file, writable: _ }) = this.machine.file_handler.handles.get_mut(&fd) {
-            let result = file.sync_all();
-            this.try_unwrap_io_result(result.map(|_| 0i32))
+        if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.get_mut(&fd) {
+            if !*writable && cfg!(windows) {
+                // sync_all() will return an error on Windows hosts if the file is not opened for writing.
+                Ok(0i32)
+            } else {
+                let result = file.sync_all();
+                this.try_unwrap_io_result(result.map(|_| 0i32))
+            }
         } else {
             this.handle_not_found()
         }
@@ -1139,9 +1144,14 @@ fn fdatasync(&mut self, fd_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
         this.check_no_isolation("fdatasync")?;
 
         let fd = this.read_scalar(fd_op)?.to_i32()?;
-        if let Some(FileHandle { file, writable: _ }) = this.machine.file_handler.handles.get_mut(&fd) {
-            let result = file.sync_data();
-            this.try_unwrap_io_result(result.map(|_| 0i32))
+        if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.get_mut(&fd) {
+            if !*writable && cfg!(windows) {
+                // sync_data() will return an error on Windows hosts if the file is not opened for writing.
+                Ok(0i32)
+            } else {
+                let result = file.sync_data();
+                this.try_unwrap_io_result(result.map(|_| 0i32))
+            }
         } else {
             this.handle_not_found()
         }