forked from nuttx/nuttx-update
fs/vfs: Add nx_close function
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
parent
7980673b59
commit
6418d13b92
2 changed files with 75 additions and 51 deletions
|
@ -56,14 +56,63 @@
|
|||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nx_close
|
||||
*
|
||||
* Description:
|
||||
* nx_close() is similar to the standard 'close' interface except that is
|
||||
* not a cancellation point and it does not modify the errno variable.
|
||||
*
|
||||
* nx_close() is an internal NuttX interface and should not be called from
|
||||
* applications.
|
||||
*
|
||||
* Returned Value:
|
||||
* The new file descriptor is returned on success; a negated errno value is
|
||||
* returned on any failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int nx_close(int fd)
|
||||
{
|
||||
/* Did we get a valid file descriptor? */
|
||||
|
||||
if (fd >= CONFIG_NFILE_DESCRIPTORS)
|
||||
{
|
||||
/* Close a socket descriptor */
|
||||
|
||||
#ifdef CONFIG_NET
|
||||
if (fd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS))
|
||||
{
|
||||
return net_close(fd);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
return -EBADF;
|
||||
}
|
||||
}
|
||||
|
||||
/* Close the driver or mountpoint. NOTES: (1) there is no
|
||||
* exclusion mechanism here, the driver or mountpoint must be
|
||||
* able to handle concurrent operations internally, (2) The driver
|
||||
* may have been opened numerous times (for different file
|
||||
* descriptors) and must also handle being closed numerous times.
|
||||
* (3) for the case of the mountpoint, we depend on the close
|
||||
* methods bing identical in signature and position in the operations
|
||||
* vtable.
|
||||
*/
|
||||
|
||||
return files_close(fd);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: close
|
||||
*
|
||||
* Description:
|
||||
* close() closes a file descriptor, so that it no longer refers to any
|
||||
* file and may be reused. Any record locks (see fcntl(2)) held on the file
|
||||
* it was associated with, and owned by the process, are removed (regardless
|
||||
* of the file descriptor that was used to obtain the lock).
|
||||
* it was associated with, and owned by the process, are removed
|
||||
* (regardless of the file descriptor that was used to obtain the lock).
|
||||
*
|
||||
* If fd is the last copy of a particular file descriptor the resources
|
||||
* associated with it are freed; if the descriptor was the last reference
|
||||
|
@ -81,64 +130,19 @@
|
|||
|
||||
int close(int fd)
|
||||
{
|
||||
int errcode;
|
||||
int ret;
|
||||
|
||||
/* close() is a cancellation point */
|
||||
|
||||
enter_cancellation_point();
|
||||
|
||||
/* Did we get a valid file descriptor? */
|
||||
|
||||
if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
|
||||
{
|
||||
/* Close a socket descriptor */
|
||||
|
||||
#ifdef CONFIG_NET
|
||||
if ((unsigned int)fd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS))
|
||||
{
|
||||
ret = net_close(fd);
|
||||
if (ret < 0)
|
||||
{
|
||||
errcode = -ret;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
leave_cancellation_point();
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
errcode = EBADF;
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
|
||||
/* Close the driver or mountpoint. NOTES: (1) there is no
|
||||
* exclusion mechanism here, the driver or mountpoint must be
|
||||
* able to handle concurrent operations internally, (2) The driver
|
||||
* may have been opened numerous times (for different file
|
||||
* descriptors) and must also handle being closed numerous times.
|
||||
* (3) for the case of the mountpoint, we depend on the close
|
||||
* methods bing identical in signature and position in the operations
|
||||
* vtable.
|
||||
*/
|
||||
|
||||
ret = files_close(fd);
|
||||
ret = nx_close(fd);
|
||||
if (ret < 0)
|
||||
{
|
||||
/* An error occurred while closing the driver */
|
||||
|
||||
errcode = -ret;
|
||||
goto errout;
|
||||
set_errno(-ret);
|
||||
ret = ERROR;
|
||||
}
|
||||
|
||||
leave_cancellation_point();
|
||||
return OK;
|
||||
|
||||
errout:
|
||||
set_errno(errcode);
|
||||
leave_cancellation_point();
|
||||
return ERROR;
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -77,6 +77,7 @@
|
|||
# else
|
||||
# define _NX_OPEN nx_open
|
||||
# endif
|
||||
# define _NX_CLOSE(f) nx_close(f)
|
||||
# define _NX_READ(f,b,s) nx_read(f,b,s)
|
||||
# define _NX_WRITE(f,b,s) nx_write(f,b,s)
|
||||
# define _NX_SEEK(f,o,w) nx_seek(f,o,w)
|
||||
|
@ -89,6 +90,7 @@
|
|||
# else
|
||||
# define _NX_OPEN open
|
||||
# endif
|
||||
# define _NX_CLOSE(f) close(f)
|
||||
# define _NX_READ(f,b,s) read(f,b,s)
|
||||
# define _NX_WRITE(f,b,s) write(f,b,s)
|
||||
# define _NX_SEEK(f,o,w) lseek(f,o,w)
|
||||
|
@ -860,6 +862,24 @@ int file_detach(int fd, FAR struct file *filep);
|
|||
|
||||
int file_close(FAR struct file *filep);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nx_close
|
||||
*
|
||||
* Description:
|
||||
* nx_close() is similar to the standard 'close' interface except that is
|
||||
* not a cancellation point and it does not modify the errno variable.
|
||||
*
|
||||
* nx_close() is an internal NuttX interface and should not be called from
|
||||
* applications.
|
||||
*
|
||||
* Returned Value:
|
||||
* The new file descriptor is returned on success; a negated errno value is
|
||||
* returned on any failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int nx_close(int fd);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: open_blockdriver
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue