storage_processing.c 19 KB

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