PostgreSQL Source Code  git master
win32pread.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * win32pread.c
4  * Implementation of pread(2) for Windows.
5  *
6  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * src/port/win32pread.c
10  *
11  *-------------------------------------------------------------------------
12  */
13 
14 
15 #include "c.h"
16 
17 #include <windows.h>
18 
19 ssize_t
20 pg_pread(int fd, void *buf, size_t size, off_t offset)
21 {
22  OVERLAPPED overlapped = {0};
23  HANDLE handle;
24  DWORD result;
25 
26  handle = (HANDLE) _get_osfhandle(fd);
27  if (handle == INVALID_HANDLE_VALUE)
28  {
29  errno = EBADF;
30  return -1;
31  }
32 
33  /* Avoid overflowing DWORD. */
34  size = Min(size, 1024 * 1024 * 1024);
35 
36  /* Note that this changes the file position, despite not using it. */
37  overlapped.Offset = offset;
38  if (!ReadFile(handle, buf, size, &result, &overlapped))
39  {
40  if (GetLastError() == ERROR_HANDLE_EOF)
41  return 0;
42 
43  _dosmaperr(GetLastError());
44  return -1;
45  }
46 
47  return result;
48 }
#define Min(x, y)
Definition: c.h:1007
static char * buf
Definition: pg_test_fsync.c:73
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static pg_noinline void Size size
Definition: slab.c:607
void _dosmaperr(unsigned long)
Definition: win32error.c:177
ssize_t pg_pread(int fd, void *buf, size_t size, off_t offset)
Definition: win32pread.c:20