storage_external_api.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. #include <core/log.h>
  2. #include <core/record.h>
  3. #include "storage.h"
  4. #include "storage_i.h"
  5. #include "storage_message.h"
  6. #include <toolbox/stream/file_stream.h>
  7. #include <toolbox/dir_walk.h>
  8. #include "toolbox/path.h"
  9. #define MAX_NAME_LENGTH 256
  10. #define MAX_EXT_LEN 16
  11. #define TAG "StorageAPI"
  12. #define S_API_PROLOGUE \
  13. FuriSemaphore* semaphore = furi_semaphore_alloc(1, 0); \
  14. furi_check(semaphore != NULL);
  15. #define S_FILE_API_PROLOGUE \
  16. Storage* storage = file->storage; \
  17. furi_assert(storage);
  18. #define S_API_EPILOGUE \
  19. furi_check( \
  20. furi_message_queue_put(storage->message_queue, &message, FuriWaitForever) == \
  21. FuriStatusOk); \
  22. furi_semaphore_acquire(semaphore, FuriWaitForever); \
  23. furi_semaphore_free(semaphore);
  24. #define S_API_MESSAGE(_command) \
  25. SAReturn return_data; \
  26. StorageMessage message = { \
  27. .semaphore = semaphore, \
  28. .command = _command, \
  29. .data = &data, \
  30. .return_data = &return_data, \
  31. };
  32. #define S_API_DATA_FILE \
  33. SAData data = { \
  34. .file = { \
  35. .file = file, \
  36. }};
  37. #define S_API_DATA_PATH \
  38. SAData data = { \
  39. .path = { \
  40. .path = path, \
  41. }};
  42. #define S_RETURN_BOOL (return_data.bool_value);
  43. #define S_RETURN_UINT16 (return_data.uint16_value);
  44. #define S_RETURN_UINT64 (return_data.uint64_value);
  45. #define S_RETURN_ERROR (return_data.error_value);
  46. #define S_RETURN_CSTRING (return_data.cstring_value);
  47. typedef enum {
  48. StorageEventFlagFileClose = (1 << 0),
  49. } StorageEventFlag;
  50. /****************** FILE ******************/
  51. static bool storage_file_open_internal(
  52. File* file,
  53. const char* path,
  54. FS_AccessMode access_mode,
  55. FS_OpenMode open_mode) {
  56. S_FILE_API_PROLOGUE;
  57. S_API_PROLOGUE;
  58. SAData data = {
  59. .fopen = {
  60. .file = file,
  61. .path = path,
  62. .access_mode = access_mode,
  63. .open_mode = open_mode,
  64. }};
  65. file->type = FileTypeOpenFile;
  66. S_API_MESSAGE(StorageCommandFileOpen);
  67. S_API_EPILOGUE;
  68. return S_RETURN_BOOL;
  69. }
  70. static void storage_file_close_callback(const void* message, void* context) {
  71. const StorageEvent* storage_event = message;
  72. if(storage_event->type == StorageEventTypeFileClose ||
  73. storage_event->type == StorageEventTypeDirClose) {
  74. furi_assert(context);
  75. FuriEventFlag* event = context;
  76. furi_event_flag_set(event, StorageEventFlagFileClose);
  77. }
  78. }
  79. bool storage_file_open(
  80. File* file,
  81. const char* path,
  82. FS_AccessMode access_mode,
  83. FS_OpenMode open_mode) {
  84. bool result;
  85. FuriEventFlag* event = furi_event_flag_alloc();
  86. FuriPubSubSubscription* subscription = furi_pubsub_subscribe(
  87. storage_get_pubsub(file->storage), storage_file_close_callback, event);
  88. do {
  89. result = storage_file_open_internal(file, path, access_mode, open_mode);
  90. if(!result && file->error_id == FSE_ALREADY_OPEN) {
  91. furi_event_flag_wait(
  92. event, StorageEventFlagFileClose, FuriFlagWaitAny, FuriWaitForever);
  93. } else {
  94. break;
  95. }
  96. } while(true);
  97. furi_pubsub_unsubscribe(storage_get_pubsub(file->storage), subscription);
  98. furi_event_flag_free(event);
  99. FURI_LOG_T(
  100. TAG, "File %p - %p open (%s)", (uint32_t)file - SRAM_BASE, file->file_id - SRAM_BASE, path);
  101. return result;
  102. }
  103. bool storage_file_close(File* file) {
  104. S_FILE_API_PROLOGUE;
  105. S_API_PROLOGUE;
  106. S_API_DATA_FILE;
  107. S_API_MESSAGE(StorageCommandFileClose);
  108. S_API_EPILOGUE;
  109. FURI_LOG_T(TAG, "File %p - %p closed", (uint32_t)file - SRAM_BASE, file->file_id - SRAM_BASE);
  110. file->type = FileTypeClosed;
  111. return S_RETURN_BOOL;
  112. }
  113. uint16_t storage_file_read(File* file, void* buff, uint16_t bytes_to_read) {
  114. if(bytes_to_read == 0) {
  115. return 0;
  116. }
  117. S_FILE_API_PROLOGUE;
  118. S_API_PROLOGUE;
  119. SAData data = {
  120. .fread = {
  121. .file = file,
  122. .buff = buff,
  123. .bytes_to_read = bytes_to_read,
  124. }};
  125. S_API_MESSAGE(StorageCommandFileRead);
  126. S_API_EPILOGUE;
  127. return S_RETURN_UINT16;
  128. }
  129. uint16_t storage_file_write(File* file, const void* buff, uint16_t bytes_to_write) {
  130. if(bytes_to_write == 0) {
  131. return 0;
  132. }
  133. S_FILE_API_PROLOGUE;
  134. S_API_PROLOGUE;
  135. SAData data = {
  136. .fwrite = {
  137. .file = file,
  138. .buff = buff,
  139. .bytes_to_write = bytes_to_write,
  140. }};
  141. S_API_MESSAGE(StorageCommandFileWrite);
  142. S_API_EPILOGUE;
  143. return S_RETURN_UINT16;
  144. }
  145. bool storage_file_seek(File* file, uint32_t offset, bool from_start) {
  146. S_FILE_API_PROLOGUE;
  147. S_API_PROLOGUE;
  148. SAData data = {
  149. .fseek = {
  150. .file = file,
  151. .offset = offset,
  152. .from_start = from_start,
  153. }};
  154. S_API_MESSAGE(StorageCommandFileSeek);
  155. S_API_EPILOGUE;
  156. return S_RETURN_BOOL;
  157. }
  158. uint64_t storage_file_tell(File* file) {
  159. S_FILE_API_PROLOGUE;
  160. S_API_PROLOGUE;
  161. S_API_DATA_FILE;
  162. S_API_MESSAGE(StorageCommandFileTell);
  163. S_API_EPILOGUE;
  164. return S_RETURN_UINT64;
  165. }
  166. bool storage_file_truncate(File* file) {
  167. S_FILE_API_PROLOGUE;
  168. S_API_PROLOGUE;
  169. S_API_DATA_FILE;
  170. S_API_MESSAGE(StorageCommandFileTruncate);
  171. S_API_EPILOGUE;
  172. return S_RETURN_BOOL;
  173. }
  174. uint64_t storage_file_size(File* file) {
  175. S_FILE_API_PROLOGUE;
  176. S_API_PROLOGUE;
  177. S_API_DATA_FILE;
  178. S_API_MESSAGE(StorageCommandFileSize);
  179. S_API_EPILOGUE;
  180. return S_RETURN_UINT64;
  181. }
  182. bool storage_file_sync(File* file) {
  183. S_FILE_API_PROLOGUE;
  184. S_API_PROLOGUE;
  185. S_API_DATA_FILE;
  186. S_API_MESSAGE(StorageCommandFileSync);
  187. S_API_EPILOGUE;
  188. return S_RETURN_BOOL;
  189. }
  190. bool storage_file_eof(File* file) {
  191. S_FILE_API_PROLOGUE;
  192. S_API_PROLOGUE;
  193. S_API_DATA_FILE;
  194. S_API_MESSAGE(StorageCommandFileEof);
  195. S_API_EPILOGUE;
  196. return S_RETURN_BOOL;
  197. }
  198. bool storage_file_exists(Storage* storage, const char* path) {
  199. bool exist = false;
  200. FileInfo fileinfo;
  201. FS_Error error = storage_common_stat(storage, path, &fileinfo);
  202. if(error == FSE_OK && !(fileinfo.flags & FSF_DIRECTORY)) {
  203. exist = true;
  204. }
  205. return exist;
  206. }
  207. /****************** DIR ******************/
  208. static bool storage_dir_open_internal(File* file, const char* path) {
  209. S_FILE_API_PROLOGUE;
  210. S_API_PROLOGUE;
  211. SAData data = {
  212. .dopen = {
  213. .file = file,
  214. .path = path,
  215. }};
  216. file->type = FileTypeOpenDir;
  217. S_API_MESSAGE(StorageCommandDirOpen);
  218. S_API_EPILOGUE;
  219. return S_RETURN_BOOL;
  220. }
  221. bool storage_dir_open(File* file, const char* path) {
  222. bool result;
  223. FuriEventFlag* event = furi_event_flag_alloc();
  224. FuriPubSubSubscription* subscription = furi_pubsub_subscribe(
  225. storage_get_pubsub(file->storage), storage_file_close_callback, event);
  226. do {
  227. result = storage_dir_open_internal(file, path);
  228. if(!result && file->error_id == FSE_ALREADY_OPEN) {
  229. furi_event_flag_wait(
  230. event, StorageEventFlagFileClose, FuriFlagWaitAny, FuriWaitForever);
  231. } else {
  232. break;
  233. }
  234. } while(true);
  235. furi_pubsub_unsubscribe(storage_get_pubsub(file->storage), subscription);
  236. furi_event_flag_free(event);
  237. FURI_LOG_T(
  238. TAG, "Dir %p - %p open (%s)", (uint32_t)file - SRAM_BASE, file->file_id - SRAM_BASE, path);
  239. return result;
  240. }
  241. bool storage_dir_close(File* file) {
  242. S_FILE_API_PROLOGUE;
  243. S_API_PROLOGUE;
  244. S_API_DATA_FILE;
  245. S_API_MESSAGE(StorageCommandDirClose);
  246. S_API_EPILOGUE;
  247. FURI_LOG_T(TAG, "Dir %p - %p closed", (uint32_t)file - SRAM_BASE, file->file_id - SRAM_BASE);
  248. file->type = FileTypeClosed;
  249. return S_RETURN_BOOL;
  250. }
  251. bool storage_dir_read(File* file, FileInfo* fileinfo, char* name, uint16_t name_length) {
  252. S_FILE_API_PROLOGUE;
  253. S_API_PROLOGUE;
  254. SAData data = {
  255. .dread = {
  256. .file = file,
  257. .fileinfo = fileinfo,
  258. .name = name,
  259. .name_length = name_length,
  260. }};
  261. S_API_MESSAGE(StorageCommandDirRead);
  262. S_API_EPILOGUE;
  263. return S_RETURN_BOOL;
  264. }
  265. bool storage_dir_rewind(File* file) {
  266. S_FILE_API_PROLOGUE;
  267. S_API_PROLOGUE;
  268. S_API_DATA_FILE;
  269. S_API_MESSAGE(StorageCommandDirRewind);
  270. S_API_EPILOGUE;
  271. return S_RETURN_BOOL;
  272. }
  273. /****************** COMMON ******************/
  274. FS_Error storage_common_stat(Storage* storage, const char* path, FileInfo* fileinfo) {
  275. S_API_PROLOGUE;
  276. SAData data = {.cstat = {.path = path, .fileinfo = fileinfo}};
  277. S_API_MESSAGE(StorageCommandCommonStat);
  278. S_API_EPILOGUE;
  279. return S_RETURN_ERROR;
  280. }
  281. FS_Error storage_common_remove(Storage* storage, const char* path) {
  282. S_API_PROLOGUE;
  283. S_API_DATA_PATH;
  284. S_API_MESSAGE(StorageCommandCommonRemove);
  285. S_API_EPILOGUE;
  286. return S_RETURN_ERROR;
  287. }
  288. FS_Error storage_common_rename(Storage* storage, const char* old_path, const char* new_path) {
  289. FS_Error error = storage_common_copy(storage, old_path, new_path);
  290. if(error == FSE_OK) {
  291. if(storage_simply_remove_recursive(storage, old_path)) {
  292. error = FSE_OK;
  293. } else {
  294. error = FSE_INTERNAL;
  295. }
  296. }
  297. return error;
  298. }
  299. static FS_Error
  300. storage_copy_recursive(Storage* storage, const char* old_path, const char* new_path) {
  301. FS_Error error = storage_common_mkdir(storage, new_path);
  302. DirWalk* dir_walk = dir_walk_alloc(storage);
  303. FuriString* path;
  304. FuriString* tmp_new_path;
  305. FuriString* tmp_old_path;
  306. FileInfo fileinfo;
  307. path = furi_string_alloc();
  308. tmp_new_path = furi_string_alloc();
  309. tmp_old_path = furi_string_alloc();
  310. do {
  311. if(error != FSE_OK) break;
  312. if(!dir_walk_open(dir_walk, old_path)) {
  313. error = dir_walk_get_error(dir_walk);
  314. break;
  315. }
  316. while(1) {
  317. DirWalkResult res = dir_walk_read(dir_walk, path, &fileinfo);
  318. if(res == DirWalkError) {
  319. error = dir_walk_get_error(dir_walk);
  320. break;
  321. } else if(res == DirWalkLast) {
  322. break;
  323. } else {
  324. furi_string_set(tmp_old_path, path);
  325. furi_string_right(path, strlen(old_path));
  326. furi_string_printf(tmp_new_path, "%s%s", new_path, furi_string_get_cstr(path));
  327. if(fileinfo.flags & FSF_DIRECTORY) {
  328. error = storage_common_mkdir(storage, furi_string_get_cstr(tmp_new_path));
  329. } else {
  330. error = storage_common_copy(
  331. storage,
  332. furi_string_get_cstr(tmp_old_path),
  333. furi_string_get_cstr(tmp_new_path));
  334. }
  335. if(error != FSE_OK) break;
  336. }
  337. }
  338. } while(false);
  339. furi_string_free(tmp_new_path);
  340. furi_string_free(tmp_old_path);
  341. furi_string_free(path);
  342. dir_walk_free(dir_walk);
  343. return error;
  344. }
  345. FS_Error storage_common_copy(Storage* storage, const char* old_path, const char* new_path) {
  346. FS_Error error;
  347. FileInfo fileinfo;
  348. error = storage_common_stat(storage, old_path, &fileinfo);
  349. if(error == FSE_OK) {
  350. if(fileinfo.flags & FSF_DIRECTORY) {
  351. error = storage_copy_recursive(storage, old_path, new_path);
  352. } else {
  353. Stream* stream_from = file_stream_alloc(storage);
  354. Stream* stream_to = file_stream_alloc(storage);
  355. do {
  356. if(!file_stream_open(stream_from, old_path, FSAM_READ, FSOM_OPEN_EXISTING)) break;
  357. if(!file_stream_open(stream_to, new_path, FSAM_WRITE, FSOM_CREATE_NEW)) break;
  358. stream_copy_full(stream_from, stream_to);
  359. } while(false);
  360. error = file_stream_get_error(stream_from);
  361. if(error == FSE_OK) {
  362. error = file_stream_get_error(stream_to);
  363. }
  364. stream_free(stream_from);
  365. stream_free(stream_to);
  366. }
  367. }
  368. return error;
  369. }
  370. static FS_Error
  371. storage_merge_recursive(Storage* storage, const char* old_path, const char* new_path) {
  372. FS_Error error = storage_common_mkdir(storage, new_path);
  373. DirWalk* dir_walk = dir_walk_alloc(storage);
  374. FuriString *path, *file_basename, *tmp_new_path;
  375. FileInfo fileinfo;
  376. path = furi_string_alloc();
  377. file_basename = furi_string_alloc();
  378. tmp_new_path = furi_string_alloc();
  379. do {
  380. if((error != FSE_OK) && (error != FSE_EXIST)) break;
  381. dir_walk_set_recursive(dir_walk, false);
  382. if(!dir_walk_open(dir_walk, old_path)) {
  383. error = dir_walk_get_error(dir_walk);
  384. break;
  385. }
  386. while(1) {
  387. DirWalkResult res = dir_walk_read(dir_walk, path, &fileinfo);
  388. if(res == DirWalkError) {
  389. error = dir_walk_get_error(dir_walk);
  390. break;
  391. } else if(res == DirWalkLast) {
  392. break;
  393. } else {
  394. path_extract_basename(furi_string_get_cstr(path), file_basename);
  395. path_concat(new_path, furi_string_get_cstr(file_basename), tmp_new_path);
  396. if(fileinfo.flags & FSF_DIRECTORY) {
  397. if(storage_common_stat(
  398. storage, furi_string_get_cstr(tmp_new_path), &fileinfo) == FSE_OK) {
  399. if(fileinfo.flags & FSF_DIRECTORY) {
  400. error =
  401. storage_common_mkdir(storage, furi_string_get_cstr(tmp_new_path));
  402. if(error != FSE_OK) {
  403. break;
  404. }
  405. }
  406. }
  407. }
  408. error = storage_common_merge(
  409. storage, furi_string_get_cstr(path), furi_string_get_cstr(tmp_new_path));
  410. if(error != FSE_OK) {
  411. break;
  412. }
  413. }
  414. }
  415. } while(false);
  416. furi_string_free(tmp_new_path);
  417. furi_string_free(file_basename);
  418. furi_string_free(path);
  419. dir_walk_free(dir_walk);
  420. return error;
  421. }
  422. FS_Error storage_common_merge(Storage* storage, const char* old_path, const char* new_path) {
  423. FS_Error error;
  424. const char* new_path_tmp;
  425. FuriString* new_path_next;
  426. new_path_next = furi_string_alloc();
  427. FileInfo fileinfo;
  428. error = storage_common_stat(storage, old_path, &fileinfo);
  429. if(error == FSE_OK) {
  430. if(fileinfo.flags & FSF_DIRECTORY) {
  431. error = storage_merge_recursive(storage, old_path, new_path);
  432. } else {
  433. error = storage_common_stat(storage, new_path, &fileinfo);
  434. if(error == FSE_OK) {
  435. furi_string_set(new_path_next, new_path);
  436. FuriString* dir_path;
  437. FuriString* filename;
  438. char extension[MAX_EXT_LEN];
  439. dir_path = furi_string_alloc();
  440. filename = furi_string_alloc();
  441. path_extract_filename(new_path_next, filename, true);
  442. path_extract_dirname(new_path, dir_path);
  443. path_extract_extension(new_path_next, extension, MAX_EXT_LEN);
  444. storage_get_next_filename(
  445. storage,
  446. furi_string_get_cstr(dir_path),
  447. furi_string_get_cstr(filename),
  448. extension,
  449. new_path_next,
  450. 255);
  451. furi_string_cat_printf(
  452. dir_path, "/%s%s", furi_string_get_cstr(new_path_next), extension);
  453. furi_string_set(new_path_next, dir_path);
  454. furi_string_free(dir_path);
  455. furi_string_free(filename);
  456. new_path_tmp = furi_string_get_cstr(new_path_next);
  457. } else {
  458. new_path_tmp = new_path;
  459. }
  460. Stream* stream_from = file_stream_alloc(storage);
  461. Stream* stream_to = file_stream_alloc(storage);
  462. do {
  463. if(!file_stream_open(stream_from, old_path, FSAM_READ, FSOM_OPEN_EXISTING)) break;
  464. if(!file_stream_open(stream_to, new_path_tmp, FSAM_WRITE, FSOM_CREATE_NEW)) break;
  465. stream_copy_full(stream_from, stream_to);
  466. } while(false);
  467. error = file_stream_get_error(stream_from);
  468. if(error == FSE_OK) {
  469. error = file_stream_get_error(stream_to);
  470. }
  471. stream_free(stream_from);
  472. stream_free(stream_to);
  473. }
  474. }
  475. furi_string_free(new_path_next);
  476. return error;
  477. }
  478. FS_Error storage_common_mkdir(Storage* storage, const char* path) {
  479. S_API_PROLOGUE;
  480. S_API_DATA_PATH;
  481. S_API_MESSAGE(StorageCommandCommonMkDir);
  482. S_API_EPILOGUE;
  483. return S_RETURN_ERROR;
  484. }
  485. FS_Error storage_common_fs_info(
  486. Storage* storage,
  487. const char* fs_path,
  488. uint64_t* total_space,
  489. uint64_t* free_space) {
  490. S_API_PROLOGUE;
  491. SAData data = {
  492. .cfsinfo = {
  493. .fs_path = fs_path,
  494. .total_space = total_space,
  495. .free_space = free_space,
  496. }};
  497. S_API_MESSAGE(StorageCommandCommonFSInfo);
  498. S_API_EPILOGUE;
  499. return S_RETURN_ERROR;
  500. }
  501. /****************** ERROR ******************/
  502. const char* storage_error_get_desc(FS_Error error_id) {
  503. return filesystem_api_error_get_desc(error_id);
  504. }
  505. FS_Error storage_file_get_error(File* file) {
  506. furi_check(file != NULL);
  507. return file->error_id;
  508. }
  509. int32_t storage_file_get_internal_error(File* file) {
  510. furi_check(file != NULL);
  511. return file->internal_error_id;
  512. }
  513. const char* storage_file_get_error_desc(File* file) {
  514. furi_check(file != NULL);
  515. return filesystem_api_error_get_desc(file->error_id);
  516. }
  517. /****************** Raw SD API ******************/
  518. FS_Error storage_sd_format(Storage* storage) {
  519. S_API_PROLOGUE;
  520. SAData data = {};
  521. S_API_MESSAGE(StorageCommandSDFormat);
  522. S_API_EPILOGUE;
  523. return S_RETURN_ERROR;
  524. }
  525. FS_Error storage_sd_unmount(Storage* storage) {
  526. S_API_PROLOGUE;
  527. SAData data = {};
  528. S_API_MESSAGE(StorageCommandSDUnmount);
  529. S_API_EPILOGUE;
  530. return S_RETURN_ERROR;
  531. }
  532. FS_Error storage_sd_info(Storage* storage, SDInfo* info) {
  533. S_API_PROLOGUE;
  534. SAData data = {
  535. .sdinfo = {
  536. .info = info,
  537. }};
  538. S_API_MESSAGE(StorageCommandSDInfo);
  539. S_API_EPILOGUE;
  540. return S_RETURN_ERROR;
  541. }
  542. FS_Error storage_sd_status(Storage* storage) {
  543. S_API_PROLOGUE;
  544. SAData data = {};
  545. S_API_MESSAGE(StorageCommandSDStatus);
  546. S_API_EPILOGUE;
  547. return S_RETURN_ERROR;
  548. }
  549. File* storage_file_alloc(Storage* storage) {
  550. File* file = malloc(sizeof(File));
  551. file->type = FileTypeClosed;
  552. file->storage = storage;
  553. FURI_LOG_T(TAG, "File/Dir %p alloc", (uint32_t)file - SRAM_BASE);
  554. return file;
  555. }
  556. bool storage_file_is_open(File* file) {
  557. return (file->type != FileTypeClosed);
  558. }
  559. bool storage_file_is_dir(File* file) {
  560. return (file->type == FileTypeOpenDir);
  561. }
  562. void storage_file_free(File* file) {
  563. if(storage_file_is_open(file)) {
  564. if(storage_file_is_dir(file)) {
  565. storage_dir_close(file);
  566. } else {
  567. storage_file_close(file);
  568. }
  569. }
  570. FURI_LOG_T(TAG, "File/Dir %p free", (uint32_t)file - SRAM_BASE);
  571. free(file);
  572. }
  573. FuriPubSub* storage_get_pubsub(Storage* storage) {
  574. return storage->pubsub;
  575. }
  576. bool storage_simply_remove_recursive(Storage* storage, const char* path) {
  577. furi_assert(storage);
  578. furi_assert(path);
  579. FileInfo fileinfo;
  580. bool result = false;
  581. FuriString* fullname;
  582. FuriString* cur_dir;
  583. if(storage_simply_remove(storage, path)) {
  584. return true;
  585. }
  586. char* name = malloc(MAX_NAME_LENGTH + 1);
  587. File* dir = storage_file_alloc(storage);
  588. cur_dir = furi_string_alloc_set(path);
  589. bool go_deeper = false;
  590. while(1) {
  591. if(!storage_dir_open(dir, furi_string_get_cstr(cur_dir))) {
  592. storage_dir_close(dir);
  593. break;
  594. }
  595. while(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  596. if(fileinfo.flags & FSF_DIRECTORY) {
  597. furi_string_cat_printf(cur_dir, "/%s", name);
  598. go_deeper = true;
  599. break;
  600. }
  601. fullname = furi_string_alloc_printf("%s/%s", furi_string_get_cstr(cur_dir), name);
  602. FS_Error error = storage_common_remove(storage, furi_string_get_cstr(fullname));
  603. furi_check(error == FSE_OK);
  604. furi_string_free(fullname);
  605. }
  606. storage_dir_close(dir);
  607. if(go_deeper) {
  608. go_deeper = false;
  609. continue;
  610. }
  611. FS_Error error = storage_common_remove(storage, furi_string_get_cstr(cur_dir));
  612. furi_check(error == FSE_OK);
  613. if(furi_string_cmp(cur_dir, path)) {
  614. size_t last_char = furi_string_search_rchar(cur_dir, '/');
  615. furi_assert(last_char != FURI_STRING_FAILURE);
  616. furi_string_left(cur_dir, last_char);
  617. } else {
  618. result = true;
  619. break;
  620. }
  621. }
  622. storage_file_free(dir);
  623. furi_string_free(cur_dir);
  624. free(name);
  625. return result;
  626. }
  627. bool storage_simply_remove(Storage* storage, const char* path) {
  628. FS_Error result;
  629. result = storage_common_remove(storage, path);
  630. return result == FSE_OK || result == FSE_NOT_EXIST;
  631. }
  632. bool storage_simply_mkdir(Storage* storage, const char* path) {
  633. FS_Error result;
  634. result = storage_common_mkdir(storage, path);
  635. return result == FSE_OK || result == FSE_EXIST;
  636. }
  637. void storage_get_next_filename(
  638. Storage* storage,
  639. const char* dirname,
  640. const char* filename,
  641. const char* fileextension,
  642. FuriString* nextfilename,
  643. uint8_t max_len) {
  644. FuriString* temp_str;
  645. uint16_t num = 0;
  646. temp_str = furi_string_alloc_printf("%s/%s%s", dirname, filename, fileextension);
  647. while(storage_common_stat(storage, furi_string_get_cstr(temp_str), NULL) == FSE_OK) {
  648. num++;
  649. furi_string_printf(temp_str, "%s/%s%d%s", dirname, filename, num, fileextension);
  650. }
  651. if(num && (max_len > strlen(filename))) {
  652. furi_string_printf(nextfilename, "%s%d", filename, num);
  653. } else {
  654. furi_string_printf(nextfilename, "%s", filename);
  655. }
  656. furi_string_free(temp_str);
  657. }