storage_processing.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. #include "storage_processing.h"
  2. #include <m-list.h>
  3. #include <m-dict.h>
  4. #define STORAGE_PATH_PREFIX_LEN 4u
  5. _Static_assert(
  6. sizeof(STORAGE_ANY_PATH_PREFIX) == STORAGE_PATH_PREFIX_LEN + 1,
  7. "Any path prefix len mismatch");
  8. _Static_assert(
  9. sizeof(STORAGE_EXT_PATH_PREFIX) == STORAGE_PATH_PREFIX_LEN + 1,
  10. "Ext path prefix len mismatch");
  11. _Static_assert(
  12. sizeof(STORAGE_INT_PATH_PREFIX) == STORAGE_PATH_PREFIX_LEN + 1,
  13. "Int path prefix len mismatch");
  14. #define FS_CALL(_storage, _fn) ret = _storage->fs_api->_fn;
  15. static bool storage_type_is_valid(StorageType type) {
  16. #ifdef FURI_RAM_EXEC
  17. return type == ST_EXT;
  18. #else
  19. return type < ST_ERROR;
  20. #endif
  21. }
  22. static StorageData* get_storage_by_file(File* file, StorageData* storages) {
  23. StorageData* storage_data = NULL;
  24. for(uint8_t i = 0; i < STORAGE_COUNT; i++) {
  25. if(storage_has_file(file, &storages[i])) {
  26. storage_data = &storages[i];
  27. }
  28. }
  29. return storage_data;
  30. }
  31. static const char* cstr_path_without_vfs_prefix(FuriString* path) {
  32. const char* path_cstr = furi_string_get_cstr(path);
  33. return path_cstr + MIN(STORAGE_PATH_PREFIX_LEN, strlen(path_cstr));
  34. }
  35. static StorageType storage_get_type_by_path(FuriString* path) {
  36. StorageType type = ST_ERROR;
  37. const char* path_cstr = furi_string_get_cstr(path);
  38. if(furi_string_size(path) > STORAGE_PATH_PREFIX_LEN) {
  39. if(path_cstr[STORAGE_PATH_PREFIX_LEN] != '/') {
  40. return ST_ERROR;
  41. }
  42. }
  43. if(memcmp(path_cstr, STORAGE_EXT_PATH_PREFIX, strlen(STORAGE_EXT_PATH_PREFIX)) == 0) {
  44. type = ST_EXT;
  45. } else if(memcmp(path_cstr, STORAGE_INT_PATH_PREFIX, strlen(STORAGE_INT_PATH_PREFIX)) == 0) {
  46. type = ST_INT;
  47. } else if(memcmp(path_cstr, STORAGE_ANY_PATH_PREFIX, strlen(STORAGE_ANY_PATH_PREFIX)) == 0) {
  48. type = ST_ANY;
  49. }
  50. return type;
  51. }
  52. static void storage_path_change_to_real_storage(FuriString* path, StorageType real_storage) {
  53. if(furi_string_search(path, STORAGE_ANY_PATH_PREFIX) == 0) {
  54. switch(real_storage) {
  55. case ST_EXT:
  56. furi_string_replace_at(
  57. path, 0, strlen(STORAGE_EXT_PATH_PREFIX), STORAGE_EXT_PATH_PREFIX);
  58. break;
  59. case ST_INT:
  60. furi_string_replace_at(
  61. path, 0, strlen(STORAGE_INT_PATH_PREFIX), STORAGE_INT_PATH_PREFIX);
  62. break;
  63. default:
  64. break;
  65. }
  66. }
  67. }
  68. FS_Error storage_get_data(Storage* app, FuriString* path, StorageData** storage) {
  69. StorageType type = storage_get_type_by_path(path);
  70. if(storage_type_is_valid(type)) {
  71. if(type == ST_ANY) {
  72. type = ST_INT;
  73. if(storage_data_status(&app->storage[ST_EXT]) == StorageStatusOK) {
  74. type = ST_EXT;
  75. }
  76. storage_path_change_to_real_storage(path, type);
  77. }
  78. furi_assert(type == ST_EXT || type == ST_INT);
  79. *storage = &app->storage[type];
  80. return FSE_OK;
  81. } else {
  82. return FSE_INVALID_NAME;
  83. }
  84. }
  85. /******************* File Functions *******************/
  86. bool storage_process_file_open(
  87. Storage* app,
  88. File* file,
  89. FuriString* path,
  90. FS_AccessMode access_mode,
  91. FS_OpenMode open_mode) {
  92. bool ret = false;
  93. StorageData* storage;
  94. file->error_id = storage_get_data(app, path, &storage);
  95. if(file->error_id == FSE_OK) {
  96. if(storage_path_already_open(path, storage->files)) {
  97. file->error_id = FSE_ALREADY_OPEN;
  98. } else {
  99. if(access_mode & FSAM_WRITE) {
  100. storage_data_timestamp(storage);
  101. }
  102. storage_push_storage_file(file, path, storage);
  103. const char* path_cstr_no_vfs = cstr_path_without_vfs_prefix(path);
  104. FS_CALL(storage, file.open(storage, file, path_cstr_no_vfs, access_mode, open_mode));
  105. }
  106. }
  107. return ret;
  108. }
  109. bool storage_process_file_close(Storage* app, File* file) {
  110. bool ret = false;
  111. StorageData* storage = get_storage_by_file(file, app->storage);
  112. if(storage == NULL) {
  113. file->error_id = FSE_INVALID_PARAMETER;
  114. } else {
  115. FS_CALL(storage, file.close(storage, file));
  116. storage_pop_storage_file(file, storage);
  117. StorageEvent event = {.type = StorageEventTypeFileClose};
  118. furi_pubsub_publish(app->pubsub, &event);
  119. }
  120. return ret;
  121. }
  122. static uint16_t
  123. storage_process_file_read(Storage* app, File* file, void* buff, uint16_t const bytes_to_read) {
  124. uint16_t ret = 0;
  125. StorageData* storage = get_storage_by_file(file, app->storage);
  126. if(storage == NULL) {
  127. file->error_id = FSE_INVALID_PARAMETER;
  128. } else {
  129. FS_CALL(storage, file.read(storage, file, buff, bytes_to_read));
  130. }
  131. return ret;
  132. }
  133. static uint16_t storage_process_file_write(
  134. Storage* app,
  135. File* file,
  136. const void* buff,
  137. uint16_t const bytes_to_write) {
  138. uint16_t ret = 0;
  139. StorageData* storage = get_storage_by_file(file, app->storage);
  140. if(storage == NULL) {
  141. file->error_id = FSE_INVALID_PARAMETER;
  142. } else {
  143. storage_data_timestamp(storage);
  144. FS_CALL(storage, file.write(storage, file, buff, bytes_to_write));
  145. }
  146. return ret;
  147. }
  148. static bool storage_process_file_seek(
  149. Storage* app,
  150. File* file,
  151. const uint32_t offset,
  152. const bool from_start) {
  153. bool ret = false;
  154. StorageData* storage = get_storage_by_file(file, app->storage);
  155. if(storage == NULL) {
  156. file->error_id = FSE_INVALID_PARAMETER;
  157. } else {
  158. FS_CALL(storage, file.seek(storage, file, offset, from_start));
  159. }
  160. return ret;
  161. }
  162. static uint64_t storage_process_file_tell(Storage* app, File* file) {
  163. uint64_t ret = 0;
  164. StorageData* storage = get_storage_by_file(file, app->storage);
  165. if(storage == NULL) {
  166. file->error_id = FSE_INVALID_PARAMETER;
  167. } else {
  168. FS_CALL(storage, file.tell(storage, file));
  169. }
  170. return ret;
  171. }
  172. static bool storage_process_file_truncate(Storage* app, File* file) {
  173. bool ret = false;
  174. StorageData* storage = get_storage_by_file(file, app->storage);
  175. if(storage == NULL) {
  176. file->error_id = FSE_INVALID_PARAMETER;
  177. } else {
  178. storage_data_timestamp(storage);
  179. FS_CALL(storage, file.truncate(storage, file));
  180. }
  181. return ret;
  182. }
  183. static bool storage_process_file_sync(Storage* app, File* file) {
  184. bool ret = false;
  185. StorageData* storage = get_storage_by_file(file, app->storage);
  186. if(storage == NULL) {
  187. file->error_id = FSE_INVALID_PARAMETER;
  188. } else {
  189. storage_data_timestamp(storage);
  190. FS_CALL(storage, file.sync(storage, file));
  191. }
  192. return ret;
  193. }
  194. static uint64_t storage_process_file_size(Storage* app, File* file) {
  195. uint64_t ret = 0;
  196. StorageData* storage = get_storage_by_file(file, app->storage);
  197. if(storage == NULL) {
  198. file->error_id = FSE_INVALID_PARAMETER;
  199. } else {
  200. FS_CALL(storage, file.size(storage, file));
  201. }
  202. return ret;
  203. }
  204. static bool storage_process_file_eof(Storage* app, File* file) {
  205. bool ret = false;
  206. StorageData* storage = get_storage_by_file(file, app->storage);
  207. if(storage == NULL) {
  208. file->error_id = FSE_INVALID_PARAMETER;
  209. } else {
  210. FS_CALL(storage, file.eof(storage, file));
  211. }
  212. return ret;
  213. }
  214. /******************* Dir Functions *******************/
  215. bool storage_process_dir_open(Storage* app, File* file, FuriString* path) {
  216. bool ret = false;
  217. StorageData* storage;
  218. file->error_id = storage_get_data(app, path, &storage);
  219. if(file->error_id == FSE_OK) {
  220. if(storage_path_already_open(path, storage->files)) {
  221. file->error_id = FSE_ALREADY_OPEN;
  222. } else {
  223. storage_push_storage_file(file, path, storage);
  224. FS_CALL(storage, dir.open(storage, file, cstr_path_without_vfs_prefix(path)));
  225. }
  226. }
  227. return ret;
  228. }
  229. bool storage_process_dir_close(Storage* app, File* file) {
  230. bool ret = false;
  231. StorageData* storage = get_storage_by_file(file, app->storage);
  232. if(storage == NULL) {
  233. file->error_id = FSE_INVALID_PARAMETER;
  234. } else {
  235. FS_CALL(storage, dir.close(storage, file));
  236. storage_pop_storage_file(file, storage);
  237. StorageEvent event = {.type = StorageEventTypeDirClose};
  238. furi_pubsub_publish(app->pubsub, &event);
  239. }
  240. return ret;
  241. }
  242. bool storage_process_dir_read(
  243. Storage* app,
  244. File* file,
  245. FileInfo* fileinfo,
  246. char* name,
  247. const uint16_t name_length) {
  248. bool ret = false;
  249. StorageData* storage = get_storage_by_file(file, app->storage);
  250. if(storage == NULL) {
  251. file->error_id = FSE_INVALID_PARAMETER;
  252. } else {
  253. FS_CALL(storage, dir.read(storage, file, fileinfo, name, name_length));
  254. }
  255. return ret;
  256. }
  257. bool storage_process_dir_rewind(Storage* app, File* file) {
  258. bool ret = false;
  259. StorageData* storage = get_storage_by_file(file, app->storage);
  260. if(storage == NULL) {
  261. file->error_id = FSE_INVALID_PARAMETER;
  262. } else {
  263. FS_CALL(storage, dir.rewind(storage, file));
  264. }
  265. return ret;
  266. }
  267. /******************* Common FS Functions *******************/
  268. static FS_Error
  269. storage_process_common_timestamp(Storage* app, FuriString* path, uint32_t* timestamp) {
  270. StorageData* storage;
  271. FS_Error ret = storage_get_data(app, path, &storage);
  272. if(ret == FSE_OK) {
  273. *timestamp = storage_data_get_timestamp(storage);
  274. }
  275. return ret;
  276. }
  277. static FS_Error storage_process_common_stat(Storage* app, FuriString* path, FileInfo* fileinfo) {
  278. StorageData* storage;
  279. FS_Error ret = storage_get_data(app, path, &storage);
  280. if(ret == FSE_OK) {
  281. FS_CALL(storage, common.stat(storage, cstr_path_without_vfs_prefix(path), fileinfo));
  282. }
  283. return ret;
  284. }
  285. static FS_Error storage_process_common_remove(Storage* app, FuriString* path) {
  286. StorageData* storage;
  287. FS_Error ret = storage_get_data(app, path, &storage);
  288. do {
  289. if(storage_path_already_open(path, storage->files)) {
  290. ret = FSE_ALREADY_OPEN;
  291. break;
  292. }
  293. storage_data_timestamp(storage);
  294. FS_CALL(storage, common.remove(storage, cstr_path_without_vfs_prefix(path)));
  295. } while(false);
  296. return ret;
  297. }
  298. static FS_Error storage_process_common_mkdir(Storage* app, FuriString* path) {
  299. StorageData* storage;
  300. FS_Error ret = storage_get_data(app, path, &storage);
  301. if(ret == FSE_OK) {
  302. storage_data_timestamp(storage);
  303. FS_CALL(storage, common.mkdir(storage, cstr_path_without_vfs_prefix(path)));
  304. }
  305. return ret;
  306. }
  307. static FS_Error storage_process_common_fs_info(
  308. Storage* app,
  309. FuriString* path,
  310. uint64_t* total_space,
  311. uint64_t* free_space) {
  312. StorageData* storage;
  313. FS_Error ret = storage_get_data(app, path, &storage);
  314. if(ret == FSE_OK) {
  315. FS_CALL(
  316. storage,
  317. common.fs_info(storage, cstr_path_without_vfs_prefix(path), total_space, free_space));
  318. }
  319. return ret;
  320. }
  321. /****************** Raw SD API ******************/
  322. // TODO think about implementing a custom storage API to split that kind of api linkage
  323. #include "storages/storage_ext.h"
  324. static FS_Error storage_process_sd_format(Storage* app) {
  325. FS_Error ret = FSE_OK;
  326. if(storage_data_status(&app->storage[ST_EXT]) == StorageStatusNotReady) {
  327. ret = FSE_NOT_READY;
  328. } else {
  329. ret = sd_format_card(&app->storage[ST_EXT]);
  330. storage_data_timestamp(&app->storage[ST_EXT]);
  331. }
  332. return ret;
  333. }
  334. static FS_Error storage_process_sd_unmount(Storage* app) {
  335. FS_Error ret = FSE_OK;
  336. if(storage_data_status(&app->storage[ST_EXT]) == StorageStatusNotReady) {
  337. ret = FSE_NOT_READY;
  338. } else {
  339. sd_unmount_card(&app->storage[ST_EXT]);
  340. storage_data_timestamp(&app->storage[ST_EXT]);
  341. }
  342. return ret;
  343. }
  344. static FS_Error storage_process_sd_info(Storage* app, SDInfo* info) {
  345. FS_Error ret = FSE_OK;
  346. if(storage_data_status(&app->storage[ST_EXT]) == StorageStatusNotReady) {
  347. ret = FSE_NOT_READY;
  348. } else {
  349. ret = sd_card_info(&app->storage[ST_EXT], info);
  350. }
  351. return ret;
  352. }
  353. static FS_Error storage_process_sd_status(Storage* app) {
  354. FS_Error ret;
  355. StorageStatus status = storage_data_status(&app->storage[ST_EXT]);
  356. switch(status) {
  357. case StorageStatusOK:
  358. ret = FSE_OK;
  359. break;
  360. case StorageStatusNotReady:
  361. ret = FSE_NOT_READY;
  362. break;
  363. default:
  364. ret = FSE_INTERNAL;
  365. break;
  366. }
  367. return ret;
  368. }
  369. /******************** Aliases processing *******************/
  370. void storage_process_alias(
  371. Storage* app,
  372. FuriString* path,
  373. FuriThreadId thread_id,
  374. bool create_folders) {
  375. if(furi_string_start_with(path, STORAGE_APP_DATA_PATH_PREFIX)) {
  376. FuriString* apps_data_path_with_appsid = furi_string_alloc_set(APPS_DATA_PATH "/");
  377. furi_string_cat(apps_data_path_with_appsid, furi_thread_get_appid(thread_id));
  378. // "/data" -> "/ext/apps_data/appsid"
  379. furi_string_replace_at(
  380. path,
  381. 0,
  382. strlen(STORAGE_APP_DATA_PATH_PREFIX),
  383. furi_string_get_cstr(apps_data_path_with_appsid));
  384. // Create app data folder if not exists
  385. if(create_folders &&
  386. storage_process_common_stat(app, apps_data_path_with_appsid, NULL) != FSE_OK) {
  387. furi_string_set(apps_data_path_with_appsid, APPS_DATA_PATH);
  388. storage_process_common_mkdir(app, apps_data_path_with_appsid);
  389. furi_string_cat(apps_data_path_with_appsid, "/");
  390. furi_string_cat(apps_data_path_with_appsid, furi_thread_get_appid(thread_id));
  391. storage_process_common_mkdir(app, apps_data_path_with_appsid);
  392. }
  393. furi_string_free(apps_data_path_with_appsid);
  394. } else if(furi_string_start_with(path, STORAGE_APP_ASSETS_PATH_PREFIX)) {
  395. FuriString* apps_assets_path_with_appsid = furi_string_alloc_set(APPS_ASSETS_PATH "/");
  396. furi_string_cat(apps_assets_path_with_appsid, furi_thread_get_appid(thread_id));
  397. // "/assets" -> "/ext/apps_assets/appsid"
  398. furi_string_replace_at(
  399. path,
  400. 0,
  401. strlen(STORAGE_APP_ASSETS_PATH_PREFIX),
  402. furi_string_get_cstr(apps_assets_path_with_appsid));
  403. furi_string_free(apps_assets_path_with_appsid);
  404. }
  405. }
  406. /****************** API calls processing ******************/
  407. void storage_process_message_internal(Storage* app, StorageMessage* message) {
  408. FuriString* path = NULL;
  409. switch(message->command) {
  410. // File operations
  411. case StorageCommandFileOpen:
  412. path = furi_string_alloc_set(message->data->fopen.path);
  413. storage_process_alias(app, path, message->data->fopen.thread_id, true);
  414. message->return_data->bool_value = storage_process_file_open(
  415. app,
  416. message->data->fopen.file,
  417. path,
  418. message->data->fopen.access_mode,
  419. message->data->fopen.open_mode);
  420. break;
  421. case StorageCommandFileClose:
  422. message->return_data->bool_value =
  423. storage_process_file_close(app, message->data->fopen.file);
  424. break;
  425. case StorageCommandFileRead:
  426. message->return_data->uint16_value = storage_process_file_read(
  427. app,
  428. message->data->fread.file,
  429. message->data->fread.buff,
  430. message->data->fread.bytes_to_read);
  431. break;
  432. case StorageCommandFileWrite:
  433. message->return_data->uint16_value = storage_process_file_write(
  434. app,
  435. message->data->fwrite.file,
  436. message->data->fwrite.buff,
  437. message->data->fwrite.bytes_to_write);
  438. break;
  439. case StorageCommandFileSeek:
  440. message->return_data->bool_value = storage_process_file_seek(
  441. app,
  442. message->data->fseek.file,
  443. message->data->fseek.offset,
  444. message->data->fseek.from_start);
  445. break;
  446. case StorageCommandFileTell:
  447. message->return_data->uint64_value =
  448. storage_process_file_tell(app, message->data->file.file);
  449. break;
  450. case StorageCommandFileTruncate:
  451. message->return_data->bool_value =
  452. storage_process_file_truncate(app, message->data->file.file);
  453. break;
  454. case StorageCommandFileSync:
  455. message->return_data->bool_value =
  456. storage_process_file_sync(app, message->data->file.file);
  457. break;
  458. case StorageCommandFileSize:
  459. message->return_data->uint64_value =
  460. storage_process_file_size(app, message->data->file.file);
  461. break;
  462. case StorageCommandFileEof:
  463. message->return_data->bool_value = storage_process_file_eof(app, message->data->file.file);
  464. break;
  465. // Dir operations
  466. case StorageCommandDirOpen:
  467. path = furi_string_alloc_set(message->data->dopen.path);
  468. storage_process_alias(app, path, message->data->dopen.thread_id, true);
  469. message->return_data->bool_value =
  470. storage_process_dir_open(app, message->data->dopen.file, path);
  471. break;
  472. case StorageCommandDirClose:
  473. message->return_data->bool_value =
  474. storage_process_dir_close(app, message->data->file.file);
  475. break;
  476. case StorageCommandDirRead:
  477. message->return_data->bool_value = storage_process_dir_read(
  478. app,
  479. message->data->dread.file,
  480. message->data->dread.fileinfo,
  481. message->data->dread.name,
  482. message->data->dread.name_length);
  483. break;
  484. case StorageCommandDirRewind:
  485. message->return_data->bool_value =
  486. storage_process_dir_rewind(app, message->data->file.file);
  487. break;
  488. // Common operations
  489. case StorageCommandCommonTimestamp:
  490. path = furi_string_alloc_set(message->data->ctimestamp.path);
  491. storage_process_alias(app, path, message->data->ctimestamp.thread_id, false);
  492. message->return_data->error_value =
  493. storage_process_common_timestamp(app, path, message->data->ctimestamp.timestamp);
  494. break;
  495. case StorageCommandCommonStat:
  496. path = furi_string_alloc_set(message->data->cstat.path);
  497. storage_process_alias(app, path, message->data->cstat.thread_id, false);
  498. message->return_data->error_value =
  499. storage_process_common_stat(app, path, message->data->cstat.fileinfo);
  500. break;
  501. case StorageCommandCommonRemove:
  502. path = furi_string_alloc_set(message->data->path.path);
  503. storage_process_alias(app, path, message->data->path.thread_id, false);
  504. message->return_data->error_value = storage_process_common_remove(app, path);
  505. break;
  506. case StorageCommandCommonMkDir:
  507. path = furi_string_alloc_set(message->data->path.path);
  508. storage_process_alias(app, path, message->data->path.thread_id, true);
  509. message->return_data->error_value = storage_process_common_mkdir(app, path);
  510. break;
  511. case StorageCommandCommonFSInfo:
  512. path = furi_string_alloc_set(message->data->cfsinfo.fs_path);
  513. storage_process_alias(app, path, message->data->cfsinfo.thread_id, false);
  514. message->return_data->error_value = storage_process_common_fs_info(
  515. app, path, message->data->cfsinfo.total_space, message->data->cfsinfo.free_space);
  516. break;
  517. case StorageCommandCommonResolvePath:
  518. storage_process_alias(
  519. app, message->data->cresolvepath.path, message->data->cresolvepath.thread_id, true);
  520. break;
  521. // SD operations
  522. case StorageCommandSDFormat:
  523. message->return_data->error_value = storage_process_sd_format(app);
  524. break;
  525. case StorageCommandSDUnmount:
  526. message->return_data->error_value = storage_process_sd_unmount(app);
  527. break;
  528. case StorageCommandSDInfo:
  529. message->return_data->error_value =
  530. storage_process_sd_info(app, message->data->sdinfo.info);
  531. break;
  532. case StorageCommandSDStatus:
  533. message->return_data->error_value = storage_process_sd_status(app);
  534. break;
  535. }
  536. if(path != NULL) { //-V547
  537. furi_string_free(path);
  538. }
  539. api_lock_unlock(message->lock);
  540. }
  541. void storage_process_message(Storage* app, StorageMessage* message) {
  542. storage_process_message_internal(app, message);
  543. }