fs/vfs: file_write() and file_pwrite() are internal OS interfaces and should not report errors via the errno

This commit is contained in:
Gregory Nutt 2017-09-28 14:49:05 -06:00
parent e761b80ea7
commit 7b7ca87941
3 changed files with 37 additions and 48 deletions

View File

@ -155,6 +155,13 @@ static void aio_write_worker(FAR void *arg)
aiocbp->aio_nbytes,
aiocbp->aio_offset);
}
/* errno is not set */
if (nwritten < 0)
{
ferr("ERROR: file_write/file_pwrite failed: %d\n", nwritten);
}
}
#endif
#if defined(AIO_HAVE_FILEP) && defined(AIO_HAVE_PSOCK)
@ -172,22 +179,21 @@ static void aio_write_worker(FAR void *arg)
nwritten = psock_send(aioc->u.aioc_psock,
(FAR const void *)aiocbp->aio_buf,
aiocbp->aio_nbytes, 0);
/* errno is set */
if (nwritten < 0)
{
int errcode = get_errno();
ferr("ERROR: psock_send failed: %d\n", errcode);
DEBUGASSERT(errcode > 0);
nwritten = -errcode;
}
}
#endif
/* Check the result of the write */
/* Save the result of the write */
if (nwritten < 0)
{
int errcode = get_errno();
ferr("ERROR: write/pwrite failed: %d\n", errcode);
DEBUGASSERT(errcode > 0);
aiocbp->aio_result = -errcode;
}
else
{
aiocbp->aio_result = nwritten;
}
aiocbp->aio_result = nwritten;
#ifdef AIO_HAVE_FILEP
errout:

View File

@ -66,38 +66,32 @@ ssize_t file_pwrite(FAR struct file *filep, FAR const void *buf,
off_t savepos;
off_t pos;
ssize_t ret;
int errcode = 0;
/* Perform the seek to the current position. This will not move the
* file pointer, but will return its current setting
*/
savepos = file_seek(filep, 0, SEEK_CUR);
if (savepos == (off_t)-1)
if (savepos < 0)
{
/* file_seek might fail if this if the media is not seekable */
return ERROR;
return (ssize_t)savepos;
}
/* Then seek to the correct position in the file */
pos = file_seek(filep, offset, SEEK_SET);
if (pos == (off_t)-1)
if (pos < 0)
{
/* This might fail is the offset is beyond the end of file */
return ERROR;
return (ssize_t)pos;
}
/* Then perform the write operation */
ret = file_write(filep, buf, nbytes);
if (ret < 0)
{
errcode = get_errno();
ret = ERROR;
}
/* Restore the file position */
@ -106,13 +100,7 @@ ssize_t file_pwrite(FAR struct file *filep, FAR const void *buf,
{
/* This really should not fail */
errcode = -pos;
ret = ERROR;
}
if (errcode != 0)
{
set_errno(errcode);
ret = (ssize_t)pos;
}
return ret;
@ -168,6 +156,11 @@ ssize_t pwrite(int fd, FAR const void *buf, size_t nbytes, off_t offset)
/* Let file_pread do the real work */
ret = file_pwrite(filep, buf, nbytes, offset);
if (ret < 0)
{
set_errno((int)-ret);
ret = ERROR;
}
}
(void)enter_cancellation_point();

View File

@ -1,7 +1,7 @@
/****************************************************************************
* fs/vfs/fs_write.c
*
* Copyright (C) 2007-2009, 2012-2014, 2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2009, 2012-2014, 2016-2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -71,15 +71,12 @@
ssize_t file_write(FAR struct file *filep, FAR const void *buf, size_t nbytes)
{
FAR struct inode *inode;
ssize_t ret;
int errcode;
/* Was this file opened for write access? */
if ((filep->f_oflags & O_WROK) == 0)
{
errcode = EBADF;
goto errout;
return -EBADF;
}
/* Is a driver registered? Does it support the write method? */
@ -87,24 +84,12 @@ ssize_t file_write(FAR struct file *filep, FAR const void *buf, size_t nbytes)
inode = filep->f_inode;
if (!inode || !inode->u.i_ops || !inode->u.i_ops->write)
{
errcode = EBADF;
goto errout;
return -EBADF;
}
/* Yes, then let the driver perform the write */
ret = inode->u.i_ops->write(filep, buf, nbytes);
if (ret < 0)
{
errcode = -ret;
goto errout;
}
return ret;
errout:
set_errno(errcode);
return ERROR;
return inode->u.i_ops->write(filep, buf, nbytes);
}
/****************************************************************************
@ -208,6 +193,11 @@ ssize_t write(int fd, FAR const void *buf, size_t nbytes)
*/
ret = file_write(filep, buf, nbytes);
if (ret < 0)
{
set_errno((int)-ret);
ret = ERROR;
}
}
}
#endif