ff_stdio.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * FreeRTOS+FAT V2.3.3
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * https://www.FreeRTOS.org
  23. * https://github.com/FreeRTOS
  24. *
  25. */
  26. /*
  27. * ff_stdio.h
  28. *
  29. * An front-end which make +FAT look like the well-known stdio-functions
  30. */
  31. #ifndef FF_STDIO_H
  32. #define FF_STDIO_H
  33. #if defined( __WIN32__ )
  34. #include <dir.h>
  35. #endif
  36. /* Standard includes. */
  37. #include <stdio.h>
  38. #include <stdarg.h>
  39. /* FreeRTOS+FAT includes. */
  40. #include "ff_headers.h"
  41. #include "ff_sys.h"
  42. #if ( ffconfigDEV_SUPPORT != 0 )
  43. #include "ff_devices.h"
  44. #endif
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /* Error return from some functions. */
  49. #define FF_EOF ( -1 )
  50. /* Bits used in the FF_Stat_t structure. */
  51. #define FF_IFDIR 0040000u /* directory */
  52. #define FF_IFCHR 0020000u /* character special */
  53. #define FF_IFBLK 0060000u /* block special */
  54. #define FF_IFREG 0100000u /* regular */
  55. /* Bits used in the FF_FindData_t structure. */
  56. #define FF_FA_NORMAL 0x00
  57. #define FF_FA_RDONLY 0x01
  58. #define FF_FA_HIDDEN 0x02
  59. #define FF_FA_SYSTEM 0x04
  60. #define FF_FA_LABEL 0x08
  61. #define FF_FA_DIREC 0x10
  62. #define FF_FA_ARCH 0x20
  63. /* FreeRTOS+FAT uses three thread local buffers. The first stores errno, the
  64. * second a pointer to the CWD structure (if one is used), and the third the more
  65. * descriptive error code. */
  66. #define stdioERRNO_THREAD_LOCAL_OFFSET ( ffconfigCWD_THREAD_LOCAL_INDEX + 0 )
  67. #define stdioCWD_THREAD_LOCAL_OFFSET ( ffconfigCWD_THREAD_LOCAL_INDEX + 1 )
  68. #define stdioFF_ERROR_THREAD_LOCAL_OFFSET ( ffconfigCWD_THREAD_LOCAL_INDEX + 2 )
  69. /* Structure used with ff_stat(). */
  70. typedef struct FF_STAT
  71. {
  72. uint32_t st_ino; /* First data cluster number. */
  73. uint32_t st_size; /* Size of the object in number of bytes. */
  74. uint16_t st_dev; /* The device on which the file can be found (see ff_sys.c) */
  75. uint16_t st_mode; /* The mode (attribute bits) of this file or directory. */
  76. #if ( ffconfigTIME_SUPPORT == 1 )
  77. uint32_t st_atime;
  78. uint32_t st_mtime;
  79. uint32_t st_ctime;
  80. #endif /* ffconfigTIME_SUPPORT */
  81. } FF_Stat_t;
  82. /* Structure used with ff_findfirst(), ff_findnext(), etc. */
  83. typedef struct
  84. {
  85. /* private */
  86. UBaseType_t
  87. #if ( ffconfigDEV_SUPPORT != 0 )
  88. bIsDeviceDir : 1,
  89. #endif
  90. bEntryPOwner : 1;
  91. struct FF_DIR_HANDLER xDirectoryHandler;
  92. FF_DirEnt_t xDirectoryEntry;
  93. /* Public fields included so FF_DirEnt_t does not need to be public. */
  94. const char * pcFileName;
  95. uint32_t ulFileSize;
  96. uint8_t ucAttributes;
  97. } FF_FindData_t;
  98. /*-----------------------------------------------------------
  99. * Get and set the task's file system errno
  100. * The most up to date API documentation is currently provided on the following URL:
  101. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  102. *-----------------------------------------------------------*/
  103. /*
  104. * int FF_GetErrno( void );
  105. * void FF_SetErrno( int ff_errno );
  106. *
  107. * _RB_ comments are incorrect and index should use the stdioERRNO_THREAD_LOCAL_OFFSET offset.
  108. */
  109. /* The errno is stored in a thread local buffer. */
  110. static portINLINE void stdioSET_ERRNO( int iErrno )
  111. {
  112. vTaskSetThreadLocalStoragePointer( NULL, ffconfigCWD_THREAD_LOCAL_INDEX, ( void * ) ( iErrno ) );
  113. }
  114. static portINLINE int stdioGET_ERRNO( void )
  115. {
  116. void * pvResult;
  117. pvResult = pvTaskGetThreadLocalStoragePointer( ( TaskHandle_t ) NULL, ffconfigCWD_THREAD_LOCAL_INDEX );
  118. return ( int ) pvResult;
  119. }
  120. #if ( ( configNUM_THREAD_LOCAL_STORAGE_POINTERS - ffconfigCWD_THREAD_LOCAL_INDEX ) < 3 )
  121. #error Please define space for 3 entries
  122. #endif
  123. /*
  124. * Store the FreeRTOS+FAT error code, which provides more detail than errno.
  125. */
  126. static portINLINE void stdioSET_FF_ERROR( FF_Error_t iFF_ERROR )
  127. {
  128. vTaskSetThreadLocalStoragePointer( NULL, stdioFF_ERROR_THREAD_LOCAL_OFFSET, ( void * ) ( iFF_ERROR ) );
  129. }
  130. /*
  131. * Read back the FreeRTOS+FAT error code, which provides more detail than
  132. * errno.
  133. */
  134. static portINLINE FF_Error_t stdioGET_FF_ERROR( void )
  135. {
  136. void * pvResult;
  137. pvResult = pvTaskGetThreadLocalStoragePointer( NULL, stdioFF_ERROR_THREAD_LOCAL_OFFSET );
  138. return ( FF_Error_t ) pvResult;
  139. }
  140. /*-----------------------------------------------------------
  141. * Open and close a file
  142. * The most up to date API documentation is currently provided on the following URL:
  143. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  144. *-----------------------------------------------------------*/
  145. FF_FILE * ff_fopen( const char * pcFile,
  146. const char * pcMode );
  147. int ff_fclose( FF_FILE * pxStream );
  148. /*-----------------------------------------------------------
  149. * Seek and tell
  150. * The most up to date API documentation is currently provided on the following URL:
  151. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  152. *-----------------------------------------------------------*/
  153. int ff_fseek( FF_FILE * pxStream,
  154. long lOffset,
  155. int iWhence );
  156. void ff_rewind( FF_FILE * pxStream );
  157. long ff_ftell( FF_FILE * pxStream );
  158. int ff_feof( FF_FILE * pxStream );
  159. /*-----------------------------------------------------------
  160. * Read and write
  161. * The most up to date API documentation is currently provided on the following URL:
  162. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  163. *-----------------------------------------------------------*/
  164. size_t ff_fread( void * pvBuffer,
  165. size_t xSize,
  166. size_t xItems,
  167. FF_FILE * pxStream );
  168. size_t ff_fwrite( const void * pvBuffer,
  169. size_t xSize,
  170. size_t xItems,
  171. FF_FILE * pxStream );
  172. /* Whenever possible, use ellipsis parameter type checking.
  173. * _RB_ Compiler specifics need to be moved to the compiler specific header files. */
  174. #if defined( __GNUC__ )
  175. /* The GNU-C compiler will check if the parameters are correct. */
  176. int ff_fprintf( FF_FILE * pxStream,
  177. const char * pcFormat,
  178. ... )
  179. __attribute__( ( format( __printf__, 2, 3 ) ) );
  180. #else
  181. int ff_fprintf( FF_FILE * pxStream,
  182. const char * pcFormat,
  183. ... );
  184. #endif
  185. int ff_fgetc( FF_FILE * pxStream );
  186. int ff_fputc( int iChar,
  187. FF_FILE * pxStream );
  188. char * ff_fgets( char * pcBuffer,
  189. size_t xCount,
  190. FF_FILE * pxStream );
  191. /*-----------------------------------------------------------
  192. * Change length of file (truncate)
  193. * File should have been opened in "w" or "a" mode
  194. * The actual length of the file will be made equal to the current writing
  195. * position
  196. * The most up to date API documentation is currently provided on the following URL:
  197. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  198. *-----------------------------------------------------------*/
  199. int ff_seteof( FF_FILE * pxStream );
  200. /*-----------------------------------------------------------
  201. * Open a file in append/update mode, truncate its length to a given value,
  202. * or write zero's up until the required length, and return a handle to the open
  203. * file. If NULL is returned, ff_errno contains an error code.
  204. * The most up to date API documentation is currently provided on the following URL:
  205. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  206. *-----------------------------------------------------------*/
  207. FF_FILE * ff_truncate( const char * pcFileName,
  208. long lTruncateSize );
  209. /*-----------------------------------------------------------
  210. * Flush to disk
  211. * The most up to date API documentation is currently provided on the following URL:
  212. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  213. *-----------------------------------------------------------*/
  214. int ff_fflush( FF_FILE * pxStream );
  215. /*-----------------------------------------------------------
  216. * Create directory, remove and rename files
  217. * The most up to date API documentation is currently provided on the following URL:
  218. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  219. *-----------------------------------------------------------*/
  220. #if ( ffconfigMKDIR_RECURSIVE == 0 )
  221. int ff_mkdir( const char * pcPath );
  222. #else
  223. /* If the parameter bRecursive is non-zero, the entire path will be checked
  224. * and if necessary, created. */
  225. int ff_mkdir( const char * pcPath,
  226. int bRecursive );
  227. #endif
  228. /*-----------------------------------------------------------
  229. * Create path specified by the pcPath parameter.
  230. * The most up to date API documentation is currently provided on the following URL:
  231. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  232. *-----------------------------------------------------------*/
  233. int ff_mkpath( const char * pcPath );
  234. /*-----------------------------------------------------------
  235. * Remove the directory specified by the pcDirectory parameter.
  236. * The most up to date API documentation is currently provided on the following URL:
  237. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  238. *-----------------------------------------------------------*/
  239. int ff_rmdir( const char * pcDirectory );
  240. /*-----------------------------------------------------------
  241. * Delete a directory and, recursively, all of its contents.
  242. * The most up to date API documentation is currently provided on the following URL:
  243. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  244. *-----------------------------------------------------------*/
  245. #if ( ffconfigUSE_DELTREE != 0 )
  246. /* By default, this function will not be compiled. The function will
  247. * recursively call itself, which is against the FreeRTOS coding standards, so
  248. * IT MUST BE USED WITH CARE.
  249. *
  250. * The cost of each recursion will be roughly:
  251. * Stack : 48 (12 stack words)
  252. * Heap : 112 + ffconfigMAX_FILENAME
  253. * These numbers may change depending on CPU and compiler. */
  254. int ff_deltree( const char * pcPath );
  255. #endif
  256. /*-----------------------------------------------------------
  257. * Remove/delete a file.
  258. * The most up to date API documentation is currently provided on the following URL:
  259. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  260. *-----------------------------------------------------------*/
  261. int ff_remove( const char * pcPath );
  262. /*-----------------------------------------------------------
  263. * Move a file, also cross-directory but not across a file system.
  264. * The most up to date API documentation is currently provided on the following URL:
  265. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  266. *-----------------------------------------------------------*/
  267. int ff_rename( const char * pcOldName,
  268. const char * pcNewName,
  269. int bDeleteIfExists );
  270. /*-----------------------------------------------------------
  271. * Get the status of a file.
  272. * The most up to date API documentation is currently provided on the following URL:
  273. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  274. *-----------------------------------------------------------*/
  275. int ff_stat( const char * pcFileName,
  276. FF_Stat_t * pxStatBuffer );
  277. /* _HT_ Keep this for a while, until the new ff_stat() is wel tested */
  278. int ff_old_stat( const char * pcName,
  279. FF_Stat_t * pxStatBuffer );
  280. /*-----------------------------------------------------------
  281. * Get the length of a file in bytes.
  282. * The most up to date API documentation is currently provided on the following URL:
  283. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  284. *-----------------------------------------------------------*/
  285. size_t ff_filelength( FF_FILE * pxFile );
  286. /*-----------------------------------------------------------
  287. * Working directory and iterating through directories.
  288. * The most up to date API documentation is currently provided on the following URL:
  289. * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_FAT/Standard_File_System_API.html
  290. *-----------------------------------------------------------*/
  291. #if ffconfigHAS_CWD
  292. int ff_chdir( const char * pcDirectoryName );
  293. char * ff_getcwd( char * pcBuffer,
  294. size_t xBufferLength );
  295. #endif
  296. int ff_findfirst( const char * pcDirectory,
  297. FF_FindData_t * pxFindData );
  298. int ff_findnext( FF_FindData_t * pxFindData );
  299. int ff_isdirempty( const char * pcPath );
  300. /* _RB_ What to do regarding documentation for the definitions below here. */
  301. #if ( ffconfig64_NUM_SUPPORT != 0 )
  302. int64_t ff_diskfree( const char * pcPath,
  303. uint32_t * pxSectorCount );
  304. #else
  305. int32_t ff_diskfree( const char * pcPath,
  306. uint32_t * pxSectorCount );
  307. #endif
  308. int ff_finddir( const char * pcPath );
  309. #if ( ffconfigHAS_CWD == 1 )
  310. /* Obtain the CWD used by the current task. */
  311. void ff_free_CWD_space( void );
  312. #endif
  313. typedef enum _EFileAction
  314. {
  315. eFileCreate,
  316. eFileRemove,
  317. eFileChange,
  318. eFileIsDir = 0x80,
  319. } eFileAction_t;
  320. void callFileEvents( const char * apPath,
  321. eFileAction_t aAction );
  322. #ifdef __cplusplus
  323. } /* extern "C" */
  324. #endif
  325. #endif /* FF_STDIO_H */