@@ -232,6 +232,18 @@ impl Default for DirHandler {
232
232
}
233
233
}
234
234
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
+
235
247
impl < ' mir , ' tcx : ' mir > EvalContextExt < ' mir , ' tcx > for crate :: MiriEvalContext < ' mir , ' tcx > { }
236
248
pub trait EvalContextExt < ' mir , ' tcx : ' mir > : crate :: MiriEvalContextExt < ' mir , ' tcx > {
237
249
fn open (
@@ -379,16 +391,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
379
391
&& cmd == this. eval_libc_i32 ( "F_FULLFSYNC" ) ?
380
392
{
381
393
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)
392
397
} else {
393
398
this. handle_not_found ( )
394
399
}
@@ -1132,15 +1137,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
1132
1137
this. check_no_isolation ( "fsync" ) ?;
1133
1138
1134
1139
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)
1144
1143
} else {
1145
1144
this. handle_not_found ( )
1146
1145
}
@@ -1152,15 +1151,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
1152
1151
this. check_no_isolation ( "fdatasync" ) ?;
1153
1152
1154
1153
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)
1164
1157
} else {
1165
1158
this. handle_not_found ( )
1166
1159
}
@@ -1196,18 +1189,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
1196
1189
return Ok ( -1 ) ;
1197
1190
}
1198
1191
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)
1211
1195
} else {
1212
1196
this. handle_not_found ( )
1213
1197
}
0 commit comments