storage_external_api.c 21 KB

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