fs/vfs: Add nx_seek function

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2020-05-03 01:25:16 +08:00 committed by patacongo
parent 1bab5b6813
commit 7980673b59
2 changed files with 64 additions and 29 deletions

View File

@ -121,6 +121,40 @@ off_t file_seek(FAR struct file *filep, off_t offset, int whence)
return filep->f_pos; return filep->f_pos;
} }
/****************************************************************************
* Name: nx_seek
*
* Description:
* nx_seek() function repositions the offset of the open file associated
* with the file descriptor fd to the argument 'offset' according to the
* directive 'whence'. nx_seek() is an internal OS function. It is
* functionally equivalent to lseek() except that:
*
* - It does not modify the errno variable, and
* - It is not a cancellation point.
*
****************************************************************************/
off_t nx_seek(int fd, off_t offset, int whence)
{
FAR struct file *filep;
int ret;
/* Get the file structure corresponding to the file descriptor. */
ret = fs_getfilep(fd, &filep);
if (ret < 0)
{
return ret;
}
DEBUGASSERT(filep != NULL);
/* Then let file_seek do the real work */
return file_seek(filep, offset, whence);
}
/**************************************************************************** /****************************************************************************
* Name: lseek * Name: lseek
* *
@ -136,10 +170,11 @@ off_t file_seek(FAR struct file *filep, off_t offset, int whence)
* SEEK_END * SEEK_END
* The offset is set to the size of the file plus offset bytes. * The offset is set to the size of the file plus offset bytes.
* *
* The lseek() function allows the file offset to be set beyond the end of the * The lseek() function allows the file offset to be set beyond the end of
* file (but this does not change the size of the file). If data is later written * the file (but this does not change the size of the file). If data is
* at this point, subsequent reads of the data in the gap (a "hole") return null * later written at this point, subsequent reads of the data in the gap (a
* bytes ('\0') until data is actually written into the gap. * "hole") return null bytes ('\0') until data is actually written into the
* gap.
* *
* Input Parameters: * Input Parameters:
* fd File descriptor of device * fd File descriptor of device
@ -147,12 +182,12 @@ off_t file_seek(FAR struct file *filep, off_t offset, int whence)
* whence Defines how to use offset * whence Defines how to use offset
* *
* Returned Value: * Returned Value:
* The resulting offset on success. -1 on failure withi errno set properly: * The resulting offset on success. -1 on failure withi errno set properly:
* *
* EBADF fd is not an open file descriptor. * EBADF fd is not an open file descriptor.
* EINVAL whence is not one of SEEK_SET, SEEK_CUR, SEEK_END; or the * EINVAL whence is not one of SEEK_SET, SEEK_CUR, SEEK_END; or the
* resulting file offset would be negative, or beyond the end of a * resulting file offset would be negative, or beyond the end of
* seekable device. * a seekable device.
* EOVERFLOW The resulting file offset cannot be represented in an off_t. * EOVERFLOW The resulting file offset cannot be represented in an off_t.
* ESPIPE fd is associated with a pipe, socket, or FIFO. * ESPIPE fd is associated with a pipe, socket, or FIFO.
* *
@ -160,34 +195,16 @@ off_t file_seek(FAR struct file *filep, off_t offset, int whence)
off_t lseek(int fd, off_t offset, int whence) off_t lseek(int fd, off_t offset, int whence)
{ {
FAR struct file *filep;
off_t newpos; off_t newpos;
int errcode;
int ret;
/* Get the file structure corresponding to the file descriptor. */ /* Let nx_seek do the real work */
ret = fs_getfilep(fd, &filep); newpos = nx_seek(fd, offset, whence);
if (ret < 0)
{
errcode = -ret;
goto errout;
}
DEBUGASSERT(filep != NULL);
/* Then let file_seek do the real work */
newpos = file_seek(filep, offset, whence);
if (newpos < 0) if (newpos < 0)
{ {
errcode = (int)-newpos; set_errno(-newpos);
goto errout; return ERROR;
} }
return newpos; return newpos;
errout:
set_errno(errcode);
return (off_t)ERROR;
} }

View File

@ -79,6 +79,7 @@
# endif # endif
# define _NX_READ(f,b,s) nx_read(f,b,s) # define _NX_READ(f,b,s) nx_read(f,b,s)
# define _NX_WRITE(f,b,s) nx_write(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)
# define _NX_GETERRNO(r) (-(r)) # define _NX_GETERRNO(r) (-(r))
# define _NX_SETERRNO(r) set_errno(-(r)) # define _NX_SETERRNO(r) set_errno(-(r))
# define _NX_GETERRVAL(r) (r) # define _NX_GETERRVAL(r) (r)
@ -90,6 +91,7 @@
# endif # endif
# define _NX_READ(f,b,s) read(f,b,s) # define _NX_READ(f,b,s) read(f,b,s)
# define _NX_WRITE(f,b,s) write(f,b,s) # define _NX_WRITE(f,b,s) write(f,b,s)
# define _NX_SEEK(f,o,w) lseek(f,o,w)
# define _NX_GETERRNO(r) errno # define _NX_GETERRNO(r) errno
# define _NX_SETERRNO(r) # define _NX_SETERRNO(r)
# define _NX_GETERRVAL(r) (-errno) # define _NX_GETERRVAL(r) (-errno)
@ -1160,6 +1162,22 @@ ssize_t file_pwrite(FAR struct file *filep, FAR const void *buf,
off_t file_seek(FAR struct file *filep, off_t offset, int whence); off_t file_seek(FAR struct file *filep, off_t offset, int whence);
/****************************************************************************
* Name: nx_seek
*
* Description:
* nx_seek() function repositions the offset of the open file associated
* with the file descriptor fd to the argument 'offset' according to the
* directive 'whence'. nx_seek() is an internal OS function. It is
* functionally equivalent to lseek() except that:
*
* - It does not modify the errno variable, and
* - It is not a cancellation point.
*
****************************************************************************/
off_t nx_seek(int fd, off_t offset, int whence);
/**************************************************************************** /****************************************************************************
* Name: file_fsync * Name: file_fsync
* *