mirror of
https://github.com/rust-lang/rust.git
synced 2026-06-02 06:28:20 +03:00
close file silently if the file is read only
This commit is contained in:
+8
-1
@@ -15,6 +15,7 @@
|
||||
#[derive(Debug)]
|
||||
pub struct FileHandle {
|
||||
file: File,
|
||||
read_only: bool,
|
||||
}
|
||||
|
||||
pub struct FileHandler {
|
||||
@@ -56,10 +57,13 @@ fn open(
|
||||
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 @@ fn open(
|
||||
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 @@ fn close(&mut self, fd_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
|
||||
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));
|
||||
|
||||
Reference in New Issue
Block a user