windows-file.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
  25. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  26. #endif
  27. #include "private-lib-core.h"
  28. int lws_plat_apply_FD_CLOEXEC(int n)
  29. {
  30. return 0;
  31. }
  32. lws_fop_fd_t
  33. _lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename,
  34. const char *vpath, lws_fop_flags_t *flags)
  35. {
  36. HANDLE ret;
  37. WCHAR buf[MAX_PATH];
  38. lws_fop_fd_t fop_fd;
  39. LARGE_INTEGER llFileSize = {0};
  40. MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, LWS_ARRAY_SIZE(buf));
  41. if (((*flags) & 7) == _O_RDONLY) {
  42. ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ,
  43. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  44. } else {
  45. ret = CreateFileW(buf, GENERIC_WRITE, 0, NULL,
  46. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  47. }
  48. if (ret == LWS_INVALID_FILE)
  49. goto bail;
  50. fop_fd = malloc(sizeof(*fop_fd));
  51. if (!fop_fd)
  52. goto bail;
  53. fop_fd->fops = fops;
  54. fop_fd->fd = ret;
  55. fop_fd->filesystem_priv = NULL; /* we don't use it */
  56. fop_fd->flags = *flags;
  57. fop_fd->len = GetFileSize(ret, NULL);
  58. if(GetFileSizeEx(ret, &llFileSize))
  59. fop_fd->len = llFileSize.QuadPart;
  60. fop_fd->pos = 0;
  61. return fop_fd;
  62. bail:
  63. return NULL;
  64. }
  65. int
  66. _lws_plat_file_close(lws_fop_fd_t *fop_fd)
  67. {
  68. HANDLE fd = (*fop_fd)->fd;
  69. free(*fop_fd);
  70. *fop_fd = NULL;
  71. CloseHandle((HANDLE)fd);
  72. return 0;
  73. }
  74. lws_fileofs_t
  75. _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
  76. {
  77. LARGE_INTEGER l;
  78. l.QuadPart = offset;
  79. return SetFilePointerEx((HANDLE)fop_fd->fd, l, NULL, FILE_CURRENT);
  80. }
  81. int
  82. _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  83. uint8_t *buf, lws_filepos_t len)
  84. {
  85. DWORD _amount;
  86. if (!ReadFile((HANDLE)fop_fd->fd, buf, (DWORD)len, &_amount, NULL)) {
  87. *amount = 0;
  88. return 1;
  89. }
  90. fop_fd->pos += _amount;
  91. *amount = (unsigned long)_amount;
  92. return 0;
  93. }
  94. int
  95. _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  96. uint8_t* buf, lws_filepos_t len)
  97. {
  98. DWORD _amount;
  99. if (!WriteFile((HANDLE)fop_fd->fd, buf, (DWORD)len, &_amount, NULL)) {
  100. *amount = 0;
  101. return 1;
  102. }
  103. fop_fd->pos += _amount;
  104. *amount = (unsigned long)_amount;
  105. return 0;
  106. }
  107. int
  108. lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
  109. int len)
  110. {
  111. int n;
  112. n = write(fd, buf, len);
  113. lseek(fd, 0, SEEK_SET);
  114. return n != len;
  115. }
  116. int
  117. lws_plat_write_file(const char *filename, void *buf, int len)
  118. {
  119. int m, fd;
  120. fd = lws_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  121. if (fd == -1)
  122. return -1;
  123. m = write(fd, buf, len);
  124. close(fd);
  125. return m != len;
  126. }
  127. int
  128. lws_plat_read_file(const char *filename, void *buf, int len)
  129. {
  130. int n, fd = lws_open(filename, O_RDONLY);
  131. if (fd == -1)
  132. return -1;
  133. n = read(fd, buf, len);
  134. close(fd);
  135. return n;
  136. }