From c01bc142194ae294356ac3b5b8666c3c351f14d5 Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 25 May 2020 19:50:06 -0500 Subject: [PATCH] Fix fsync shim for Windows hosts with RO files --- src/shims/fs.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/shims/fs.rs b/src/shims/fs.rs index 66e191e3ed4..7f1c5caaf64 100644 --- a/src/shims/fs.rs +++ b/src/shims/fs.rs @@ -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() } -- 2.44.0