windows-file.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2018 Andy Green <[email protected]>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
  22. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  23. #endif
  24. #include "core/private.h"
  25. int lws_plat_apply_FD_CLOEXEC(int n)
  26. {
  27. return 0;
  28. }
  29. lws_fop_fd_t
  30. _lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename,
  31. const char *vpath, lws_fop_flags_t *flags)
  32. {
  33. HANDLE ret;
  34. WCHAR buf[MAX_PATH];
  35. lws_fop_fd_t fop_fd;
  36. FILE_STANDARD_INFO fInfo = {0};
  37. MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, LWS_ARRAY_SIZE(buf));
  38. #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0602 // Windows 8 (minimum when UWP_ENABLED, but can be used in Windows builds)
  39. CREATEFILE2_EXTENDED_PARAMETERS extParams = {0};
  40. extParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
  41. if (((*flags) & 7) == _O_RDONLY) {
  42. ret = CreateFile2(buf, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, &extParams);
  43. } else {
  44. ret = CreateFile2(buf, GENERIC_WRITE, 0, CREATE_ALWAYS, &extParams);
  45. }
  46. #else
  47. if (((*flags) & 7) == _O_RDONLY) {
  48. ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ,
  49. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  50. } else {
  51. ret = CreateFileW(buf, GENERIC_WRITE, 0, NULL,
  52. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  53. }
  54. #endif
  55. if (ret == LWS_INVALID_FILE)
  56. goto bail;
  57. fop_fd = malloc(sizeof(*fop_fd));
  58. if (!fop_fd)
  59. goto bail;
  60. fop_fd->fops = fops;
  61. fop_fd->fd = ret;
  62. fop_fd->filesystem_priv = NULL; /* we don't use it */
  63. fop_fd->flags = *flags;
  64. fop_fd->len = 0;
  65. if(GetFileInformationByHandleEx(ret, FileStandardInfo, &fInfo, sizeof(fInfo)))
  66. fop_fd->len = fInfo.EndOfFile.QuadPart;
  67. fop_fd->pos = 0;
  68. return fop_fd;
  69. bail:
  70. return NULL;
  71. }
  72. int
  73. _lws_plat_file_close(lws_fop_fd_t *fop_fd)
  74. {
  75. HANDLE fd = (*fop_fd)->fd;
  76. free(*fop_fd);
  77. *fop_fd = NULL;
  78. CloseHandle((HANDLE)fd);
  79. return 0;
  80. }
  81. lws_fileofs_t
  82. _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
  83. {
  84. LARGE_INTEGER l;
  85. l.QuadPart = offset;
  86. return SetFilePointerEx((HANDLE)fop_fd->fd, l, NULL, FILE_CURRENT);
  87. }
  88. int
  89. _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  90. uint8_t *buf, lws_filepos_t len)
  91. {
  92. DWORD _amount;
  93. if (!ReadFile((HANDLE)fop_fd->fd, buf, (DWORD)len, &_amount, NULL)) {
  94. *amount = 0;
  95. return 1;
  96. }
  97. fop_fd->pos += _amount;
  98. *amount = (unsigned long)_amount;
  99. return 0;
  100. }
  101. int
  102. _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  103. uint8_t* buf, lws_filepos_t len)
  104. {
  105. DWORD _amount;
  106. if (!WriteFile((HANDLE)fop_fd->fd, buf, (DWORD)len, &_amount, NULL)) {
  107. *amount = 0;
  108. return 1;
  109. }
  110. fop_fd->pos += _amount;
  111. *amount = (unsigned long)_amount;
  112. return 0;
  113. }
  114. int
  115. lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
  116. int len)
  117. {
  118. int n;
  119. n = write(fd, buf, len);
  120. lseek(fd, 0, SEEK_SET);
  121. return n != len;
  122. }
  123. int
  124. lws_plat_write_file(const char *filename, void *buf, int len)
  125. {
  126. int m, fd;
  127. fd = lws_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  128. if (fd == -1)
  129. return -1;
  130. m = write(fd, buf, len);
  131. close(fd);
  132. return m != len;
  133. }
  134. int
  135. lws_plat_read_file(const char *filename, void *buf, int len)
  136. {
  137. int n, fd = lws_open(filename, O_RDONLY);
  138. if (fd == -1)
  139. return -1;
  140. n = read(fd, buf, len);
  141. close(fd);
  142. return n;
  143. }