Skip to content

Commit a60c130

Browse files
committed
Extract common logic for Windows host workaround
1 parent e352d4f commit a60c130

File tree

1 file changed

+24
-40
lines changed

1 file changed

+24
-40
lines changed

src/shims/fs.rs

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@ impl Default for DirHandler {
232232
}
233233
}
234234

235+
fn maybe_sync_file(file: &File, writable: bool, operation: fn(&File) -> std::io::Result<()>) -> std::io::Result<i32> {
236+
if !writable && cfg!(windows) {
237+
// sync_all() and sync_data() will return an error on Windows hosts if the file is not opened
238+
// for writing. (FlushFileBuffers requires that the file handle have the
239+
// GENERIC_WRITE right)
240+
Ok(0i32)
241+
} else {
242+
let result = operation(file);
243+
result.map(|_| 0i32)
244+
}
245+
}
246+
235247
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
236248
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
237249
fn open(
@@ -379,16 +391,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
379391
&& cmd == this.eval_libc_i32("F_FULLFSYNC")?
380392
{
381393
let &[_, _] = check_arg_count(args)?;
382-
if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.get_mut(&fd) {
383-
if !*writable && cfg!(windows) {
384-
// sync_all() will return an error on Windows hosts if the file is not opened
385-
// for writing. (FlushFileBuffers requires that the file handle have the
386-
// GENERIC_WRITE right)
387-
Ok(0i32)
388-
} else {
389-
let result = file.sync_all();
390-
this.try_unwrap_io_result(result.map(|_| 0i32))
391-
}
394+
if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.get(&fd) {
395+
let io_result = maybe_sync_file(file, *writable, File::sync_all);
396+
this.try_unwrap_io_result(io_result)
392397
} else {
393398
this.handle_not_found()
394399
}
@@ -1132,15 +1137,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
11321137
this.check_no_isolation("fsync")?;
11331138

11341139
let fd = this.read_scalar(fd_op)?.to_i32()?;
1135-
if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.get_mut(&fd) {
1136-
if !*writable && cfg!(windows) {
1137-
// sync_all() will return an error on Windows hosts if the file is not opened for writing.
1138-
// (FlushFileBuffers requires that the file handle have the GENERIC_WRITE right)
1139-
Ok(0i32)
1140-
} else {
1141-
let result = file.sync_all();
1142-
this.try_unwrap_io_result(result.map(|_| 0i32))
1143-
}
1140+
if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.get(&fd) {
1141+
let io_result = maybe_sync_file(file, *writable, File::sync_all);
1142+
this.try_unwrap_io_result(io_result)
11441143
} else {
11451144
this.handle_not_found()
11461145
}
@@ -1152,15 +1151,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
11521151
this.check_no_isolation("fdatasync")?;
11531152

11541153
let fd = this.read_scalar(fd_op)?.to_i32()?;
1155-
if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.get_mut(&fd) {
1156-
if !*writable && cfg!(windows) {
1157-
// sync_data() will return an error on Windows hosts if the file is not opened for writing.
1158-
// (FlushFileBuffers requires that the file handle have the GENERIC_WRITE right)
1159-
Ok(0i32)
1160-
} else {
1161-
let result = file.sync_data();
1162-
this.try_unwrap_io_result(result.map(|_| 0i32))
1163-
}
1154+
if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.get(&fd) {
1155+
let io_result = maybe_sync_file(file, *writable, File::sync_data);
1156+
this.try_unwrap_io_result(io_result)
11641157
} else {
11651158
this.handle_not_found()
11661159
}
@@ -1196,18 +1189,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
11961189
return Ok(-1);
11971190
}
11981191

1199-
if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.get_mut(&fd) {
1200-
if !*writable && cfg!(windows) {
1201-
// sync_data() will return an error on Windows hosts if the file is not opened for
1202-
// writing. (FlushFileBuffers requires that the file handle have the GENERIC_WRITE
1203-
// right)
1204-
Ok(0i32)
1205-
} else {
1206-
// In the interest of host compatibility, we conservatively ignore
1207-
// offset, nbytes, and flags, and sync the entire file.
1208-
let result = file.sync_data();
1209-
this.try_unwrap_io_result(result.map(|_| 0i32))
1210-
}
1192+
if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.get(&fd) {
1193+
let io_result = maybe_sync_file(file, *writable, File::sync_data);
1194+
this.try_unwrap_io_result(io_result)
12111195
} else {
12121196
this.handle_not_found()
12131197
}

0 commit comments

Comments
 (0)