vfs.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. #include "c_stdlib.h"
  2. #include "vfs.h"
  3. #define LDRV_TRAVERSAL 0
  4. // ---------------------------------------------------------------------------
  5. // RTC system interface
  6. //
  7. static sint32_t (*rtc_cb)( vfs_time *tm ) = NULL;
  8. // called by operating system
  9. void vfs_register_rtc_cb( sint32_t (*cb)( vfs_time *tm ) )
  10. {
  11. // allow NULL pointer to unregister callback function
  12. rtc_cb = cb;
  13. }
  14. // called by file system drivers
  15. sint32_t vfs_get_rtc( vfs_time *tm )
  16. {
  17. if (rtc_cb) {
  18. return rtc_cb( tm );
  19. }
  20. return VFS_RES_ERR;
  21. }
  22. static int dir_level = 1;
  23. static const char *normalize_path( const char *path )
  24. {
  25. #if ! LDRV_TRAVERSAL
  26. return path;
  27. #else
  28. const char *temp = path;
  29. size_t len;
  30. while ((len = c_strlen( temp )) >= 2) {
  31. if (temp[0] == '.' && temp[1] == '.') {
  32. --dir_level;
  33. if (len >= 4 && dir_level > 0) {
  34. // prepare next step
  35. temp = &(temp[4]);
  36. } else {
  37. // we're at top, the remainder is expected be an absolute path
  38. temp = &(temp[3]);
  39. }
  40. } else {
  41. break;
  42. }
  43. }
  44. if (dir_level > 0) {
  45. // no traversal on root level
  46. return path;
  47. } else {
  48. // path traverses via root
  49. return temp;
  50. }
  51. #endif
  52. }
  53. // ---------------------------------------------------------------------------
  54. // file system functions
  55. //
  56. vfs_vol *vfs_mount( const char *name, int num )
  57. {
  58. vfs_fs_fns *fs_fns;
  59. const char *normname = normalize_path( name );
  60. char *outname;
  61. #ifdef BUILD_SPIFFS
  62. if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
  63. return fs_fns->mount( outname, num );
  64. }
  65. #endif
  66. #ifdef BUILD_FATFS
  67. if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
  68. vfs_vol *r = fs_fns->mount( outname, num );
  69. c_free( outname );
  70. return r;
  71. }
  72. #endif
  73. return NULL;
  74. }
  75. int vfs_open( const char *name, const char *mode )
  76. {
  77. vfs_fs_fns *fs_fns;
  78. const char *normname = normalize_path( name );
  79. char *outname;
  80. #ifdef BUILD_SPIFFS
  81. if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
  82. return (int)fs_fns->open( outname, mode );
  83. }
  84. #endif
  85. #ifdef BUILD_FATFS
  86. if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
  87. int r = (int)fs_fns->open( outname, mode );
  88. c_free( outname );
  89. return r;
  90. }
  91. #endif
  92. return 0;
  93. }
  94. vfs_dir *vfs_opendir( const char *name )
  95. {
  96. vfs_fs_fns *fs_fns;
  97. const char *normname = normalize_path( name );
  98. char *outname;
  99. #ifdef BUILD_SPIFFS
  100. if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
  101. return fs_fns->opendir( outname );
  102. }
  103. #endif
  104. #ifdef BUILD_FATFS
  105. if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
  106. vfs_dir *r = fs_fns->opendir( outname );
  107. c_free( outname );
  108. return r;
  109. }
  110. #endif
  111. return NULL;
  112. }
  113. vfs_item *vfs_stat( const char *name )
  114. {
  115. vfs_fs_fns *fs_fns;
  116. const char *normname = normalize_path( name );
  117. char *outname;
  118. #ifdef BUILD_SPIFFS
  119. if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
  120. return fs_fns->stat( outname );
  121. }
  122. #endif
  123. #ifdef BUILD_FATFS
  124. if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
  125. vfs_item *r = fs_fns->stat( outname );
  126. c_free( outname );
  127. return r;
  128. }
  129. #endif
  130. return NULL;
  131. }
  132. sint32_t vfs_remove( const char *name )
  133. {
  134. vfs_fs_fns *fs_fns;
  135. const char *normname = normalize_path( name );
  136. char *outname;
  137. #ifdef BUILD_SPIFFS
  138. if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
  139. return fs_fns->remove( outname );
  140. }
  141. #endif
  142. #ifdef BUILD_FATFS
  143. if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
  144. sint32_t r = fs_fns->remove( outname );
  145. c_free( outname );
  146. return r;
  147. }
  148. #endif
  149. return VFS_RES_ERR;
  150. }
  151. sint32_t vfs_rename( const char *oldname, const char *newname )
  152. {
  153. vfs_fs_fns *fs_fns;
  154. const char *normoldname = normalize_path( oldname );
  155. const char *normnewname = normalize_path( newname );
  156. char *oldoutname, *newoutname;
  157. #ifdef BUILD_SPIFFS
  158. if (myspiffs_realm( normoldname, &oldoutname, FALSE )) {
  159. if (fs_fns = myspiffs_realm( normnewname, &newoutname, FALSE )) {
  160. return fs_fns->rename( oldoutname, newoutname );
  161. }
  162. }
  163. #endif
  164. #ifdef BUILD_FATFS
  165. if (myfatfs_realm( normoldname, &oldoutname, FALSE )) {
  166. if (fs_fns = myfatfs_realm( normnewname, &newoutname, FALSE )) {
  167. sint32_t r = fs_fns->rename( oldoutname, newoutname );
  168. c_free( oldoutname );
  169. c_free( newoutname );
  170. return r;
  171. }
  172. c_free( oldoutname );
  173. }
  174. #endif
  175. return -1;
  176. }
  177. sint32_t vfs_mkdir( const char *name )
  178. {
  179. vfs_fs_fns *fs_fns;
  180. const char *normname = normalize_path( name );
  181. char *outname;
  182. #ifdef BUILD_SPIFFS
  183. // not supported
  184. #endif
  185. #ifdef BUILD_FATFS
  186. if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
  187. sint32_t r = fs_fns->mkdir( outname );
  188. c_free( outname );
  189. return r;
  190. }
  191. #endif
  192. return VFS_RES_ERR;
  193. }
  194. sint32_t vfs_fsinfo( const char *name, uint32_t *total, uint32_t *used )
  195. {
  196. vfs_fs_fns *fs_fns;
  197. char *outname;
  198. if (!name) name = ""; // current drive
  199. const char *normname = normalize_path( name );
  200. #ifdef BUILD_SPIFFS
  201. if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
  202. return fs_fns->fsinfo( total, used );
  203. }
  204. #endif
  205. #ifdef BUILD_FATFS
  206. if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
  207. c_free( outname );
  208. return fs_fns->fsinfo( total, used );
  209. }
  210. #endif
  211. return VFS_RES_ERR;
  212. }
  213. sint32_t vfs_fscfg( const char *name, uint32_t *phys_addr, uint32_t *phys_size)
  214. {
  215. vfs_fs_fns *fs_fns;
  216. char *outname;
  217. #ifdef BUILD_SPIFFS
  218. if (fs_fns = myspiffs_realm( "/FLASH", &outname, FALSE )) {
  219. return fs_fns->fscfg( phys_addr, phys_size );
  220. }
  221. #endif
  222. #ifdef BUILD_FATFS
  223. // not supported
  224. #endif
  225. // Error
  226. return VFS_RES_ERR;
  227. }
  228. sint32_t vfs_format( void )
  229. {
  230. vfs_fs_fns *fs_fns;
  231. char *outname;
  232. #ifdef BUILD_SPIFFS
  233. if (fs_fns = myspiffs_realm( "/FLASH", &outname, FALSE )) {
  234. return fs_fns->format();
  235. }
  236. #endif
  237. #ifdef BUILD_FATFS
  238. // not supported
  239. #endif
  240. // Error
  241. return 0;
  242. }
  243. sint32_t vfs_chdir( const char *path )
  244. {
  245. vfs_fs_fns *fs_fns;
  246. const char *normpath = normalize_path( path );
  247. const char *level;
  248. char *outname;
  249. int ok = VFS_RES_ERR;
  250. #if LDRV_TRAVERSAL
  251. // track dir level
  252. if (normpath[0] == '/') {
  253. dir_level = 0;
  254. level = &(normpath[1]);
  255. } else {
  256. level = normpath;
  257. }
  258. while (c_strlen( level ) > 0) {
  259. dir_level++;
  260. if (level = c_strchr( level, '/' )) {
  261. level++;
  262. } else {
  263. break;
  264. }
  265. }
  266. #endif
  267. #ifdef BUILD_SPIFFS
  268. if (fs_fns = myspiffs_realm( normpath, &outname, TRUE )) {
  269. // our SPIFFS integration doesn't support directories
  270. if (c_strlen( outname ) == 0) {
  271. ok = VFS_RES_OK;
  272. }
  273. }
  274. #endif
  275. #ifdef BUILD_FATFS
  276. if (fs_fns = myfatfs_realm( normpath, &outname, TRUE )) {
  277. if (c_strchr( outname, ':' )) {
  278. // need to set FatFS' default drive
  279. fs_fns->chdrive( outname );
  280. // and force chdir to root in case path points only to root
  281. fs_fns->chdir( "/" );
  282. }
  283. if (fs_fns->chdir( outname ) == VFS_RES_OK) {
  284. ok = VFS_RES_OK;
  285. }
  286. c_free( outname );
  287. }
  288. #endif
  289. return ok == VFS_RES_OK ? VFS_RES_OK : VFS_RES_ERR;
  290. }
  291. sint32_t vfs_errno( const char *name )
  292. {
  293. vfs_fs_fns *fs_fns;
  294. const char *normname = normalize_path( name );
  295. char *outname;
  296. if (!name) name = ""; // current drive
  297. #ifdef BUILD_SPIFFS
  298. if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
  299. return fs_fns->ferrno( );
  300. }
  301. #endif
  302. #ifdef BUILD_FATFS
  303. if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
  304. sint32_t r = fs_fns->ferrno( );
  305. c_free( outname );
  306. return r;
  307. }
  308. #endif
  309. return VFS_RES_ERR;
  310. }
  311. sint32_t vfs_ferrno( int fd )
  312. {
  313. vfs_file *f = (vfs_file *)fd;
  314. if (f) {
  315. return f->fns->ferrno ? f->fns->ferrno( f ) : 0;
  316. } else {
  317. vfs_fs_fns *fs_fns;
  318. const char *name = ""; // current drive
  319. char *outname;
  320. #ifdef BUILD_SPIFFS
  321. if (fs_fns = myspiffs_realm( name, &outname, FALSE )) {
  322. return fs_fns->ferrno( );
  323. }
  324. #endif
  325. #ifdef BUILD_FATFS
  326. if (fs_fns = myfatfs_realm( name, &outname, FALSE )) {
  327. sint32_t r = fs_fns->ferrno( );
  328. c_free( outname );
  329. return r;
  330. }
  331. #endif
  332. }
  333. }
  334. void vfs_clearerr( const char *name )
  335. {
  336. vfs_fs_fns *fs_fns;
  337. char *outname;
  338. if (!name) name = ""; // current drive
  339. const char *normname = normalize_path( name );
  340. #ifdef BUILD_SPIFFS
  341. if (fs_fns = myspiffs_realm( normname, &outname, FALSE )) {
  342. fs_fns->clearerr( );
  343. }
  344. #endif
  345. #ifdef BUILD_FATFS
  346. if (fs_fns = myfatfs_realm( normname, &outname, FALSE )) {
  347. fs_fns->clearerr( );
  348. c_free( outname );
  349. }
  350. #endif
  351. }
  352. const char *vfs_basename( const char *path )
  353. {
  354. const char *basename;
  355. // deduce basename (incl. extension) for length check
  356. if (basename = c_strrchr( path, '/' )) {
  357. basename++;
  358. } else if (basename = c_strrchr( path, ':' )) {
  359. basename++;
  360. } else {
  361. basename = path;
  362. }
  363. return basename;
  364. }
  365. // ---------------------------------------------------------------------------
  366. // supplementary functions
  367. //
  368. int vfs_getc( int fd )
  369. {
  370. unsigned char c = 0xFF;
  371. sint32_t res;
  372. if(!vfs_eof( fd )) {
  373. if (1 != vfs_read( fd, &c, 1 )) {
  374. NODE_DBG("getc errno %i\n", vfs_ferrno( fd ));
  375. return VFS_EOF;
  376. } else {
  377. return (int)c;
  378. }
  379. }
  380. return VFS_EOF;
  381. }
  382. int vfs_ungetc( int c, int fd )
  383. {
  384. return vfs_lseek( fd, -1, VFS_SEEK_CUR );
  385. }