Skip to content

Commit e352d4f

Browse files
committed
Finish fixing Windows host support
1 parent c01bc14 commit e352d4f

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

src/shims/fs.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
379379
&& cmd == this.eval_libc_i32("F_FULLFSYNC")?
380380
{
381381
let &[_, _] = check_arg_count(args)?;
382-
if let Some(FileHandle { file, writable: _ }) = this.machine.file_handler.handles.get_mut(&fd) {
383-
let result = file.sync_all();
384-
this.try_unwrap_io_result(result.map(|_| 0i32))
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+
}
385392
} else {
386393
this.handle_not_found()
387394
}
@@ -1128,6 +1135,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
11281135
if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.get_mut(&fd) {
11291136
if !*writable && cfg!(windows) {
11301137
// 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)
11311139
Ok(0i32)
11321140
} else {
11331141
let result = file.sync_all();
@@ -1147,6 +1155,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
11471155
if let Some(FileHandle { file, writable }) = this.machine.file_handler.handles.get_mut(&fd) {
11481156
if !*writable && cfg!(windows) {
11491157
// 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)
11501159
Ok(0i32)
11511160
} else {
11521161
let result = file.sync_data();
@@ -1187,11 +1196,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
11871196
return Ok(-1);
11881197
}
11891198

1190-
if let Some(FileHandle { file, writable: _ }) = this.machine.file_handler.handles.get_mut(&fd) {
1191-
// In the interest of host compatibility, we conservatively ignore
1192-
// offset, nbytes, and flags, and sync the entire file.
1193-
let result = file.sync_data();
1194-
this.try_unwrap_io_result(result.map(|_| 0i32))
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+
}
11951211
} else {
11961212
this.handle_not_found()
11971213
}

tests/run-pass/fs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ fn test_file_sync() {
192192
file.sync_data().unwrap();
193193
file.sync_all().unwrap();
194194

195+
// Test that we can call sync_data and sync_all on a file opened for reading.
196+
let file = File::open(&path).unwrap();
197+
file.sync_data().unwrap();
198+
file.sync_all().unwrap();
199+
195200
remove_file(&path).unwrap();
196201
}
197202

tests/run-pass/libc.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,22 @@ fn test_sync_file_range() {
5555
let bytes = b"Hello, World!\n";
5656
file.write(bytes).unwrap();
5757

58-
// Test calling sync_file_range on a file.
59-
let result = unsafe {
58+
// Test calling sync_file_range on the file.
59+
let result_1 = unsafe {
60+
libc::sync_file_range(
61+
file.as_raw_fd(),
62+
0,
63+
0,
64+
libc::SYNC_FILE_RANGE_WAIT_BEFORE
65+
| libc::SYNC_FILE_RANGE_WRITE
66+
| libc::SYNC_FILE_RANGE_WAIT_AFTER,
67+
)
68+
};
69+
drop(file);
70+
71+
// Test calling sync_file_range on a file opened for reading.
72+
let file = File::open(&path).unwrap();
73+
let result_2 = unsafe {
6074
libc::sync_file_range(
6175
file.as_raw_fd(),
6276
0,
@@ -67,8 +81,10 @@ fn test_sync_file_range() {
6781
)
6882
};
6983
drop(file);
84+
7085
remove_file(&path).unwrap();
71-
assert_eq!(result, 0);
86+
assert_eq!(result_1, 0);
87+
assert_eq!(result_2, 0);
7288
}
7389

7490
fn test_mutex_libc_init_recursive() {

0 commit comments

Comments
 (0)