1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
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
25 #include "private-lib-core.h"
26
27 void
lws_set_fops(struct lws_context * context,const struct lws_plat_file_ops * fops)28 lws_set_fops(struct lws_context *context, const struct lws_plat_file_ops *fops)
29 {
30 context->fops = fops;
31 }
32
33 lws_filepos_t
lws_vfs_tell(lws_fop_fd_t fop_fd)34 lws_vfs_tell(lws_fop_fd_t fop_fd)
35 {
36 return fop_fd->pos;
37 }
38
39 lws_filepos_t
lws_vfs_get_length(lws_fop_fd_t fop_fd)40 lws_vfs_get_length(lws_fop_fd_t fop_fd)
41 {
42 return fop_fd->len;
43 }
44
45 uint32_t
lws_vfs_get_mod_time(lws_fop_fd_t fop_fd)46 lws_vfs_get_mod_time(lws_fop_fd_t fop_fd)
47 {
48 return fop_fd->mod_time;
49 }
50
51 lws_fileofs_t
lws_vfs_file_seek_set(lws_fop_fd_t fop_fd,lws_fileofs_t offset)52 lws_vfs_file_seek_set(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
53 {
54 lws_fileofs_t ofs;
55
56 ofs = fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, offset - fop_fd->pos);
57
58 return ofs;
59 }
60
61
62 lws_fileofs_t
lws_vfs_file_seek_end(lws_fop_fd_t fop_fd,lws_fileofs_t offset)63 lws_vfs_file_seek_end(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
64 {
65 return fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, fop_fd->len +
66 fop_fd->pos + offset);
67 }
68
69
70 const struct lws_plat_file_ops *
lws_vfs_select_fops(const struct lws_plat_file_ops * fops,const char * vfs_path,const char ** vpath)71 lws_vfs_select_fops(const struct lws_plat_file_ops *fops, const char *vfs_path,
72 const char **vpath)
73 {
74 const struct lws_plat_file_ops *pf;
75 const char *p = vfs_path;
76 int n;
77
78 *vpath = NULL;
79
80 /* no non-platform fops, just use that */
81
82 if (!fops->next)
83 return fops;
84
85 /*
86 * scan the vfs path looking for indications we are to be
87 * handled by a specific fops
88 */
89
90 while (p && *p) {
91 if (*p != '/') {
92 p++;
93 continue;
94 }
95 /* the first one is always platform fops, so skip */
96 pf = fops->next;
97 while (pf) {
98 n = 0;
99 while (n < (int)LWS_ARRAY_SIZE(pf->fi) && pf->fi[n].sig) {
100 if (p >= vfs_path + pf->fi[n].len)
101 if (!strncmp(p - (pf->fi[n].len - 1),
102 pf->fi[n].sig,
103 pf->fi[n].len - 1)) {
104 *vpath = p + 1;
105 return pf;
106 }
107
108 n++;
109 }
110 pf = pf->next;
111 }
112 p++;
113 }
114
115 return fops;
116 }
117
118 lws_fop_fd_t LWS_WARN_UNUSED_RESULT
lws_vfs_file_open(const struct lws_plat_file_ops * fops,const char * vfs_path,lws_fop_flags_t * flags)119 lws_vfs_file_open(const struct lws_plat_file_ops *fops, const char *vfs_path,
120 lws_fop_flags_t *flags)
121 {
122 const char *vpath = "";
123 const struct lws_plat_file_ops *selected;
124
125 selected = lws_vfs_select_fops(fops, vfs_path, &vpath);
126
127 return selected->LWS_FOP_OPEN(fops, vfs_path, vpath, flags);
128 }
129
130
131 struct lws_plat_file_ops *
lws_get_fops(struct lws_context * context)132 lws_get_fops(struct lws_context *context)
133 {
134 return (struct lws_plat_file_ops *)context->fops;
135 }
136
137