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

Last change on this file was 442, checked in by coas-nagasima, 4 years ago

ntshellアプリはnewlibを使うよう変更し、syscallの実装部分と区別がつくよう更新。

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 6.7 KB
Line 
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
48#ifndef _MSC_VER
49#ifndef strcat_s
50#define strcat_s(dst, dsz, src) strcat(dst, src)
51#endif
52#endif
53
54//#define FILE_DUMP
55#ifdef FILE_DUMP
56char path[256] = "";
57FATFS fs;
58#endif
59
60#ifndef NULL
61#define NULL 0
62#endif /* NULL */
63
64/*-----------------------------------------------------------------------------------*/
65#ifdef FILE_DUMP
66FRESULT scan_files(char* path, int size)
67{
68 FRESULT res;
69 FILINFO fno;
70 FATFS_DIR dir;
71 int i;
72 char *fn; /* This function assumes non-Unicode configuration */
73
74 res = f_opendir(&dir, path);
75 if (res == FR_OK) {
76 i = strlen(path);
77 for (;;) {
78 res = f_readdir(&dir, &fno);
79 if (res != FR_OK || fno.fname[0] == 0) break;
80 fn = fno.fname;
81 if (fno.fattrib & AM_DIR) {
82 sprintf(&path[i], "0:/%s", fn);
83 res = scan_files(path, size);
84 if (res != FR_OK) break;
85 path[i] = 0;
86 }
87 else {
88 printf("%s/%s\n", path, fn);
89 }
90 }
91 }
92
93 return res;
94}
95#endif
96/*-----------------------------------------------------------------------------------*/
97int
98httpd_fs_open(char *name, int len, struct httpd_fs_file *file)
99{
100 FRESULT res;
101 FIL *fd = (FIL *)&file->fd;
102 FILINFO fno;
103 FATFS_DIR dir;
104
105 file->pos = 0;
106 file->len = 0;
107 file->name = name;
108 file->redirect = 0;
109 memset(fd, 0, sizeof(FIL));
110
111 if ((res = f_open(fd, name, FA_OPEN_EXISTING | FA_READ)) != FR_OK) {
112 if ((res = f_opendir(&dir, name)) != FR_OK) {
113 printf("f_opendir(%s) => %d\n", name, res);
114 return 0;
115 }
116
117 if ((res = f_readdir(&dir, &fno)) != FR_OK) {
118 printf("f_readdir(%s) => %d\n", name, res);
119 return 0;
120 }
121
122 if (len != 0/*fno.fattrib & AM_DIR*/) {
123 strcat_s(name, len, http_index_html);
124 res = f_open(fd, name, FA_OPEN_EXISTING | FA_READ);
125 file->redirect = res == FR_OK;
126 }
127 else
128 res = FR_NO_FILE;
129
130 if (res != FR_OK) {
131 printf("f_open(%s) => %d %x\n", name, res, fno.fattrib);
132 return 0;
133 }
134 }
135
136 file->len = fd->fsize;
137
138 //printf("httpd_fs_open(%s) %d\n", name, file->len);
139
140 return 1;
141}
142
143/*-----------------------------------------------------------------------------------*/
144int
145httpd_fs_create(char *name, struct httpd_fs_file *file)
146{
147 FRESULT res;
148 FIL *fd = (FIL *)&file->fd;
149
150 file->pos = 0;
151 file->len = 0;
152 file->name = name;
153 file->redirect = 0;
154 memset(fd, 0, sizeof(FIL));
155
156 if ((res = f_open(fd, name, FA_CREATE_ALWAYS | FA_WRITE)) != FR_OK) {
157 printf("f_open(%s) => %d\n", name, res);
158 return 0;
159 }
160
161 file->pos = 0;
162 file->len = 0;
163
164 //printf("httpd_fs_create(%s) %d\n", file->name, file->len);
165
166 return 1;
167}
168
169/*-----------------------------------------------------------------------------------*/
170int
171httpd_fs_read(struct httpd_fs_file *file, void *dst, int len)
172{
173 FRESULT ret;
174 UINT rlen = 0;
175 FIL *fd = (FIL *)&file->fd;
176
177 if ((ret = f_lseek(fd, file->pos)) != FR_OK) {
178 printf("f_lseek(%s, %d) => %d\n", file->name, file->pos, ret);
179 return 0;
180 }
181
182 if (file->pos != fd->fptr) {
183 printf("f_lseek(%s, %d) != %d\n", file->name, file->pos, fd->fptr);
184 }
185
186 if ((ret = f_read(fd, dst, len, &rlen)) != FR_OK) {
187 printf("f_read(%s, 0x%p, %d) => %d\n", file->name, dst, len, ret);
188 return 0;
189 }
190
191 //printf("httpd_fs_read(%s, %d, %d) => %d\n", file->name, file->pos, len, rlen);
192
193 return rlen;
194}
195
196/*-----------------------------------------------------------------------------------*/
197int
198httpd_fs_write(struct httpd_fs_file *file, const void *src, int len)
199{
200 FRESULT ret;
201 UINT rlen = 0;
202 FIL *fd = (FIL *)&file->fd;
203
204 if ((ret = f_lseek(fd, file->pos)) != FR_OK) {
205 printf("f_lseek(%s, %d) => %d\n", file->name, file->pos, ret);
206 return 0;
207 }
208
209 if (file->pos != fd->fptr) {
210 printf("f_lseek(%s, %d) != %d\n", file->name, file->pos, fd->fptr);
211 }
212
213 if ((ret = f_write(fd, src, len, &rlen)) != FR_OK) {
214 printf("f_write(%s, 0x%p, %d) => %d\n", file->name, src, len, ret);
215 return 0;
216 }
217
218 file->pos += rlen;
219 file->len += rlen;
220
221 //printf("httpd_fs_write(%s, %d, %d) => %d\n", file->name, file->pos, len, rlen);
222
223 return rlen;
224}
225
226/*-----------------------------------------------------------------------------------*/
227int httpd_fs_close(struct httpd_fs_file *file)
228{
229 FRESULT ret;
230 FIL *fd = (FIL *)&file->fd;
231
232 if ((ret = f_close(fd)) != FR_OK) {
233 printf("f_close(%s) => %d\n", file->name, ret);
234 return 0;
235 }
236
237 memset(fd, 0, sizeof(FIL));
238
239 return 1;
240}
Note: See TracBrowser for help on using the repository browser.