xref: /utopia/UTPA2-700.0.x/mxlib/msfs/ecos/MsFS.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 
79 ////////////////////////////////////////////////////////////////////////////////
80 //
81 // Copyright (c) 2006-2007 MStar Semiconductor, Inc.
82 // All rights reserved.
83 //
84 // Unless otherwise stipulated in writing, any and all information contained
85 // herein regardless in any format shall remain the sole proprietary of
86 // MStar Semiconductor Inc. and be kept in strict confidence
87 // (��MStar Confidential Information��) by the recipient.
88 // Any unauthorized act including without limitation unauthorized disclosure,
89 // copying, use, reproduction, sale, distribution, modification, disassembling,
90 // reverse engineering and compiling of the contents of MStar Confidential
91 // Information is unlawful and strictly prohibited. MStar hereby reserves the
92 // rights to any and all damages, losses, costs and expenses resulting therefrom.
93 //
94 // @file   apiFS.c
95 // @author MStar Semiconductor Inc.
96 // @brief  FileSystem wrapper
97 // @note   Most of the APIs follows POSIX Specification including: \n
98 //                  - Files and Directories [POSIX Section 5] \n
99 //                  - Input and Output [POSIX Section 6]
100 // @note   With some limitations: \n
101 //                  - Doesn't support symolic link, user/group/other ID, ...
102 // @note   Plz reference Open Group's Single Unix Specification V3 for detailed POSIX API descriptions: \n
103 //                  - http://www.opengroup.org/onlinepubs/009695399/ \n
104 ///////////////////////////////////////////////////////////////////////////////////////////////////
105 
106 #if defined (MSOS_TYPE_ECOS)
107 
108 //-------------------------------------------------------------------------------------------------
109 // Include Files
110 //-------------------------------------------------------------------------------------------------
111 #include <stdio.h>
112 #include <fcntl.h>
113 #include <sys/stat.h>
114 #include <dirent.h>
115 #include <sys/types.h>
116 #include <fcntl.h>
117 #include <unistd.h>
118 #include <cyg/fileio/fileio.h>
119 #include "MsCommon.h"
120 #include "MsFS.h"
121 
122 //-------------------------------------------------------------------------------------------------
123 // Local Defines
124 //------------------------------------------------------------------------------------------------
125 
126 //-------------------------------------------------------------------------------------------------
127 // Macros
128 //-------------------------------------------------------------------------------------------------
129 #define ID_OUT_FS(x)    ((int)x+1)
130 #define ID_IN_FS(x)     ((int)x-1)
131 
132 //-------------------------------------------------------------------------------------------------
133 // Global Variables
134 //-------------------------------------------------------------------------------------------------
135 
136 //-------------------------------------------------------------------------------------------------
137 // Local Variables
138 //-------------------------------------------------------------------------------------------------
139 static int (*popen)(const char*, int, ... ) = open;
140 static int (*punlink)(const char *path) = unlink;
141 static int (*pmkdir)(const char *path, mode_t mode) = mkdir;
142 static int (*prmdir)(const char *path) = rmdir;
143 static int (*prename)(const char*, const char*) = rename;
144 static int (*plink)(const char *path1, const char *path2) = link;
145 static DIR* (*popendir)(const char *dirname) = opendir;
146 static int (*pchdir)(const char *path) = chdir;
147 static int (*pstat)(const char *path, struct stat *buf) = stat;
148 
149 //-------------------------------------------------------------------------------------------------
150 // Local Function Prototypes
151 //-------------------------------------------------------------------------------------------------
152 
153 //-------------------------------------------------------------------------------------------------
154 /// File system API initiatiion
155 /// @param  bUnicode \b IN: 0 support ASCII only, otherwise it supports unicode
156 /// @return 0 : no error
157 /// @return otherwise : error
158 //-------------------------------------------------------------------------------------------------
MsFS_Init(int bUnicode)159 int MsFS_Init( int bUnicode)
160 {
161     if (bUnicode)
162     {
163         popen = (int(*)(const char*, int, ... ))wopen;
164         punlink = (int(*)(const char *path)) wunlink;
165         pmkdir = (int(*)(const char*, mode_t)) wmkdir;
166         prmdir = (int(*)(const char *path)) wrmdir;
167         prename = (int(*)(const char*, const char*)) wrename;
168         plink = (int(*)(const char*, const char*)) wlink;
169         popendir = (DIR*(*)(const char*)) wopendir;
170         pchdir = (int(*)(const char*)) wchdir;
171         pstat = (int(*)(const char*, struct stat*)) wstat;
172         // pgetinfo = wgetinfo;
173         // psetinfo = wsetinfo;
174     }
175     else
176     {
177         popen = open;
178         punlink = unlink;
179         pmkdir = mkdir;
180         prmdir = rmdir;
181         prename = rename;
182         plink = link;
183         popendir = opendir;
184         pchdir = chdir;
185         pstat = stat;
186         // pgetinfo = getinfo;
187         // psetinfo = setinfo;
188     }
189     return 0;
190 }
191 
192 //
193 // Mount/Umount operations
194 //
195 //-------------------------------------------------------------------------------------------------
196 /// Mount a filesystem
197 /// @param  devname \b IN: name of hardware device:     eg. ""      , "/dev/fd0/"   , ...
198 /// @param  dir     \b IN: name of mount point:             "/ram"  , "/floppy"     , ...
199 /// @param  fsname  \b IN: name of implementing filesystem: "ramfs" , "fatfs"       , ...
200 /// @return 0 : no error
201 /// @return otherwise : error
202 //-------------------------------------------------------------------------------------------------
MsFS_Mount(const char * devname,const char * dir,const char * fsname,unsigned long mountflags,const void * data)203 int MsFS_Mount( const char *devname, const char *dir, const char *fsname, unsigned long mountflags, const void* data )
204 {
205     return mount( devname, dir, fsname);
206 }
207 
208 //-------------------------------------------------------------------------------------------------
209 /// Unmount a filesystem
210 /// @param  dir     \b IN: name of mount point:         eg.   "/ram"  , "/floppy"     , ...
211 /// @return 0 : no error
212 /// @return otherwise : error
213 //-------------------------------------------------------------------------------------------------
MsFS_Umount(const char * dir)214 int MsFS_Umount( const char *dir)
215 {
216     return umount( dir );
217 }
218 
219 //
220 // File/Directory operations
221 //
222 //-------------------------------------------------------------------------------------------------
223 /// Open a file
224 /// @param  path    \b IN: the pathname of the file
225 /// @param  oflag   \b IN: bitwise-OR flags of O_RDONLY, O_WRONLY, O_RDWR, O_APPEND, O_CREAT, O_EXCL, O_TRUNC, O_NONBLOCK
226 /// @param  ...     \b IN: not support
227 /// @return >=0 : the file descriptor associated with the file
228 /// @return <0 : errno set to indicate the error
229 /// @note   creat() is redundant and can be replaced by open( path, O_WRONLY | O_CREAT | O_TRUNC, mode );
230 /// @note   The maximum number of open files allowed is determined by the CYGNUM_FILEIO_NFILE
231 /// @note   Standard C Library's counterparts: \n
232 ///         - fopen()   is equivalent to    open();
233 //-------------------------------------------------------------------------------------------------
MsFS_Open(const char * path,int oflag,...)234 int MsFS_Open( const char *path, int oflag, ... )
235 {
236     return popen( path, oflag );
237 }
238 
239 //-------------------------------------------------------------------------------------------------
240 /// Unlink/Remove a file
241 /// @param  path    \b IN: the pathname of the file
242 /// @return =0 : the file descriptor associated with the named file
243 /// @return -1 : errno set to indicate the error
244 /// @note   Remove a link to a file; When the file's link count becomes 0 and no process has the file open,
245 ///         the space occupied by the file will be freed.
246 /// @note   Cannot unlink directories; use rmdir() instead
247 //-------------------------------------------------------------------------------------------------
MsFS_Unlink(const char * path)248 int MsFS_Unlink( const char *path )
249 {
250     return punlink( path );
251 }
252 
253 //-------------------------------------------------------------------------------------------------
254 /// Make a directory
255 /// @param  path    \b IN: the pathname of the directory
256 /// @param  mode    \b IN: not support
257 /// @return =0 : succeed
258 /// @return -1 : errno set to indicate the error
259 //-------------------------------------------------------------------------------------------------
MsFS_MkDir(const char * path,mode_t mode)260 int MsFS_MkDir( const char *path, mode_t mode )
261 {
262     return pmkdir( path, mode );
263 }
264 
265 //-------------------------------------------------------------------------------------------------
266 /// Remove a directory
267 /// @param  path    \b IN: the pathname of the directory
268 /// @return =0 : succeed
269 /// @return -1 : errno set to indicate the error
270 //-------------------------------------------------------------------------------------------------
MsFS_RmDir(const char * path)271 int MsFS_RmDir( const char *path )
272 {
273     return prmdir( path );
274 }
275 
276 //-------------------------------------------------------------------------------------------------
277 /// Change current working directory
278 /// @param  path    \b IN:  the pathname of the new working directory
279 /// @return =0 : succeed
280 /// @return -1 : errno set to indicate the error
281 //-------------------------------------------------------------------------------------------------
MsFS_ChDir(const char * path)282 int MsFS_ChDir(const char *path)
283 {
284     return pchdir( path );
285 }
286 
287 //-------------------------------------------------------------------------------------------------
288 /// Rename a file/directory
289 /// @param  path1    \b IN: the old pathname of the file
290 /// @param  path2    \b IN: The new pathname of the file
291 /// @return =0 : succeed
292 /// @return -1 : errno set to indicate the error
293 //-------------------------------------------------------------------------------------------------
MsFS_Rename(const char * path1,const char * path2)294 int MsFS_Rename( const char *path1, const char *path2 )
295 {
296     return prename( path1, path2 );
297 }
298 
299 //-------------------------------------------------------------------------------------------------
300 /// Create a link (directory entry) from an existing file (path1) to a new one (path2)
301 /// @param  path1    \b IN: the pathname of an existing file
302 /// @param  path2    \b IN: the pathname of the new directory entry
303 /// @return =0 : succeed
304 /// @return -1 : errno set to indicate the error
305 /// @note   RAM filesystem supports this API but FAT filesystem doesn't
306 //-------------------------------------------------------------------------------------------------
MsFS_Link(const char * path1,const char * path2)307 int MsFS_Link( const char *path1, const char *path2 )
308 {
309     return plink( path1, path2 );
310 }
311 
312 //-------------------------------------------------------------------------------------------------
313 /// Get file status
314 /// @param  path    \b IN:  the pathname of the file
315 /// @param  buf     \b IN:  a pointer to a stat structure as defined in <sys/stat.h>
316 /// @return =0 : succeed
317 /// @return -1 : errno set to indicate the error
318 /// @note   equivalent to MsFS_FStat
319 //-------------------------------------------------------------------------------------------------
MsFS_Stat(const char * path,struct stat * buf)320 int MsFS_Stat( const char *path, struct stat *buf )
321 {
322     return pstat( path, buf );
323 }
324 
325 //-------------------------------------------------------------------------------------------------
326 /// Get file status
327 /// @param  fd      \b IN:  tthe file descriptor associated with the file
328 /// @param  buf     \b IN:  a pointer to a stat structure as defined in <sys/stat.h>
329 /// @return =0 : succeed
330 /// @return -1 : errno set to indicate the error
331 /// @note   equivalent to   MsFS_Stat
332 //-------------------------------------------------------------------------------------------------
MsFS_FStat(int fd,struct stat * buf)333 int MsFS_FStat( int fd, struct stat *buf )
334 {
335     return fstat( fd, buf );
336 }
337 
338 //-------------------------------------------------------------------------------------------------
339 /// Get configurable pathname variables
340 /// @param  path    \b IN:  the pathname of the file or directory
341 /// @param  name    \b IN:  variable in <limits.h> to be queried relative to that file or directory
342 /// @return current variable value for the file or directory
343 /// @return -1 : errno set to indicate the error
344 /// @note   equivalent to   MsFS_FPathConf
345 //-------------------------------------------------------------------------------------------------
MsFS_PathConf(const char * path,int name)346 long MsFS_PathConf( const char *path, int name )
347 {
348     return pathconf( path, name );
349 }
350 
351 //-------------------------------------------------------------------------------------------------
352 /// Get configurable pathname variables
353 /// @param  fd      \b IN:  the file descriptor associated with the file
354 /// @param  name    \b IN:  variable in <limits.h> to be queried relative to that file or directory
355 /// @return current variable value for the file or directory
356 /// @return -1 : errno set to indicate the error
357 /// @note   equivalent to   MsFS_PathConf
358 //-------------------------------------------------------------------------------------------------
MsFS_FPathConf(int fd,int name)359 long MsFS_FPathConf( int fd, int name )
360 {
361     return fpathconf( fd, name );
362 }
363 
364 //-------------------------------------------------------------------------------------------------
365 /// Determine the accessibility of a file
366 /// @param  path    \b IN:  the pathname of the file or directory
367 /// @param  amode   \b IN:  the accessibility bit pattern: only existence test F_OK currently
368 /// @return 0: the requested access is permitted
369 /// @return -1 : errno set to indicate the error
370 //-------------------------------------------------------------------------------------------------
MsFS_Access(const char * path,int amode)371 int MsFS_Access( const char *path, int amode )
372 {
373     return access( path, amode );
374 }
375 
376 //-------------------------------------------------------------------------------------------------
377 /// Get the pathname of the current working directory
378 /// @param  buf     \b IN:  points to the buffer stored the absolute pathname of the current working directory
379 /// @param  size    \b IN:  the buffer size
380 /// @return buf argument
381 /// @return null : errno set to indicate the error
382 //-------------------------------------------------------------------------------------------------
MsFS_GetCwd(char * buf,size_t size)383 char * MsFS_GetCwd( char *buf, size_t size )
384 {
385     return getcwd( buf, size );
386 }
387 
388 //-------------------------------------------------------------------------------------------------
389 /// Open a directory stream for reading
390 /// @param  dirname \b IN:  the pathname of the directory
391 /// @return a pointer to DIR object
392 /// @return null : errno set to indicate the error.
393 /// @note   The directory stream (an ordered sequence of all the directory entries in a particular directory)
394 ///             is positioned at the first directory entry
395 //-------------------------------------------------------------------------------------------------
MsFS_OpenDir(const char * dirname)396 DIR * MsFS_OpenDir( const char *dirname )
397 {
398     return popendir( dirname );
399 }
400 
401 //-------------------------------------------------------------------------------------------------
402 /// Read a directory entry
403 /// @param  dirp    \b IN:  a pointer to DIR object
404 /// @return a pointer to dirent structure representing the directory entry at the current position in the directory
405 /// @return null : errno set to indicate the error
406 //-------------------------------------------------------------------------------------------------
MsFS_ReadDir(DIR * dirp)407 struct dirent * MsFS_ReadDir( DIR *dirp )
408 {
409     return readdir( dirp );
410 }
411 
412 //-------------------------------------------------------------------------------------------------
413 /// Reset the position of a directory stream to the beginning of a directory
414 /// @param  dirp    \b IN:  a pointer to DIR object
415 /// @return NONE
416 //-------------------------------------------------------------------------------------------------
MsFS_RewindDir(DIR * dirp)417 void MsFS_RewindDir( DIR *dirp )
418 {
419     rewinddir( dirp );
420 }
421 
422 //-------------------------------------------------------------------------------------------------
423 /// Close a directory stream
424 /// @param  dirp    \b IN:  a pointer to DIR object
425 /// @return NONE
426 //-------------------------------------------------------------------------------------------------
MsFS_CloseDir(DIR * dirp)427 void MsFS_CloseDir( DIR *dirp )
428 {
429     closedir( dirp );
430 }
431 
432 
433 //
434 // File IO operations
435 //
436 //-------------------------------------------------------------------------------------------------
437 /// Read from a file
438 /// @param  fd      \b IN:  the file descriptor associated with the file
439 /// @param  buf     \b OUT: read buffer
440 /// @param  len     \b IN:  number of bytes to be read
441 /// @return number of bytes actually read
442 /// @return -1 : errno set to indicate the error
443 /// @note   Standard C Library's counterparts: \n
444 ///         - fread()   is equivalent to    read();
445 //-------------------------------------------------------------------------------------------------
MsFS_Read(int fd,void * buf,size_t len)446 ssize_t MsFS_Read( int fd, void *buf, size_t len )
447 {
448     return read( fd, buf, len );
449 }
450 
451 //-------------------------------------------------------------------------------------------------
452 /// Write to a file
453 /// @param  fd      \b IN:  the file descriptor associated with the file
454 /// @param  buf     \b IN:  write buffer
455 /// @param  len     \b IN:  number of bytes to be written
456 /// @return number of bytes actually written
457 /// @return -1 : errno set to indicate the error
458 /// @note   Standard C Library's counterparts: \n
459 ///         - fwrite()  is equivalent to    write();
460 //-------------------------------------------------------------------------------------------------
MsFS_Write(int fd,const void * buf,size_t len)461 ssize_t MsFS_Write( int fd, const void *buf, size_t len )
462 {
463     return write( fd, buf, len );
464 }
465 
466 //-------------------------------------------------------------------------------------------------
467 /// Close a file
468 /// @param  fd      \b IN:  the file descriptor associated with the file
469 /// @return 0: succeed
470 /// @return -1 : errno set to indicate the error
471 /// @note   Standard C Library's counterparts: \n
472 ///         - fclose()  is equivalent to    close();
473 //-------------------------------------------------------------------------------------------------
MsFS_Close(int fd)474 int MsFS_Close( int fd )
475 {
476     return close( fd );
477 }
478 
479 //-------------------------------------------------------------------------------------------------
480 /// Seek a file
481 /// @param  fd      \b IN:  the file descriptor associated with the file
482 /// @param  pos     \b IN:  file byte offset relative to whence
483 /// @param  whence  \b IN:  SEEK_SET / SEEK_CUR / SEEK_END
484 /// @return byte offset from the beginning of the file
485 /// @return -1 : errno set to indicate the error
486 /// @note   Standard C Library's counterparts: \n
487 ///         - fseek()   is equivalent to    lseek();    except the return value \n
488 ///         - rewind()  is equivalent to    lseek(fd, 0, SEEK_SET); \n
489 ///         - ftell()   is equivalent to    lseek(fd, 0, SEEK_CUR); \n
490 //-------------------------------------------------------------------------------------------------
MsFS_Lseek(int fd,MS_U64 pos,int whence)491 MS_U64 MsFS_Lseek( int fd, MS_U64 pos, int whence )
492 {
493     return lseek( fd, pos, whence );
494 }
495 
496 //-------------------------------------------------------------------------------------------------
497 /// File control
498 /// @param  fd      \b IN:  the file descriptor associated with the file
499 /// @param  cmd     \b IN:  only F_DUPFD currently
500 /// @param  ...     \b IN:  only lowest-numbered file descriptor for F_DUPFD currently
501 /// @return 0: succeed
502 /// @return -1 : errno set to indicate the error
503 /// @note   fid = dup(fd);  is equivalent to  fid = fcntl(fd, F_DUPFD, 0);
504 /// @note   fid = dup2(fd, fd2);  is equivalent to  close(fd2); fid = fcntl(fd, F_DUPFD, fd2);
505 //-------------------------------------------------------------------------------------------------
MsFS_FCntl(int fd,int cmd,...)506 int MsFS_FCntl( int fd, int cmd, ... )
507 {
508     va_list a;
509     va_start( a, cmd );
510     int fda = va_arg(a, int);
511     va_end(a);
512     return fcntl( fd, cmd, fda);
513 }
514 
515 //-------------------------------------------------------------------------------------------------
516 /// Synchronize changes to a file (all data for the open file will be transferred to the storage device)
517 /// @param  fd      \b IN:  the file descriptor associated with the file
518 /// @return 0: succeed
519 /// @return -1 : errno set to indicate the error
520 /// @note   The function will not return until the system has completed that action or until an error is detected
521 //-------------------------------------------------------------------------------------------------
MsFS_FSync(int fd)522 int MsFS_FSync( int fd )
523 {
524     return fsync( fd );
525 }
526 
MsFS_Sync(void)527 void MsFS_Sync(void)
528 {
529     MsOS_FlushMemory();
530     MsOS_ReadMemory();
531 }
532 
533 
534 //
535 // Standard C Library's counterparts
536 //
537 //-------------------------------------------------------------------------------------------------
538 /// Open a file
539 /// @param  filename \b IN: the pathname of the file
540 /// @param  mode     \b IN:  r or rb - Open file for reading.
541 ///                         w or wb - Truncate to zero length or create file for writing.
542 ///                         a or ab - Append; open or create file for writing at end-of-file.
543 ///                         r+ or rb+ or r+b - Open file for update (reading and writing).
544 ///                         w+ or wb+ or w+b - Truncate to zero length or create file for update.
545 ///                         a+ or ab+ or a+b Append; open or create file for update, writing at end-of-file.
546 /// @return the file descriptor associated with the file
547 /// @return NULL : errno set to indicate the error
548 /// @note   The maximum number of open files allowed is determined by the CYGNUM_FILEIO_NFILE
549 //-------------------------------------------------------------------------------------------------
MsFS_Fopen(const char * filename,const char * mode)550 FILE * MsFS_Fopen( const char *filename, const char *mode )
551 {
552     int s32Ret = -1;
553 
554     int oflags = 0, omode;
555     int i;
556     if(mode == NULL)
557     	return NULL;
558     switch (*mode)
559     {
560      	case 'r':
561      		omode = O_RDONLY;
562      		break;
563      	case 'w':
564      		omode = O_WRONLY;
565      		oflags = O_CREAT|O_TRUNC;
566      		break;
567      	case 'a':
568      		omode = O_WRONLY;
569      		oflags = O_CREAT|O_APPEND;
570      		break;
571      	default:
572      		return NULL;
573     }
574     for (i = 1; i < 7; ++i)
575     {
576      	switch (*++mode)
577      	{
578      		case '\0':
579      			break;
580      		case '+':
581      			omode = O_RDWR;
582      			continue;
583      		case 'b':
584      			continue;
585      		case 'x':
586      			oflags |= O_EXCL;
587      			continue;
588      		default:
589      			/* Ignore. */
590      			continue;
591      	}
592      	break;
593     }
594     s32Ret = MsFS_Open( filename, omode|oflags );
595 
596     if ( s32Ret == -1 )
597     {
598         return NULL;
599     }
600     else
601     {
602         return (FILE *)ID_OUT_FS(s32Ret);
603     }
604 }
605 
606 //-------------------------------------------------------------------------------------------------
607 /// Close a file
608 /// @param  stream  \b IN:  the file descriptor associated with the file
609 /// @return 0: succeed
610 /// @return EOF(-1): errno set to indicate the error
611 //-------------------------------------------------------------------------------------------------
MsFS_Fclose(FILE * stream)612 int MsFS_Fclose( FILE *stream )
613 {
614     int Ret;
615 
616     Ret = MsFS_Close ( (int)ID_IN_FS(stream) );
617 
618     if (Ret == -1)
619         return EOF;
620     else
621         return 0;
622 }
623 
624 //-------------------------------------------------------------------------------------------------
625 /// Read from a file
626 /// @param  data     \b OUT: read buffer
627 /// @param  itemsize \b IN:  item size in bytes
628 /// @param  nitems   \b IN:  number of items
629 /// @param  stream   \b IN:  the file descriptor associated with the file
630 /// @return number of items actually read
631 /// @return -1 : errno set to indicate the error
632 //-------------------------------------------------------------------------------------------------
MsFS_Fread(void * data,size_t itemsize,size_t nitems,FILE * stream)633 size_t MsFS_Fread( void *data, size_t itemsize, size_t nitems, FILE *stream )
634 {
635     size_t read;
636 
637     read = MsFS_Read( (int)ID_IN_FS(stream), data, nitems*itemsize );
638 
639     if ( read == -1)
640         return -1;
641     else
642         return read/itemsize;
643 }
644 
645 //-------------------------------------------------------------------------------------------------
646 /// Write to a file
647 /// @param  data     \b IN: write buffer
648 /// @param  itemsize \b IN: item size in bytes
649 /// @param  nitems   \b IN: number of items
650 /// @param  stream   \b IN: the file descriptor associated with the file
651 /// @return number of items actually written
652 /// @return -1 : errno set to indicate the error
653 //-------------------------------------------------------------------------------------------------
MsFS_Fwrite(const void * data,size_t itemsize,size_t nitems,FILE * stream)654 size_t MsFS_Fwrite( const void *data, size_t itemsize, size_t nitems, FILE *stream )
655 {
656     size_t written;
657 
658     written = MsFS_Write( (int)ID_IN_FS(stream), data, nitems*itemsize );
659 
660     if ( written == -1)
661         return -1;
662     else
663         return written/itemsize;
664 }
665 
666 //-------------------------------------------------------------------------------------------------
667 /// Truncate a file
668 /// @param  stream   \b IN:  the file descriptor associated with the file
669 /// @param  length   \b IN:  the file size to truncate to.
670 /// @return  0: succeed
671 /// @return -1: errno set to indicate the error
MsFS_Fftruncate(FILE * stream,MS_U64 length)672 int MsFS_Fftruncate( FILE *stream, MS_U64 length)
673 {
674     return ftruncate((int)ID_IN_FS(stream), (off_t)length);
675 }
676 
677 //-------------------------------------------------------------------------------------------------
678 /// Seek a file
679 /// @param  stream  \b IN:  the file descriptor associated with the file
680 /// @param  pos     \b IN:  file byte offset relative to whence
681 /// @param  whence  \b IN:  SEEK_SET / SEEK_CUR / SEEK_END
682 /// @return 0: succeed
683 /// @return -1 : errno set to indicate the error
684 //-------------------------------------------------------------------------------------------------
MsFS_Fseek(FILE * stream,MS_U64 pos,int whence)685 int MsFS_Fseek( FILE *stream, MS_U64 pos, int whence )
686 {
687     int offset;
688 
689     offset = MsFS_Lseek( (int)ID_IN_FS(stream), (MS_U64)pos, whence );
690 
691     if ( offset == -1 )
692         return -1;
693     else
694         return 0;
695 }
696 
697 //-------------------------------------------------------------------------------------------------
698 /// Reset the file position
699 /// @param  stream  \b IN:  the file descriptor associated with the file
700 /// @return NONE
701 //-------------------------------------------------------------------------------------------------
MsFS_Rewind(FILE * stream)702 void MsFS_Rewind( FILE *stream )
703 {
704     MsFS_Lseek( (int)ID_IN_FS(stream), 0, SEEK_SET );
705 }
706 
707 //-------------------------------------------------------------------------------------------------
708 /// Obtain current file position
709 /// @param  stream  \b IN:  the file descriptor associated with the file
710 /// @return current file position measured in bytes from the beginning of the file
711 /// @return -1 : errno set to indicate the error
712 //-------------------------------------------------------------------------------------------------
MsFS_Ftell(FILE * stream)713 MS_U64 MsFS_Ftell( FILE *stream )
714 {
715     return ( MsFS_Lseek( (int)ID_IN_FS(stream), 0, SEEK_CUR ) );
716 }
717 
MsFS_Fflush(FILE * stream)718 int MsFS_Fflush( FILE *stream )
719 {
720     return MsFS_FSync( (int)ID_IN_FS(stream));
721 }
722 
MApi_FS_Info(const char * path,MS_BOOL bUnicode,MApi_FsInfo * pFsInfo)723 MS_BOOL MApi_FS_Info(const char *path, MS_BOOL bUnicode, MApi_FsInfo* pFsInfo)
724 {
725     struct cyg_fs_block_usage FsUsage;
726     MS_BOOL bRet;
727     bRet = (ENOERR == ((bUnicode)? cyg_fs_wgetinfo((const utf16*)path, FS_INFO_BLOCK_USAGE, (void*)&FsUsage, sizeof(FsUsage)):
728                                    cyg_fs_getinfo((const char*)path, FS_INFO_BLOCK_USAGE, (void*)&FsUsage, sizeof(FsUsage))));
729     if (bRet)
730     {
731         pFsInfo->u32ClusTotal = FsUsage.total_blocks;
732         pFsInfo->u32ClusFree = FsUsage.free_blocks;
733         pFsInfo->u32ClusSize = FsUsage.block_size;
734     }
735     return bRet;
736 }
737 
738 #endif // #if defined (MSOS_TYPE_ECOS)
739