vfs.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. void
  26. lws_set_fops(struct lws_context *context, const struct lws_plat_file_ops *fops)
  27. {
  28. context->fops = fops;
  29. }
  30. lws_filepos_t
  31. lws_vfs_tell(lws_fop_fd_t fop_fd)
  32. {
  33. return fop_fd->pos;
  34. }
  35. lws_filepos_t
  36. lws_vfs_get_length(lws_fop_fd_t fop_fd)
  37. {
  38. return fop_fd->len;
  39. }
  40. uint32_t
  41. lws_vfs_get_mod_time(lws_fop_fd_t fop_fd)
  42. {
  43. return fop_fd->mod_time;
  44. }
  45. lws_fileofs_t
  46. lws_vfs_file_seek_set(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
  47. {
  48. lws_fileofs_t ofs;
  49. ofs = fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, offset - fop_fd->pos);
  50. return ofs;
  51. }
  52. lws_fileofs_t
  53. lws_vfs_file_seek_end(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
  54. {
  55. return fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, fop_fd->len +
  56. fop_fd->pos + offset);
  57. }
  58. const struct lws_plat_file_ops *
  59. lws_vfs_select_fops(const struct lws_plat_file_ops *fops, const char *vfs_path,
  60. const char **vpath)
  61. {
  62. const struct lws_plat_file_ops *pf;
  63. const char *p = vfs_path;
  64. int n;
  65. *vpath = NULL;
  66. /* no non-platform fops, just use that */
  67. if (!fops->next)
  68. return fops;
  69. /*
  70. * scan the vfs path looking for indications we are to be
  71. * handled by a specific fops
  72. */
  73. while (p && *p) {
  74. if (*p != '/') {
  75. p++;
  76. continue;
  77. }
  78. /* the first one is always platform fops, so skip */
  79. pf = fops->next;
  80. while (pf) {
  81. n = 0;
  82. while (n < (int)LWS_ARRAY_SIZE(pf->fi) && pf->fi[n].sig) {
  83. if (p >= vfs_path + pf->fi[n].len)
  84. if (!strncmp(p - (pf->fi[n].len - 1),
  85. pf->fi[n].sig,
  86. pf->fi[n].len - 1)) {
  87. *vpath = p + 1;
  88. return pf;
  89. }
  90. n++;
  91. }
  92. pf = pf->next;
  93. }
  94. p++;
  95. }
  96. return fops;
  97. }
  98. lws_fop_fd_t LWS_WARN_UNUSED_RESULT
  99. lws_vfs_file_open(const struct lws_plat_file_ops *fops, const char *vfs_path,
  100. lws_fop_flags_t *flags)
  101. {
  102. const char *vpath = "";
  103. const struct lws_plat_file_ops *selected;
  104. selected = lws_vfs_select_fops(fops, vfs_path, &vpath);
  105. return selected->LWS_FOP_OPEN(fops, vfs_path, vpath, flags);
  106. }
  107. struct lws_plat_file_ops *
  108. lws_get_fops(struct lws_context *context)
  109. {
  110. return (struct lws_plat_file_ops *)context->fops;
  111. }