You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.1KB

  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. #include "postgres.h"
  17. #ifdef WIN32
  18. #include <windows.h>
  19. #else
  20. #include <unistd.h>
  21. #endif
  22. ssize_t
  23. pg_pwrite(int fd, const void *buf, size_t size, off_t offset)
  24. {
  25. #ifdef WIN32
  26. OVERLAPPED overlapped = {0};
  27. HANDLE handle;
  28. DWORD result;
  29. handle = (HANDLE) _get_osfhandle(fd);
  30. if (handle == INVALID_HANDLE_VALUE)
  31. {
  32. errno = EBADF;
  33. return -1;
  34. }
  35. overlapped.Offset = offset;
  36. if (!WriteFile(handle, buf, size, &result, &overlapped))
  37. {
  38. _dosmaperr(GetLastError());
  39. return -1;
  40. }
  41. return result;
  42. #else
  43. if (lseek(fd, offset, SEEK_SET) < 0)
  44. return -1;
  45. return write(fd, buf, size);
  46. #endif
  47. }