]> git.lizzy.rs Git - rust.git/commitdiff
Add methods to FileHandler
authorDavid Cook <divergentdave@gmail.com>
Fri, 31 Jan 2020 00:38:30 +0000 (18:38 -0600)
committerDavid Cook <divergentdave@gmail.com>
Tue, 18 Feb 2020 04:24:33 +0000 (22:24 -0600)
src/shims/fs.rs

index d17ef79f0eed3c8423129a9736f7f58d2e2315e8..792996f678d4e04dc60f9fd5adc3e597ada729e1 100644 (file)
@@ -23,6 +23,17 @@ pub struct FileHandler {
     low: i32,
 }
 
+impl FileHandler {
+    fn next_fd(&self) -> i32 {
+        self.low + 1
+    }
+
+    fn register_fd(&mut self, fd: i32, handle: FileHandle) {
+        self.low = fd;
+        self.handles.insert(fd, handle).unwrap_none();
+    }
+}
+
 impl Default for FileHandler {
     fn default() -> Self {
         FileHandler {
@@ -107,10 +118,10 @@ fn open(
         let path = this.read_os_str_from_c_str(this.read_scalar(path_op)?.not_undef()?)?;
 
         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, writable }).unwrap_none();
-            fh.low
+            let fh = &mut this.machine.file_handler;
+            let fd = fh.next_fd();
+            fh.register_fd(fd, FileHandle { file, writable });
+            fd
         });
 
         this.try_unwrap_io_result(fd)
@@ -153,9 +164,8 @@ fn fcntl(
                 None => return this.handle_not_found(),
             };
             let fd_result = file_result.map(|duplicated| {
-                let new_fd = std::cmp::max(fh.low + 1, arg);
-                fh.low = new_fd;
-                fh.handles.insert(fh.low, FileHandle { file: duplicated, writable }).unwrap_none();
+                let new_fd = std::cmp::max(fh.next_fd(), arg);
+                fh.register_fd(new_fd, FileHandle { file: duplicated, writable });
                 new_fd
             });
             this.try_unwrap_io_result(fd_result)