Donny9 commented on code in PR #16499:
URL: https://github.com/apache/nuttx/pull/16499#discussion_r2135964572


##########
fs/inode/fs_files.c:
##########
@@ -445,90 +454,120 @@ void files_putlist(FAR struct filelist *list)
     {
       for (j = CONFIG_NFILE_DESCRIPTORS_PER_BLOCK - 1; j >= 0; j--)
         {
-          file_close(&list->fl_files[i][j]);
+          fdlist_close(list, i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + j);
         }
 
       if (i != 0)
         {
-          fs_heap_free(list->fl_files[i]);
+          fs_heap_free(list->fl_fds[i]);
         }
     }
 
-  if (list->fl_files != &list->fl_prefile)
+  if (list->fl_fds != &list->fl_prefd)
     {
-      fs_heap_free(list->fl_files);
+      fs_heap_free(list->fl_fds);
     }
 }
 
 /****************************************************************************
- * Name: files_countlist
+ * Name: fdlist_count
  *
  * Description:
- *   Get file count from file list.
+ *   Get file descriptor count from file list.
  *
  * Input Parameters:
- *   list - Pointer to the file list structure.
+ *   list - Pointer to the file descriptor list structure.
  *
  * Returned Value:
  *   file count of file list.
  *
  ****************************************************************************/
 
-int files_countlist(FAR struct filelist *list)
+int fdlist_count(FAR struct fdlist *list)
 {
   return list->fl_rows * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK;
 }
 
 /****************************************************************************
- * Name: files_fget
+ * Name: fdlist_get2
  *
  * Description:
- *   Get the instance of struct file from file list by file descriptor.
+ *   Given a file descriptor, return the corresponding instance of struct
+ *   fd and filep.
  *
  * Input Parameters:
- *   list - The list of files for a task.
- *   fd   - A valid descriptor between 0 and files_countlist(list).
+ *   list  - Pointer to the file descriptor list structure.
+ *   fd    - The file descriptor
+ *   filep - The location to return the struct file instance
+ *   fdp   - The location to return the struct fd instance
  *
  * Returned Value:
- *   Pointer to file structure of list[fd].
+ *   Return the pointer to file structure of list[fd] when list[fd].f_file
+ *   is valid, otherwise, a null pointer is returned.
  *
  ****************************************************************************/
 
-FAR struct file *files_fget(FAR struct filelist *list, int fd)
+int fdlist_get2(FAR struct fdlist *list, int fd,
+                FAR struct file **filep, FAR struct fd **fdp)
 {
-  return files_fget_by_index(list, fd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK,
-                             fd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK, NULL);
+  if (list == NULL)
+    {
+      return -EAGAIN;

Review Comment:
   Done~



##########
fs/inode/fs_files.c:
##########
@@ -839,20 +934,39 @@ int file_put(FAR struct file *filep)
  *
  ****************************************************************************/
 
-int nx_dup2_from_tcb(FAR struct tcb_s *tcb, int fd1, int fd2)
+int fdlist_dup2(FAR struct fdlist *list, int fd1, int fd2)
 {
-  return nx_dup3_from_tcb(tcb, fd1, fd2, 0);
+  /* If fd1 is a valid file descriptor, and fd1 == fd2, then dup2() does
+   * nothing, and returns fd2.
+   */
+
+  if (fd1 == fd2)
+    {
+      FAR struct file *filep;
+
+      if (file_get(fd1, &filep) < 0)

Review Comment:
   Done~



##########
fs/inode/fs_files.c:
##########
@@ -676,91 +789,68 @@ int files_duplist(FAR struct filelist *plist, FAR struct 
filelist *clist,
               continue;
             }
 
-          ret = files_extend(clist, i + 1);
+          ret = fdlist_extend(clist, i + 1);
           if (ret < 0)
             {
               file_put(filep);
               return ret;
             }
 
-          /* Yes... duplicate it for the child, include O_CLOEXEC flag. */
+          /* Assign filep to the child's descriptor list. Omit the flags */
 
-          filep2 = files_fget_by_index(clist, i, j, &new);
-          ret = file_dup2(filep, filep2);
-          file_put(filep2);
+          fdlist_install(clist, fd, filep, 0);
           file_put(filep);
-          if (ret < 0)
-            {
-              if (new)
-                {
-                  file_put(filep2);
-                }
-
-              return ret;
-            }
         }
     }
 
   return OK;
 }
 
 /****************************************************************************
- * Name: file_get
+ * Name: file_get2
  *
  * Description:
  *   Given a file descriptor, return the corresponding instance of struct
- *   file.
+ *   fd and filep
  *
  * Input Parameters:
  *   fd    - The file descriptor
  *   filep - The location to return the struct file instance
+ *   fdp   - The location to return the struct fd instance
  *
  * Returned Value:
  *   Zero (OK) is returned on success; a negated errno value is returned on
  *   any failure.
  *
  ****************************************************************************/
 
-int file_get(int fd, FAR struct file **filep)
+int file_get2(int fd, FAR struct file **filep, FAR struct fd **fdp)
 {
-  FAR struct filelist *list;
-
-#ifdef CONFIG_FDCHECK
-  fd = fdcheck_restore(fd);
-#endif
-
-  *filep = NULL;
+  FAR struct fdlist *list;
+  FAR struct file *filep1;
+  FAR struct fd *fdp1;
+  int ret;
 
-  list = nxsched_get_files();
+  list = nxsched_get_fdlist();
 
-  /* The file list can be NULL under two cases:  (1) One is an obscure
-   * cornercase:  When memory management debug output is enabled.  Then
-   * there may be attempts to write to stdout from malloc before the group
-   * data has been allocated.  The other other is (2) if this is a kernel
-   * thread.  Kernel threads have no allocated file descriptors.
+  /* The descriptor is in a valid range to file descriptor... Get the
+   * thread-specific file list.
    */
 
-  if (list == NULL)
+  ret = fdlist_get2(list, fd, &filep1, &fdp1);

Review Comment:
   Done~



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to