1 /*
2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25 * OF SUCH DAMAGE.
26 *
27 * This file is part of the lwIP TCP/IP stack.
28 *
29 * Author: Adam Dunkels <adam@sics.se>
30 *
31 */
32
33 #include "lwip/apps/httpd_opts.h"
34 #include "lwip/def.h"
35 #include "lwip/apps/fs.h"
36 #include <string.h>
37
38
39 #include HTTPD_FSDATA_FILE
40
41 /*-----------------------------------------------------------------------------------*/
42 err_t
fs_open(struct fs_file * file,const char * name)43 fs_open(struct fs_file *file, const char *name)
44 {
45 const struct fsdata_file *f;
46
47 if ((file == NULL) || (name == NULL)) {
48 return ERR_ARG;
49 }
50
51 #if LWIP_HTTPD_CUSTOM_FILES
52 if (fs_open_custom(file, name)) {
53 file->flags |= FS_FILE_FLAGS_CUSTOM;
54 return ERR_OK;
55 }
56 #endif /* LWIP_HTTPD_CUSTOM_FILES */
57
58 for (f = FS_ROOT; f != NULL; f = f->next) {
59 if (!strcmp(name, (const char *)f->name)) {
60 file->data = (const char *)f->data;
61 file->len = f->len;
62 file->index = f->len;
63 file->flags = f->flags;
64 #if HTTPD_PRECALCULATED_CHECKSUM
65 file->chksum_count = f->chksum_count;
66 file->chksum = f->chksum;
67 #endif /* HTTPD_PRECALCULATED_CHECKSUM */
68 #if LWIP_HTTPD_FILE_EXTENSION
69 file->pextension = NULL;
70 #endif /* LWIP_HTTPD_FILE_EXTENSION */
71 #if LWIP_HTTPD_FILE_STATE
72 file->state = fs_state_init(file, name);
73 #endif /* #if LWIP_HTTPD_FILE_STATE */
74 return ERR_OK;
75 }
76 }
77 /* file not found */
78 return ERR_VAL;
79 }
80
81 /*-----------------------------------------------------------------------------------*/
82 void
fs_close(struct fs_file * file)83 fs_close(struct fs_file *file)
84 {
85 #if LWIP_HTTPD_CUSTOM_FILES
86 if ((file->flags & FS_FILE_FLAGS_CUSTOM) != 0) {
87 fs_close_custom(file);
88 }
89 #endif /* LWIP_HTTPD_CUSTOM_FILES */
90 #if LWIP_HTTPD_FILE_STATE
91 fs_state_free(file, file->state);
92 #endif /* #if LWIP_HTTPD_FILE_STATE */
93 LWIP_UNUSED_ARG(file);
94 }
95 /*-----------------------------------------------------------------------------------*/
96 #if LWIP_HTTPD_DYNAMIC_FILE_READ
97 #if LWIP_HTTPD_FS_ASYNC_READ
98 int
fs_read_async(struct fs_file * file,char * buffer,int count,fs_wait_cb callback_fn,void * callback_arg)99 fs_read_async(struct fs_file *file, char *buffer, int count, fs_wait_cb callback_fn, void *callback_arg)
100 #else /* LWIP_HTTPD_FS_ASYNC_READ */
101 int
102 fs_read(struct fs_file *file, char *buffer, int count)
103 #endif /* LWIP_HTTPD_FS_ASYNC_READ */
104 {
105 int read;
106 if (file->index == file->len) {
107 return FS_READ_EOF;
108 }
109 #if LWIP_HTTPD_FS_ASYNC_READ
110 LWIP_UNUSED_ARG(callback_fn);
111 LWIP_UNUSED_ARG(callback_arg);
112 #endif /* LWIP_HTTPD_FS_ASYNC_READ */
113 #if LWIP_HTTPD_CUSTOM_FILES
114 if ((file->flags & FS_FILE_FLAGS_CUSTOM) != 0) {
115 #if LWIP_HTTPD_FS_ASYNC_READ
116 return fs_read_async_custom(file, buffer, count, callback_fn, callback_arg);
117 #else /* LWIP_HTTPD_FS_ASYNC_READ */
118 return fs_read_custom(file, buffer, count);
119 #endif /* LWIP_HTTPD_FS_ASYNC_READ */
120 }
121 #endif /* LWIP_HTTPD_CUSTOM_FILES */
122
123 read = file->len - file->index;
124 if (read > count) {
125 read = count;
126 }
127
128 MEMCPY(buffer, (file->data + file->index), read);
129 file->index += read;
130
131 return (read);
132 }
133 #endif /* LWIP_HTTPD_DYNAMIC_FILE_READ */
134 /*-----------------------------------------------------------------------------------*/
135 #if LWIP_HTTPD_FS_ASYNC_READ
136 int
fs_is_file_ready(struct fs_file * file,fs_wait_cb callback_fn,void * callback_arg)137 fs_is_file_ready(struct fs_file *file, fs_wait_cb callback_fn, void *callback_arg)
138 {
139 if (file != NULL) {
140 #if LWIP_HTTPD_FS_ASYNC_READ
141 #if LWIP_HTTPD_CUSTOM_FILES
142 if (!fs_canread_custom(file)) {
143 if (fs_wait_read_custom(file, callback_fn, callback_arg)) {
144 return 0;
145 }
146 }
147 #else /* LWIP_HTTPD_CUSTOM_FILES */
148 LWIP_UNUSED_ARG(callback_fn);
149 LWIP_UNUSED_ARG(callback_arg);
150 #endif /* LWIP_HTTPD_CUSTOM_FILES */
151 #endif /* LWIP_HTTPD_FS_ASYNC_READ */
152 }
153 return 1;
154 }
155 #endif /* LWIP_HTTPD_FS_ASYNC_READ */
156 /*-----------------------------------------------------------------------------------*/
157 int
fs_bytes_left(struct fs_file * file)158 fs_bytes_left(struct fs_file *file)
159 {
160 return file->len - file->index;
161 }
162