Implement preadv and pwritev

This commit is contained in:
YAMAMOTO Takashi
2022-04-28 09:26:17 +09:00
committed by Xiang Xiao
parent d832df88a5
commit ded4fd33c1
5 changed files with 197 additions and 0 deletions

View File

@@ -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)
}

View File

@@ -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 **"
1 __errno errno.h defined(CONFIG_BUILD_FLAT) FAR int *
147 ntohs arpa/inet.h uint16_t
148 perror stdio.h defined(CONFIG_FILE_STREAM) void
149 pipe unistd.h defined(CONFIG_PIPES) && CONFIG_DEV_PIPE_SIZE > 0 int
150 preadv sys/uio.h ssize_t
151 printf stdio.h int
152 pthread_attr_destroy pthread.h !defined(CONFIG_DISABLE_PTHREAD) int
153 pthread_attr_getinheritsched pthread.h !defined(CONFIG_DISABLE_PTHREAD) int
184 pthread_setname_np pthread.h !defined(CONFIG_DISABLE_PTHREAD) int
185 pthread_yield pthread.h !defined(CONFIG_DISABLE_PTHREAD) void
186 puts stdio.h defined(CONFIG_FILE_STREAM) int
187 pwritev sys/uio.h ssize_t
188 qsort stdlib.h void
189 rand stdlib.h int
190 readdir_r dirent.h int

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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;
}