PostgreSQL Source Code  git master
preadv.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * preadv.c
4  * Implementation of preadv(2) for platforms that lack one.
5  *
6  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * src/port/preadv.c
10  *
11  * Note that this implementation changes the current file position, unlike
12  * the POSIX-like function, so we use the name pg_preadv().
13  *
14  *-------------------------------------------------------------------------
15  */
16 
17 
18 #include "postgres.h"
19 
20 #ifdef WIN32
21 #include <windows.h>
22 #else
23 #include <unistd.h>
24 #endif
25 
26 #include "port/pg_iovec.h"
27 
28 ssize_t
29 pg_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset)
30 {
31 #ifdef HAVE_READV
32  if (iovcnt == 1)
33  return pg_pread(fd, iov[0].iov_base, iov[0].iov_len, offset);
34  if (lseek(fd, offset, SEEK_SET) < 0)
35  return -1;
36  return readv(fd, iov, iovcnt);
37 #else
38  ssize_t sum = 0;
39  ssize_t part;
40 
41  for (int i = 0; i < iovcnt; ++i)
42  {
43  part = pg_pread(fd, iov[i].iov_base, iov[i].iov_len, offset);
44  if (part < 0)
45  {
46  if (i == 0)
47  return -1;
48  else
49  return sum;
50  }
51  sum += part;
52  offset += part;
53  if (part < iov[i].iov_len)
54  return sum;
55  }
56  return sum;
57 #endif
58 }
int i
Definition: isn.c:73
ssize_t pg_pread(int fd, void *buf, size_t nbyte, off_t offset)
Definition: pread.c:27
ssize_t pg_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset)
Definition: preadv.c:29
static int fd(const char *x, int i)
Definition: preproc-init.c:105
Definition: pg_iovec.h:25