freertos-file.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #include "private-lib-core.h"
  25. int lws_plat_apply_FD_CLOEXEC(int n)
  26. {
  27. return 0;
  28. }
  29. lws_fop_fd_t IRAM_ATTR
  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. struct stat stat_buf;
  34. lws_fop_fd_t fop_fd;
  35. int ret = open(filename, *flags, 0664);
  36. if (ret < 0)
  37. return NULL;
  38. if (fstat(ret, &stat_buf) < 0)
  39. goto bail;
  40. fop_fd = lws_malloc(sizeof(*fop_fd), "fops open");
  41. if (!fop_fd)
  42. goto bail;
  43. fop_fd->fops = fops;
  44. fop_fd->fd = ret;
  45. fop_fd->flags = *flags;
  46. fop_fd->filesystem_priv = NULL; /* we don't use it */
  47. fop_fd->pos = 0;
  48. fop_fd->len = stat_buf.st_size;
  49. return fop_fd;
  50. bail:
  51. close(ret);
  52. return NULL;
  53. }
  54. int IRAM_ATTR
  55. _lws_plat_file_close(lws_fop_fd_t *fops_fd)
  56. {
  57. int fd = (*fops_fd)->fd;
  58. lws_free(*fops_fd);
  59. *fops_fd = NULL;
  60. return close(fd);
  61. }
  62. lws_fileofs_t IRAM_ATTR
  63. _lws_plat_file_seek_cur(lws_fop_fd_t fops_fd, lws_fileofs_t offset)
  64. {
  65. return lseek(fops_fd->fd, offset, SEEK_CUR);
  66. }
  67. int IRAM_ATTR
  68. _lws_plat_file_read(lws_fop_fd_t fops_fd, lws_filepos_t *amount,
  69. uint8_t *buf, lws_filepos_t len)
  70. {
  71. long n;
  72. n = read(fops_fd->fd, buf, len);
  73. if (n == -1) {
  74. *amount = 0;
  75. return -1;
  76. }
  77. fops_fd->pos += n;
  78. *amount = n;
  79. return 0;
  80. }
  81. int IRAM_ATTR
  82. _lws_plat_file_write(lws_fop_fd_t fops_fd, lws_filepos_t *amount,
  83. uint8_t *buf, lws_filepos_t len)
  84. {
  85. long n;
  86. n = write(fops_fd->fd, buf, len);
  87. if (n == -1) {
  88. *amount = 0;
  89. return -1;
  90. }
  91. fops_fd->pos += n;
  92. *amount = n;
  93. return 0;
  94. }
  95. #if defined(LWS_AMAZON_RTOS)
  96. int
  97. lws_find_string_in_file(const char *filename, const char *string, int stringlen)
  98. {
  99. return 0;
  100. }
  101. #else
  102. int
  103. lws_find_string_in_file(const char *filename, const char *string, int stringlen)
  104. {
  105. nvs_handle nvh;
  106. size_t s;
  107. int n;
  108. char buf[64], result[64];
  109. const char *p = strchr(string, ':'), *q;
  110. if (!p)
  111. return 0;
  112. q = string;
  113. n = 0;
  114. while ((size_t)n < sizeof(buf) - 1 && q != p)
  115. buf[n++] = *q++;
  116. buf[n] = '\0';
  117. ESP_ERROR_CHECK(nvs_open(filename, NVS_READWRITE, &nvh));
  118. s = sizeof(result) - 1;
  119. n = nvs_get_str(nvh, buf, result, &s);
  120. nvs_close(nvh);
  121. if (n != ESP_OK)
  122. return 0;
  123. return !strcmp(p + 1, result);
  124. }
  125. #endif
  126. #if !defined(LWS_AMAZON_RTOS)
  127. int
  128. lws_plat_write_file(const char *filename, void *buf, int len)
  129. {
  130. nvs_handle nvh;
  131. int n;
  132. if (nvs_open("lws-station", NVS_READWRITE, &nvh)) {
  133. lwsl_notice("%s: failed to open nvs\n", __func__);
  134. return -1;
  135. }
  136. n = nvs_set_blob(nvh, filename, buf, len);
  137. if (n >= 0)
  138. nvs_commit(nvh);
  139. nvs_close(nvh);
  140. lwsl_notice("%s: wrote %s (%d)\n", __func__, filename, n);
  141. return n;
  142. }
  143. /* we write vhostname.cert.pem and vhostname.key.pem, 0 return means OK */
  144. int
  145. lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
  146. int len)
  147. {
  148. const char *name = vhost->tls.alloc_cert_path;
  149. if (is_key)
  150. name = vhost->tls.key_path;
  151. return lws_plat_write_file(name, buf, len) < 0;
  152. }
  153. int
  154. lws_plat_read_file(const char *filename, void *buf, int len)
  155. {
  156. nvs_handle nvh;
  157. size_t s = 0;
  158. int n = 0;
  159. if (nvs_open("lws-station", NVS_READWRITE, &nvh)) {
  160. lwsl_notice("%s: failed to open nvs\n", __func__);
  161. return 1;
  162. }
  163. ESP_ERROR_CHECK(nvs_open("lws-station", NVS_READWRITE, &nvh));
  164. if (nvs_get_blob(nvh, filename, NULL, &s) != ESP_OK)
  165. goto bail;
  166. if (s > (size_t)len)
  167. goto bail;
  168. n = nvs_get_blob(nvh, filename, buf, &s);
  169. nvs_close(nvh);
  170. lwsl_notice("%s: read %s (%d)\n", __func__, filename, (int)s);
  171. if (n)
  172. return -1;
  173. return (int)s;
  174. bail:
  175. nvs_close(nvh);
  176. return -1;
  177. }
  178. #endif /* LWS_AMAZON_RTOS */