fs/vfs: Change the return value of internal function fs_getfilep(). It no longer sets the errno variable but, rather, returns errors in the same manner as other internal OS functions.

This commit is contained in:
Gregory Nutt 2017-10-11 08:39:19 -06:00
parent 2f1894f2b4
commit 536e4d7fa6
20 changed files with 313 additions and 275 deletions

View File

@ -99,10 +99,8 @@ static void aio_fsync_worker(FAR void *arg)
ret = file_fsync(aioc->u.aioc_filep); ret = file_fsync(aioc->u.aioc_filep);
if (ret < 0) if (ret < 0)
{ {
int errcode = get_errno(); ferr("ERROR: file_fsync failed: %d\n", ret);
ferr("ERROR: fsync failed: %d\n", errcode); aiocbp->aio_result = ret;
DEBUGASSERT(errcode > 0);
aiocbp->aio_result = -errcode;
} }
else else
{ {

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* fs/aio/aioc_contain.c * fs/aio/aioc_contain.c
* *
* Copyright (C) 2014 Gregory Nutt. All rights reserved. * Copyright (C) 2014, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -86,6 +86,7 @@ FAR struct aio_container_s *aio_contain(FAR struct aiocb *aiocbp)
#ifdef CONFIG_PRIORITY_INHERITANCE #ifdef CONFIG_PRIORITY_INHERITANCE
struct sched_param param; struct sched_param param;
#endif #endif
int ret;
#if defined(AIO_HAVE_FILEP) && defined(AIO_HAVE_PSOCK) #if defined(AIO_HAVE_FILEP) && defined(AIO_HAVE_PSOCK)
if (aiocbp->aio_fildes < CONFIG_NFILE_DESCRIPTORS) if (aiocbp->aio_fildes < CONFIG_NFILE_DESCRIPTORS)
@ -94,13 +95,13 @@ FAR struct aio_container_s *aio_contain(FAR struct aiocb *aiocbp)
{ {
/* Get the file structure corresponding to the file descriptor. */ /* Get the file structure corresponding to the file descriptor. */
u.filep = fs_getfilep(aiocbp->aio_fildes); ret = fs_getfilep(aiocbp->aio_fildes, &u.filep);
if (!u.filep) if (ret < 0)
{ {
/* The errno value has already been set */ goto errout;
return NULL;
} }
DEBUGASSERT(u.filep != NULL);
} }
#endif #endif
#if defined(AIO_HAVE_FILEP) && defined(AIO_HAVE_PSOCK) #if defined(AIO_HAVE_FILEP) && defined(AIO_HAVE_PSOCK)
@ -111,12 +112,14 @@ FAR struct aio_container_s *aio_contain(FAR struct aiocb *aiocbp)
/* Get the socket structure corresponding to the socket descriptor */ /* Get the socket structure corresponding to the socket descriptor */
u.psock = sockfd_socket(aiocbp->aio_fildes); u.psock = sockfd_socket(aiocbp->aio_fildes);
if (!u.psock) if (u.psock == NULL)
{ {
/* Does not set the errno. EBADF is the most likely explanation. */ /* Does not return error information. EBADF is the most likely
* explanation.
*/
set_errno(EBADF); ret = -EBADF;
return NULL; goto errout;
} }
} }
#endif #endif
@ -146,6 +149,10 @@ FAR struct aio_container_s *aio_contain(FAR struct aiocb *aiocbp)
dq_addlast(&aioc->aioc_link, &g_aio_pending); dq_addlast(&aioc->aioc_link, &g_aio_pending);
aio_unlock(); aio_unlock();
return aioc; return aioc;
errout:
set_errno(-ret);
return NULL;
} }
/**************************************************************************** /****************************************************************************

View File

@ -55,10 +55,6 @@
#define DUP_ISOPEN(filep) (filep->f_inode != NULL) #define DUP_ISOPEN(filep) (filep->f_inode != NULL)
/****************************************************************************
* Private Functions
****************************************************************************/
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
@ -128,24 +124,27 @@ int fs_dupfd(int fd, int minfd)
/* Get the file structure corresponding to the file descriptor. */ /* Get the file structure corresponding to the file descriptor. */
filep = fs_getfilep(fd); ret = fs_getfilep(fd, &filep);
if (!filep) if (ret < 0)
{ {
/* The errno value has already been set */ goto errout;
return ERROR;
} }
DEBUGASSERT(filep != NULL);
/* Let file_dup() do the real work */ /* Let file_dup() do the real work */
ret = file_dup(filep, minfd); ret = file_dup(filep, minfd);
if (ret < 0) if (ret < 0)
{ {
set_errno(-ret); goto errout;
return ERROR;
} }
return OK; return OK;
errout:
set_errno(-ret);
return ERROR;
} }
#endif /* CONFIG_NFILE_DESCRIPTORS > 0 */ #endif /* CONFIG_NFILE_DESCRIPTORS > 0 */

View File

@ -1,7 +1,8 @@
/**************************************************************************** /****************************************************************************
* fs/vfs/fs_dupfd2.c * fs/vfs/fs_dupfd2.c
* *
* Copyright (C) 2007-2009, 2011-2014 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009, 2011-2014, 2017 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -84,27 +85,30 @@ int dup2(int fd1, int fd2)
#endif #endif
{ {
FAR struct file *filep1; FAR struct file *filep1;
FAR struct file *filep2; FAR struct file *filep2 = NULL;
int ret; int ret;
/* Get the file structures corresponding to the file descriptors. */ /* Get the file structures corresponding to the file descriptors. */
filep1 = fs_getfilep(fd1); ret = fs_getfilep(fd1, &filep1);
filep2 = fs_getfilep(fd2); if (ret >= 0)
if (!filep1 || !filep2)
{ {
/* The errno value has already been set */ ret = fs_getfilep(fd2, &filep2);
return ERROR;
} }
if (ret < 0)
{
goto errout;
}
DEBUGASSERT(filep1 != NULL && filep2 != NULL);
/* Verify that fd1 is a valid, open file descriptor */ /* Verify that fd1 is a valid, open file descriptor */
if (!DUP_ISOPEN(filep1)) if (!DUP_ISOPEN(filep1))
{ {
set_errno(EBADF); ret = -EBADF;
return ERROR; goto errout;
} }
/* Handle a special case */ /* Handle a special case */
@ -119,11 +123,14 @@ int dup2(int fd1, int fd2)
ret = file_dup2(filep1, filep2); ret = file_dup2(filep1, filep2);
if (ret < 0) if (ret < 0)
{ {
set_errno(-ret); goto errout;
return ERROR;
} }
return OK; return OK;
errout:
set_errno(-ret);
return ERROR;
} }
#endif /* CONFIG_NFILE_DESCRIPTORS > 0 */ #endif /* CONFIG_NFILE_DESCRIPTORS > 0 */

View File

@ -254,21 +254,17 @@ int fcntl(int fd, int cmd, ...)
{ {
/* Get the file structure corresponding to the file descriptor. */ /* Get the file structure corresponding to the file descriptor. */
filep = fs_getfilep(fd); ret = fs_getfilep(fd, &filep);
if (!filep) if (ret >= 0)
{ {
/* The errno value has already been set */ DEBUGASSERT(filep != NULL);
va_end(ap); /* Let file_vfcntl() do the real work. The errno is not set on
leave_cancellation_point(); * failures.
return ERROR; */
ret = file_vfcntl(filep, cmd, ap);
} }
/* Let file_vfcntl() do the real work. The errno is not set on
* failures.
*/
ret = file_vfcntl(filep, cmd, ap);
} }
else else
#endif #endif

View File

@ -67,17 +67,16 @@ static inline int fs_checkfd(FAR struct tcb_s *tcb, int fd, int oflags)
{ {
FAR struct file *filep; FAR struct file *filep;
FAR struct inode *inode; FAR struct inode *inode;
int ret;
DEBUGASSERT(tcb && tcb->group); DEBUGASSERT(tcb && tcb->group);
/* Get the file structure corresponding to the file descriptor. */ /* Get the file structure corresponding to the file descriptor. */
filep = fs_getfilep(fd); ret = fs_getfilep(fd, &filep);
if (!filep) if (ret < 0)
{ {
/* The errno value has already been set */ return ret;
return ERROR;
} }
/* Get the inode associated with the file descriptor. This should /* Get the inode associated with the file descriptor. This should

View File

@ -84,8 +84,8 @@ int fstat(int fd, FAR struct stat *buf)
{ {
/* No networking... it is a bad descriptor in any event */ /* No networking... it is a bad descriptor in any event */
set_errno(EBADF); ret = -EBADF;
return ERROR; goto errout;
} }
/* The descriptor is in a valid range to file descriptor... do the /* The descriptor is in a valid range to file descriptor... do the
@ -93,14 +93,14 @@ int fstat(int fd, FAR struct stat *buf)
* fs_getfilep() will set the errno variable. * fs_getfilep() will set the errno variable.
*/ */
filep = fs_getfilep(fd); ret = fs_getfilep(fd, &filep);
if (filep == NULL) if (ret < 0)
{ {
/* The errno value has already been set */ goto errout;
return ERROR;
} }
DEBUGASSERT(filep != NULL);
/* Get the inode from the file structure */ /* Get the inode from the file structure */
inode = filep->f_inode; inode = filep->f_inode;
@ -135,13 +135,14 @@ int fstat(int fd, FAR struct stat *buf)
/* Check if the fstat operation was successful */ /* Check if the fstat operation was successful */
if (ret < 0) if (ret >= 0)
{ {
set_errno(-ret); /* Successfully fstat'ed the file */
return ERROR;
return OK;
} }
/* Successfully fstat'ed the file */ errout:
set_errno(-ret);
return OK; return ERROR;
} }

View File

@ -81,8 +81,8 @@ int fstatfs(int fd, FAR struct statfs *buf)
{ {
/* It is a bad, out-of-range descriptor */ /* It is a bad, out-of-range descriptor */
set_errno(EBADF); ret = -EBADF;
return ERROR; goto errout;
} }
/* The descriptor is in a valid range to file descriptor... do the /* The descriptor is in a valid range to file descriptor... do the
@ -90,14 +90,14 @@ int fstatfs(int fd, FAR struct statfs *buf)
* fs_getfilep() will set the errno variable. * fs_getfilep() will set the errno variable.
*/ */
filep = fs_getfilep(fd); ret = fs_getfilep(fd, &filep);
if (filep == NULL) if (ret < 0)
{ {
/* The errno value has already been set */ goto errout;
return ERROR;
} }
DEBUGASSERT(filep != NULL);
/* Get the inode from the file structure */ /* Get the inode from the file structure */
inode = filep->f_inode; inode = filep->f_inode;
@ -144,13 +144,14 @@ int fstatfs(int fd, FAR struct statfs *buf)
/* Check if the fstat operation was successful */ /* Check if the fstat operation was successful */
if (ret < 0) if (ret >= 0)
{ {
set_errno(-ret); /* Successfully statfs'ed the file */
return ERROR;
return OK;
} }
/* Successfully statfs'ed the file */ errout:
set_errno(-ret);
return OK; return ERROR;
} }

View File

@ -1,7 +1,8 @@
/**************************************************************************** /****************************************************************************
* fs/vfs/fs_fsync.c * fs/vfs/fs_fsync.c
* *
* Copyright (C) 2007-2009, 2013-2014, 2016 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009, 2013-2014, 2016-2017 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -61,22 +62,20 @@
* *
* Description: * Description:
* Equivalent to the standard fsync() function except that is accepts a * Equivalent to the standard fsync() function except that is accepts a
* struct file instance instead of a file descriptor. Currently used * struct file instance instead of a file descriptor and it does not set
* only by aio_fsync(); * the errno variable.
* *
****************************************************************************/ ****************************************************************************/
int file_fsync(FAR struct file *filep) int file_fsync(FAR struct file *filep)
{ {
struct inode *inode; struct inode *inode;
int ret;
/* Was this file opened for write access? */ /* Was this file opened for write access? */
if ((filep->f_oflags & O_WROK) == 0) if ((filep->f_oflags & O_WROK) == 0)
{ {
ret = EBADF; return -EBADF;
goto errout;
} }
/* Is this inode a registered mountpoint? Does it support the /* Is this inode a registered mountpoint? Does it support the
@ -88,23 +87,12 @@ int file_fsync(FAR struct file *filep)
if (!inode || !INODE_IS_MOUNTPT(inode) || if (!inode || !INODE_IS_MOUNTPT(inode) ||
!inode->u.i_mops || !inode->u.i_mops->sync) !inode->u.i_mops || !inode->u.i_mops->sync)
{ {
ret = EINVAL; return -EINVAL;
goto errout;
} }
/* Yes, then tell the mountpoint to sync this file */ /* Yes, then tell the mountpoint to sync this file */
ret = inode->u.i_mops->sync(filep); return inode->u.i_mops->sync(filep);
if (ret >= 0)
{
return OK;
}
ret = -ret;
errout:
set_errno(ret);
return ERROR;
} }
/**************************************************************************** /****************************************************************************
@ -126,20 +114,29 @@ int fsync(int fd)
/* Get the file structure corresponding to the file descriptor. */ /* Get the file structure corresponding to the file descriptor. */
filep = fs_getfilep(fd); ret = fs_getfilep(fd, &filep);
if (!filep) if (ret < 0)
{ {
/* The errno value has already been set */ goto errout;
leave_cancellation_point();
return ERROR;
} }
DEBUGASSERT(filep != NULL);
/* Perform the fsync operation */ /* Perform the fsync operation */
ret = file_fsync(filep); ret = file_fsync(filep);
if (ret < 0)
{
goto errout;
}
leave_cancellation_point(); leave_cancellation_point();
return ret; return ret;
errout:
leave_cancellation_point();
set_errno(-ret);
return ERROR;
} }
#endif /* !CONFIG_DISABLE_MOUNTPOINT */ #endif /* !CONFIG_DISABLE_MOUNTPOINT */

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* fs/vfs/fs_getfilep.c * fs/vfs/fs_getfilep.c
* *
* Copyright (C) 2014, 2016 Gregory Nutt. All rights reserved. * Copyright (C) 2014, 2016-2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -48,10 +48,6 @@
#include "inode/inode.h" #include "inode/inode.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
@ -64,25 +60,26 @@
* file. NOTE that this function will currently fail if it is provided * file. NOTE that this function will currently fail if it is provided
* with a socket descriptor. * with a socket descriptor.
* *
* Parameters: * Input Parameters:
* fd - The file descriptor * fd - The file descriptor
* filep - The location to return the struct file instance
* *
* Return: * Returned Value:
* A point to the corresponding struct file instance is returned on * Zero (OK) is returned on success; a negated errno value is returned on
* success. On failure, NULL is returned and the errno value is * any failure.
* set appropriately (EBADF).
* *
****************************************************************************/ ****************************************************************************/
FAR struct file *fs_getfilep(int fd) int fs_getfilep(int fd, FAR struct file **filep)
{ {
FAR struct filelist *list; FAR struct filelist *list;
int errcode;
DEBUGASSERT(filep != NULL);
*filep = (FAR struct file *)NULL;
if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS) if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
{ {
errcode = EBADF; return -EBADF;
goto errout;
} }
/* The descriptor is in a valid range to file descriptor... Get the /* The descriptor is in a valid range to file descriptor... Get the
@ -100,15 +97,11 @@ FAR struct file *fs_getfilep(int fd)
if (list == NULL) if (list == NULL)
{ {
errcode = EAGAIN; return -EAGAIN;
goto errout;
} }
/* And return the file pointer from the list */ /* And return the file pointer from the list */
return &list->fl_files[fd]; *filep = &list->fl_files[fd];
return OK;
errout:
set_errno(errcode);
return NULL;
} }

View File

@ -1,7 +1,8 @@
/**************************************************************************** /****************************************************************************
* fs/vfs/fs_ioctl.c * fs/vfs/fs_ioctl.c
* *
* Copyright (C) 2007-2010, 2012-2014, 2016 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2010, 2012-2014, 2016-2017 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -68,7 +69,9 @@
* arg The argument of the ioctl cmd * arg The argument of the ioctl cmd
* *
* Return: * Return:
* See ioctl() below. * Returns a non-negative number on success; A negated errno value is
* returned on any failure (see comments ioctl() for a list of appropriate
* errno values).
* *
****************************************************************************/ ****************************************************************************/
@ -76,8 +79,6 @@
int file_ioctl(FAR struct file *filep, int req, unsigned long arg) int file_ioctl(FAR struct file *filep, int req, unsigned long arg)
{ {
FAR struct inode *inode; FAR struct inode *inode;
int errcode;
int ret;
DEBUGASSERT(filep != NULL); DEBUGASSERT(filep != NULL);
@ -86,32 +87,19 @@ int file_ioctl(FAR struct file *filep, int req, unsigned long arg)
inode = filep->f_inode; inode = filep->f_inode;
if (!inode) if (!inode)
{ {
errcode = EBADF; return -EBADF;
goto errout;
} }
/* Does the driver support the ioctl method? */ /* Does the driver support the ioctl method? */
if (inode->u.i_ops == NULL || inode->u.i_ops->ioctl == NULL) if (inode->u.i_ops == NULL || inode->u.i_ops->ioctl == NULL)
{ {
errcode = ENOTTY; return -ENOTTY;
goto errout;
} }
/* Yes on both accounts. Let the driver perform the ioctl command */ /* Yes on both accounts. Let the driver perform the ioctl command */
ret = (int)inode->u.i_ops->ioctl(filep, req, arg); return (int)inode->u.i_ops->ioctl(filep, req, arg);
if (ret < 0)
{
errcode = -ret;
goto errout;
}
return ret;
errout:
set_errno(errcode);
return ERROR;
} }
#endif /* CONFIG_NFILE_DESCRIPTORS > 0 */ #endif /* CONFIG_NFILE_DESCRIPTORS > 0 */
@ -153,6 +141,7 @@ int ioctl(int fd, int req, unsigned long arg)
int errcode; int errcode;
#if CONFIG_NFILE_DESCRIPTORS > 0 #if CONFIG_NFILE_DESCRIPTORS > 0
FAR struct file *filep; FAR struct file *filep;
int ret;
/* Did we get a valid file descriptor? */ /* Did we get a valid file descriptor? */
@ -164,7 +153,7 @@ int ioctl(int fd, int req, unsigned long arg)
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 #if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0
if ((unsigned int)fd < (CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS)) if ((unsigned int)fd < (CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS))
{ {
int ret = netdev_ioctl(fd, req, arg); ret = netdev_ioctl(fd, req, arg);
if (ret < 0) if (ret < 0)
{ {
errcode = -ret; errcode = -ret;
@ -184,22 +173,27 @@ int ioctl(int fd, int req, unsigned long arg)
#if CONFIG_NFILE_DESCRIPTORS > 0 #if CONFIG_NFILE_DESCRIPTORS > 0
/* Get the file structure corresponding to the file descriptor. */ /* Get the file structure corresponding to the file descriptor. */
filep = fs_getfilep(fd); ret = fs_getfilep(fd, &filep);
if (!filep) if (ret < 0)
{ {
/* Apparently, the fd does not correspond to any open file. In the errcode = -ret;
* case of errors, the errno value has already been set by goto errout;
* fs_getfilep().
*/
return ERROR;
} }
DEBUGASSERT(filep != NULL);
/* Perform the file ioctl. If file_ioctl() fails, it will set the errno /* Perform the file ioctl. If file_ioctl() fails, it will set the errno
* value appropriately. * value appropriately.
*/ */
return file_ioctl(filep, req, arg); ret = file_ioctl(filep, req, arg);
if (ret < 0)
{
errcode = -ret;
goto errout;
}
return ret;
#else #else
errcode = ENOTTY; errcode = ENOTTY;
#endif #endif

View File

@ -162,27 +162,34 @@ off_t lseek(int fd, off_t offset, int whence)
{ {
FAR struct file *filep; FAR struct file *filep;
off_t newpos; off_t newpos;
int errcode;
int ret;
/* Get the file structure corresponding to the file descriptor. */ /* Get the file structure corresponding to the file descriptor. */
filep = fs_getfilep(fd); ret = fs_getfilep(fd, &filep);
if (!filep) if (ret < 0)
{ {
/* The errno value has already been set */ errcode = -ret;
goto errout;
return (off_t)ERROR;
} }
DEBUGASSERT(filep != NULL);
/* Then let file_seek do the real work */ /* Then let file_seek do the real work */
newpos = file_seek(filep, offset, whence); newpos = file_seek(filep, offset, whence);
if (newpos < 0) if (newpos < 0)
{ {
set_errno((int)-newpos); errcode = (int)-newpos;
return (off_t)ERROR; goto errout;
} }
return newpos; return newpos;
errout:
set_errno(errcode);
return (off_t)ERROR;
} }
#endif #endif

View File

@ -1,7 +1,8 @@
/**************************************************************************** /****************************************************************************
* fs/vfs/fs_open.c * fs/vfs/fs_open.c
* *
* Copyright (C) 2007-2009, 2011-2012, 2016-2017 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009, 2011-2012, 2016-2017 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -216,12 +217,11 @@ int open(const char *path, int oflags, ...)
/* Get the file structure corresponding to the file descriptor. */ /* Get the file structure corresponding to the file descriptor. */
filep = fs_getfilep(fd); ret = fs_getfilep(fd, &filep);
if (!filep) if (ret < 0)
{ {
/* The errno value has already been set */ ret = -ret;
goto errout_with_inode;
goto errout;
} }
/* Perform the driver open operation. NOTE that the open method may be /* Perform the driver open operation. NOTE that the open method may be

View File

@ -321,7 +321,8 @@ int file_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup)
* setup - true: Setup up the poll; false: Teardown the poll * setup - true: Setup up the poll; false: Teardown the poll
* *
* Returned Value: * Returned Value:
* 0: Success; Negated errno on failure * Zero (OK) is returned on success; a negated errno value is returned on
* any failure.
* *
****************************************************************************/ ****************************************************************************/
@ -329,19 +330,18 @@ int file_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup)
int fdesc_poll(int fd, FAR struct pollfd *fds, bool setup) int fdesc_poll(int fd, FAR struct pollfd *fds, bool setup)
{ {
FAR struct file *filep; FAR struct file *filep;
int ret;
/* Get the file pointer corresponding to this file descriptor */ /* Get the file pointer corresponding to this file descriptor */
filep = fs_getfilep(fd); ret = fs_getfilep(fd, &filep);
if (!filep) if (ret < 0)
{ {
/* The errno value has already been set */ return ret;
int errorcode = get_errno();
DEBUGASSERT(errorcode > 0);
return -errorcode;
} }
DEBUGASSERT(filep != NULL);
/* Let file_poll() do the rest */ /* Let file_poll() do the rest */
return file_poll(filep, fds, setup); return file_poll(filep, fds, setup);

View File

@ -41,6 +41,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <nuttx/cancelpt.h> #include <nuttx/cancelpt.h>
@ -146,25 +147,27 @@ ssize_t pread(int fd, FAR void *buf, size_t nbytes, off_t offset)
/* Get the file structure corresponding to the file descriptor. */ /* Get the file structure corresponding to the file descriptor. */
filep = fs_getfilep(fd); ret = (ssize_t)fs_getfilep(fd, &filep);
if (!filep) if (ret < 0)
{ {
/* The errno value has already been set */ goto errout;
ret = (ssize_t)ERROR;
} }
else
{
/* Let file_pread do the real work */
ret = file_pread(filep, buf, nbytes, offset); DEBUGASSERT(filep != NULL);
if (ret < 0)
{ /* Let file_pread do the real work */
set_errno((int)-ret);
ret = (ssize_t)ERROR; ret = file_pread(filep, buf, nbytes, offset);
} if (ret < 0)
{
goto errout;
} }
leave_cancellation_point(); leave_cancellation_point();
return ret; return ret;
errout:
set_errno((int)-ret);
leave_cancellation_point();
return (ssize_t)ERROR;
} }

View File

@ -144,25 +144,25 @@ ssize_t pwrite(int fd, FAR const void *buf, size_t nbytes, off_t offset)
/* Get the file structure corresponding to the file descriptor. */ /* Get the file structure corresponding to the file descriptor. */
filep = fs_getfilep(fd); ret = (ssize_t)fs_getfilep(fd, &filep);
if (!filep) if (ret < 0)
{ {
/* The errno value has already been set */ goto errout;
ret = (ssize_t)ERROR;
}
else
{
/* Let file_pread do the real work */
ret = file_pwrite(filep, buf, nbytes, offset);
if (ret < 0)
{
set_errno((int)-ret);
ret = (ssize_t)ERROR;
}
} }
(void)enter_cancellation_point(); /* Let file_pwrite do the real work */
ret = file_pwrite(filep, buf, nbytes, offset);
if (ret < 0)
{
goto errout;
}
leave_cancellation_point();
return ret; return ret;
errout:
set_errno((int)-ret);
leave_cancellation_point();
return (ssize_t)ERROR;
} }

View File

@ -1,7 +1,8 @@
/**************************************************************************** /****************************************************************************
* fs/vfs/fs_read.c * fs/vfs/fs_read.c
* *
* Copyright (C) 2007-2009, 2012-2014, 2016-2017 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009, 2012-2014, 2016-2017 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -148,8 +149,8 @@ ssize_t read(int fd, FAR void *buf, size_t nbytes)
#else #else
/* No networking... it is a bad descriptor in any event */ /* No networking... it is a bad descriptor in any event */
set_errno(EBADF); ret = -EBADF;
ret = ERROR; goto errout;
#endif #endif
} }
@ -163,27 +164,27 @@ ssize_t read(int fd, FAR void *buf, size_t nbytes)
* fs_getfilep() will set the errno variable. * fs_getfilep() will set the errno variable.
*/ */
filep = fs_getfilep(fd); ret = (ssize_t)fs_getfilep(fd, &filep);
if (filep == NULL) if (ret < 0)
{ {
/* The errno value has already been set */ goto errout;
ret = ERROR;
} }
else
{
/* Then let file_read do all of the work. */
ret = file_read(filep, buf, nbytes); /* Then let file_read do all of the work. */
if (ret < 0)
{ ret = file_read(filep, buf, nbytes);
set_errno((int)-ret); if (ret < 0)
ret = ERROR; {
} goto errout;
} }
} }
#endif #endif
leave_cancellation_point(); leave_cancellation_point();
return ret; return ret;
errout:
set_errno((int)-ret);
leave_cancellation_point();
return (ssize_t)ERROR;
} }

View File

@ -1,7 +1,8 @@
/**************************************************************************** /****************************************************************************
* fs/vfs/fs_sendfile.c * fs/vfs/fs_sendfile.c
* *
* Copyright (C) 2007, 2009, 2011, 2013, 2017 Gregory Nutt. All rights reserved. * Copyright (C) 2007, 2009, 2011, 2013, 2017 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -111,19 +112,21 @@ ssize_t sendfile(int outfd, int infd, off_t *offset, size_t count)
(unsigned int)infd < CONFIG_NFILE_DESCRIPTORS) (unsigned int)infd < CONFIG_NFILE_DESCRIPTORS)
{ {
FAR struct file *filep; FAR struct file *filep;
int ret;
/* This appears to be a file-to-socket transfer. Get the file /* This appears to be a file-to-socket transfer. Get the file
* structure. * structure.
*/ */
filep = fs_getfilep(infd); ret = fs_getfilep(infd, &filep);
if (!filep) if (ret < 0)
{ {
/* The errno value has already been set */ set_errno(-ret);
return ERROR; return ERROR;
} }
DEBUGASSERT(filep != NULL);
/* Then let net_sendfile do the work. */ /* Then let net_sendfile do the work. */
return net_sendfile(outfd, filep, offset, count); return net_sendfile(outfd, filep, offset, count);

View File

@ -1,7 +1,8 @@
/**************************************************************************** /****************************************************************************
* fs/vfs/fs_write.c * fs/vfs/fs_write.c
* *
* Copyright (C) 2007-2009, 2012-2014, 2016-2017 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009, 2012-2014, 2016-2017 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -155,8 +156,7 @@ ssize_t write(int fd, FAR const void *buf, size_t nbytes)
if (buf == NULL) if (buf == NULL)
{ {
set_errno(EINVAL); ret = -EINVAL;
ret = ERROR;
goto errout; goto errout;
} }
@ -173,8 +173,8 @@ ssize_t write(int fd, FAR const void *buf, size_t nbytes)
ret = send(fd, buf, nbytes, 0); ret = send(fd, buf, nbytes, 0);
#else #else
set_errno(EBADF); ret = -EBADF;
ret = ERROR; goto errout;
#endif #endif
} }
@ -186,30 +186,29 @@ ssize_t write(int fd, FAR const void *buf, size_t nbytes)
* failure. * failure.
*/ */
filep = fs_getfilep(fd); ret = (ssize_t)fs_getfilep(fd, &filep);
if (filep == NULL) if (ret < 0)
{ {
/* The errno value has already been set */ goto errout;
ret = ERROR;
} }
else
{
/* Perform the write operation using the file descriptor as an
* index. Note that file_write() will set the errno on failure.
*/
ret = file_write(filep, buf, nbytes); /* Perform the write operation using the file descriptor as an
if (ret < 0) * index. Note that file_write() will set the errno on failure.
{ */
set_errno((int)-ret);
ret = ERROR; ret = file_write(filep, buf, nbytes);
} if (ret < 0)
{
goto errout;
} }
} }
#endif #endif
errout:
leave_cancellation_point(); leave_cancellation_point();
return ret; return ret;
errout:
set_errno(-ret);
leave_cancellation_point();
return ERROR;
} }

View File

@ -1,7 +1,8 @@
/**************************************************************************** /****************************************************************************
* include/nuttx/fs/fs.h * include/nuttx/fs/fs.h
* *
* Copyright (C) 2007-2009, 2011-2013, 2015-2017 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009, 2011-2013, 2015-2017 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -61,6 +62,36 @@
* Pre-processor Definitions * Pre-processor Definitions
****************************************************************************/ ****************************************************************************/
/* Most internal OS interfaces are not available in the user space in
* PROTECTED and KERNEL builds. In that context, the corresponding
* application interfaces must be used. The differences between the two
* sets of interfaces are: The internal OS interfaces (1) do not cause
* cancellation points and (2) they do not modify the errno variable.
*
* This is only important when compiling libraries (libc or libnx) that are
* used both by the OS (libkc.a and libknx.a) or by the applications
* (libuc.a and libunx.a). The that case, the correct interface must be
* used for the build context.
*
* The interfaces open(), close(), creat(), read(), pread(), write(),
* pwrite(), poll(), select(), fcntl(), and aio_suspend() are all
* cancellation points.
*
* REVISIT: These cancellation points are an issue and may cause
* violations: It use of these internally will cause the calling function
* to become a cancellation points!
*/
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
# define _NX_WRITE(f,b,s) nx_write(s,b,s)
# define _NX_ERRNO(r) (-(r))
# define _NX_ERRVAL(r) (r)
#else
# define _NX_WRITE(f,b,s) rite(s,b,s)
# define _NX_ERRNO(r) errno
# define _NX_ERRVAL(r) (-errno)
#endif
/* Stream flags for the fs_flags field of in struct file_struct */ /* Stream flags for the fs_flags field of in struct file_struct */
#define __FS_FLAG_EOF (1 << 0) /* EOF detected by a read operation */ #define __FS_FLAG_EOF (1 << 0) /* EOF detected by a read operation */
@ -739,13 +770,13 @@ int file_close_detached(FAR struct file *filep);
* Description: * Description:
* Return the inode of the block driver specified by 'pathname' * Return the inode of the block driver specified by 'pathname'
* *
* Inputs: * Input Parameters:
* pathname - the full path to the block driver to be opened * pathname - the full path to the block driver to be opened
* mountflags - if MS_RDONLY is not set, then driver must support write * mountflags - if MS_RDONLY is not set, then driver must support write
* operations (see include/sys/mount.h) * operations (see include/sys/mount.h)
* ppinode - address of the location to return the inode reference * ppinode - address of the location to return the inode reference
* *
* Return: * Returned Value:
* Returns zero on success or a negated errno on failure: * Returns zero on success or a negated errno on failure:
* *
* EINVAL - pathname or pinode is NULL * EINVAL - pathname or pinode is NULL
@ -767,10 +798,10 @@ int open_blockdriver(FAR const char *pathname, int mountflags,
* Description: * Description:
* Call the close method and release the inode * Call the close method and release the inode
* *
* Inputs: * Input Parameters:
* inode - reference to the inode of a block driver opened by open_blockdriver * inode - reference to the inode of a block driver opened by open_blockdriver
* *
* Return: * Returned Value:
* Returns zero on success or a negated errno on failure: * Returns zero on success or a negated errno on failure:
* *
* EINVAL - inode is NULL * EINVAL - inode is NULL
@ -788,12 +819,12 @@ int close_blockdriver(FAR struct inode *inode);
* Description: * Description:
* Perform device specific operations. * Perform device specific operations.
* *
* Parameters: * Input Parameters:
* fd File/socket descriptor of device * fd File/socket descriptor of device
* req The ioctl command * req The ioctl command
* arg The argument of the ioctl cmd * arg The argument of the ioctl cmd
* *
* Return: * Returned Value:
* >=0 on success (positive non-zero values are cmd-specific) * >=0 on success (positive non-zero values are cmd-specific)
* -1 on failure with errno set properly: * -1 on failure with errno set properly:
* *
@ -862,18 +893,18 @@ ssize_t lib_sendfile(int outfd, int infd, off_t *offset, size_t count);
* file. NOTE that this function will currently fail if it is provided * file. NOTE that this function will currently fail if it is provided
* with a socket descriptor. * with a socket descriptor.
* *
* Parameters: * Input Parameters:
* fd - The file descriptor * fd - The file descriptor
* filep - The location to return the struct file instance
* *
* Return: * Returned Value:
* A point to the corresponding struct file instance is returned on * Zero (OK) is returned on success; a negated errno value is returned on
* success. On failure, NULL is returned and the errno value is * any failure.
* set appropriately (EBADF).
* *
****************************************************************************/ ****************************************************************************/
#if CONFIG_NFILE_DESCRIPTORS > 0 #if CONFIG_NFILE_DESCRIPTORS > 0
FAR struct file *fs_getfilep(int fd); int fs_getfilep(int fd, FAR struct file **filep);
#endif #endif
/**************************************************************************** /****************************************************************************
@ -953,8 +984,8 @@ off_t file_seek(FAR struct file *filep, off_t offset, int whence);
* *
* Description: * Description:
* Equivalent to the standard fsync() function except that is accepts a * Equivalent to the standard fsync() function except that is accepts a
* struct file instance instead of a file descriptor. Currently used * struct file instance instead of a file descriptor and it does not set
* only by aio_fsync(); * the errno variable.
* *
****************************************************************************/ ****************************************************************************/
@ -974,7 +1005,9 @@ int file_fsync(FAR struct file *filep);
* arg The argument of the ioctl cmd * arg The argument of the ioctl cmd
* *
* Return: * Return:
* See ioctl() below. * Returns a non-negative number on success; A negated errno value is
* returned on any failure (see comments ioctl() for a list of appropriate
* errno values).
* *
****************************************************************************/ ****************************************************************************/