unix-file.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #if !defined(_GNU_SOURCE)
  25. #define _GNU_SOURCE
  26. #endif
  27. #include "private-lib-core.h"
  28. #include <pwd.h>
  29. #include <grp.h>
  30. #ifdef LWS_WITH_PLUGINS
  31. #include <dlfcn.h>
  32. #endif
  33. #include <dirent.h>
  34. int lws_plat_apply_FD_CLOEXEC(int n)
  35. {
  36. if (n == -1)
  37. return 0;
  38. return fcntl(n, F_SETFD, FD_CLOEXEC);
  39. }
  40. int
  41. lws_plat_write_file(const char *filename, void *buf, int len)
  42. {
  43. int m, fd;
  44. fd = lws_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  45. if (fd == -1)
  46. return 1;
  47. m = write(fd, buf, len);
  48. close(fd);
  49. return m != len;
  50. }
  51. int
  52. lws_plat_read_file(const char *filename, void *buf, int len)
  53. {
  54. int n, fd = lws_open(filename, O_RDONLY);
  55. if (fd == -1)
  56. return -1;
  57. n = read(fd, buf, len);
  58. close(fd);
  59. return n;
  60. }
  61. lws_fop_fd_t
  62. _lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename,
  63. const char *vpath, lws_fop_flags_t *flags)
  64. {
  65. struct stat stat_buf;
  66. int ret = lws_open(filename, (*flags) & LWS_FOP_FLAGS_MASK, 0664);
  67. lws_fop_fd_t fop_fd;
  68. if (ret < 0)
  69. return NULL;
  70. if (fstat(ret, &stat_buf) < 0)
  71. goto bail;
  72. fop_fd = malloc(sizeof(*fop_fd));
  73. if (!fop_fd)
  74. goto bail;
  75. fop_fd->fops = fops;
  76. fop_fd->flags = *flags;
  77. fop_fd->fd = ret;
  78. fop_fd->filesystem_priv = NULL; /* we don't use it */
  79. fop_fd->len = stat_buf.st_size;
  80. fop_fd->pos = 0;
  81. return fop_fd;
  82. bail:
  83. close(ret);
  84. return NULL;
  85. }
  86. int
  87. _lws_plat_file_close(lws_fop_fd_t *fop_fd)
  88. {
  89. int fd = (*fop_fd)->fd;
  90. free(*fop_fd);
  91. *fop_fd = NULL;
  92. return close(fd);
  93. }
  94. lws_fileofs_t
  95. _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
  96. {
  97. lws_fileofs_t r;
  98. if (offset > 0 &&
  99. offset > (lws_fileofs_t)fop_fd->len - (lws_fileofs_t)fop_fd->pos)
  100. offset = fop_fd->len - fop_fd->pos;
  101. if ((lws_fileofs_t)fop_fd->pos + offset < 0)
  102. offset = -fop_fd->pos;
  103. r = lseek(fop_fd->fd, offset, SEEK_CUR);
  104. if (r >= 0)
  105. fop_fd->pos = r;
  106. else
  107. lwsl_err("error seeking from cur %ld, offset %ld\n",
  108. (long)fop_fd->pos, (long)offset);
  109. return r;
  110. }
  111. int
  112. _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  113. uint8_t *buf, lws_filepos_t len)
  114. {
  115. long n;
  116. n = read((int)fop_fd->fd, buf, len);
  117. if (n == -1) {
  118. *amount = 0;
  119. return -1;
  120. }
  121. fop_fd->pos += n;
  122. lwsl_debug("%s: read %ld of req %ld, pos %ld, len %ld\n", __func__, n,
  123. (long)len, (long)fop_fd->pos, (long)fop_fd->len);
  124. *amount = n;
  125. return 0;
  126. }
  127. int
  128. _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
  129. uint8_t *buf, lws_filepos_t len)
  130. {
  131. long n;
  132. n = write((int)fop_fd->fd, buf, len);
  133. if (n == -1) {
  134. *amount = 0;
  135. return -1;
  136. }
  137. fop_fd->pos += n;
  138. *amount = n;
  139. return 0;
  140. }