Fixes insert_file()
[akaros.git] / kern / src / vfs.c
index 38560c8..bc43859 100644 (file)
@@ -2226,7 +2226,8 @@ static int __claim_fd(struct files_struct *open_files, int file_desc)
                return -ENFILE; /* Should never really happen. Here to catch bugs. */
 
        SET_BITMASK_BIT(open_files->open_fds->fds_bits, file_desc);
-       assert(file_desc < open_files->max_files && open_files->fd[0].fd_file == 0);
+       assert(file_desc < open_files->max_files &&
+              open_files->fd[file_desc].fd_file == 0);
        if (file_desc >= open_files->next_fd)
                open_files->next_fd = file_desc + 1;
        return 0;
@@ -2244,11 +2245,23 @@ int claim_fd(struct files_struct *open_files, int file_desc)
 
 /* Inserts the file in the files_struct, returning the corresponding new file
  * descriptor, or an error code.  We start looking for open fds from low_fd. */
-int insert_file(struct files_struct *open_files, struct file *file, int low_fd)
+int insert_file(struct files_struct *open_files, struct file *file, int low_fd,
+                bool must)
 {
-       int slot;
+       int slot, ret;
        spin_lock(&open_files->lock);
-       slot = __get_fd(open_files, low_fd);
+       if (must) {
+               ret = __claim_fd(open_files, low_fd);
+               if (ret < 0) {
+                       spin_unlock(&open_files->lock);
+                       return ret;
+               }
+               assert(!ret);   /* issues with claim_fd returning status, not the fd */
+               slot = low_fd;
+       } else {
+               slot = __get_fd(open_files, low_fd);
+       }
+
        if (slot < 0) {
                spin_unlock(&open_files->lock);
                return slot;