This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 7980673b590c3ac5893afe59510a45bbea1c7984
Author: Xiang Xiao <xiaoxi...@xiaomi.com>
AuthorDate: Sun May 3 01:25:16 2020 +0800

    fs/vfs: Add nx_seek function
    
    Signed-off-by: Xiang Xiao <xiaoxi...@xiaomi.com>
---
 fs/vfs/fs_lseek.c     | 75 +++++++++++++++++++++++++++++++--------------------
 include/nuttx/fs/fs.h | 18 +++++++++++++
 2 files changed, 64 insertions(+), 29 deletions(-)

diff --git a/fs/vfs/fs_lseek.c b/fs/vfs/fs_lseek.c
index 9322851..3500144 100644
--- a/fs/vfs/fs_lseek.c
+++ b/fs/vfs/fs_lseek.c
@@ -122,6 +122,40 @@ 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)
+{
+  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
  *
  * Description:
@@ -136,10 +170,11 @@ off_t file_seek(FAR struct file *filep, off_t offset, int 
whence)
  *   SEEK_END
  *      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
- *  file (but this does not change the size of the file). If data is later 
written
- *  at this point, subsequent reads of the data in the gap (a "hole") return 
null
- *  bytes ('\0') until data is actually written into the gap.
+ *  The lseek() function allows the file offset to be set beyond the end of
+ *  the file (but this does not change the size of the file). If data is
+ *  later written at this point, subsequent reads of the data in the gap (a
+ *  "hole") return null bytes ('\0') until data is actually written into the
+ *  gap.
  *
  * Input Parameters:
  *   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
  *
  * 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.
  *   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
- *              seekable device.
+ *              resulting file offset would be negative, or beyond the end of
+ *              a seekable device.
  *   EOVERFLOW  The resulting file offset cannot be represented in an off_t.
  *   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)
 {
-  FAR struct file *filep;
   off_t newpos;
-  int errcode;
-  int ret;
-
-  /* Get the file structure corresponding to the file descriptor. */
 
-  ret = fs_getfilep(fd, &filep);
-  if (ret < 0)
-    {
-      errcode = -ret;
-      goto errout;
-    }
+  /* Let nx_seek do the real work */
 
-  DEBUGASSERT(filep != NULL);
-
-  /* Then let file_seek do the real work */
-
-  newpos = file_seek(filep, offset, whence);
+  newpos = nx_seek(fd, offset, whence);
   if (newpos < 0)
     {
-      errcode = (int)-newpos;
-      goto errout;
+      set_errno(-newpos);
+      return ERROR;
     }
 
   return newpos;
-
-errout:
-  set_errno(errcode);
-  return (off_t)ERROR;
 }
diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h
index 3758db4..4640424 100644
--- a/include/nuttx/fs/fs.h
+++ b/include/nuttx/fs/fs.h
@@ -79,6 +79,7 @@
 #  endif
 #  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)
 #  define _NX_GETERRNO(r)      (-(r))
 #  define _NX_SETERRNO(r)      set_errno(-(r))
 #  define _NX_GETERRVAL(r)     (r)
@@ -90,6 +91,7 @@
 #  endif
 #  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)
 #  define _NX_GETERRNO(r)      errno
 #  define _NX_SETERRNO(r)
 #  define _NX_GETERRVAL(r)     (-errno)
@@ -1161,6 +1163,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);
 
 /****************************************************************************
+ * 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
  *
  * Description:

Reply via email to