xref: /utopia/UTPA2-700.0.x/mxlib/msfs/linux/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_LINUX)
107 
108 #define _LARGEFILE_SOURCE
109 #define _FILE_OFFSET_BITS 64
110 
111 //-------------------------------------------------------------------------------------------------
112 // Include Files
113 //-------------------------------------------------------------------------------------------------
114 #include <stdio.h>
115 #include <fcntl.h>
116 #include <sys/stat.h>
117 #include <dirent.h>
118 #include <sys/types.h>
119 #include <fcntl.h>
120 #include <unistd.h>
121 #include <sys/mount.h>
122 #include "MsCommon.h"
123 #include "MsFS.h"
124 #if (defined (MSOS_TYPE_LINUX) && !defined(ANDROID))
125 #include <sys/statvfs.h>
126 #endif
127 #if defined (ANDROID)
128 #include <sys/vfs.h>
129 #define statvfs statfs
130 #endif
131 #include <string.h>
132 
133 // extern struct dirent *readdir(DIR *dir);
134 
135 //-------------------------------------------------------------------------------------------------
136 // Local Defines
137 //------------------------------------------------------------------------------------------------
138 
139 //-------------------------------------------------------------------------------------------------
140 // Macros
141 //-------------------------------------------------------------------------------------------------
142 #define MAX_CHARACTER_SIZE    8
143 #define MAX_FILENAME_LENGTH    512
144 
145 //-------------------------------------------------------------------------------------------------
146 // Global Variables
147 //-------------------------------------------------------------------------------------------------
148 static int _bUnicode=0;
149 
150 //-------------------------------------------------------------------------------------------------
151 // Local Variables
152 //-------------------------------------------------------------------------------------------------
153 
154 //-------------------------------------------------------------------------------------------------
155 // Local Function Prototypes
156 //-------------------------------------------------------------------------------------------------
157 static unsigned char * _MsFS_UnicodeStrToUTF8Str (const unsigned short * unicode_str,unsigned char * utf8_str, int utf8_str_size);
158 
159 
160 
161 //-------------------------------------------------------------------------------------------------
162 /// File system API initiatiion
163 /// @param  bUnicode \b IN: 0 support ASCII only, otherwise it supports unicode
164 /// @return 0 : no error
165 /// @return otherwise : error
166 //-------------------------------------------------------------------------------------------------
MsFS_Init(int bUnicode)167 int MsFS_Init( int bUnicode)
168 {
169     _bUnicode= bUnicode;
170     return 0;
171 }
172 
173 //
174 // Mount/Umount operations
175 //
176 //-------------------------------------------------------------------------------------------------
177 /// Mount a filesystem
178 /// @param  devname \b IN: name of hardware device:     eg. ""      , "/dev/fd0/"   , ...
179 /// @param  dir     \b IN: name of mount point:             "/ram"  , "/floppy"     , ...
180 /// @param  fsname  \b IN: name of implementing filesystem: "ramfs" , "fatfs"       , ...
181 /// @return 0 : no error
182 /// @return otherwise : error
183 //-------------------------------------------------------------------------------------------------
MsFS_Mount(const char * devname,const char * dir,const char * fsname,unsigned long mountflags,const void * data)184 int MsFS_Mount( const char *devname, const char *dir, const char *fsname, unsigned long mountflags, const void* data )
185 {
186     return mount( devname, dir, fsname, mountflags, data);
187 }
188 
189 //-------------------------------------------------------------------------------------------------
190 /// Unmount a filesystem
191 /// @param  dir     \b IN: name of mount point:         eg.   "/ram"  , "/floppy"     , ...
192 /// @return 0 : no error
193 /// @return otherwise : error
194 //-------------------------------------------------------------------------------------------------
MsFS_Umount(const char * dir)195 int MsFS_Umount( const char *dir)
196 {
197     return umount( dir );
198 }
199 
200 //
201 // File/Directory operations
202 //
203 //-------------------------------------------------------------------------------------------------
204 /// Open a file
205 /// @param  path    \b IN: the pathname of the file
206 /// @param  oflag   \b IN: bitwise-OR flags of O_RDONLY, O_WRONLY, O_RDWR, O_APPEND, O_CREAT, O_EXCL, O_TRUNC, O_NONBLOCK
207 /// @param  ...     \b IN: not support
208 /// @return >=0 : the file descriptor associated with the file
209 /// @return <0 : errno set to indicate the error
210 /// @note   creat() is redundant and can be replaced by open( path, O_WRONLY | O_CREAT | O_TRUNC, mode );
211 /// @note   The maximum number of open files allowed is determined by the CYGNUM_FILEIO_NFILE
212 /// @note   Standard C Library's counterparts: \n
213 ///         - fopen()   is equivalent to    open();
214 //-------------------------------------------------------------------------------------------------
MsFS_Open(const char * path,int oflag,...)215 int MsFS_Open( const char *path, int oflag, ... )
216 {
217     char u8path[MAX_FILENAME_LENGTH] = {0};
218     if(_bUnicode)
219     {
220         _MsFS_UnicodeStrToUTF8Str((const unsigned short *)path,(unsigned char *)u8path,MAX_FILENAME_LENGTH);
221         return open(u8path,oflag);
222     }
223     return open( path, oflag );
224 }
225 
226 //-------------------------------------------------------------------------------------------------
227 /// Unlink/Remove a file
228 /// @param  path    \b IN: the pathname of the file
229 /// @return =0 : the file descriptor associated with the named file
230 /// @return -1 : errno set to indicate the error
231 /// @note   Remove a link to a file; When the file's link count becomes 0 and no process has the file open,
232 ///         the space occupied by the file will be freed.
233 /// @note   Cannot unlink directories; use rmdir() instead
234 //-------------------------------------------------------------------------------------------------
MsFS_Unlink(const char * path)235 int MsFS_Unlink( const char *path )
236 {
237     char u8path[MAX_FILENAME_LENGTH] = {0};
238     if(_bUnicode)
239     {
240         _MsFS_UnicodeStrToUTF8Str((const unsigned short *)path,(unsigned char *)u8path,MAX_FILENAME_LENGTH);
241         return unlink(u8path);
242     }
243     return unlink( path );
244 }
245 
246 //-------------------------------------------------------------------------------------------------
247 /// Make a directory
248 /// @param  path    \b IN: the pathname of the directory
249 /// @param  mode    \b IN: not support
250 /// @return =0 : succeed
251 /// @return -1 : errno set to indicate the error
252 //-------------------------------------------------------------------------------------------------
MsFS_MkDir(const char * path,mode_t mode)253 int MsFS_MkDir( const char *path, mode_t mode )
254 {
255     char u8path[MAX_FILENAME_LENGTH] = {0};
256     if(_bUnicode)
257     {
258         _MsFS_UnicodeStrToUTF8Str((const unsigned short *)path,(unsigned char *)u8path,MAX_FILENAME_LENGTH);
259         return mkdir( u8path, mode );
260     }
261 
262     return mkdir( 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     char u8path[MAX_FILENAME_LENGTH] = {0};
274     if(_bUnicode)
275     {
276         _MsFS_UnicodeStrToUTF8Str((const unsigned short *)path,(unsigned char *)u8path,MAX_FILENAME_LENGTH);
277         return rmdir( u8path);
278     }
279 
280     return rmdir( path );
281 }
282 
283 //-------------------------------------------------------------------------------------------------
284 /// Change current working directory
285 /// @param  path    \b IN:  the pathname of the new working directory
286 /// @return =0 : succeed
287 /// @return -1 : errno set to indicate the error
288 //-------------------------------------------------------------------------------------------------
MsFS_ChDir(const char * path)289 int MsFS_ChDir(const char *path)
290 {
291     char u8path[MAX_FILENAME_LENGTH] = {0};
292     if(_bUnicode)
293     {
294         _MsFS_UnicodeStrToUTF8Str((const unsigned short *)path,(unsigned char *)u8path,MAX_FILENAME_LENGTH);
295         return chdir( u8path);
296     }
297 
298     return chdir( path );
299 }
300 
301 //-------------------------------------------------------------------------------------------------
302 /// Rename a file/directory
303 /// @param  path1    \b IN: the old pathname of the file
304 /// @param  path2    \b IN: The new pathname of the file
305 /// @return =0 : succeed
306 /// @return -1 : errno set to indicate the error
307 //-------------------------------------------------------------------------------------------------
MsFS_Rename(const char * path1,const char * path2)308 int MsFS_Rename( const char *path1, const char *path2 )
309 {
310     char u8path1[MAX_FILENAME_LENGTH] = {0};
311     char u8path2[MAX_FILENAME_LENGTH] = {0};
312     if(_bUnicode)
313     {
314         _MsFS_UnicodeStrToUTF8Str((const unsigned short *)path1,(unsigned char *)u8path1,MAX_FILENAME_LENGTH);
315         _MsFS_UnicodeStrToUTF8Str((const unsigned short *)path2,(unsigned char *)u8path2,MAX_FILENAME_LENGTH);
316         return rename( u8path1, u8path2 );
317     }
318 
319     return rename( path1, path2 );
320 }
321 
322 //-------------------------------------------------------------------------------------------------
323 /// Create a link (directory entry) from an existing file (path1) to a new one (path2)
324 /// @param  path1    \b IN: the pathname of an existing file
325 /// @param  path2    \b IN: the pathname of the new directory entry
326 /// @return =0 : succeed
327 /// @return -1 : errno set to indicate the error
328 /// @note   RAM filesystem supports this API but FAT filesystem doesn't
329 //-------------------------------------------------------------------------------------------------
MsFS_Link(const char * path1,const char * path2)330 int MsFS_Link( const char *path1, const char *path2 )
331 {
332     char u8path1[MAX_FILENAME_LENGTH] = {0};
333     char u8path2[MAX_FILENAME_LENGTH] = {0};
334     if(_bUnicode)
335     {
336         _MsFS_UnicodeStrToUTF8Str((const unsigned short *)path1,(unsigned char *)u8path1,MAX_FILENAME_LENGTH);
337         _MsFS_UnicodeStrToUTF8Str((const unsigned short *)path2,(unsigned char *)u8path2,MAX_FILENAME_LENGTH);
338         return link( u8path1, u8path2 );
339     }
340 
341     return link( path1, path2 );
342 }
343 
344 //-------------------------------------------------------------------------------------------------
345 /// Get file status
346 /// @param  path    \b IN:  the pathname of the file
347 /// @param  buf     \b IN:  a pointer to a stat structure as defined in <sys/stat.h>
348 /// @return =0 : succeed
349 /// @return -1 : errno set to indicate the error
350 /// @note   equivalent to MsFS_FStat
351 //-------------------------------------------------------------------------------------------------
MsFS_Stat(const char * path,struct stat * buf)352 int MsFS_Stat( const char *path, struct stat *buf )
353 {
354     char u8path[MAX_FILENAME_LENGTH] = {0};
355     if(_bUnicode)
356     {
357         _MsFS_UnicodeStrToUTF8Str((const unsigned short *)path,(unsigned char *)u8path,MAX_FILENAME_LENGTH);
358         return stat( u8path, buf );
359     }
360 
361     return stat( path, buf );
362 }
363 
364 //-------------------------------------------------------------------------------------------------
365 /// Get file status
366 /// @param  fd      \b IN:  tthe file descriptor associated with the file
367 /// @param  buf     \b IN:  a pointer to a stat structure as defined in <sys/stat.h>
368 /// @return =0 : succeed
369 /// @return -1 : errno set to indicate the error
370 /// @note   equivalent to   MsFS_Stat
371 //-------------------------------------------------------------------------------------------------
MsFS_FStat(int fd,struct stat * buf)372 int MsFS_FStat( int fd, struct stat *buf )
373 {
374     return fstat( fd, buf );
375 }
376 
377 //-------------------------------------------------------------------------------------------------
378 /// Get configurable pathname variables
379 /// @param  path    \b IN:  the pathname of the file or directory
380 /// @param  name    \b IN:  variable in <limits.h> to be queried relative to that file or directory
381 /// @return current variable value for the file or directory
382 /// @return -1 : errno set to indicate the error
383 /// @note   equivalent to   MsFS_FPathConf
384 //-------------------------------------------------------------------------------------------------
MsFS_PathConf(const char * path,int name)385 long MsFS_PathConf( const char *path, int name )
386 {
387     return pathconf( path, name );
388 }
389 
390 //-------------------------------------------------------------------------------------------------
391 /// Get configurable pathname variables
392 /// @param  fd      \b IN:  the file descriptor associated with the file
393 /// @param  name    \b IN:  variable in <limits.h> to be queried relative to that file or directory
394 /// @return current variable value for the file or directory
395 /// @return -1 : errno set to indicate the error
396 /// @note   equivalent to   MsFS_PathConf
397 //-------------------------------------------------------------------------------------------------
MsFS_FPathConf(int fd,int name)398 long MsFS_FPathConf( int fd, int name )
399 {
400     return fpathconf( fd, name );
401 }
402 
403 //-------------------------------------------------------------------------------------------------
404 /// Determine the accessibility of a file
405 /// @param  path    \b IN:  the pathname of the file or directory
406 /// @param  amode   \b IN:  the accessibility bit pattern: only existence test F_OK currently
407 /// @return 0: the requested access is permitted
408 /// @return -1 : errno set to indicate the error
409 //-------------------------------------------------------------------------------------------------
MsFS_Access(const char * path,int amode)410 int MsFS_Access( const char *path, int amode )
411 {
412     char u8path[MAX_FILENAME_LENGTH] = {0};
413     if(_bUnicode)
414     {
415         _MsFS_UnicodeStrToUTF8Str((const unsigned short *)path,(unsigned char *)u8path,MAX_FILENAME_LENGTH);
416         return access(u8path, amode );
417     }
418 
419     return access( path, amode );
420 }
421 
422 //-------------------------------------------------------------------------------------------------
423 /// Get the pathname of the current working directory
424 /// @param  buf     \b IN:  points to the buffer stored the absolute pathname of the current working directory
425 /// @param  size    \b IN:  the buffer size
426 /// @return buf argument
427 /// @return null : errno set to indicate the error
428 //-------------------------------------------------------------------------------------------------
MsFS_GetCwd(char * buf,size_t size)429 char * MsFS_GetCwd( char *buf, size_t size )
430 {
431     return getcwd( buf, size );
432 }
433 
434 //-------------------------------------------------------------------------------------------------
435 /// Open a directory stream for reading
436 /// @param  dirname \b IN:  the pathname of the directory
437 /// @return a pointer to DIR object
438 /// @return null : errno set to indicate the error.
439 /// @note   The directory stream (an ordered sequence of all the directory entries in a particular directory)
440 ///             is positioned at the first directory entry
441 //-------------------------------------------------------------------------------------------------
MsFS_OpenDir(const char * dirname)442 DIR * MsFS_OpenDir( const char *dirname )
443 {
444     char u8dirname[MAX_FILENAME_LENGTH] = {0};
445     if(_bUnicode)
446     {
447         _MsFS_UnicodeStrToUTF8Str((const unsigned short *)dirname,(unsigned char *)u8dirname,MAX_FILENAME_LENGTH);
448         return opendir(u8dirname );
449     }
450 
451     return opendir( dirname );
452 }
453 
454 //-------------------------------------------------------------------------------------------------
455 /// Read a directory entry
456 /// @param  dirp    \b IN:  a pointer to DIR object
457 /// @return a pointer to dirent structure representing the directory entry at the current position in the directory
458 /// @return null : errno set to indicate the error
459 //-------------------------------------------------------------------------------------------------
MsFS_ReadDir(DIR * dirp)460 struct dirent * MsFS_ReadDir( DIR *dirp )
461 {
462     printf("[%s][%d] MsFS_ReadDir is not supported at this stage\n", __FUNCTION__, __LINE__);
463     return NULL;
464     // return readdir( dirp );
465 }
466 
467 //-------------------------------------------------------------------------------------------------
468 /// Reset the position of a directory stream to the beginning of a directory
469 /// @param  dirp    \b IN:  a pointer to DIR object
470 /// @return NONE
471 //-------------------------------------------------------------------------------------------------
MsFS_RewindDir(DIR * dirp)472 void MsFS_RewindDir( DIR *dirp )
473 {
474     rewinddir( dirp );
475 }
476 
477 //-------------------------------------------------------------------------------------------------
478 /// Close a directory stream
479 /// @param  dirp    \b IN:  a pointer to DIR object
480 /// @return NONE
481 //-------------------------------------------------------------------------------------------------
MsFS_CloseDir(DIR * dirp)482 void MsFS_CloseDir( DIR *dirp )
483 {
484     closedir( dirp );
485 }
486 
487 
488 //
489 // File IO operations
490 //
491 //-------------------------------------------------------------------------------------------------
492 /// Read from a file
493 /// @param  fd      \b IN:  the file descriptor associated with the file
494 /// @param  buf     \b OUT: read buffer
495 /// @param  len     \b IN:  number of bytes to be read
496 /// @return number of bytes actually read
497 /// @return -1 : errno set to indicate the error
498 /// @note   Standard C Library's counterparts: \n
499 ///         - fread()   is equivalent to    read();
500 //-------------------------------------------------------------------------------------------------
MsFS_Read(int fd,void * buf,size_t len)501 ssize_t MsFS_Read( int fd, void *buf, size_t len )
502 {
503     return read( fd, buf, len );
504 }
505 
506 //-------------------------------------------------------------------------------------------------
507 /// Write to a file
508 /// @param  fd      \b IN:  the file descriptor associated with the file
509 /// @param  buf     \b IN:  write buffer
510 /// @param  len     \b IN:  number of bytes to be written
511 /// @return number of bytes actually written
512 /// @return -1 : errno set to indicate the error
513 /// @note   Standard C Library's counterparts: \n
514 ///         - fwrite()  is equivalent to    write();
515 //-------------------------------------------------------------------------------------------------
MsFS_Write(int fd,const void * buf,size_t len)516 ssize_t MsFS_Write( int fd, const void *buf, size_t len )
517 {
518     return write( fd, buf, len );
519 }
520 
521 //-------------------------------------------------------------------------------------------------
522 /// Close a file
523 /// @param  fd      \b IN:  the file descriptor associated with the file
524 /// @return 0: succeed
525 /// @return -1 : errno set to indicate the error
526 /// @note   Standard C Library's counterparts: \n
527 ///         - fclose()  is equivalent to    close();
528 //-------------------------------------------------------------------------------------------------
MsFS_Close(int fd)529 int MsFS_Close( int fd )
530 {
531     return close( fd );
532 }
533 
534 //-------------------------------------------------------------------------------------------------
535 /// Seek a file
536 /// @param  fd      \b IN:  the file descriptor associated with the file
537 /// @param  pos     \b IN:  file byte offset relative to whence
538 /// @param  whence  \b IN:  SEEK_SET / SEEK_CUR / SEEK_END
539 /// @return byte offset from the beginning of the file
540 /// @return -1 : errno set to indicate the error
541 /// @note   Standard C Library's counterparts: \n
542 ///         - fseek()   is equivalent to    lseek();    except the return value \n
543 ///         - rewind()  is equivalent to    lseek(fd, 0, SEEK_SET); \n
544 ///         - ftell()   is equivalent to    lseek(fd, 0, SEEK_CUR); \n
545 //-------------------------------------------------------------------------------------------------
MsFS_Lseek(int fd,MS_U64 pos,int whence)546 MS_U64 MsFS_Lseek( int fd, MS_U64 pos, int whence )
547 {
548     return lseek( fd, pos, whence );
549 }
550 
551 //-------------------------------------------------------------------------------------------------
552 /// File control
553 /// @param  fd      \b IN:  the file descriptor associated with the file
554 /// @param  cmd     \b IN:  only F_DUPFD currently
555 /// @param  ...     \b IN:  only lowest-numbered file descriptor for F_DUPFD currently
556 /// @return 0: succeed
557 /// @return -1 : errno set to indicate the error
558 /// @note   fid = dup(fd);  is equivalent to  fid = fcntl(fd, F_DUPFD, 0);
559 /// @note   fid = dup2(fd, fd2);  is equivalent to  close(fd2); fid = fcntl(fd, F_DUPFD, fd2);
560 //-------------------------------------------------------------------------------------------------
MsFS_FCntl(int fd,int cmd,...)561 int MsFS_FCntl( int fd, int cmd, ... )
562 {
563     va_list a;
564     va_start( a, cmd );
565     int fda = va_arg(a, int);
566     va_end(a);
567     return fcntl( fd, cmd, fda);
568 }
569 
570 //-------------------------------------------------------------------------------------------------
571 /// Synchronize changes to a file (all data for the open file will be transferred to the storage device)
572 /// @param  fd      \b IN:  the file descriptor associated with the file
573 /// @return 0: succeed
574 /// @return -1 : errno set to indicate the error
575 /// @note   The function will not return until the system has completed that action or until an error is detected
576 //-------------------------------------------------------------------------------------------------
MsFS_FSync(int fd)577 int MsFS_FSync( int fd )
578 {
579     return fsync( fd );
580 }
581 
MsFS_Sync(void)582 void MsFS_Sync(void)
583 {
584     sync();
585 }
586 
587 
588 //
589 // Standard C Library's counterparts
590 //
591 //-------------------------------------------------------------------------------------------------
592 /// Open a file
593 /// @param  filename \b IN: the pathname of the file
594 /// @param  mode     \b IN:  r or rb - Open file for reading.
595 ///                         w or wb - Truncate to zero length or create file for writing.
596 ///                         a or ab - Append; open or create file for writing at end-of-file.
597 ///                         r+ or rb+ or r+b - Open file for update (reading and writing).
598 ///                         w+ or wb+ or w+b - Truncate to zero length or create file for update.
599 ///                         a+ or ab+ or a+b Append; open or create file for update, writing at end-of-file.
600 /// @return the file descriptor associated with the file
601 /// @return NULL : errno set to indicate the error
602 /// @note   The maximum number of open files allowed is determined by the CYGNUM_FILEIO_NFILE
603 //-------------------------------------------------------------------------------------------------
MsFS_Fopen(const char * filename,const char * mode)604 FILE * MsFS_Fopen( const char *filename, const char *mode )
605 {
606 #if 0
607     FILE* a = fopen(filename, mode);
608     printf("[%s][%d] 0x%08x fopen %s %s\n", __FUNCTION__, __LINE__, (int)a, filename, mode);
609     return a;
610 #else
611     char u8filename[MAX_FILENAME_LENGTH] = {0};
612     if(_bUnicode)
613     {
614         _MsFS_UnicodeStrToUTF8Str((const unsigned short *)filename,(unsigned char *)u8filename,MAX_FILENAME_LENGTH);
615         return fopen(u8filename, mode );
616     }
617 
618     return fopen(filename, mode);
619 #endif
620 }
621 
622 //-------------------------------------------------------------------------------------------------
623 /// Close a file
624 /// @param  stream  \b IN:  the file descriptor associated with the file
625 /// @return 0: succeed
626 /// @return EOF(-1): errno set to indicate the error
627 //-------------------------------------------------------------------------------------------------
MsFS_Fclose(FILE * stream)628 int MsFS_Fclose( FILE *stream )
629 {
630 #if 0
631     int a = fclose(stream);
632     printf("[%s][%d] 0x%08x fclose %d\n", __FUNCTION__, __LINE__, (int)stream, a);
633     return a;
634 #else
635     return fclose(stream);
636 #endif
637 }
638 
639 //-------------------------------------------------------------------------------------------------
640 /// Read from a file
641 /// @param  data     \b OUT: read buffer
642 /// @param  itemsize \b IN:  item size in bytes
643 /// @param  nitems   \b IN:  number of items
644 /// @param  stream   \b IN:  the file descriptor associated with the file
645 /// @return number of items actually read
646 /// @return -1 : errno set to indicate the error
647 //-------------------------------------------------------------------------------------------------
MsFS_Fread(void * data,size_t itemsize,size_t nitems,FILE * stream)648 size_t MsFS_Fread( void *data, size_t itemsize, size_t nitems, FILE *stream )
649 {
650 #if 0
651     size_t a = fread(data, itemsize, nitems, stream);
652     printf("[%s][%d] 0x%08x read size %d\n", __FUNCTION__, __LINE__, (int)stream, a);
653     return a;
654 #else
655     return fread(data, itemsize, nitems, stream);
656 #endif
657 }
658 
659 //-------------------------------------------------------------------------------------------------
660 /// Write to a file
661 /// @param  data     \b IN: write buffer
662 /// @param  itemsize \b IN: item size in bytes
663 /// @param  nitems   \b IN: number of items
664 /// @param  stream   \b IN: the file descriptor associated with the file
665 /// @return number of items actually written
666 /// @return -1 : errno set to indicate the error
667 //-------------------------------------------------------------------------------------------------
MsFS_Fwrite(const void * data,size_t itemsize,size_t nitems,FILE * stream)668 size_t MsFS_Fwrite( const void *data, size_t itemsize, size_t nitems, FILE *stream )
669 {
670 #if 0
671     size_t a = fwrite(data, itemsize, nitems, stream);
672     printf("[%s][%d] 0x%08x write %d\n", __FUNCTION__, __LINE__, (int) stream, a);
673     return a;
674 #else
675     return fwrite(data, itemsize, nitems, stream);
676 #endif
677 }
678 
679 //-------------------------------------------------------------------------------------------------
680 /// Truncate a file
681 /// @param  stream   \b IN:  the file descriptor associated with the file
682 /// @param  length   \b IN:  the file size to truncate to.
683 /// @return  0: succeed
684 /// @return -1: errno set to indicate the error
MsFS_Fftruncate(FILE * stream,MS_U64 length)685 int MsFS_Fftruncate( FILE *stream, MS_U64 length)
686 {
687     int fd = fileno(stream);
688     return ftruncate(fd, (off_t)length);
689 }
690 
691 //-------------------------------------------------------------------------------------------------
692 /// Seek a file
693 /// @param  stream  \b IN:  the file descriptor associated with the file
694 /// @param  pos     \b IN:  file byte offset relative to whence
695 /// @param  whence  \b IN:  SEEK_SET / SEEK_CUR / SEEK_END
696 /// @return 0: succeed
697 /// @return -1 : errno set to indicate the error
698 //-------------------------------------------------------------------------------------------------
MsFS_Fseek(FILE * stream,MS_U64 pos,int whence)699 int MsFS_Fseek( FILE *stream, MS_U64 pos, int whence )
700 {
701 #if 0
702     int a = fseeko(stream, pos, whence);
703     printf("[%s][%d] 0x%08x write %d\n", __FUNCTION__, __LINE__, (int) stream, a);
704     return a;
705 #else
706     return fseeko(stream, pos, whence);
707 #endif
708 }
709 
710 //-------------------------------------------------------------------------------------------------
711 /// Reset the file position
712 /// @param  stream  \b IN:  the file descriptor associated with the file
713 /// @return NONE
714 //-------------------------------------------------------------------------------------------------
MsFS_Rewind(FILE * stream)715 void MsFS_Rewind( FILE *stream )
716 {
717     rewind(stream);
718 }
719 
720 //-------------------------------------------------------------------------------------------------
721 /// Obtain current file position
722 /// @param  stream  \b IN:  the file descriptor associated with the file
723 /// @return current file position measured in bytes from the beginning of the file
724 /// @return -1 : errno set to indicate the error
725 //-------------------------------------------------------------------------------------------------
MsFS_Ftell(FILE * stream)726 MS_U64 MsFS_Ftell( FILE *stream )
727 {
728 #if 0
729     MS_U64 a = ftello(stream);
730     // printf("[%s][%d] 0x%08x tell %ld\n", __FUNCTION__, __LINE__, (int) stream, a);
731     return a;
732 #else
733     return ftello(stream);
734 #endif
735 }
736 
MsFS_Fflush(FILE * stream)737 int MsFS_Fflush(FILE *stream)
738 {
739     return fflush(stream);
740 }
741 
MApi_FS_Info(const char * path,MS_BOOL bUnicode,MApi_FsInfo * pFsInfo)742 MS_BOOL MApi_FS_Info(const char *path, MS_BOOL bUnicode, MApi_FsInfo* pFsInfo)
743 {
744     struct statvfs buf;
745     char u8path[MAX_FILENAME_LENGTH] = {0};
746 
747     if (!pFsInfo)
748     {
749         return FALSE;
750     }
751 
752     if(_bUnicode)
753     {
754         _MsFS_UnicodeStrToUTF8Str((const unsigned short *)path,(unsigned char *)u8path,MAX_FILENAME_LENGTH);
755         if (0 != statvfs(u8path, &buf))
756         {
757             return FALSE;
758         }
759     }
760     else
761     {
762         if (0 != statvfs(path, &buf))
763         {
764             return FALSE;
765         }
766     }
767     pFsInfo->u32ClusTotal = buf.f_blocks;
768     pFsInfo->u32ClusFree = buf.f_bfree;
769     pFsInfo->u32ClusSize = buf.f_frsize;
770     return TRUE;
771 }
772 
773 
_MsFS_UnicodeToUTF8(int unicode,unsigned char * p)774 static unsigned char * _MsFS_UnicodeToUTF8( int unicode,unsigned char *p)
775 {
776     unsigned char *e = p;
777     if(e!=NULL)
778     {
779         if(unicode < 0x80)
780         {
781             *e = unicode;
782             e++;
783         }
784         else if(unicode < 0x800)
785         {
786 
787             *e = ((unicode >> 6) & 0x1f)|0xc0;
788             e++;
789             *e = (unicode & 0x3f)|0x80;
790             e++;
791         }
792         else if(unicode < 0x10000)
793         {
794 
795             *e = ((unicode >> 12) & 0x0f)|0xe0;
796             e++;
797             *e = ((unicode >> 6) & 0x3f)|0x80;
798             e++;
799             *e = (unicode & 0x3f)|0x80;
800             e++;
801         }
802         else if(unicode < 0x200000)
803         {
804 
805             *e = ((unicode >> 18) & 0x07)|0xf0;
806             e++;
807             *e = ((unicode >> 12) & 0x3f)|0x80;
808             e++;
809             *e = ((unicode >> 6) & 0x3f)|0x80;
810             e++;
811             *e = (unicode & 0x3f)|0x80;
812             e++;
813         }
814         else if(unicode < 0x4000000)
815         {
816 
817             *e = ((unicode >> 24) & 0x03)|0xf8 ;
818             e++;
819             *e = ((unicode >> 18) & 0x3f)|0x80;
820             e++;
821             *e = ((unicode >> 12) & 0x3f)|0x80;
822             e++;
823             *e = ((unicode >> 6) & 0x3f)|0x80;
824             e++;
825             *e = (unicode & 0x3f)|0x80;
826             e++;
827         }
828         else
829         {
830 
831             *e = ((unicode >> 30) & 0x01)|0xfc;
832             e++;
833             *e = ((unicode >> 24) & 0x3f)|0x80;
834             e++;
835             *e = ((unicode >> 18) & 0x3f)|0x80;
836             e++;
837             *e = ((unicode >> 12) & 0x3f)|0x80;
838             e++;
839             *e = ((unicode >> 6) & 0x3f)|0x80;
840             e++;
841             *e = (unicode & 0x3f)|0x80;
842             e++;
843         }
844     }
845 
846     return e;
847 }
848 
849 
_MsFS_UnicodeStrToUTF8Str(const unsigned short * unicode_str,unsigned char * utf8_str,int utf8_str_size)850 static unsigned char * _MsFS_UnicodeStrToUTF8Str (const unsigned short * unicode_str,unsigned char * utf8_str, int utf8_str_size)
851 {
852     int idx = 0;
853     int unicode = 0;
854     unsigned char *e = NULL, *s = NULL;
855     unsigned char utf8_ch[MAX_CHARACTER_SIZE]={0};
856     s = utf8_str;
857     if ((unicode_str!=NULL) && (s!=NULL))
858     {
859         for(idx=0, unicode=(int)unicode_str[idx];unicode!=0;idx=idx+1,unicode=(int)unicode_str[idx])
860         {
861             memset (utf8_ch, 0, sizeof (utf8_ch));
862             e = _MsFS_UnicodeToUTF8 (unicode, utf8_ch);
863             if (e > utf8_ch)
864             {
865                 *e = '\0';
866 
867                 /* Judge whether exceed the destination buffer */
868                 if ((int)(s - utf8_str + strlen ((const char *) utf8_ch)) >= utf8_str_size)
869                 {
870                     return s;
871                 }
872                 else
873                 {
874                     memcpy (s, utf8_ch, strlen ((const char *) utf8_ch));
875                     s += strlen ((const char *) utf8_ch);
876                     *s = '\0';
877                 }
878             }
879             else
880             {
881                 /* Converting error occurs */
882                 return s;
883             }
884         }
885     }
886 
887     return s;
888 }
889 #endif // #if defined (MSOS_TYPE_LINUX)
890