xref: /third_party/libwebsockets/lib/core/vfs.c (revision d4afb5ce)
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
27void
28lws_set_fops(struct lws_context *context, const struct lws_plat_file_ops *fops)
29{
30	context->fops = fops;
31}
32
33lws_filepos_t
34lws_vfs_tell(lws_fop_fd_t fop_fd)
35{
36	return fop_fd->pos;
37}
38
39lws_filepos_t
40lws_vfs_get_length(lws_fop_fd_t fop_fd)
41{
42	return fop_fd->len;
43}
44
45uint32_t
46lws_vfs_get_mod_time(lws_fop_fd_t fop_fd)
47{
48	return fop_fd->mod_time;
49}
50
51lws_fileofs_t
52lws_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,
57			offset - (lws_fileofs_t)fop_fd->pos);
58
59	return ofs;
60}
61
62
63lws_fileofs_t
64lws_vfs_file_seek_end(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
65{
66	return fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd,
67			(lws_fileofs_t)fop_fd->len + (lws_fileofs_t)fop_fd->pos + offset);
68}
69
70
71const struct lws_plat_file_ops *
72lws_vfs_select_fops(const struct lws_plat_file_ops *fops, const char *vfs_path,
73		    const char **vpath)
74{
75	const struct lws_plat_file_ops *pf;
76	const char *p = vfs_path;
77	int n;
78
79	*vpath = NULL;
80
81	/* no non-platform fops, just use that */
82
83	if (!fops->next)
84		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	while (p && *p) {
92		if (*p != '/') {
93			p++;
94			continue;
95		}
96		/* the first one is always platform fops, so skip */
97		pf = fops->next;
98		while (pf) {
99			n = 0;
100			while (n < (int)LWS_ARRAY_SIZE(pf->fi) && pf->fi[n].sig) {
101				if (p >= vfs_path + pf->fi[n].len)
102					if (!strncmp(p - (pf->fi[n].len - 1),
103						     pf->fi[n].sig,
104						     (unsigned int)(pf->fi[n].len - 1))) {
105						*vpath = p + 1;
106						return pf;
107					}
108
109				n++;
110			}
111			pf = pf->next;
112		}
113		p++;
114	}
115
116	return fops;
117}
118
119lws_fop_fd_t LWS_WARN_UNUSED_RESULT
120lws_vfs_file_open(const struct lws_plat_file_ops *fops, const char *vfs_path,
121		  lws_fop_flags_t *flags)
122{
123	const char *vpath = "";
124	const struct lws_plat_file_ops *selected;
125
126	selected = lws_vfs_select_fops(fops, vfs_path, &vpath);
127
128	return selected->LWS_FOP_OPEN(fops, vfs_path, vpath, flags);
129}
130
131
132struct lws_plat_file_ops *
133lws_get_fops(struct lws_context *context)
134{
135	return (struct lws_plat_file_ops *)context->fops;
136}
137
138