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 154 2016-02-02 12:54:35Z 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 |
|
---|
43 | //#define FILE_DUMP
|
---|
44 | #ifdef FILE_DUMP
|
---|
45 | char path[256] = "";
|
---|
46 | FATFS fs;
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | #ifndef NULL
|
---|
50 | #define NULL 0
|
---|
51 | #endif /* NULL */
|
---|
52 |
|
---|
53 | /*-----------------------------------------------------------------------------------*/
|
---|
54 | #ifdef FILE_DUMP
|
---|
55 | FRESULT scan_files (FATFS *fs, char* path, int size)
|
---|
56 | {
|
---|
57 | FRESULT res;
|
---|
58 | FILINFO fno;
|
---|
59 | DIR dir;
|
---|
60 | int i;
|
---|
61 | char *fn; /* This function assumes non-Unicode configuration */
|
---|
62 | #if _USE_LFN
|
---|
63 | char LFName[256];
|
---|
64 | fno.lfname = LFName;
|
---|
65 | fno.lfsize = sizeof(LFName);
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | dir.fs = fs;
|
---|
69 | res = pf_opendir(&dir, path);
|
---|
70 | if (res == FR_OK) {
|
---|
71 | i = strlen(path);
|
---|
72 | for (;;) {
|
---|
73 | res = pf_readdir(&dir, &fno);
|
---|
74 | if (res != FR_OK || fno.fname[0] == 0) break;
|
---|
75 | #if _USE_LFN
|
---|
76 | fn = *fno.lfname ? fno.lfname : fno.fname;
|
---|
77 | #else
|
---|
78 | fn = fno.fname;
|
---|
79 | #endif
|
---|
80 | if (fno.fattrib & AM_DIR) {
|
---|
81 | sprintf_s(&path[i], size - i, "/%s", fn);
|
---|
82 | res = scan_files(fs, path, size);
|
---|
83 | if (res != FR_OK) break;
|
---|
84 | path[i] = 0;
|
---|
85 | } else {
|
---|
86 | printf("%s/%s\n", path, fn);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | return res;
|
---|
92 | }
|
---|
93 | #endif
|
---|
94 | /*-----------------------------------------------------------------------------------*/
|
---|
95 | void
|
---|
96 | httpd_fs_init(void)
|
---|
97 | {
|
---|
98 | #ifdef FILE_DUMP
|
---|
99 | FRESULT res;
|
---|
100 | #endif
|
---|
101 | ramdisk_initialize();
|
---|
102 | mmc_rspi_initialize();
|
---|
103 | #ifdef FILE_DUMP
|
---|
104 | if((res = pf_mount(&fs)) != FR_OK){
|
---|
105 | printf("pf_mount() => %d\n", res);
|
---|
106 | return;
|
---|
107 | }
|
---|
108 | if((res = scan_files(&fs, path, sizeof(path))) != FR_OK){
|
---|
109 | printf("scan_files() => %d\n", res);
|
---|
110 | return;
|
---|
111 | }
|
---|
112 | #endif
|
---|
113 | }
|
---|
114 | /*-----------------------------------------------------------------------------------*/
|
---|
115 | int
|
---|
116 | httpd_fs_open(int drv, const char *name, struct httpd_fs_file *file)
|
---|
117 | {
|
---|
118 | FRESULT res;
|
---|
119 | FATFS *fs = (FATFS *)file->fs;
|
---|
120 |
|
---|
121 | file->pos = 0;
|
---|
122 | file->len = 0;
|
---|
123 | file->drv = drv;
|
---|
124 | file->name = name;
|
---|
125 | memset(fs, 0, sizeof(FATFS));
|
---|
126 |
|
---|
127 | if (drv == 0) {
|
---|
128 | fs->disk_get_status = ramdisk_get_status;
|
---|
129 | fs->disk_readp = ramdisk_readp;
|
---|
130 | fs->disk_writep = ramdisk_writep;
|
---|
131 | }
|
---|
132 | else {
|
---|
133 | fs->disk_get_status = mmc_rspi_get_status;
|
---|
134 | fs->disk_readp = mmc_rspi_readp;
|
---|
135 | fs->disk_writep = mmc_rspi_writep;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (fs->fs_type == 0){
|
---|
139 | if((res = pf_mount(fs)) != FR_OK){
|
---|
140 | printf("pf_mount() => %d\n", res);
|
---|
141 | return 0;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | if((res = pf_open(fs, name)) != FR_OK){
|
---|
146 | printf("pf_open(%d:%s) => %d\n", drv, name, res);
|
---|
147 | return 0;
|
---|
148 | }
|
---|
149 |
|
---|
150 | file->len = fs->fsize;
|
---|
151 |
|
---|
152 | //printf("httpd_fs_open(%d:%s) %d\n", drv, name, file->len);
|
---|
153 |
|
---|
154 | return 1;
|
---|
155 | }
|
---|
156 | /*-----------------------------------------------------------------------------------*/
|
---|
157 | int
|
---|
158 | httpd_fs_read(struct httpd_fs_file *file, void *dst, int len)
|
---|
159 | {
|
---|
160 | FRESULT ret;
|
---|
161 | UINT rlen;
|
---|
162 | FATFS *fs = (FATFS *)file->fs;
|
---|
163 |
|
---|
164 | if((ret = pf_lseek(fs, file->pos)) != FR_OK){
|
---|
165 | printf("pf_lseek(%d:%s, %d) => %d\n", file->drv, file->name, file->pos, ret);
|
---|
166 | return 0;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if(file->pos != fs->fptr){
|
---|
170 | printf("pf_lseek(%d:%s, %d) != %d\n", file->drv, file->name, file->pos, fs->fptr);
|
---|
171 | }
|
---|
172 |
|
---|
173 | if((ret = pf_read(fs, dst, len, &rlen)) != FR_OK){
|
---|
174 | printf("pf_read(%d:%s, 0x%p, %d) => %d\n", file->drv, file->name, dst, len, ret);
|
---|
175 | return 0;
|
---|
176 | }
|
---|
177 |
|
---|
178 | //printf("httpd_fs_read(%d:%s, %d, %d) => %d\n", file->drv, file->name, file->pos, len, rlen);
|
---|
179 |
|
---|
180 | return rlen;
|
---|
181 | }
|
---|
182 | /*-----------------------------------------------------------------------------------*/
|
---|