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