ff_sys.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #include <stdio.h>
  27. #include <string.h>
  28. /* FreeRTOS includes. */
  29. #include "FreeRTOS.h"
  30. #include "task.h"
  31. #include "portable.h"
  32. #include "ff_headers.h"
  33. #include "ff_sys.h"
  34. #ifndef ARRAY_SIZE
  35. #define ARRAY_SIZE( x ) ( int ) ( sizeof( x ) / sizeof( x )[ 0 ] )
  36. #endif
  37. /*
  38. * Define a collection of 'file systems' as a simple array
  39. */
  40. typedef struct xSYSTEM
  41. {
  42. FF_SubSystem_t xSystems[ ffconfigMAX_FILE_SYS ];
  43. volatile BaseType_t xFileSystemCount;
  44. } ff_sys_t;
  45. static ff_sys_t file_systems;
  46. static const char rootDir[] = "/";
  47. int FF_FS_Count( void )
  48. {
  49. return ( int ) file_systems.xFileSystemCount;
  50. }
  51. /*-----------------------------------------------------------*/
  52. void FF_FS_Init( void )
  53. {
  54. memset( &file_systems, '\0', sizeof( file_systems ) );
  55. /* There is always a root file system, even if it doesn't have a
  56. * IO manager. */
  57. file_systems.xFileSystemCount = ( BaseType_t ) 1;
  58. /* Set to "/", second byte is already zero. */
  59. file_systems.xSystems[ 0 ].pcPath[ 0 ] = ( char ) '/';
  60. file_systems.xSystems[ 0 ].xPathlen = 1;
  61. }
  62. /*-----------------------------------------------------------*/
  63. int FF_FS_Add( const char * pcPath,
  64. FF_Disk_t * pxDisk )
  65. {
  66. int iReturn = pdFALSE;
  67. configASSERT( pxDisk );
  68. if( *pcPath != ( char ) '/' )
  69. {
  70. FF_PRINTF( "FF_FS_Add: Need a \"/\": '%s'\n", pcPath );
  71. }
  72. else
  73. {
  74. BaseType_t xUseIndex = -1;
  75. size_t uxPathLength = strlen( pcPath );
  76. vTaskSuspendAll();
  77. {
  78. if( file_systems.xFileSystemCount == ( BaseType_t ) 0 )
  79. {
  80. FF_FS_Init();
  81. }
  82. if( uxPathLength == ( size_t ) 1u )
  83. {
  84. /* This is the "/" path
  85. * and will always be put at index 0 */
  86. xUseIndex = ( BaseType_t ) 0;
  87. }
  88. else
  89. {
  90. BaseType_t xIndex, xFreeIndex = -1;
  91. FF_SubSystem_t * pxSubSystem = file_systems.xSystems + 1; /* Skip the root entry */
  92. for( xIndex = ( BaseType_t ) 1; xIndex < file_systems.xFileSystemCount; xIndex++, pxSubSystem++ )
  93. {
  94. if( ( pxSubSystem->xPathlen == ( BaseType_t ) uxPathLength ) &&
  95. ( memcmp( pxSubSystem->pcPath, pcPath, uxPathLength ) == 0 ) )
  96. {
  97. /* A system is updated with a new handler. */
  98. xUseIndex = xIndex;
  99. break;
  100. }
  101. if( ( pxSubSystem->pxManager == NULL ) && ( xFreeIndex < 0 ) )
  102. {
  103. /* Remember the first free slot. */
  104. xFreeIndex = xIndex;
  105. }
  106. }
  107. if( xUseIndex < ( BaseType_t ) 0 )
  108. {
  109. if( xFreeIndex >= ( BaseType_t ) 0 )
  110. {
  111. /* Use the first free slot. */
  112. xUseIndex = xFreeIndex;
  113. }
  114. else if( file_systems.xFileSystemCount < ARRAY_SIZE( file_systems.xSystems ) )
  115. {
  116. /* Fill a new entry. */
  117. xUseIndex = file_systems.xFileSystemCount++;
  118. }
  119. }
  120. } /* uxPathLength != 1 */
  121. if( xUseIndex >= ( BaseType_t ) 0 )
  122. {
  123. iReturn = pdTRUE;
  124. strncpy( file_systems.xSystems[ xUseIndex ].pcPath, pcPath, sizeof( file_systems.xSystems[ xUseIndex ].pcPath ) );
  125. file_systems.xSystems[ xUseIndex ].xPathlen = uxPathLength;
  126. file_systems.xSystems[ xUseIndex ].pxManager = pxDisk->pxIOManager;
  127. }
  128. }
  129. xTaskResumeAll();
  130. if( iReturn == pdFALSE )
  131. {
  132. FF_PRINTF( "FF_FS_Add: Table full '%s' (max = %d)\n", pcPath, ( int ) ARRAY_SIZE( file_systems.xSystems ) );
  133. }
  134. }
  135. return iReturn;
  136. }
  137. /*-----------------------------------------------------------*/
  138. void FF_FS_Remove( const char * pcPath )
  139. {
  140. BaseType_t xUseIndex, xIndex;
  141. size_t uxPathLength;
  142. if( pcPath[ 0 ] == ( char ) '/' )
  143. {
  144. xUseIndex = -1;
  145. uxPathLength = strlen( pcPath );
  146. /* Is it the "/" path ? */
  147. if( uxPathLength == ( size_t ) 1u )
  148. {
  149. xUseIndex = 0;
  150. }
  151. else
  152. {
  153. FF_SubSystem_t * pxSubSystem = file_systems.xSystems + 1;
  154. for( xIndex = 1; xIndex < file_systems.xFileSystemCount; xIndex++, pxSubSystem++ )
  155. {
  156. if( ( pxSubSystem->xPathlen == ( BaseType_t ) uxPathLength ) &&
  157. ( memcmp( pxSubSystem->pcPath, pcPath, uxPathLength ) == 0 ) )
  158. {
  159. xUseIndex = xIndex;
  160. break;
  161. }
  162. }
  163. }
  164. if( xUseIndex >= 0 )
  165. {
  166. vTaskSuspendAll();
  167. {
  168. file_systems.xSystems[ xUseIndex ].pxManager = NULL;
  169. file_systems.xSystems[ xUseIndex ].xPathlen = ( BaseType_t ) 0;
  170. for( xIndex = file_systems.xFileSystemCount - 1; xIndex > 0; xIndex-- )
  171. {
  172. if( file_systems.xSystems[ xIndex ].pxManager != NULL )
  173. {
  174. /* The slot at 'xIndex' is still in use. */
  175. break;
  176. }
  177. }
  178. file_systems.xFileSystemCount = xIndex + 1;
  179. }
  180. xTaskResumeAll();
  181. }
  182. }
  183. }
  184. /*-----------------------------------------------------------*/
  185. int FF_FS_Find( const char * pcPath,
  186. FF_DirHandler_t * pxHandler )
  187. {
  188. FF_SubSystem_t * pxSubSystem;
  189. size_t uxPathLength;
  190. BaseType_t xUseIndex;
  191. int iReturn;
  192. pxSubSystem = file_systems.xSystems + 1;
  193. uxPathLength = strlen( pcPath );
  194. memset( pxHandler, '\0', sizeof( *pxHandler ) );
  195. for( xUseIndex = 1; xUseIndex < file_systems.xFileSystemCount; xUseIndex++, pxSubSystem++ )
  196. {
  197. if( ( uxPathLength >= ( size_t ) pxSubSystem->xPathlen ) &&
  198. ( memcmp( pxSubSystem->pcPath, pcPath, ( size_t ) pxSubSystem->xPathlen ) == 0 ) &&
  199. ( ( pcPath[ pxSubSystem->xPathlen ] == '\0' ) || ( pcPath[ pxSubSystem->xPathlen ] == '/' ) ) ) /* System "/ram" should not match with "/ramc/etc". */
  200. {
  201. if( pcPath[ pxSubSystem->xPathlen ] == '\0' )
  202. {
  203. pxHandler->pcPath = rootDir;
  204. }
  205. else
  206. {
  207. pxHandler->pcPath = pcPath + pxSubSystem->xPathlen;
  208. }
  209. pxHandler->pxManager = pxSubSystem->pxManager;
  210. break;
  211. }
  212. }
  213. if( xUseIndex == file_systems.xFileSystemCount )
  214. {
  215. pxHandler->pcPath = pcPath;
  216. pxHandler->pxManager = file_systems.xSystems[ 0 ].pxManager;
  217. }
  218. if( FF_Mounted( pxHandler->pxManager ) )
  219. {
  220. iReturn = pdTRUE;
  221. }
  222. else
  223. {
  224. iReturn = pdFALSE;
  225. }
  226. return iReturn;
  227. }
  228. /*-----------------------------------------------------------*/
  229. int FF_FS_Get( int xIndex,
  230. FF_SubSystem_t * pxSystem )
  231. {
  232. int iReturn;
  233. /* Get a copy of a fs info. */
  234. if( ( xIndex < 0 ) || ( xIndex >= file_systems.xFileSystemCount ) )
  235. {
  236. iReturn = pdFALSE;
  237. }
  238. else
  239. {
  240. /* Note: it will copy the contents of 'FF_SubSystem_t'. */
  241. *pxSystem = file_systems.xSystems[ xIndex ];
  242. iReturn = pdTRUE;
  243. }
  244. return iReturn;
  245. }
  246. /*-----------------------------------------------------------*/