You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/04/28 05:40:31 UTC

[incubator-nuttx] 02/02: Implement preadv and pwritev

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

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

commit ded4fd33c17753b6127970c1716ec9f751f5ce44
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Thu Apr 28 09:26:17 2022 +0900

    Implement preadv and pwritev
---
 include/sys/uio.h           |  6 +++
 libs/libc/libc.csv          |  2 +
 libs/libc/uio/Make.defs     |  1 +
 libs/libc/uio/lib_preadv.c  | 97 +++++++++++++++++++++++++++++++++++++++++++++
 libs/libc/uio/lib_pwritev.c | 91 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 197 insertions(+)

diff --git a/include/sys/uio.h b/include/sys/uio.h
index 551cc5c26e..4555d5e044 100644
--- a/include/sys/uio.h
+++ b/include/sys/uio.h
@@ -156,6 +156,12 @@ ssize_t readv(int fildes, FAR const struct iovec *iov, int iovcnt);
 
 ssize_t writev(int fildes, FAR const struct iovec *iov, int iovcnt);
 
+ssize_t preadv(int fildes, FAR const struct iovec *iov, int iovcnt,
+               off_t offset);
+
+ssize_t pwritev(int fildes, FAR const struct iovec *iov, int iovcnt,
+                off_t offset);
+
 #undef EXTERN
 #if defined(__cplusplus)
 }
diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv
index fde50b25d4..a746df93cb 100644
--- a/libs/libc/libc.csv
+++ b/libs/libc/libc.csv
@@ -147,6 +147,7 @@
 "ntohs","arpa/inet.h","","uint16_t","uint16_t"
 "perror","stdio.h","defined(CONFIG_FILE_STREAM)","void","FAR const char *"
 "pipe","unistd.h","defined(CONFIG_PIPES) && CONFIG_DEV_PIPE_SIZE > 0","int","int [2]|FAR int *"
+"preadv","sys/uio.h","","ssize_t","int","FAR const struct iovec *","int","off_t"
 "printf","stdio.h","","int","FAR const IPTR char *","..."
 "pthread_attr_destroy","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_attr_t *"
 "pthread_attr_getinheritsched","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR const pthread_attr_t *","FAR int *"
@@ -183,6 +184,7 @@
 "pthread_setname_np","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","pthread_t","const char *"
 "pthread_yield","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","void"
 "puts","stdio.h","defined(CONFIG_FILE_STREAM)","int","FAR const IPTR char *"
+"pwritev","sys/uio.h","","ssize_t","int","FAR const struct iovec *","int","off_t"
 "qsort","stdlib.h","","void","FAR void *","size_t","size_t","int(*)(FAR const void *","FAR const void *)"
 "rand","stdlib.h","","int"
 "readdir_r","dirent.h","","int","FAR DIR *","FAR struct dirent *","FAR struct dirent **"
diff --git a/libs/libc/uio/Make.defs b/libs/libc/uio/Make.defs
index 03cab6e236..eca08968bc 100644
--- a/libs/libc/uio/Make.defs
+++ b/libs/libc/uio/Make.defs
@@ -21,6 +21,7 @@
 # Add the uio.h C files to the build
 
 CSRCS += lib_readv.c lib_writev.c
+CSRCS += lib_preadv.c lib_pwritev.c
 
 # Add the uio.h directory to the build
 
diff --git a/libs/libc/uio/lib_preadv.c b/libs/libc/uio/lib_preadv.c
new file mode 100644
index 0000000000..427d5124fb
--- /dev/null
+++ b/libs/libc/uio/lib_preadv.c
@@ -0,0 +1,97 @@
+/****************************************************************************
+ * libs/libc/uio/lib_preadv.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <sys/types.h>
+#include <sys/uio.h>
+#include <unistd.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: preadv()
+ *
+ * Description:
+ *   The preadv() function is equivalent to pread(), except it takes
+ *   an iov array.
+ *
+ ****************************************************************************/
+
+ssize_t preadv(int fildes, FAR const struct iovec *iov, int iovcnt,
+               off_t offset)
+{
+  ssize_t ntotal;
+  ssize_t nread;
+  size_t remaining;
+  FAR uint8_t *buffer;
+  int i;
+
+  /* Process each entry in the struct iovec array */
+
+  for (i = 0, ntotal = 0; i < iovcnt; i++)
+    {
+      /* Ignore zero-length reads */
+
+      if (iov[i].iov_len > 0)
+        {
+          buffer    = iov[i].iov_base;
+          remaining = iov[i].iov_len;
+
+          /* Read repeatedly as necessary to fill buffer */
+
+          do
+            {
+              /* NOTE: pread() is a cancellation point */
+
+              nread = pread(fildes, buffer, remaining, offset + ntotal);
+
+              /* Check for a read error */
+
+              if (nread < 0)
+                {
+                  return nread;
+                }
+
+              /* Check for an end-of-file condition */
+
+              else if (nread == 0)
+                {
+                  return ntotal;
+                }
+
+              /* Update pointers and counts in order to handle partial
+               * buffer reads.
+               */
+
+              buffer    += nread;
+              remaining -= nread;
+              ntotal    += nread;
+            }
+          while (remaining > 0);
+        }
+    }
+
+  return ntotal;
+}
diff --git a/libs/libc/uio/lib_pwritev.c b/libs/libc/uio/lib_pwritev.c
new file mode 100644
index 0000000000..1203e20f0c
--- /dev/null
+++ b/libs/libc/uio/lib_pwritev.c
@@ -0,0 +1,91 @@
+/****************************************************************************
+ * libs/libc/uio/lib_pwritev.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <sys/types.h>
+#include <sys/uio.h>
+#include <unistd.h>
+#include <errno.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: pwritev()
+ *
+ * Description:
+ *   The pwritev() function is equivalent to write(), except it takes
+ *   an iov array.
+ *
+ ****************************************************************************/
+
+ssize_t pwritev(int fildes, FAR const struct iovec *iov, int iovcnt,
+                off_t offset)
+{
+  ssize_t ntotal;
+  ssize_t nwritten;
+  size_t remaining;
+  FAR uint8_t *buffer;
+  int i;
+
+  /* Process each entry in the struct iovec array */
+
+  for (i = 0, ntotal = 0; i < iovcnt; i++)
+    {
+      /* Ignore zero-length writes */
+
+      if (iov[i].iov_len > 0)
+        {
+          buffer    = iov[i].iov_base;
+          remaining = iov[i].iov_len;
+
+          /* Write repeatedly as necessary to write the entire buffer */
+
+          do
+            {
+              /* NOTE: pwrite() is a cancellation point */
+
+              nwritten = pwrite(fildes, buffer, remaining, offset + ntotal);
+
+              /* Check for a write error */
+
+              if (nwritten < 0)
+                {
+                  return ntotal ? ntotal : ERROR;
+                }
+
+              /* Update pointers and counts in order to handle partial
+               * buffer writes.
+               */
+
+              buffer    += nwritten;
+              remaining -= nwritten;
+              ntotal    += nwritten;
+            }
+          while (remaining > 0);
+        }
+    }
+
+  return ntotal;
+}