/src/libwebsockets/lib/core/vfs.c
Line | Count | Source |
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 |
28 | | lws_set_fops(struct lws_context *context, const struct lws_plat_file_ops *fops) |
29 | 0 | { |
30 | 0 | context->fops = fops; |
31 | 0 | } |
32 | | |
33 | | lws_filepos_t |
34 | | lws_vfs_tell(lws_fop_fd_t fop_fd) |
35 | 0 | { |
36 | 0 | return fop_fd->pos; |
37 | 0 | } |
38 | | |
39 | | lws_filepos_t |
40 | | lws_vfs_get_length(lws_fop_fd_t fop_fd) |
41 | 0 | { |
42 | 0 | return fop_fd->len; |
43 | 0 | } |
44 | | |
45 | | uint32_t |
46 | | lws_vfs_get_mod_time(lws_fop_fd_t fop_fd) |
47 | 0 | { |
48 | 0 | return fop_fd->mod_time; |
49 | 0 | } |
50 | | |
51 | | lws_fileofs_t |
52 | | lws_vfs_file_seek_set(lws_fop_fd_t fop_fd, lws_fileofs_t offset) |
53 | 0 | { |
54 | 0 | lws_fileofs_t ofs; |
55 | |
|
56 | 0 | ofs = fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, |
57 | 0 | offset - (lws_fileofs_t)fop_fd->pos); |
58 | |
|
59 | 0 | return ofs; |
60 | 0 | } |
61 | | |
62 | | |
63 | | lws_fileofs_t |
64 | | lws_vfs_file_seek_end(lws_fop_fd_t fop_fd, lws_fileofs_t offset) |
65 | 0 | { |
66 | 0 | return fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, |
67 | 0 | (lws_fileofs_t)fop_fd->len + (lws_fileofs_t)fop_fd->pos + offset); |
68 | 0 | } |
69 | | |
70 | | |
71 | | const struct lws_plat_file_ops * |
72 | | lws_vfs_select_fops(const struct lws_plat_file_ops *fops, const char *vfs_path, |
73 | | const char **vpath) |
74 | 0 | { |
75 | 0 | const struct lws_plat_file_ops *pf; |
76 | 0 | const char *p = vfs_path; |
77 | 0 | int n; |
78 | |
|
79 | 0 | *vpath = NULL; |
80 | | |
81 | | /* no non-platform fops, just use that */ |
82 | |
|
83 | 0 | if (!fops->next) |
84 | 0 | return fops; |
85 | | |
86 | | /* |
87 | | * scan the vfs path looking for indications we are to be |
88 | | * handled by a specific fops |
89 | | */ |
90 | | |
91 | 0 | pf = fops->next; /* the first one is always platform fops, so skip */ |
92 | 0 | while (pf) { |
93 | 0 | n = 0; |
94 | 0 | while (pf && n < (int)LWS_ARRAY_SIZE(pf->fi) && pf->fi[n].sig) { |
95 | 0 | if (!strncmp(p, pf->fi[n].sig, pf->fi[n].len)) { |
96 | 0 | *vpath = p + pf->fi[n].len; |
97 | | //lwsl_notice("%s: hit, vpath '%s'\n", |
98 | | // __func__, *vpath); |
99 | 0 | return pf; |
100 | 0 | } |
101 | 0 | pf = pf->next; |
102 | 0 | n++; |
103 | 0 | } |
104 | 0 | } |
105 | | |
106 | 0 | while (p && *p) { |
107 | 0 | if (*p != '/') { |
108 | 0 | p++; |
109 | 0 | continue; |
110 | 0 | } |
111 | | |
112 | 0 | pf = fops->next; /* the first one is always platform fops, so skip */ |
113 | 0 | while (pf) { |
114 | 0 | n = 0; |
115 | 0 | while (n < (int)LWS_ARRAY_SIZE(pf->fi) && pf->fi[n].sig) { |
116 | 0 | lwsl_warn("%s %s\n", p, pf->fi[n].sig); |
117 | 0 | if (p >= vfs_path + pf->fi[n].len) |
118 | | /* |
119 | | * Accept sigs like .... .zip or |
120 | | * mysig... |
121 | | */ |
122 | 0 | if (!strncmp(p - (pf->fi[n].len - 1), |
123 | 0 | pf->fi[n].sig, |
124 | 0 | (unsigned int)(pf->fi[n].len - 1)) || |
125 | 0 | !strncmp(p, pf->fi[n].sig, pf->fi[n].len)) { |
126 | 0 | *vpath = p + 1; |
127 | 0 | return pf; |
128 | 0 | } |
129 | | |
130 | 0 | n++; |
131 | 0 | } |
132 | 0 | pf = pf->next; |
133 | 0 | } |
134 | 0 | p++; |
135 | 0 | } |
136 | | |
137 | 0 | return fops; |
138 | 0 | } |
139 | | |
140 | | lws_fop_fd_t LWS_WARN_UNUSED_RESULT |
141 | | lws_vfs_file_open(const struct lws_plat_file_ops *fops, const char *vfs_path, |
142 | | lws_fop_flags_t *flags) |
143 | 0 | { |
144 | 0 | const char *vpath = ""; |
145 | 0 | const struct lws_plat_file_ops *selected; |
146 | |
|
147 | 0 | selected = lws_vfs_select_fops(fops, vfs_path, &vpath); |
148 | |
|
149 | 0 | return selected->LWS_FOP_OPEN(selected, fops, vfs_path, vpath, flags); |
150 | 0 | } |
151 | | |
152 | | |
153 | | struct lws_plat_file_ops * |
154 | | lws_get_fops(struct lws_context *context) |
155 | 0 | { |
156 | 0 | return (struct lws_plat_file_ops *)context->fops; |
157 | 0 | } |
158 | | |