Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

There is an internal OS interface called file_detach() that is prototyped and described in the header file include/nuttx/fs/fs.h:

Code Block

    /****************************************************************************
   * Name: file_detach

...


   *
   * Description:
   *   This function is used to device drivers to create a task-independent
   *   handle to an entity in the file system.  file_detach() duplicates

...

 the
   *   'struct file' that underlies the file descriptor, then closes the

...

 file
   *   descriptor.
   *
   *   This function will fail if fd is not a valid file descriptor.  In
   *   particular, it will fail if fd is a socket descriptor.
   *
   * Input Parameters:
   *   fd    - The file descriptor to be detached.  This descriptor will be
   *           closed and invalid if the file was successfully detached.
   *   filep - A pointer to a user provided memory location in which to
   *           received the duplicated, detached file structure.
   *
   * Returned Value:
   *   Zero (OK) is returned on success; A negated errno value is returned on
   *   any failure to indicate the nature of the failure.
   *
   ****************************************************************************/

...

Code Block
    
    #if CONFIG_NFILE_DESCRIPTORS > 0
    int file_detach(int fd, FAR struct file *filep);
    #endif

In order to use file_detach() you would do the following:

...

There is also a file_close_detached() internal OS function that you can use to recover resources if you no longer need the contained driver instance:

Code Block

    /****************************************************************************
   * Name: file_close_detached

...


   *
   * Description:
   *   Close a file that was previously detached with file_detach().

...


   *
   * Input Parameters:
   *   filep - A pointer to a user provided memory location containing the
   *           open file data returned by file_detach().

...


   *
   * Returned Value:
   *   Zero (OK) is returned on success; A negated errno value is returned

...

 on
   *   any failure to indicate the nature of the failure.
   *
   ****************************************************************************/

...

Code Block
    
    int file_close_detached(FAR struct file *filep);

...