storage_external_api.c 22 KB

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