1 | /*
|
---|
2 | * TOPPERS ECHONET Lite Communication Middleware
|
---|
3 | *
|
---|
4 | * Copyright (C) 2017 Cores Co., Ltd. Japan
|
---|
5 | *
|
---|
6 | * ä¸è¨èä½æ¨©è
|
---|
7 | ã¯ï¼ä»¥ä¸ã®(1)ï½(4)ã®æ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§
|
---|
8 | * ã¢ï¼æ¬ã½ããã¦ã§ã¢ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹
|
---|
9 | * å¤ã»åé
|
---|
10 | å¸ï¼ä»¥ä¸ï¼å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
11 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
12 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
13 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
14 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
15 | * ç¨ã§ããå½¢ã§åé
|
---|
16 | å¸ããå ´åã«ã¯ï¼åé
|
---|
17 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
18 | * è
|
---|
19 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
20 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
21 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
22 | * ç¨ã§ããªãå½¢ã§åé
|
---|
23 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
24 | * ã¨ï¼
|
---|
25 | * (a) åé
|
---|
26 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
27 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
28 | * ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
29 | * (b) åé
|
---|
30 | å¸ã®å½¢æ
|
---|
31 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
32 | * å ±åãããã¨ï¼
|
---|
33 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
34 | * 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
35 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
36 | 責ãããã¨ï¼
|
---|
37 | * ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®ã¦ã¼ã¶ã¾ãã¯ã¨ã³ãã¦ã¼ã¶ããã®ãããªãç
|
---|
38 | * ç±ã«åºã¥ãè«æ±ãããï¼ä¸è¨èä½æ¨©è
|
---|
39 | ããã³TOPPERSããã¸ã§ã¯ãã
|
---|
40 | * å
|
---|
41 | 責ãããã¨ï¼
|
---|
42 | *
|
---|
43 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
44 | ã
|
---|
45 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ç¹å®ã®ä½¿ç¨ç®ç
|
---|
46 | * ã«å¯¾ããé©åæ§ãå«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§
|
---|
47 | * ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ã
|
---|
48 | * ã®è²¬ä»»ãè² ããªãï¼
|
---|
49 | *
|
---|
50 | * @(#) $Id: httpd-fs.c 279 2017-04-29 07:33:37Z coas-nagasima $
|
---|
51 | */
|
---|
52 |
|
---|
53 | #include "httpd.h"
|
---|
54 | #include "httpd-fs.h"
|
---|
55 | #include "ff.h"
|
---|
56 | #include "diskio.h"
|
---|
57 | #include <string.h>
|
---|
58 | #include "http-strings.h"
|
---|
59 | #include <kernel.h>
|
---|
60 | #include "kernel_cfg.h"
|
---|
61 | #include "syssvc/syslog.h"
|
---|
62 | #include "ntstdio.h"
|
---|
63 |
|
---|
64 | #ifndef _MSC_VER
|
---|
65 | #ifndef strcat_s
|
---|
66 | #define strcat_s(dst, dsz, src) strcat(dst, src)
|
---|
67 | #endif
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | //#define FILE_DUMP
|
---|
71 | #ifdef FILE_DUMP
|
---|
72 | char path[256] = "";
|
---|
73 | FATFS fs;
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | #ifndef NULL
|
---|
77 | #define NULL 0
|
---|
78 | #endif /* NULL */
|
---|
79 |
|
---|
80 | /*-----------------------------------------------------------------------------------*/
|
---|
81 | #ifdef FILE_DUMP
|
---|
82 | FRESULT scan_files(char* path, int size)
|
---|
83 | {
|
---|
84 | FRESULT res;
|
---|
85 | FILINFO fno;
|
---|
86 | DIR dir;
|
---|
87 | int i;
|
---|
88 | char *fn; /* This function assumes non-Unicode configuration */
|
---|
89 |
|
---|
90 | res = f_opendir(&dir, path);
|
---|
91 | if (res == FR_OK) {
|
---|
92 | i = strlen(path);
|
---|
93 | for (;;) {
|
---|
94 | res = f_readdir(&dir, &fno);
|
---|
95 | if (res != FR_OK || fno.fname[0] == 0) break;
|
---|
96 | fn = fno.fname;
|
---|
97 | if (fno.fattrib & AM_DIR) {
|
---|
98 | ntstdio_sprintf(&path[i], "0:/%s", fn);
|
---|
99 | res = scan_files(path, size);
|
---|
100 | if (res != FR_OK) break;
|
---|
101 | path[i] = 0;
|
---|
102 | }
|
---|
103 | else {
|
---|
104 | syslog(LOG_ERROR, "%s/%s\n", path, fn);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | return res;
|
---|
110 | }
|
---|
111 | #endif
|
---|
112 | /*-----------------------------------------------------------------------------------*/
|
---|
113 | int
|
---|
114 | httpd_fs_open(char *name, int len, struct httpd_fs_file *file)
|
---|
115 | {
|
---|
116 | FRESULT res;
|
---|
117 | FIL *fd = (FIL *)&file->fd;
|
---|
118 | FILINFO fno;
|
---|
119 | DIR dir;
|
---|
120 |
|
---|
121 | file->pos = 0;
|
---|
122 | file->len = 0;
|
---|
123 | file->name = name;
|
---|
124 | file->redirect = 0;
|
---|
125 | memset(fd, 0, sizeof(FIL));
|
---|
126 |
|
---|
127 | if ((res = f_open(fd, name, FA_OPEN_EXISTING | FA_READ)) != FR_OK) {
|
---|
128 | if ((res = f_opendir(&dir, name)) != FR_OK) {
|
---|
129 | syslog(LOG_ERROR, "f_opendir(%s) => %d\n", name, res);
|
---|
130 | return 0;
|
---|
131 | }
|
---|
132 |
|
---|
133 | if ((res = f_readdir(&dir, &fno)) != FR_OK) {
|
---|
134 | syslog(LOG_ERROR, "f_readdir(%s) => %d\n", name, res);
|
---|
135 | return 0;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (len != 0/*fno.fattrib & AM_DIR*/) {
|
---|
139 | strcat_s(name, len, http_index_html);
|
---|
140 | res = f_open(fd, name, FA_OPEN_EXISTING | FA_READ);
|
---|
141 | file->redirect = res == FR_OK;
|
---|
142 | }
|
---|
143 | else
|
---|
144 | res = FR_NO_FILE;
|
---|
145 |
|
---|
146 | if (res != FR_OK) {
|
---|
147 | syslog(LOG_ERROR, "f_open(%s) => %d %x\n", name, res, fno.fattrib);
|
---|
148 | return 0;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | file->len = fd->fsize;
|
---|
153 |
|
---|
154 | //syslog(LOG_ERROR, "httpd_fs_open(%d:%s) %d\n", drv, name, file->len);
|
---|
155 |
|
---|
156 | return 1;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /*-----------------------------------------------------------------------------------*/
|
---|
160 | int
|
---|
161 | httpd_fs_create(char *name, struct httpd_fs_file *file)
|
---|
162 | {
|
---|
163 | FRESULT res;
|
---|
164 | FIL *fd = (FIL *)&file->fd;
|
---|
165 |
|
---|
166 | file->pos = 0;
|
---|
167 | file->len = 0;
|
---|
168 | file->name = name;
|
---|
169 | file->redirect = 0;
|
---|
170 | memset(fd, 0, sizeof(FIL));
|
---|
171 |
|
---|
172 | if ((res = f_open(fd, name, FA_CREATE_ALWAYS | FA_WRITE)) != FR_OK) {
|
---|
173 | syslog(LOG_ERROR, "f_open(%s) => %d\n", name, res);
|
---|
174 | return 0;
|
---|
175 | }
|
---|
176 |
|
---|
177 | file->pos = 0;
|
---|
178 | file->len = 0;
|
---|
179 |
|
---|
180 | //syslog(LOG_ERROR, "httpd_fs_create(%d:%s) %d\n", drv, name, file->len);
|
---|
181 |
|
---|
182 | return 1;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /*-----------------------------------------------------------------------------------*/
|
---|
186 | int
|
---|
187 | httpd_fs_read(struct httpd_fs_file *file, void *dst, int len)
|
---|
188 | {
|
---|
189 | FRESULT ret;
|
---|
190 | UINT rlen = 0;
|
---|
191 | FIL *fd = (FIL *)&file->fd;
|
---|
192 |
|
---|
193 | if ((ret = f_lseek(fd, file->pos)) != FR_OK) {
|
---|
194 | syslog(LOG_ERROR, "f_lseek(%s, %d) => %d\n", file->name, file->pos, ret);
|
---|
195 | return 0;
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (file->pos != fd->fptr) {
|
---|
199 | syslog(LOG_ERROR, "f_lseek(%s, %d) != %d\n", file->name, file->pos, fd->fptr);
|
---|
200 | }
|
---|
201 |
|
---|
202 | if ((ret = f_read(fd, dst, len, &rlen)) != FR_OK) {
|
---|
203 | syslog(LOG_ERROR, "f_read(%s, 0x%p, %d) => %d\n", file->name, dst, len, ret);
|
---|
204 | return 0;
|
---|
205 | }
|
---|
206 |
|
---|
207 | //syslog(LOG_ERROR, "httpd_fs_read(%d:%s, %d, %d) => %d\n", file->drv, file->name, file->pos, len, rlen);
|
---|
208 |
|
---|
209 | return rlen;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /*-----------------------------------------------------------------------------------*/
|
---|
213 | int
|
---|
214 | httpd_fs_write(struct httpd_fs_file *file, const void *src, int len)
|
---|
215 | {
|
---|
216 | FRESULT ret;
|
---|
217 | UINT rlen = 0;
|
---|
218 | FIL *fd = (FIL *)&file->fd;
|
---|
219 |
|
---|
220 | if ((ret = f_lseek(fd, file->pos)) != FR_OK) {
|
---|
221 | syslog(LOG_ERROR, "f_lseek(%s, %d) => %d\n", file->name, file->pos, ret);
|
---|
222 | return 0;
|
---|
223 | }
|
---|
224 |
|
---|
225 | if (file->pos != fd->fptr) {
|
---|
226 | syslog(LOG_ERROR, "f_lseek(%s, %d) != %d\n", file->name, file->pos, fd->fptr);
|
---|
227 | }
|
---|
228 |
|
---|
229 | if ((ret = f_write(fd, src, len, &rlen)) != FR_OK) {
|
---|
230 | syslog(LOG_ERROR, "f_write(%s, 0x%p, %d) => %d\n", file->name, src, len, ret);
|
---|
231 | return 0;
|
---|
232 | }
|
---|
233 |
|
---|
234 | file->pos += rlen;
|
---|
235 | file->len += rlen;
|
---|
236 |
|
---|
237 | //syslog(LOG_ERROR, "httpd_fs_write(%d:%s, %d, %d) => %d\n", file->drv, file->name, file->pos, len, rlen);
|
---|
238 |
|
---|
239 | return rlen;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /*-----------------------------------------------------------------------------------*/
|
---|
243 | int httpd_fs_close(struct httpd_fs_file *file)
|
---|
244 | {
|
---|
245 | FRESULT ret;
|
---|
246 | FIL *fd = (FIL *)&file->fd;
|
---|
247 |
|
---|
248 | if ((ret = f_close(fd)) != FR_OK) {
|
---|
249 | syslog(LOG_ERROR, "f_close(%s) => %d\n", file->name, ret);
|
---|
250 | return 0;
|
---|
251 | }
|
---|
252 |
|
---|
253 | memset(fd, 0, sizeof(FIL));
|
---|
254 |
|
---|
255 | return 1;
|
---|
256 | }
|
---|