source: EcnlProtoTool/trunk/ntshell/webserver/httpd-fs.c@ 279

Last change on this file since 279 was 279, checked in by coas-nagasima, 7 years ago

ファイルを追加、更新。

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 7.0 KB
Line 
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
72char path[256] = "";
73FATFS fs;
74#endif
75
76#ifndef NULL
77#define NULL 0
78#endif /* NULL */
79
80/*-----------------------------------------------------------------------------------*/
81#ifdef FILE_DUMP
82FRESULT 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/*-----------------------------------------------------------------------------------*/
113int
114httpd_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/*-----------------------------------------------------------------------------------*/
160int
161httpd_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/*-----------------------------------------------------------------------------------*/
186int
187httpd_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/*-----------------------------------------------------------------------------------*/
213int
214httpd_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/*-----------------------------------------------------------------------------------*/
243int 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}
Note: See TracBrowser for help on using the repository browser.