PostgreSQL Source Code  git master
pwrite.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pwrite.c
4  * Implementation of pwrite(2) for platforms that lack one.
5  *
6  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * src/port/pwrite.c
10  *
11  * Note that this implementation changes the current file position, unlike
12  * the POSIX function, so we use the name pg_pwrite().
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 ssize_t
27 pg_pwrite(int fd, const void *buf, size_t size, off_t offset)
28 {
29 #ifdef WIN32
30  OVERLAPPED overlapped = {0};
31  HANDLE handle;
32  DWORD result;
33 
34  handle = (HANDLE) _get_osfhandle(fd);
35  if (handle == INVALID_HANDLE_VALUE)
36  {
37  errno = EBADF;
38  return -1;
39  }
40 
41  overlapped.Offset = offset;
42  if (!WriteFile(handle, buf, size, &result, &overlapped))
43  {
44  _dosmaperr(GetLastError());
45  return -1;
46  }
47 
48  return result;
49 #else
50  if (lseek(fd, offset, SEEK_SET) < 0)
51  return -1;
52 
53  return write(fd, buf, size);
54 #endif
55 }
#define write(a, b, c)
Definition: win32.h:14
void _dosmaperr(unsigned long)
Definition: win32error.c:171
ssize_t pg_pwrite(int fd, const void *buf, size_t size, off_t offset)
Definition: pwrite.c:27
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static char * buf
Definition: pg_test_fsync.c:67