Skip to content

Commit eda35e1

Browse files
committed
Add methods to FileHandler
1 parent 1de9d10 commit eda35e1

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/shims/fs.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ pub struct FileHandler {
2323
low: i32,
2424
}
2525

26+
impl FileHandler {
27+
fn next_fd(&self) -> i32 {
28+
self.low + 1
29+
}
30+
31+
fn register_fd(&mut self, fd: i32, handle: FileHandle) {
32+
self.low = fd;
33+
self.handles.insert(fd, handle).unwrap_none();
34+
}
35+
}
36+
2637
impl Default for FileHandler {
2738
fn default() -> Self {
2839
FileHandler {
@@ -107,10 +118,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
107118
let path = this.read_os_str_from_c_str(this.read_scalar(path_op)?.not_undef()?)?;
108119

109120
let fd = options.open(&path).map(|file| {
110-
let mut fh = &mut this.machine.file_handler;
111-
fh.low += 1;
112-
fh.handles.insert(fh.low, FileHandle { file, writable }).unwrap_none();
113-
fh.low
121+
let fh = &mut this.machine.file_handler;
122+
let fd = fh.next_fd();
123+
fh.register_fd(fd, FileHandle { file, writable });
124+
fd
114125
});
115126

116127
this.try_unwrap_io_result(fd)
@@ -153,9 +164,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
153164
None => return this.handle_not_found(),
154165
};
155166
let fd_result = file_result.map(|duplicated| {
156-
let new_fd = std::cmp::max(fh.low + 1, arg);
157-
fh.low = new_fd;
158-
fh.handles.insert(fh.low, FileHandle { file: duplicated, writable }).unwrap_none();
167+
let new_fd = std::cmp::max(fh.next_fd(), arg);
168+
fh.register_fd(new_fd, FileHandle { file: duplicated, writable });
159169
new_fd
160170
});
161171
this.try_unwrap_io_result(fd_result)

0 commit comments

Comments
 (0)