1 | /*
|
---|
2 | * Copyright (c) 2001, Swedish Institute of Computer Science.
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | * 1. Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer in the
|
---|
12 | * documentation and/or other materials provided with the distribution.
|
---|
13 | * 3. Neither the name of the Institute nor the names of its contributors
|
---|
14 | * may be used to endorse or promote products derived from this software
|
---|
15 | * without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
---|
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
---|
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
27 | * SUCH DAMAGE.
|
---|
28 | *
|
---|
29 | * This file is part of the lwIP TCP/IP stack.
|
---|
30 | *
|
---|
31 | * Author: Adam Dunkels <adam@sics.se>
|
---|
32 | *
|
---|
33 | * $Id: httpd-fs.c 155 2016-02-04 14:11:09Z coas-nagasima $
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include "httpd.h"
|
---|
37 | #include "httpd-fs.h"
|
---|
38 | #include "pff.h"
|
---|
39 | #include "diskio.h"
|
---|
40 | #include "mmc_rspi.h"
|
---|
41 | #include <string.h>
|
---|
42 | #include "http-strings.h"
|
---|
43 |
|
---|
44 | #ifndef _MSC_VER
|
---|
45 | #ifndef strcat_s
|
---|
46 | #define strcat_s(dst, dsz, src) strcat(dst, src)
|
---|
47 | #endif
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | //#define FILE_DUMP
|
---|
51 | #ifdef FILE_DUMP
|
---|
52 | char path[256] = "";
|
---|
53 | FATFS fs;
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | #ifndef NULL
|
---|
57 | #define NULL 0
|
---|
58 | #endif /* NULL */
|
---|
59 |
|
---|
60 | /*-----------------------------------------------------------------------------------*/
|
---|
61 | #ifdef FILE_DUMP
|
---|
62 | FRESULT scan_files (FATFS *fs, char* path, int size)
|
---|
63 | {
|
---|
64 | FRESULT res;
|
---|
65 | FILINFO fno;
|
---|
66 | DIR dir;
|
---|
67 | int i;
|
---|
68 | char *fn; /* This function assumes non-Unicode configuration */
|
---|
69 | #if _USE_LFN
|
---|
70 | char LFName[256];
|
---|
71 | fno.lfname = LFName;
|
---|
72 | fno.lfsize = sizeof(LFName);
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | dir.fs = fs;
|
---|
76 | res = pf_opendir(&dir, path);
|
---|
77 | if (res == FR_OK) {
|
---|
78 | i = strlen(path);
|
---|
79 | for (;;) {
|
---|
80 | res = pf_readdir(&dir, &fno);
|
---|
81 | if (res != FR_OK || fno.fname[0] == 0) break;
|
---|
82 | #if _USE_LFN
|
---|
83 | fn = *fno.lfname ? fno.lfname : fno.fname;
|
---|
84 | #else
|
---|
85 | fn = fno.fname;
|
---|
86 | #endif
|
---|
87 | if (fno.fattrib & AM_DIR) {
|
---|
88 | sprintf_s(&path[i], size - i, "/%s", fn);
|
---|
89 | res = scan_files(fs, path, size);
|
---|
90 | if (res != FR_OK) break;
|
---|
91 | path[i] = 0;
|
---|
92 | } else {
|
---|
93 | printf("%s/%s\n", path, fn);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | return res;
|
---|
99 | }
|
---|
100 | #endif
|
---|
101 | /*-----------------------------------------------------------------------------------*/
|
---|
102 | void
|
---|
103 | httpd_fs_init(void)
|
---|
104 | {
|
---|
105 | #ifdef FILE_DUMP
|
---|
106 | FRESULT res;
|
---|
107 | #endif
|
---|
108 | ramdisk_initialize();
|
---|
109 | mmc_rspi_initialize();
|
---|
110 | #ifdef FILE_DUMP
|
---|
111 | if((res = pf_mount(&fs)) != FR_OK){
|
---|
112 | printf("pf_mount() => %d\n", res);
|
---|
113 | return;
|
---|
114 | }
|
---|
115 | if((res = scan_files(&fs, path, sizeof(path))) != FR_OK){
|
---|
116 | printf("scan_files() => %d\n", res);
|
---|
117 | return;
|
---|
118 | }
|
---|
119 | #endif
|
---|
120 | }
|
---|
121 | /*-----------------------------------------------------------------------------------*/
|
---|
122 | int
|
---|
123 | httpd_fs_open(int drv, char *name, int len, struct httpd_fs_file *file)
|
---|
124 | {
|
---|
125 | FRESULT res;
|
---|
126 | FATFS *fs = (FATFS *)&file->fs;
|
---|
127 | FILINFO fno;
|
---|
128 | DIR dir;
|
---|
129 |
|
---|
130 | file->pos = 0;
|
---|
131 | file->len = 0;
|
---|
132 | file->drv = drv;
|
---|
133 | file->name = name;
|
---|
134 | file->redirect = 0;
|
---|
135 | memset(fs, 0, sizeof(FATFS));
|
---|
136 |
|
---|
137 | if (drv == 0) {
|
---|
138 | fs->disk_get_status = ramdisk_get_status;
|
---|
139 | fs->disk_readp = ramdisk_readp;
|
---|
140 | fs->disk_writep = ramdisk_writep;
|
---|
141 | }
|
---|
142 | else {
|
---|
143 | fs->disk_get_status = mmc_rspi_get_status;
|
---|
144 | fs->disk_readp = mmc_rspi_readp;
|
---|
145 | fs->disk_writep = mmc_rspi_writep;
|
---|
146 | }
|
---|
147 |
|
---|
148 | if (fs->fs_type == 0){
|
---|
149 | if((res = pf_mount(fs)) != FR_OK){
|
---|
150 | printf("pf_mount() => %d\n", res);
|
---|
151 | return 0;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | if ((res = pf_open(fs, name)) != FR_OK) {
|
---|
156 | dir.fs = fs;
|
---|
157 | if ((res = pf_opendir(&dir, name)) != FR_OK) {
|
---|
158 | printf("pf_opendir(%d:%s) => %d\n", drv, name, res);
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 |
|
---|
162 | if ((res = pf_readdir(&dir, &fno)) != FR_OK) {
|
---|
163 | printf("pf_readdir(%d:%s) => %d\n", drv, name, res);
|
---|
164 | return 0;
|
---|
165 | }
|
---|
166 |
|
---|
167 | if (len != 0/*fno.fattrib & AM_DIR*/) {
|
---|
168 | strcat_s(name, len, http_index_html);
|
---|
169 | res = pf_open(fs, name);
|
---|
170 | file->redirect = res == FR_OK;
|
---|
171 | }
|
---|
172 | else
|
---|
173 | res = FR_NO_FILE;
|
---|
174 |
|
---|
175 | if (res != FR_OK) {
|
---|
176 | printf("pf_open(%d:%s) => %d %x\n", drv, name, res, fno.fattrib);
|
---|
177 | return 0;
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | file->len = fs->fsize;
|
---|
182 |
|
---|
183 | //printf("httpd_fs_open(%d:%s) %d\n", drv, name, file->len);
|
---|
184 |
|
---|
185 | return 1;
|
---|
186 | }
|
---|
187 | /*-----------------------------------------------------------------------------------*/
|
---|
188 | int
|
---|
189 | httpd_fs_read(struct httpd_fs_file *file, void *dst, int len)
|
---|
190 | {
|
---|
191 | FRESULT ret;
|
---|
192 | UINT rlen;
|
---|
193 | FATFS *fs = (FATFS *)&file->fs;
|
---|
194 |
|
---|
195 | if((ret = pf_lseek(fs, file->pos)) != FR_OK){
|
---|
196 | printf("pf_lseek(%d:%s, %d) => %d\n", file->drv, file->name, file->pos, ret);
|
---|
197 | return 0;
|
---|
198 | }
|
---|
199 |
|
---|
200 | if(file->pos != fs->fptr){
|
---|
201 | printf("pf_lseek(%d:%s, %d) != %d\n", file->drv, file->name, file->pos, fs->fptr);
|
---|
202 | }
|
---|
203 |
|
---|
204 | if((ret = pf_read(fs, dst, len, &rlen)) != FR_OK){
|
---|
205 | printf("pf_read(%d:%s, 0x%p, %d) => %d\n", file->drv, file->name, dst, len, ret);
|
---|
206 | return 0;
|
---|
207 | }
|
---|
208 |
|
---|
209 | //printf("httpd_fs_read(%d:%s, %d, %d) => %d\n", file->drv, file->name, file->pos, len, rlen);
|
---|
210 |
|
---|
211 | return rlen;
|
---|
212 | }
|
---|
213 | /*-----------------------------------------------------------------------------------*/
|
---|