storage_external_api.c 22 KB

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