1 | /*
|
---|
2 | * TOPPERS ECHONET Lite Communication Middleware
|
---|
3 | *
|
---|
4 | * Copyright (C) 2017 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 | /*-----------------------------------------------------------------------------------*/
|
---|
66 | #ifdef FILE_DUMP
|
---|
67 | FRESULT scan_files(char* path, int size)
|
---|
68 | {
|
---|
69 | FRESULT res;
|
---|
70 | FILINFO fno;
|
---|
71 | FATFS_DIR dir;
|
---|
72 | int i;
|
---|
73 | char *fn; /* This function assumes non-Unicode configuration */
|
---|
74 |
|
---|
75 | res = f_opendir(&dir, path);
|
---|
76 | if (res == FR_OK) {
|
---|
77 | i = ntlibc_strlen(path);
|
---|
78 | for (;;) {
|
---|
79 | res = f_readdir(&dir, &fno);
|
---|
80 | if (res != FR_OK || fno.fname[0] == 0) break;
|
---|
81 | fn = fno.fname;
|
---|
82 | if (fno.fattrib & AM_DIR) {
|
---|
83 | ntstdio_sprintf(&path[i], "0:/%s", fn);
|
---|
84 | res = scan_files(path, size);
|
---|
85 | if (res != FR_OK) break;
|
---|
86 | path[i] = 0;
|
---|
87 | }
|
---|
88 | else {
|
---|
89 | syslog(LOG_ERROR, "%s/%s\n", path, fn);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | return res;
|
---|
95 | }
|
---|
96 | #endif
|
---|
97 | /*-----------------------------------------------------------------------------------*/
|
---|
98 | int
|
---|
99 | httpd_fs_open(char *name, int len, struct httpd_fs_file *file)
|
---|
100 | {
|
---|
101 | FRESULT res;
|
---|
102 | FIL *fd = (FIL *)&file->fd;
|
---|
103 | FILINFO fno;
|
---|
104 | FATFS_DIR dir;
|
---|
105 |
|
---|
106 | file->pos = 0;
|
---|
107 | file->len = 0;
|
---|
108 | file->name = name;
|
---|
109 | file->redirect = 0;
|
---|
110 | memset(fd, 0, sizeof(FIL));
|
---|
111 |
|
---|
112 | if ((res = f_open(fd, name, FA_OPEN_EXISTING | FA_READ)) != FR_OK) {
|
---|
113 | if ((res = f_opendir(&dir, name)) != FR_OK) {
|
---|
114 | syslog(LOG_ERROR, "f_opendir(%s) => %d\n", name, res);
|
---|
115 | return 0;
|
---|
116 | }
|
---|
117 |
|
---|
118 | if ((res = f_readdir(&dir, &fno)) != FR_OK) {
|
---|
119 | syslog(LOG_ERROR, "f_readdir(%s) => %d\n", name, res);
|
---|
120 | return 0;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (len != 0/*fno.fattrib & AM_DIR*/) {
|
---|
124 | strcat_s(name, len, http_index_html);
|
---|
125 | res = f_open(fd, name, FA_OPEN_EXISTING | FA_READ);
|
---|
126 | file->redirect = res == FR_OK;
|
---|
127 | }
|
---|
128 | else
|
---|
129 | res = FR_NO_FILE;
|
---|
130 |
|
---|
131 | if (res != FR_OK) {
|
---|
132 | syslog(LOG_ERROR, "f_open(%s) => %d %x\n", name, res, fno.fattrib);
|
---|
133 | return 0;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | file->len = fd->fsize;
|
---|
138 |
|
---|
139 | //syslog(LOG_ERROR, "httpd_fs_open(%d:%s) %d\n", drv, name, file->len);
|
---|
140 |
|
---|
141 | return 1;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /*-----------------------------------------------------------------------------------*/
|
---|
145 | int
|
---|
146 | httpd_fs_create(char *name, struct httpd_fs_file *file)
|
---|
147 | {
|
---|
148 | FRESULT res;
|
---|
149 | FIL *fd = (FIL *)&file->fd;
|
---|
150 |
|
---|
151 | file->pos = 0;
|
---|
152 | file->len = 0;
|
---|
153 | file->name = name;
|
---|
154 | file->redirect = 0;
|
---|
155 | memset(fd, 0, sizeof(FIL));
|
---|
156 |
|
---|
157 | if ((res = f_open(fd, name, FA_CREATE_ALWAYS | FA_WRITE)) != FR_OK) {
|
---|
158 | syslog(LOG_ERROR, "f_open(%s) => %d\n", name, res);
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 |
|
---|
162 | file->pos = 0;
|
---|
163 | file->len = 0;
|
---|
164 |
|
---|
165 | //syslog(LOG_ERROR, "httpd_fs_create(%d:%s) %d\n", drv, name, file->len);
|
---|
166 |
|
---|
167 | return 1;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /*-----------------------------------------------------------------------------------*/
|
---|
171 | int
|
---|
172 | httpd_fs_read(struct httpd_fs_file *file, void *dst, int len)
|
---|
173 | {
|
---|
174 | FRESULT ret;
|
---|
175 | UINT rlen = 0;
|
---|
176 | FIL *fd = (FIL *)&file->fd;
|
---|
177 |
|
---|
178 | if ((ret = f_lseek(fd, file->pos)) != FR_OK) {
|
---|
179 | syslog(LOG_ERROR, "f_lseek(%s, %d) => %d\n", file->name, file->pos, ret);
|
---|
180 | return 0;
|
---|
181 | }
|
---|
182 |
|
---|
183 | if (file->pos != fd->fptr) {
|
---|
184 | syslog(LOG_ERROR, "f_lseek(%s, %d) != %d\n", file->name, file->pos, fd->fptr);
|
---|
185 | }
|
---|
186 |
|
---|
187 | if ((ret = f_read(fd, dst, len, &rlen)) != FR_OK) {
|
---|
188 | syslog(LOG_ERROR, "f_read(%s, 0x%p, %d) => %d\n", file->name, dst, len, ret);
|
---|
189 | return 0;
|
---|
190 | }
|
---|
191 |
|
---|
192 | //syslog(LOG_ERROR, "httpd_fs_read(%d:%s, %d, %d) => %d\n", file->drv, file->name, file->pos, len, rlen);
|
---|
193 |
|
---|
194 | return rlen;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /*-----------------------------------------------------------------------------------*/
|
---|
198 | int
|
---|
199 | httpd_fs_write(struct httpd_fs_file *file, const void *src, int len)
|
---|
200 | {
|
---|
201 | FRESULT ret;
|
---|
202 | UINT rlen = 0;
|
---|
203 | FIL *fd = (FIL *)&file->fd;
|
---|
204 |
|
---|
205 | if ((ret = f_lseek(fd, file->pos)) != FR_OK) {
|
---|
206 | syslog(LOG_ERROR, "f_lseek(%s, %d) => %d\n", file->name, file->pos, ret);
|
---|
207 | return 0;
|
---|
208 | }
|
---|
209 |
|
---|
210 | if (file->pos != fd->fptr) {
|
---|
211 | syslog(LOG_ERROR, "f_lseek(%s, %d) != %d\n", file->name, file->pos, fd->fptr);
|
---|
212 | }
|
---|
213 |
|
---|
214 | if ((ret = f_write(fd, src, len, &rlen)) != FR_OK) {
|
---|
215 | syslog(LOG_ERROR, "f_write(%s, 0x%p, %d) => %d\n", file->name, src, len, ret);
|
---|
216 | return 0;
|
---|
217 | }
|
---|
218 |
|
---|
219 | file->pos += rlen;
|
---|
220 | file->len += rlen;
|
---|
221 |
|
---|
222 | //syslog(LOG_ERROR, "httpd_fs_write(%d:%s, %d, %d) => %d\n", file->drv, file->name, file->pos, len, rlen);
|
---|
223 |
|
---|
224 | return rlen;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /*-----------------------------------------------------------------------------------*/
|
---|
228 | int httpd_fs_close(struct httpd_fs_file *file)
|
---|
229 | {
|
---|
230 | FRESULT ret;
|
---|
231 | FIL *fd = (FIL *)&file->fd;
|
---|
232 |
|
---|
233 | if ((ret = f_close(fd)) != FR_OK) {
|
---|
234 | syslog(LOG_ERROR, "f_close(%s) => %d\n", file->name, ret);
|
---|
235 | return 0;
|
---|
236 | }
|
---|
237 |
|
---|
238 | memset(fd, 0, sizeof(FIL));
|
---|
239 |
|
---|
240 | return 1;
|
---|
241 | }
|
---|