close file silently if the file is read only

This commit is contained in:
Christian Poveda 2019-12-27 08:34:35 -05:00
parent 0f1dec05dd
commit dc4b8ac2e6
No known key found for this signature in database
GPG key ID: 27525EF5E7420A50

View file

@ -15,6 +15,7 @@ use shims::time::system_time_to_duration;
#[derive(Debug)]
pub struct FileHandle {
file: File,
read_only: bool,
}
pub struct FileHandler {
@ -56,10 +57,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
if (o_rdonly | o_wronly | o_rdwr) & !0b11 != 0 {
throw_unsup_format!("Access mode flags on this platform are unsupported");
}
let mut read_only = false;
// Now we check the access mode
let access_mode = flag & 0b11;
if access_mode == o_rdonly {
read_only = true;
options.read(true);
} else if access_mode == o_wronly {
options.write(true);
@ -105,7 +109,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let fd = options.open(&path).map(|file| {
let mut fh = &mut this.machine.file_handler;
fh.low += 1;
fh.handles.insert(fh.low, FileHandle { file }).unwrap_none();
fh.handles.insert(fh.low, FileHandle { file, read_only }).unwrap_none();
fh.low
});
@ -148,6 +152,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let fd = this.read_scalar(fd_op)?.to_i32()?;
if let Some(handle) = this.machine.file_handler.handles.remove(&fd) {
if handle.read_only {
return Ok(0);
}
// `File::sync_all` does the checks that are done when closing a file. We do this to
// to handle possible errors correctly.
let result = this.try_unwrap_io_result(handle.file.sync_all().map(|_| 0i32));