2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
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.
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
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
38 /** Set this to 1 to include "fsdata_custom.c" instead of "fsdata.c" for the
39 * file system (to prevent changing the file included in CVS) */
40 #ifndef HTTPD_USE_CUSTUM_FSDATA
41 #define HTTPD_USE_CUSTUM_FSDATA 0
44 #if HTTPD_USE_CUSTUM_FSDATA
45 #include "fsdata_custom.c"
46 #else /* HTTPD_USE_CUSTUM_FSDATA */
48 #endif /* HTTPD_USE_CUSTUM_FSDATA */
50 /*-----------------------------------------------------------------------------------*/
51 /* Define the number of open files that we can support. */
52 #ifndef LWIP_MAX_OPEN_FILES
53 #define LWIP_MAX_OPEN_FILES 10
56 /* Define the file system memory allocation structure. */
62 /* Allocate file system memory */
63 struct fs_table fs_memory[LWIP_MAX_OPEN_FILES];
65 #if LWIP_HTTPD_CUSTOM_FILES
66 int fs_open_custom(struct fs_file *file, const char *name);
67 void fs_close_custom(struct fs_file *file);
68 #endif /* LWIP_HTTPD_CUSTOM_FILES */
70 /*-----------------------------------------------------------------------------------*/
71 static struct fs_file *
75 for(i = 0; i < LWIP_MAX_OPEN_FILES; i++) {
76 if(fs_memory[i].inuse == 0) {
77 fs_memory[i].inuse = 1;
78 return(&fs_memory[i].file);
84 /*-----------------------------------------------------------------------------------*/
86 fs_free(struct fs_file *file)
89 for(i = 0; i < LWIP_MAX_OPEN_FILES; i++) {
90 if(&fs_memory[i].file == file) {
91 fs_memory[i].inuse = 0;
98 /*-----------------------------------------------------------------------------------*/
100 fs_open(const char *name)
102 struct fs_file *file;
103 const struct fsdata_file *f;
110 #if LWIP_HTTPD_CUSTOM_FILES
111 if(fs_open_custom(file, name)) {
112 file->is_custom_file = 1;
115 file->is_custom_file = 0;
116 #endif /* LWIP_HTTPD_CUSTOM_FILES */
118 for(f = FS_ROOT; f != NULL; f = f->next) {
119 if (!strcmp(name, (char *)f->name)) {
120 file->data = (const char *)f->data;
122 file->index = f->len;
123 file->pextension = NULL;
124 file->http_header_included = f->http_header_included;
125 #if HTTPD_PRECALCULATED_CHECKSUM
126 file->chksum_count = f->chksum_count;
127 file->chksum = f->chksum;
128 #endif /* HTTPD_PRECALCULATED_CHECKSUM */
129 #if LWIP_HTTPD_FILE_STATE
130 file->state = fs_state_init(file, name);
131 #endif /* #if LWIP_HTTPD_FILE_STATE */
139 /*-----------------------------------------------------------------------------------*/
141 fs_close(struct fs_file *file)
143 #if LWIP_HTTPD_CUSTOM_FILES
144 if (file->is_custom_file) {
145 fs_close_custom(file);
147 #endif /* LWIP_HTTPD_CUSTOM_FILES */
148 #if LWIP_HTTPD_FILE_STATE
149 fs_state_free(file, file->state);
150 #endif /* #if LWIP_HTTPD_FILE_STATE */
153 /*-----------------------------------------------------------------------------------*/
155 fs_read(struct fs_file *file, char *buffer, int count)
159 if(file->index == file->len) {
163 read = file->len - file->index;
168 MEMCPY(buffer, (file->data + file->index), read);
173 /*-----------------------------------------------------------------------------------*/
174 int fs_bytes_left(struct fs_file *file)
176 return file->len - file->index;