1 | /*
|
---|
2 | * TOPPERS PROJECT Home Network Working Group Software
|
---|
3 | *
|
---|
4 | * Copyright (C) 2017-2019 Cores Co., Ltd. Japan
|
---|
5 | *
|
---|
6 | * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
7 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
8 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
9 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
10 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
11 | * スコード中に含まれていること.
|
---|
12 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
13 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
14 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
15 | * の無保証規定を掲載すること.
|
---|
16 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
17 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
18 | * と.
|
---|
19 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
20 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
21 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
22 | * 報告すること.
|
---|
23 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
24 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
25 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
26 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
27 | * 免責すること.
|
---|
28 | *
|
---|
29 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
30 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
31 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
32 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
33 | * の責任を負わない.
|
---|
34 | *
|
---|
35 | * @(#) $Id$
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include "httpd.h"
|
---|
39 | #include "httpd-fs.h"
|
---|
40 | #include "ff.h"
|
---|
41 | #include "diskio.h"
|
---|
42 | #include <string.h>
|
---|
43 | #include "http-strings.h"
|
---|
44 | #include <kernel.h>
|
---|
45 | #include "kernel_cfg.h"
|
---|
46 | #include "syssvc/syslog.h"
|
---|
47 | #include "util/ntstdio.h"
|
---|
48 |
|
---|
49 | #ifndef _MSC_VER
|
---|
50 | #ifndef strcat_s
|
---|
51 | #define strcat_s(dst, dsz, src) ntlibc_strcat(dst, src)
|
---|
52 | #endif
|
---|
53 | #endif
|
---|
54 |
|
---|
55 | //#define FILE_DUMP
|
---|
56 | #ifdef FILE_DUMP
|
---|
57 | char path[256] = "";
|
---|
58 | FATFS fs;
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | #ifndef NULL
|
---|
62 | #define NULL 0
|
---|
63 | #endif /* NULL */
|
---|
64 |
|
---|
65 | extern ntstdio_t ntstdio;
|
---|
66 |
|
---|
67 | /*-----------------------------------------------------------------------------------*/
|
---|
68 | #ifdef FILE_DUMP
|
---|
69 | FRESULT scan_files(char* path, int size)
|
---|
70 | {
|
---|
71 | FRESULT res;
|
---|
72 | FILINFO fno;
|
---|
73 | FATFS_DIR dir;
|
---|
74 | int i;
|
---|
75 | char *fn; /* This function assumes non-Unicode configuration */
|
---|
76 |
|
---|
77 | res = f_opendir(&dir, path);
|
---|
78 | if (res == FR_OK) {
|
---|
79 | i = ntlibc_strlen(path);
|
---|
80 | for (;;) {
|
---|
81 | res = f_readdir(&dir, &fno);
|
---|
82 | if (res != FR_OK || fno.fname[0] == 0) break;
|
---|
83 | fn = fno.fname;
|
---|
84 | if (fno.fattrib & AM_DIR) {
|
---|
85 | ntstdio_sprintf(&path[i], "0:/%s", fn);
|
---|
86 | res = scan_files(path, size);
|
---|
87 | if (res != FR_OK) break;
|
---|
88 | path[i] = 0;
|
---|
89 | }
|
---|
90 | else {
|
---|
91 | ntstdio_printf(&ntstdio, "%s/%s\n", path, fn);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | return res;
|
---|
97 | }
|
---|
98 | #endif
|
---|
99 | /*-----------------------------------------------------------------------------------*/
|
---|
100 | int
|
---|
101 | httpd_fs_open(char *name, int len, struct httpd_fs_file *file)
|
---|
102 | {
|
---|
103 | FRESULT res;
|
---|
104 | FIL *fd = (FIL *)&file->fd;
|
---|
105 | FILINFO fno;
|
---|
106 | FATFS_DIR dir;
|
---|
107 |
|
---|
108 | file->pos = 0;
|
---|
109 | file->len = 0;
|
---|
110 | file->name = name;
|
---|
111 | file->redirect = 0;
|
---|
112 | memset(fd, 0, sizeof(FIL));
|
---|
113 |
|
---|
114 | if ((res = f_open(fd, name, FA_OPEN_EXISTING | FA_READ)) != FR_OK) {
|
---|
115 | if ((res = f_opendir(&dir, name)) != FR_OK) {
|
---|
116 | ntstdio_printf(&ntstdio, "f_opendir(%s) => %d\n", name, res);
|
---|
117 | return 0;
|
---|
118 | }
|
---|
119 |
|
---|
120 | if ((res = f_readdir(&dir, &fno)) != FR_OK) {
|
---|
121 | ntstdio_printf(&ntstdio, "f_readdir(%s) => %d\n", name, res);
|
---|
122 | return 0;
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (len != 0/*fno.fattrib & AM_DIR*/) {
|
---|
126 | strcat_s(name, len, http_index_html);
|
---|
127 | res = f_open(fd, name, FA_OPEN_EXISTING | FA_READ);
|
---|
128 | file->redirect = res == FR_OK;
|
---|
129 | }
|
---|
130 | else
|
---|
131 | res = FR_NO_FILE;
|
---|
132 |
|
---|
133 | if (res != FR_OK) {
|
---|
134 | ntstdio_printf(&ntstdio, "f_open(%s) => %d %x\n", name, res, fno.fattrib);
|
---|
135 | return 0;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | file->len = fd->fsize;
|
---|
140 |
|
---|
141 | //ntstdio_printf(&ntstdio, "httpd_fs_open(%s) %d\n", name, file->len);
|
---|
142 |
|
---|
143 | return 1;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /*-----------------------------------------------------------------------------------*/
|
---|
147 | int
|
---|
148 | httpd_fs_create(char *name, struct httpd_fs_file *file)
|
---|
149 | {
|
---|
150 | FRESULT res;
|
---|
151 | FIL *fd = (FIL *)&file->fd;
|
---|
152 |
|
---|
153 | file->pos = 0;
|
---|
154 | file->len = 0;
|
---|
155 | file->name = name;
|
---|
156 | file->redirect = 0;
|
---|
157 | memset(fd, 0, sizeof(FIL));
|
---|
158 |
|
---|
159 | if ((res = f_open(fd, name, FA_CREATE_ALWAYS | FA_WRITE)) != FR_OK) {
|
---|
160 | ntstdio_printf(&ntstdio, "f_open(%s) => %d\n", name, res);
|
---|
161 | return 0;
|
---|
162 | }
|
---|
163 |
|
---|
164 | file->pos = 0;
|
---|
165 | file->len = 0;
|
---|
166 |
|
---|
167 | //ntstdio_printf(&ntstdio, "httpd_fs_create(%s) %d\n", file->name, file->len);
|
---|
168 |
|
---|
169 | return 1;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /*-----------------------------------------------------------------------------------*/
|
---|
173 | int
|
---|
174 | httpd_fs_read(struct httpd_fs_file *file, void *dst, int len)
|
---|
175 | {
|
---|
176 | FRESULT ret;
|
---|
177 | UINT rlen = 0;
|
---|
178 | FIL *fd = (FIL *)&file->fd;
|
---|
179 |
|
---|
180 | if ((ret = f_lseek(fd, file->pos)) != FR_OK) {
|
---|
181 | ntstdio_printf(&ntstdio, "f_lseek(%s, %d) => %d\n", file->name, file->pos, ret);
|
---|
182 | return 0;
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (file->pos != fd->fptr) {
|
---|
186 | ntstdio_printf(&ntstdio, "f_lseek(%s, %d) != %d\n", file->name, file->pos, fd->fptr);
|
---|
187 | }
|
---|
188 |
|
---|
189 | if ((ret = f_read(fd, dst, len, &rlen)) != FR_OK) {
|
---|
190 | ntstdio_printf(&ntstdio, "f_read(%s, 0x%p, %d) => %d\n", file->name, dst, len, ret);
|
---|
191 | return 0;
|
---|
192 | }
|
---|
193 |
|
---|
194 | //ntstdio_printf(&ntstdio, "httpd_fs_read(%s, %d, %d) => %d\n", file->name, file->pos, len, rlen);
|
---|
195 |
|
---|
196 | return rlen;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /*-----------------------------------------------------------------------------------*/
|
---|
200 | int
|
---|
201 | httpd_fs_write(struct httpd_fs_file *file, const void *src, int len)
|
---|
202 | {
|
---|
203 | FRESULT ret;
|
---|
204 | UINT rlen = 0;
|
---|
205 | FIL *fd = (FIL *)&file->fd;
|
---|
206 |
|
---|
207 | if ((ret = f_lseek(fd, file->pos)) != FR_OK) {
|
---|
208 | ntstdio_printf(&ntstdio, "f_lseek(%s, %d) => %d\n", file->name, file->pos, ret);
|
---|
209 | return 0;
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (file->pos != fd->fptr) {
|
---|
213 | ntstdio_printf(&ntstdio, "f_lseek(%s, %d) != %d\n", file->name, file->pos, fd->fptr);
|
---|
214 | }
|
---|
215 |
|
---|
216 | if ((ret = f_write(fd, src, len, &rlen)) != FR_OK) {
|
---|
217 | ntstdio_printf(&ntstdio, "f_write(%s, 0x%p, %d) => %d\n", file->name, src, len, ret);
|
---|
218 | return 0;
|
---|
219 | }
|
---|
220 |
|
---|
221 | file->pos += rlen;
|
---|
222 | file->len += rlen;
|
---|
223 |
|
---|
224 | //ntstdio_printf(&ntstdio, "httpd_fs_write(%s, %d, %d) => %d\n", file->name, file->pos, len, rlen);
|
---|
225 |
|
---|
226 | return rlen;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*-----------------------------------------------------------------------------------*/
|
---|
230 | int httpd_fs_close(struct httpd_fs_file *file)
|
---|
231 | {
|
---|
232 | FRESULT ret;
|
---|
233 | FIL *fd = (FIL *)&file->fd;
|
---|
234 |
|
---|
235 | if ((ret = f_close(fd)) != FR_OK) {
|
---|
236 | ntstdio_printf(&ntstdio, "f_close(%s) => %d\n", file->name, ret);
|
---|
237 | return 0;
|
---|
238 | }
|
---|
239 |
|
---|
240 | memset(fd, 0, sizeof(FIL));
|
---|
241 |
|
---|
242 | return 1;
|
---|
243 | }
|
---|