source: azure_iot_hub_f767zi/trunk/asp_baseplatform/files/ff/fffcntl.c@ 457

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

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 11.8 KB
RevLine 
[457]1/*
2 * TOPPERS/ASP Kernel
3 * Toyohashi Open Platform for Embedded Real-Time Systems/
4 * Advanced Standard Profile Kernel
5 *
6 * Copyright (C) 2012 by GT Development Center RICOH COMPANY,LTD. JAPAN
7 *
8 * 上記著作権者は,以下の (1)~(4) の条件か,Free Software Foundation
9 * によって公表されている GNU General Public License の Version 2 に記
10 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
11 * を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
12 * 利用と呼ぶ)することを無償で許諾する.
13 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
14 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
15 * スコード中に含まれていること.
16 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
17 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
18 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
19 * の無保証規定を掲載すること.
20 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
21 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
22 * と.
23 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
24 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
25 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
26 * 報告すること.
27 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
28 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
29 *
30 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
31 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
32 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
33 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
34 *
35 * @(#) $Id$
36 */
37
38/*
39 * FatFS用POSIX変換用関数
40 * 1. ff_opendir FatFSファイルのオープンディレクトリ
41 * 2. ff_closedir FatFSファイルのクローズディレクトリ
42 * 3. ff_readdir FatFSファイルのディレクトリエントリ読み出し
43 * 4. ff_mkdir FatFSファイルのディレクトリ作成
44 * 5. ff_unlink FatFSファイルのディレクトリまたはファイル削除
45 * 6. ff_rename FatFSファイルのリネーム
46 * 7. ff_chmod FatFSファイルのチェンジモード
47 * 8. ff_stat FatFSファイルのダイレクトステートの取り出し
48 * 9. ff_statfs FatFSファイルのシステムステータスの取り出し
49 * 10.ff_open FatFSファイルのオープン
50 * 11.ff_close FatFSファイルのクローズ
51 * 12.ff_fstat FatFSファイルのステートの取り出し
52 * 13.ff_fseek FatFSファイルのシーク
53 * 14.ff_read FatFSファイルからのデータ読み出し
54 * 15.ff_write FatFSファイルへのデータ書き込み
55 *
56 */
57
58#include <stdlib.h>
59#include <string.h>
60#include "storagedevice.h"
61#include "ff.h"
62
63#ifndef NUM_DIRNEST
64#define NUM_DIRNEST 4
65#endif
66#ifndef NUM_FIL
67#define NUM_FIL 16
68#endif
69
70#define S_IRUSR 0000400 /* read permission, owner */
71#define S_IWUSR 0000200 /* write permission, owner */
72#define S_IXUSR 0000100/* execute/search permission, owner */
73#define S_IRGRP 0000040 /* read permission, group */
74#define S_IWGRP 0000020 /* write permission, grougroup */
75#define S_IXGRP 0000010/* execute/search permission, group */
76#define S_IROTH 0000004 /* read permission, other */
77#define S_IWOTH 0000002 /* write permission, other */
78#define S_IXOTH 0000001/* execute/search permission, other */
79
80#define FF_EINVAL 22 /* Invalid argument */
81
82static void *ff_opendir(const char *pathname);
83static int ff_closedir(void *dir);
84static int ff_readdir(void *dir, void *pdirent);
85static int ff_mkdir(const char *pathname);
86static int ff_unlink(const char *pathname);
87static int ff_rename(const char *oname, const char *nname);
88static int ff_chmod(const char *name, int mode);
89static int ff_stat(const char *name, struct stat *buf);
90static int ff_statfs(const char *name, void *status);
91static int ff_open(int devno, const char *pathname, int flags);
92static int ff_close(int fd);
93static int ff_fstat(int fd, struct stat *buf);
94static off_t ff_lseek(int fd, off_t offset, int whence, int *res);
95static long ff_read(int fd, void *buf, long count, int *res);
96static long ff_write(int fd, const void *buf, long count, int *res);
97
98
99const StorageDeviceFileFunc_t fatfsSDeviceFileFunc = {
100 ff_opendir,
101 ff_closedir,
102 ff_readdir,
103 ff_mkdir,
104 ff_unlink,
105 ff_unlink,
106 ff_rename,
107 ff_chmod,
108 ff_stat,
109 ff_statfs,
110 ff_open,
111 ff_close,
112 ff_fstat,
113 ff_lseek,
114 ff_read,
115 ff_write,
116 0
117};
118
119typedef struct DIRPACK {
120 DIR dir;
121 FILINFO finfo;
122} DIRP;
123
124static FIL FileObj[NUM_FIL];
125static DIRP dirobj[NUM_DIRNEST];
126static UH long_file_name[128];
127static int opendircount = 0;
128static char sddevname[256];
129static int ff_errno;
130
131/*
132 * FatFsファイルディレクトリオープン
133 */
134static void *ff_opendir(const char *pathname)
135{
136 DIRP *dirp;
137 FRESULT result;
138
139 if(opendircount >= NUM_DIRNEST)
140 return NULL;
141 dirp = &dirobj[opendircount++];
142 if((result = f_opendir(&dirp->dir, pathname)) != FR_OK){
143 ff_errno = result;
144 opendircount--;
145 return NULL;
146 }
147 else
148 return dirp;
149}
150
151/*
152 * FatFsファイルディレクトリクローズ
153 */
154static int ff_closedir(void *dir)
155{
156 if(opendircount > 0)
157 opendircount--;
158 return 0;
159}
160
161/*
162 * FatFsファイルディレクトリ読み込み
163 */
164static int ff_readdir(void *dir, void *pdirent)
165{
166 struct dirent2 *pd = pdirent;
167 DIRP *dirp = dir;
168 FRESULT result;
169
170#if _USE_LFN != 0
171 dirp->dir.lfn = long_file_name;
172#endif
173 if((result = f_readdir(&dirp->dir, &dirp->finfo)) != FR_OK){
174 ff_errno = result;
175 return 0;
176 }
177 if(dirp->dir.sect == 0)
178 return 0;
179 pd->d_fsize = dirp->finfo.fsize;
180 pd->d_date = dirp->finfo.fdate;
181 pd->d_time = dirp->finfo.ftime;
182 pd->d_type = dirp->finfo.fattrib;
183#if _USE_LFN != 0
184 if(dirp->dir.lfn_idx != 0xffff){
185 UH *pw, data;
186 UB *p, *pe;
187 pw = dirp->dir.lfn;
188 p = pd->d_name;
189 pe = p+254;
190 while(*pw != 0 && p < pe){
191 data = ff_convert(*pw, 0);
192 if(data <= 0x80)
193 *p++ = (UB)data;
194 else{
195 *p++ = data >> 8;
196 *p++ = (UB)data;
197 }
198 pw++;
199 }
200 *p = 0;
201 }
202 else
203#endif
204 {
205 int i;
206 for(i = 0 ; i < (8+1+3) && dirp->finfo.fname[i] != 0 ; i++)
207 pd->d_name[i] = dirp->finfo.fname[i];
208 pd->d_name[i] = 0;
209 }
210 return 1;
211}
212
213/*
214 * FatFsディレクトリ作成
215 */
216static int ff_mkdir(const char *pathname)
217{
218 int result = f_mkdir(pathname);
219 if(result == FR_OK)
220 return 0;
221 else{
222 ff_errno = result;
223 return -1;
224 }
225}
226
227/*
228 * FatFsディレクトリ/ファイル削除
229 */
230static int ff_unlink(const char *pathname)
231{
232 int result = f_unlink(pathname);
233 if(result == FR_OK)
234 return 0;
235 else{
236 ff_errno = result;
237 return -1;
238 }
239}
240
241/*
242 * FatFsリネーム
243 */
244static int ff_rename(const char *oname, const char *nname)
245{
246 int result = f_rename(oname, nname);
247 if(result == FR_OK)
248 return 0;
249 else{
250 ff_errno = result;
251 return -1;
252 }
253}
254
255/*
256 * FatFsモード変更
257 */
258static int ff_chmod(const char *name, int mode)
259{
260 FRESULT result;
261 BYTE value = 0;
262 BYTE mask = AM_RDO;
263
264 if((mode & (S_IWUSR|S_IWGRP|S_IWOTH)) == 0){
265 value |= AM_RDO;
266 }
267 result = f_chmod(name, value, mask);
268 if(result == FR_OK)
269 return 0;
270 else{
271 ff_errno = result;
272 return -1;
273 }
274}
275
276/*
277 * FatFs直接ファイルステート取得
278 */
279static int ff_stat(const char *name, struct stat *buf)
280{
281 FILINFO finfo;
282 FRESULT result;
283
284 memset(buf, 0, sizeof(struct stat));
285 finfo.lfname = NULL;
286 result = f_stat(name, &finfo);
287 if(result == FR_OK){
288 buf->st_mode = S_IRUSR|S_IRGRP|S_IROTH;
289 if((finfo.fattrib & AM_RDO) == 0)
290 buf->st_mode |= S_IWUSR|S_IWGRP|S_IWOTH;
291 buf->st_blksize = 512;
292 buf->st_blocks = (finfo.fsize+buf->st_blksize-1)/buf->st_blksize;
293 buf->st_size = finfo.fsize;
294 return 0;
295 }
296 else{
297 ff_errno = result;
298 return -1;
299 }
300}
301
302/*
303 * FatFsシステムステータスの取り出し
304 */
305static int ff_statfs(const char *name, void *status)
306{
307 struct statfs2 *p = status;
308 FATFS *fatfs;
309 DWORD nclust;
310 FRESULT result;
311
312 result = f_getfree(name, &nclust, &fatfs);
313 if(result == FR_OK){
314 memset(p, 0, sizeof(struct statfs2));
315 p->f_bsize = fatfs->csize * _MAX_SS;
316 p->f_blocks = fatfs->max_clust-1;
317 p->f_bfree = nclust;
318 return 0;
319 }
320 else{
321 ff_errno = result;
322 return -1;
323 }
324}
325
326/*
327 * FatFsファイルオープン
328 */
329static int ff_open(int devno, const char *pathname, int flags)
330{
331 int result, i;
332 unsigned char mode;
333
334 if(devno < 0 || devno >= MAXIMUM_DEVNO){
335 ff_errno = FF_EINVAL;
336 return -1;
337 }
338 sddevname[0] = devno + '0';
339 sddevname[1] = ':';
340 strcpy(&sddevname[2], pathname);
341 mode = (flags+1) & 3;
342 if(flags & O_CREAT)
343 mode |= FA_OPEN_ALWAYS;
344 if(flags & O_EXCL)
345 mode |= FA_CREATE_NEW;
346 result = -1;
347 for(i = 0 ; i < NUM_FIL ; i++){
348 if(FileObj[i].fs == 0){
349 result = f_open(&FileObj[i], sddevname, mode);
350 if(result == 0)
351 return (i);
352 else
353 FileObj[i].fs = 0;
354 break;
355 }
356 }
357 ff_errno = result;
358 return -1;
359}
360
361/*
362 * FatFsファイルクローズ
363 */
364static int ff_close(int fd)
365{
366 FRESULT result;
367
368 if(fd >= NUM_FIL){
369 ff_errno = FF_EINVAL;
370 return -1;
371 }
372 result = f_close(&FileObj[fd]);
373 if(result == FR_OK)
374 return 0;
375 else{
376 ff_errno = result;
377 return -1;
378 }
379}
380
381/*
382 * FatFsファイルステート取得
383 */
384static int ff_fstat(int fd, struct stat *buf)
385{
386 if(fd >= NUM_FIL){
387 ff_errno = FF_EINVAL;
388 return -1;
389 }
390 memset(buf, 0, sizeof(struct stat));
391 buf->st_size = FileObj[fd].fsize;
392#ifdef _EXT_RTOS
393 buf->st_mtime = (FileObj[fd.fdate<<16)
394 | FileObj[fd].ftime;
395#endif
396 buf->st_atime = buf->st_ctime
397 = buf->st_mtime;
398 buf->st_mode = S_IRUSR|S_IRGRP|S_IROTH;
399 if((FileObj[fd].flag & FA_WRITE) != 0)
400 buf->st_mode |= S_IWUSR|S_IWGRP|S_IWOTH;
401 return 0;
402}
403
404/*
405 * FatFsファイルシーク
406 */
407static off_t ff_lseek(int fd, off_t offset, int whence, int *res)
408{
409 off_t off = 0;
410 FRESULT result;
411
412 *res = -1;
413 if(fd >= NUM_FIL){
414 ff_errno = FF_EINVAL;
415 return -1;
416 }
417 switch (whence) {
418 case SEEK_SET:
419 off = offset;
420 break;
421 case SEEK_CUR:
422 off = FileObj[fd].fptr + offset;
423 break;
424 case SEEK_END:
425 off = FileObj[fd].fsize + offset;
426 break;
427 default:
428 ff_errno = FF_EINVAL;
429 return -1;
430 }
431 result = f_lseek(&FileObj[fd], off);
432 if(result != FR_OK){
433 ff_errno = result;
434 off = -1;
435 }
436 else
437 *res = 0;
438 return off;
439}
440
441/*
442 * FatFsファイル読みだし
443 */
444static long ff_read(int fd, void *buf, long count, int *res)
445{
446 long n;
447 FRESULT result;
448
449 *res = -1;
450 if(fd >= NUM_FIL){
451 ff_errno = FF_EINVAL;
452 return 0;
453 }
454 result = f_read(&FileObj[fd], buf, count, (unsigned int *)&n);
455 if(result != FR_OK){
456 ff_errno = result;
457 n = 0;
458 }
459 else
460 *res = 0;
461 return n;
462}
463
464/*
465 * FatFsファイル書き込み
466 */
467static long ff_write(int fd, const void *buf, long count, int *res)
468{
469 long n = 0;
470 FRESULT result;
471
472 *res = -1;
473 if(fd >= NUM_FIL){
474 ff_errno = FF_EINVAL;
475 return 0;
476 }
477 result = f_write(&FileObj[fd], buf, count, (unsigned int *)&n);
478 if(result != FR_OK){
479 ff_errno = result;
480 n = 0;
481 }
482 else
483 *res = 0;
484 return n;
485}
486
487/* This function is called each time errno is evaluated. */
488int *__errno(void)
489{
490 return (int *)&ff_errno;
491}
492
Note: See TracBrowser for help on using the repository browser.