From: David Cook Date: Tue, 26 May 2020 00:50:06 +0000 (-0500) Subject: Fix fsync shim for Windows hosts with RO files X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=c01bc142194ae294356ac3b5b8666c3c351f14d5;p=rust.git Fix fsync shim for Windows hosts with RO files --- 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() }