[270] | 1 | /*
|
---|
[436] | 2 | * TOPPERS PROJECT Home Network Working Group Software
|
---|
[270] | 3 | *
|
---|
[436] | 4 | * Copyright (C) 2017-2019 Cores Co., Ltd. Japan
|
---|
[270] | 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
|
---|
[442] | 50 | #define strcat_s(dst, dsz, src) strcat(dst, src)
|
---|
[270] | 51 | #endif
|
---|
| 52 | #endif
|
---|
| 53 |
|
---|
| 54 | //#define FILE_DUMP
|
---|
| 55 | #ifdef FILE_DUMP
|
---|
| 56 | char path[256] = "";
|
---|
| 57 | FATFS fs;
|
---|
| 58 | #endif
|
---|
| 59 |
|
---|
| 60 | #ifndef NULL
|
---|
| 61 | #define NULL 0
|
---|
| 62 | #endif /* NULL */
|
---|
| 63 |
|
---|
| 64 | /*-----------------------------------------------------------------------------------*/
|
---|
| 65 | #ifdef FILE_DUMP
|
---|
| 66 | FRESULT scan_files(char* path, int size)
|
---|
| 67 | {
|
---|
| 68 | FRESULT res;
|
---|
| 69 | FILINFO fno;
|
---|
[434] | 70 | FATFS_DIR dir;
|
---|
[270] | 71 | int i;
|
---|
| 72 | char *fn; /* This function assumes non-Unicode configuration */
|
---|
| 73 |
|
---|
| 74 | res = f_opendir(&dir, path);
|
---|
| 75 | if (res == FR_OK) {
|
---|
[442] | 76 | i = strlen(path);
|
---|
[270] | 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) {
|
---|
[442] | 82 | sprintf(&path[i], "0:/%s", fn);
|
---|
[270] | 83 | res = scan_files(path, size);
|
---|
| 84 | if (res != FR_OK) break;
|
---|
| 85 | path[i] = 0;
|
---|
| 86 | }
|
---|
| 87 | else {
|
---|
[442] | 88 | printf("%s/%s\n", path, fn);
|
---|
[270] | 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | return res;
|
---|
| 94 | }
|
---|
| 95 | #endif
|
---|
| 96 | /*-----------------------------------------------------------------------------------*/
|
---|
| 97 | int
|
---|
[279] | 98 | httpd_fs_open(char *name, int len, struct httpd_fs_file *file)
|
---|
[270] | 99 | {
|
---|
| 100 | FRESULT res;
|
---|
| 101 | FIL *fd = (FIL *)&file->fd;
|
---|
| 102 | FILINFO fno;
|
---|
[434] | 103 | FATFS_DIR dir;
|
---|
[270] | 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) {
|
---|
[442] | 113 | printf("f_opendir(%s) => %d\n", name, res);
|
---|
[270] | 114 | return 0;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | if ((res = f_readdir(&dir, &fno)) != FR_OK) {
|
---|
[442] | 118 | printf("f_readdir(%s) => %d\n", name, res);
|
---|
[270] | 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) {
|
---|
[442] | 131 | printf("f_open(%s) => %d %x\n", name, res, fno.fattrib);
|
---|
[270] | 132 | return 0;
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | file->len = fd->fsize;
|
---|
| 137 |
|
---|
[442] | 138 | //printf("httpd_fs_open(%s) %d\n", name, file->len);
|
---|
[270] | 139 |
|
---|
| 140 | return 1;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /*-----------------------------------------------------------------------------------*/
|
---|
| 144 | int
|
---|
[279] | 145 | httpd_fs_create(char *name, struct httpd_fs_file *file)
|
---|
[270] | 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) {
|
---|
[442] | 157 | printf("f_open(%s) => %d\n", name, res);
|
---|
[270] | 158 | return 0;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | file->pos = 0;
|
---|
| 162 | file->len = 0;
|
---|
| 163 |
|
---|
[442] | 164 | //printf("httpd_fs_create(%s) %d\n", file->name, file->len);
|
---|
[270] | 165 |
|
---|
| 166 | return 1;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /*-----------------------------------------------------------------------------------*/
|
---|
| 170 | int
|
---|
[279] | 171 | httpd_fs_read(struct httpd_fs_file *file, void *dst, int len)
|
---|
[270] | 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) {
|
---|
[442] | 178 | printf("f_lseek(%s, %d) => %d\n", file->name, file->pos, ret);
|
---|
[270] | 179 | return 0;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | if (file->pos != fd->fptr) {
|
---|
[442] | 183 | printf("f_lseek(%s, %d) != %d\n", file->name, file->pos, fd->fptr);
|
---|
[270] | 184 | }
|
---|
| 185 |
|
---|
| 186 | if ((ret = f_read(fd, dst, len, &rlen)) != FR_OK) {
|
---|
[442] | 187 | printf("f_read(%s, 0x%p, %d) => %d\n", file->name, dst, len, ret);
|
---|
[270] | 188 | return 0;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[442] | 191 | //printf("httpd_fs_read(%s, %d, %d) => %d\n", file->name, file->pos, len, rlen);
|
---|
[270] | 192 |
|
---|
| 193 | return rlen;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /*-----------------------------------------------------------------------------------*/
|
---|
| 197 | int
|
---|
[279] | 198 | httpd_fs_write(struct httpd_fs_file *file, const void *src, int len)
|
---|
[270] | 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) {
|
---|
[442] | 205 | printf("f_lseek(%s, %d) => %d\n", file->name, file->pos, ret);
|
---|
[270] | 206 | return 0;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | if (file->pos != fd->fptr) {
|
---|
[442] | 210 | printf("f_lseek(%s, %d) != %d\n", file->name, file->pos, fd->fptr);
|
---|
[270] | 211 | }
|
---|
| 212 |
|
---|
| 213 | if ((ret = f_write(fd, src, len, &rlen)) != FR_OK) {
|
---|
[442] | 214 | printf("f_write(%s, 0x%p, %d) => %d\n", file->name, src, len, ret);
|
---|
[270] | 215 | return 0;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | file->pos += rlen;
|
---|
| 219 | file->len += rlen;
|
---|
| 220 |
|
---|
[442] | 221 | //printf("httpd_fs_write(%s, %d, %d) => %d\n", file->name, file->pos, len, rlen);
|
---|
[270] | 222 |
|
---|
| 223 | return rlen;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | /*-----------------------------------------------------------------------------------*/
|
---|
[279] | 227 | int httpd_fs_close(struct httpd_fs_file *file)
|
---|
[270] | 228 | {
|
---|
| 229 | FRESULT ret;
|
---|
| 230 | FIL *fd = (FIL *)&file->fd;
|
---|
| 231 |
|
---|
| 232 | if ((ret = f_close(fd)) != FR_OK) {
|
---|
[442] | 233 | printf("f_close(%s) => %d\n", file->name, ret);
|
---|
[270] | 234 | return 0;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | memset(fd, 0, sizeof(FIL));
|
---|
| 238 |
|
---|
| 239 | return 1;
|
---|
| 240 | }
|
---|