libwebsockets
Lightweight C library for HTML5 websockets
lws-vfs.h
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 
41 
48 #if defined(LWS_WITH_ESP32)
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 
69 struct lws_plat_file_ops;
70 
71 struct lws_fop_fd {
72  lws_filefd_type fd;
74  const struct lws_plat_file_ops *fops;
78  lws_filepos_t pos;
80  lws_filepos_t len;
82  lws_fop_flags_t flags;
84  uint32_t mod_time;
87 };
88 typedef struct lws_fop_fd *lws_fop_fd_t;
89 
91  const char *sig; /* NULL or vfs signature, eg, ".zip/" */
92  uint8_t len; /* length of above string */
93 };
94 
97  const char *filename, const char *vpath,
98  lws_fop_flags_t *flags);
108  int (*LWS_FOP_CLOSE)(lws_fop_fd_t *fop_fd);
110  lws_fileofs_t (*LWS_FOP_SEEK_CUR)(lws_fop_fd_t fop_fd,
111  lws_fileofs_t offset_from_cur_pos);
113  int (*LWS_FOP_READ)(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
114  uint8_t *buf, lws_filepos_t len);
116  int (*LWS_FOP_WRITE)(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
117  uint8_t *buf, lws_filepos_t len);
120  struct lws_fops_index fi[3];
123  const struct lws_plat_file_ops *next;
126  /* Add new things just above here ---^
127  * This is part of the ABI, don't needlessly break compatibility */
128 };
129 
135 LWS_VISIBLE LWS_EXTERN struct lws_plat_file_ops * LWS_WARN_UNUSED_RESULT
136 lws_get_fops(struct lws_context *context);
137 LWS_VISIBLE LWS_EXTERN void
138 lws_set_fops(struct lws_context *context, const struct lws_plat_file_ops *fops);
144 LWS_VISIBLE LWS_EXTERN lws_filepos_t LWS_WARN_UNUSED_RESULT
145 lws_vfs_tell(lws_fop_fd_t fop_fd);
151 LWS_VISIBLE LWS_EXTERN lws_filepos_t LWS_WARN_UNUSED_RESULT
158 LWS_VISIBLE LWS_EXTERN uint32_t LWS_WARN_UNUSED_RESULT
166 LWS_VISIBLE LWS_EXTERN lws_fileofs_t
167 lws_vfs_file_seek_set(lws_fop_fd_t fop_fd, lws_fileofs_t offset);
174 LWS_VISIBLE LWS_EXTERN lws_fileofs_t
175 lws_vfs_file_seek_end(lws_fop_fd_t fop_fd, lws_fileofs_t offset);
176 
177 extern struct lws_plat_file_ops fops_zip;
178 
193 LWS_VISIBLE LWS_EXTERN lws_fop_fd_t LWS_WARN_UNUSED_RESULT
194 lws_vfs_file_open(const struct lws_plat_file_ops *fops, const char *vfs_path,
195  lws_fop_flags_t *flags);
196 
202 static LWS_INLINE int
203 lws_vfs_file_close(lws_fop_fd_t *fop_fd)
204 {
205  return (*fop_fd)->fops->LWS_FOP_CLOSE(fop_fd);
206 }
207 
215 static LWS_INLINE lws_fileofs_t
216 lws_vfs_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
217 {
218  return fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, offset);
219 }
228 static LWS_INLINE int LWS_WARN_UNUSED_RESULT
229 lws_vfs_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
230  uint8_t *buf, lws_filepos_t len)
231 {
232  return fop_fd->fops->LWS_FOP_READ(fop_fd, amount, buf, len);
233 }
242 static LWS_INLINE int LWS_WARN_UNUSED_RESULT
243 lws_vfs_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
244  uint8_t *buf, lws_filepos_t len)
245 {
246  return fop_fd->fops->LWS_FOP_WRITE(fop_fd, amount, buf, len);
247 }
248 
249 /* these are the platform file operations implementations... they can
250  * be called directly and used in fops arrays
251  */
252 
253 LWS_VISIBLE LWS_EXTERN lws_fop_fd_t
254 _lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename,
255  const char *vpath, lws_fop_flags_t *flags);
256 LWS_VISIBLE LWS_EXTERN int
257 _lws_plat_file_close(lws_fop_fd_t *fop_fd);
258 LWS_VISIBLE LWS_EXTERN lws_fileofs_t
259 _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset);
260 LWS_VISIBLE LWS_EXTERN int
261 _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
262  uint8_t *buf, lws_filepos_t len);
263 LWS_VISIBLE LWS_EXTERN int
264 _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
265  uint8_t *buf, lws_filepos_t len);
266 
267 LWS_VISIBLE LWS_EXTERN int
268 lws_alloc_vfs_file(struct lws_context *context, const char *filename,
269  uint8_t **buf, lws_filepos_t *amount);
LWS_VISIBLE LWS_EXTERN 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)
const struct lws_plat_file_ops * fops
Definition: lws-vfs.h:74
Definition: lws-vfs.h:90
lws_fop_fd_t(* LWS_FOP_OPEN)(const struct lws_plat_file_ops *fops, const char *filename, const char *vpath, lws_fop_flags_t *flags)
Definition: lws-vfs.h:96
int(* LWS_FOP_CLOSE)(lws_fop_fd_t *fop_fd)
Definition: lws-vfs.h:108
LWS_VISIBLE LWS_EXTERN lws_filepos_t LWS_WARN_UNUSED_RESULT lws_vfs_tell(lws_fop_fd_t fop_fd)
LWS_VISIBLE LWS_EXTERN lws_filepos_t LWS_WARN_UNUSED_RESULT lws_vfs_get_length(lws_fop_fd_t fop_fd)
int(* LWS_FOP_WRITE)(lws_fop_fd_t fop_fd, lws_filepos_t *amount, uint8_t *buf, lws_filepos_t len)
Definition: lws-vfs.h:116
lws_filepos_t len
Definition: lws-vfs.h:80
struct lws_fops_index fi[3]
Definition: lws-vfs.h:120
lws_filefd_type fd
Definition: lws-vfs.h:72
lws_fileofs_t(* LWS_FOP_SEEK_CUR)(lws_fop_fd_t fop_fd, lws_fileofs_t offset_from_cur_pos)
Definition: lws-vfs.h:110
LWS_VISIBLE LWS_EXTERN struct lws_plat_file_ops *LWS_WARN_UNUSED_RESULT lws_get_fops(struct lws_context *context)
LWS_VISIBLE LWS_EXTERN lws_fileofs_t lws_vfs_file_seek_end(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
void * filesystem_priv
Definition: lws-vfs.h:76
lws_fop_flags_t flags
Definition: lws-vfs.h:82
Definition: lws-vfs.h:71
lws_filepos_t pos
Definition: lws-vfs.h:78
LWS_VISIBLE LWS_EXTERN lws_fileofs_t lws_vfs_file_seek_set(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
int(* LWS_FOP_READ)(lws_fop_fd_t fop_fd, lws_filepos_t *amount, uint8_t *buf, lws_filepos_t len)
Definition: lws-vfs.h:113
Definition: lws-vfs.h:95
const struct lws_plat_file_ops * next
Definition: lws-vfs.h:123
uint32_t mod_time
Definition: lws-vfs.h:84
LWS_VISIBLE LWS_EXTERN uint32_t LWS_WARN_UNUSED_RESULT lws_vfs_get_mod_time(lws_fop_fd_t fop_fd)