storage_processing.c 20 KB

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