diff --git a/fs/vfs/fs_close.c b/fs/vfs/fs_close.c index 6425a2ce8b..f490b2e389 100644 --- a/fs/vfs/fs_close.c +++ b/fs/vfs/fs_close.c @@ -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; } diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h index 4640424e81..0855d95588 100644 --- a/include/nuttx/fs/fs.h +++ b/include/nuttx/fs/fs.h @@ -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 *