Skip to content

Commit c01bc14

Browse files
committed
Fix fsync shim for Windows hosts with RO files
1 parent 3252082 commit c01bc14

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/shims/fs.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,9 +1125,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
11251125
this.check_no_isolation("fsync")?;
11261126

11271127
let fd = this.read_scalar(fd_op)?.to_i32()?;
1128-
if let Some(FileHandle { file, writable: _ }) = this.machine.file_handler.handles.get_mut(&fd) {
1129-
let result = file.sync_all();
1130-
this.try_unwrap_io_result(result.map(|_| 0i32))
1128+
if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.get_mut(&fd) {
1129+
if !*writable && cfg!(windows) {
1130+
// sync_all() will return an error on Windows hosts if the file is not opened for writing.
1131+
Ok(0i32)
1132+
} else {
1133+
let result = file.sync_all();
1134+
this.try_unwrap_io_result(result.map(|_| 0i32))
1135+
}
11311136
} else {
11321137
this.handle_not_found()
11331138
}
@@ -1139,9 +1144,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
11391144
this.check_no_isolation("fdatasync")?;
11401145

11411146
let fd = this.read_scalar(fd_op)?.to_i32()?;
1142-
if let Some(FileHandle { file, writable: _ }) = this.machine.file_handler.handles.get_mut(&fd) {
1143-
let result = file.sync_data();
1144-
this.try_unwrap_io_result(result.map(|_| 0i32))
1147+
if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.get_mut(&fd) {
1148+
if !*writable && cfg!(windows) {
1149+
// sync_data() will return an error on Windows hosts if the file is not opened for writing.
1150+
Ok(0i32)
1151+
} else {
1152+
let result = file.sync_data();
1153+
this.try_unwrap_io_result(result.map(|_| 0i32))
1154+
}
11451155
} else {
11461156
this.handle_not_found()
11471157
}

0 commit comments

Comments
 (0)