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/*! \defgroup fops file operation wrapping
26 *
27 * ##File operation wrapping
28 *
29 * Use these helper functions if you want to access a file from the perspective
30 * of a specific wsi, which is usually the case. If you just want contextless
31 * file access, use the fops callbacks directly with NULL wsi instead of these
32 * helpers.
33 *
34 * If so, then it calls the platform handler or user overrides where present
35 * (as defined in info->fops)
36 *
37 * The advantage from all this is user code can be portable for file operations
38 * without having to deal with differences between platforms.
39 */
40//@{
41
42/** struct lws_plat_file_ops - Platform-specific file operations
43 *
44 * These provide platform-agnostic ways to deal with filesystem access in the
45 * library and in the user code.
46 */
47
48#if defined(LWS_PLAT_FREERTOS)
49/* sdk preprocessor defs? compiler issue? gets confused with member names */
50#define LWS_FOP_OPEN _open
51#define LWS_FOP_CLOSE _close
52#define LWS_FOP_SEEK_CUR _seek_cur
53#define LWS_FOP_READ _read
54#define LWS_FOP_WRITE _write
55#else
56#define LWS_FOP_OPEN open
57#define LWS_FOP_CLOSE close
58#define LWS_FOP_SEEK_CUR seek_cur
59#define LWS_FOP_READ read
60#define LWS_FOP_WRITE write
61#endif
62
63#define LWS_FOP_FLAGS_MASK ((1 << 23) - 1)
64#define LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP (1 << 24)
65#define LWS_FOP_FLAG_COMPR_IS_GZIP (1 << 25)
66#define LWS_FOP_FLAG_MOD_TIME_VALID (1 << 26)
67#define LWS_FOP_FLAG_VIRTUAL (1 << 27)
68
69struct lws_plat_file_ops;
70
71struct lws_fop_fd {
72 lws_filefd_type fd;
73 /**< real file descriptor related to the file... */
74 const struct lws_plat_file_ops *fops;
75 /**< fops that apply to this fop_fd */
76 void *filesystem_priv;
77 /**< ignored by lws; owned by the fops handlers */
78 lws_filepos_t pos;
79 /**< generic "position in file" */
80 lws_filepos_t len;
81 /**< generic "length of file" */
82 lws_fop_flags_t flags;
83 /**< copy of the returned flags */
84 uint32_t mod_time;
85 /**< optional "modification time of file", only valid if .open()
86 * set the LWS_FOP_FLAG_MOD_TIME_VALID flag */
87};
88typedef struct lws_fop_fd *lws_fop_fd_t;
89
90struct lws_fops_index {
91 const char *sig; /* NULL or vfs signature, eg, ".zip/" */
92 uint8_t len; /* length of above string */
93};
94
95struct lws_plat_file_ops {
96 lws_fop_fd_t (*LWS_FOP_OPEN)(const struct lws_plat_file_ops *fops_own,
97 const struct lws_plat_file_ops *fops,
98 const char *filename, const char *vpath,
99 lws_fop_flags_t *flags);
100 /**< Open file (always binary access if plat supports it)
101 * fops_own is the fops this was called through. fops is the base
102 * fops the open can use to find files to process as present as its own,
103 * like the zip fops does.
104 *
105 * vpath may be NULL, or if the fops understands it, the point at which
106 * the filename's virtual part starts.
107 * *flags & LWS_FOP_FLAGS_MASK should be set to O_RDONLY or O_RDWR.
108 * If the file may be gzip-compressed,
109 * LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP is set. If it actually is
110 * gzip-compressed, then the open handler should OR
111 * LWS_FOP_FLAG_COMPR_IS_GZIP on to *flags before returning.
112 */
113 int (*LWS_FOP_CLOSE)(lws_fop_fd_t *fop_fd);
114 /**< close file AND set the pointer to NULL */
115 lws_fileofs_t (*LWS_FOP_SEEK_CUR)(lws_fop_fd_t fop_fd,
116 lws_fileofs_t offset_from_cur_pos);
117 /**< seek from current position */
118 int (*LWS_FOP_READ)(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
119 uint8_t *buf, lws_filepos_t len);
120 /**< Read from file, on exit *amount is set to amount actually read */
121 int (*LWS_FOP_WRITE)(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
122 uint8_t *buf, lws_filepos_t len);
123 /**< Write to file, on exit *amount is set to amount actually written */
124
125 struct lws_fops_index fi[3];
126 /**< vfs path signatures implying use of this fops */
127
128 const struct lws_plat_file_ops *next;
129 /**< NULL or next fops in list... eg copy static fops def to heap
130 * and modify copy at runtime */
131
132 struct lws_context *cx;
133 /**< the lws_context... eg copy static fops def to heap
134 * and modify copy at runtime */
135
136 /* Add new things just above here ---^
137 * This is part of the ABI, don't needlessly break compatibility */
138};
139
140/**
141 * lws_get_fops() - get current file ops
142 *
143 * \param context: context
144 */
145LWS_VISIBLE LWS_EXTERN struct lws_plat_file_ops * LWS_WARN_UNUSED_RESULT
146lws_get_fops(struct lws_context *context);
147LWS_VISIBLE LWS_EXTERN void
148lws_set_fops(struct lws_context *context, const struct lws_plat_file_ops *fops);
149/**
150 * lws_vfs_tell() - get current file position
151 *
152 * \param fop_fd: fop_fd we are asking about
153 */
154LWS_VISIBLE LWS_EXTERN lws_filepos_t LWS_WARN_UNUSED_RESULT
155lws_vfs_tell(lws_fop_fd_t fop_fd);
156/**
157 * lws_vfs_get_length() - get current file total length in bytes
158 *
159 * \param fop_fd: fop_fd we are asking about
160 */
161LWS_VISIBLE LWS_EXTERN lws_filepos_t LWS_WARN_UNUSED_RESULT
162lws_vfs_get_length(lws_fop_fd_t fop_fd);
163/**
164 * lws_vfs_get_mod_time() - get time file last modified
165 *
166 * \param fop_fd: fop_fd we are asking about
167 */
168LWS_VISIBLE LWS_EXTERN uint32_t LWS_WARN_UNUSED_RESULT
169lws_vfs_get_mod_time(lws_fop_fd_t fop_fd);
170/**
171 * lws_vfs_file_seek_set() - seek relative to start of file
172 *
173 * \param fop_fd: fop_fd we are seeking in
174 * \param offset: offset from start of file
175 */
176LWS_VISIBLE LWS_EXTERN lws_fileofs_t
177lws_vfs_file_seek_set(lws_fop_fd_t fop_fd, lws_fileofs_t offset);
178/**
179 * lws_vfs_file_seek_end() - seek relative to end of file
180 *
181 * \param fop_fd: fop_fd we are seeking in
182 * \param offset: offset from start of file
183 */
184LWS_VISIBLE LWS_EXTERN lws_fileofs_t
185lws_vfs_file_seek_end(lws_fop_fd_t fop_fd, lws_fileofs_t offset);
186
187extern struct lws_plat_file_ops fops_zip;
188
189/**
190 * lws_plat_file_open() - open vfs filepath
191 *
192 * \param fops: file ops struct that applies to this descriptor
193 * \param vfs_path: filename to open
194 * \param flags: pointer to open flags
195 *
196 * The vfs_path is scanned for known fops signatures, and the open directed
197 * to any matching fops open.
198 *
199 * User code should use this api to perform vfs opens.
200 *
201 * returns semi-opaque handle
202 */
203LWS_VISIBLE LWS_EXTERN lws_fop_fd_t LWS_WARN_UNUSED_RESULT
204lws_vfs_file_open(const struct lws_plat_file_ops *fops, const char *vfs_path,
205 lws_fop_flags_t *flags);
206
207/**
208 * lws_plat_file_close() - close file
209 *
210 * \param fop_fd: file handle to close
211 */
212static LWS_INLINE int
213lws_vfs_file_close(lws_fop_fd_t *fop_fd)
214{
215 if (*fop_fd && (*fop_fd)->fops)
216 return (*fop_fd)->fops->LWS_FOP_CLOSE(fop_fd);
217
218 return 0;
219}
220
221/**
222 * lws_plat_file_seek_cur() - close file
223 *
224 *
225 * \param fop_fd: file handle
226 * \param offset: position to seek to
227 */
228static LWS_INLINE lws_fileofs_t
229lws_vfs_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
230{
231 return fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, offset);
232}
233/**
234 * lws_plat_file_read() - read from file
235 *
236 * \param fop_fd: file handle
237 * \param amount: how much to read (rewritten by call)
238 * \param buf: buffer to write to
239 * \param len: max length
240 */
241static LWS_INLINE int LWS_WARN_UNUSED_RESULT
242lws_vfs_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
243 uint8_t *buf, lws_filepos_t len)
244{
245 return fop_fd->fops->LWS_FOP_READ(fop_fd, amount, buf, len);
246}
247/**
248 * lws_plat_file_write() - write from file
249 *
250 * \param fop_fd: file handle
251 * \param amount: how much to write (rewritten by call)
252 * \param buf: buffer to read from
253 * \param len: max length
254 */
255static LWS_INLINE int LWS_WARN_UNUSED_RESULT
256lws_vfs_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
257 uint8_t *buf, lws_filepos_t len)
258{
259 return fop_fd->fops->LWS_FOP_WRITE(fop_fd, amount, buf, len);
260}
261
262/* these are the platform file operations implementations... they can
263 * be called directly and used in fops arrays
264 */
265
266LWS_VISIBLE LWS_EXTERN lws_fop_fd_t
267_lws_plat_file_open(const struct lws_plat_file_ops *fops_own,
268 const struct lws_plat_file_ops *fops, const char *filename,
269 const char *vpath, lws_fop_flags_t *flags);
270LWS_VISIBLE LWS_EXTERN int
271_lws_plat_file_close(lws_fop_fd_t *fop_fd);
272LWS_VISIBLE LWS_EXTERN lws_fileofs_t
273_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset);
274LWS_VISIBLE LWS_EXTERN int
275_lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
276 uint8_t *buf, lws_filepos_t len);
277LWS_VISIBLE LWS_EXTERN int
278_lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
279 uint8_t *buf, lws_filepos_t len);
280
281LWS_VISIBLE LWS_EXTERN int
282lws_alloc_vfs_file(struct lws_context *context, const char *filename,
283 uint8_t **buf, lws_filepos_t *amount);
284//@}
285