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