storage_external_api.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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. /****************** DIR ******************/
  200. static bool storage_dir_open_internal(File* file, const char* path) {
  201. S_FILE_API_PROLOGUE;
  202. S_API_PROLOGUE;
  203. SAData data = {
  204. .dopen = {
  205. .file = file,
  206. .path = path,
  207. }};
  208. file->type = FileTypeOpenDir;
  209. S_API_MESSAGE(StorageCommandDirOpen);
  210. S_API_EPILOGUE;
  211. return S_RETURN_BOOL;
  212. }
  213. bool storage_dir_open(File* file, const char* path) {
  214. bool result;
  215. FuriEventFlag* event = furi_event_flag_alloc();
  216. FuriPubSubSubscription* subscription = furi_pubsub_subscribe(
  217. storage_get_pubsub(file->storage), storage_file_close_callback, event);
  218. do {
  219. result = storage_dir_open_internal(file, path);
  220. if(!result && file->error_id == FSE_ALREADY_OPEN) {
  221. furi_event_flag_wait(
  222. event, StorageEventFlagFileClose, FuriFlagWaitAny, FuriWaitForever);
  223. } else {
  224. break;
  225. }
  226. } while(true);
  227. furi_pubsub_unsubscribe(storage_get_pubsub(file->storage), subscription);
  228. furi_event_flag_free(event);
  229. FURI_LOG_T(
  230. TAG, "Dir %p - %p open (%s)", (uint32_t)file - SRAM_BASE, file->file_id - SRAM_BASE, path);
  231. return result;
  232. }
  233. bool storage_dir_close(File* file) {
  234. S_FILE_API_PROLOGUE;
  235. S_API_PROLOGUE;
  236. S_API_DATA_FILE;
  237. S_API_MESSAGE(StorageCommandDirClose);
  238. S_API_EPILOGUE;
  239. FURI_LOG_T(TAG, "Dir %p - %p closed", (uint32_t)file - SRAM_BASE, file->file_id - SRAM_BASE);
  240. file->type = FileTypeClosed;
  241. return S_RETURN_BOOL;
  242. }
  243. bool storage_dir_read(File* file, FileInfo* fileinfo, char* name, uint16_t name_length) {
  244. S_FILE_API_PROLOGUE;
  245. S_API_PROLOGUE;
  246. SAData data = {
  247. .dread = {
  248. .file = file,
  249. .fileinfo = fileinfo,
  250. .name = name,
  251. .name_length = name_length,
  252. }};
  253. S_API_MESSAGE(StorageCommandDirRead);
  254. S_API_EPILOGUE;
  255. return S_RETURN_BOOL;
  256. }
  257. bool storage_dir_rewind(File* file) {
  258. S_FILE_API_PROLOGUE;
  259. S_API_PROLOGUE;
  260. S_API_DATA_FILE;
  261. S_API_MESSAGE(StorageCommandDirRewind);
  262. S_API_EPILOGUE;
  263. return S_RETURN_BOOL;
  264. }
  265. /****************** COMMON ******************/
  266. FS_Error storage_common_stat(Storage* storage, const char* path, FileInfo* fileinfo) {
  267. S_API_PROLOGUE;
  268. SAData data = {.cstat = {.path = path, .fileinfo = fileinfo}};
  269. S_API_MESSAGE(StorageCommandCommonStat);
  270. S_API_EPILOGUE;
  271. return S_RETURN_ERROR;
  272. }
  273. FS_Error storage_common_remove(Storage* storage, const char* path) {
  274. S_API_PROLOGUE;
  275. S_API_DATA_PATH;
  276. S_API_MESSAGE(StorageCommandCommonRemove);
  277. S_API_EPILOGUE;
  278. return S_RETURN_ERROR;
  279. }
  280. FS_Error storage_common_rename(Storage* storage, const char* old_path, const char* new_path) {
  281. FS_Error error = storage_common_copy(storage, old_path, new_path);
  282. if(error == FSE_OK) {
  283. if(storage_simply_remove_recursive(storage, old_path)) {
  284. error = FSE_OK;
  285. } else {
  286. error = FSE_INTERNAL;
  287. }
  288. }
  289. return error;
  290. }
  291. static FS_Error
  292. storage_copy_recursive(Storage* storage, const char* old_path, const char* new_path) {
  293. FS_Error error = storage_common_mkdir(storage, new_path);
  294. DirWalk* dir_walk = dir_walk_alloc(storage);
  295. string_t path;
  296. string_t tmp_new_path;
  297. string_t tmp_old_path;
  298. FileInfo fileinfo;
  299. string_init(path);
  300. string_init(tmp_new_path);
  301. string_init(tmp_old_path);
  302. do {
  303. if(error != FSE_OK) break;
  304. if(!dir_walk_open(dir_walk, old_path)) {
  305. error = dir_walk_get_error(dir_walk);
  306. break;
  307. }
  308. while(1) {
  309. DirWalkResult res = dir_walk_read(dir_walk, path, &fileinfo);
  310. if(res == DirWalkError) {
  311. error = dir_walk_get_error(dir_walk);
  312. break;
  313. } else if(res == DirWalkLast) {
  314. break;
  315. } else {
  316. string_set(tmp_old_path, path);
  317. string_right(path, strlen(old_path));
  318. string_printf(tmp_new_path, "%s%s", new_path, string_get_cstr(path));
  319. if(fileinfo.flags & FSF_DIRECTORY) {
  320. error = storage_common_mkdir(storage, string_get_cstr(tmp_new_path));
  321. } else {
  322. error = storage_common_copy(
  323. storage, string_get_cstr(tmp_old_path), string_get_cstr(tmp_new_path));
  324. }
  325. if(error != FSE_OK) break;
  326. }
  327. }
  328. } while(false);
  329. string_clear(tmp_new_path);
  330. string_clear(tmp_old_path);
  331. string_clear(path);
  332. dir_walk_free(dir_walk);
  333. return error;
  334. }
  335. FS_Error storage_common_copy(Storage* storage, const char* old_path, const char* new_path) {
  336. FS_Error error;
  337. FileInfo fileinfo;
  338. error = storage_common_stat(storage, old_path, &fileinfo);
  339. if(error == FSE_OK) {
  340. if(fileinfo.flags & FSF_DIRECTORY) {
  341. error = storage_copy_recursive(storage, old_path, new_path);
  342. } else {
  343. Stream* stream_from = file_stream_alloc(storage);
  344. Stream* stream_to = file_stream_alloc(storage);
  345. do {
  346. if(!file_stream_open(stream_from, old_path, FSAM_READ, FSOM_OPEN_EXISTING)) break;
  347. if(!file_stream_open(stream_to, new_path, FSAM_WRITE, FSOM_CREATE_NEW)) break;
  348. stream_copy_full(stream_from, stream_to);
  349. } while(false);
  350. error = file_stream_get_error(stream_from);
  351. if(error == FSE_OK) {
  352. error = file_stream_get_error(stream_to);
  353. }
  354. stream_free(stream_from);
  355. stream_free(stream_to);
  356. }
  357. }
  358. return error;
  359. }
  360. static FS_Error
  361. storage_merge_recursive(Storage* storage, const char* old_path, const char* new_path) {
  362. FS_Error error = storage_common_mkdir(storage, new_path);
  363. DirWalk* dir_walk = dir_walk_alloc(storage);
  364. string_t path;
  365. string_t tmp_new_path;
  366. string_t tmp_old_path;
  367. FileInfo fileinfo;
  368. string_init(path);
  369. string_init(tmp_new_path);
  370. string_init(tmp_old_path);
  371. do {
  372. if((error != FSE_OK) && (error != FSE_EXIST)) break;
  373. if(!dir_walk_open(dir_walk, old_path)) {
  374. error = dir_walk_get_error(dir_walk);
  375. break;
  376. }
  377. while(1) {
  378. DirWalkResult res = dir_walk_read(dir_walk, path, &fileinfo);
  379. if(res == DirWalkError) {
  380. error = dir_walk_get_error(dir_walk);
  381. break;
  382. } else if(res == DirWalkLast) {
  383. break;
  384. } else {
  385. string_set(tmp_old_path, path);
  386. string_right(path, strlen(old_path));
  387. string_printf(tmp_new_path, "%s%s", new_path, string_get_cstr(path));
  388. if(fileinfo.flags & FSF_DIRECTORY) {
  389. if(storage_common_stat(storage, string_get_cstr(tmp_new_path), &fileinfo) ==
  390. FSE_OK) {
  391. if(fileinfo.flags & FSF_DIRECTORY) {
  392. error = storage_common_mkdir(storage, string_get_cstr(tmp_new_path));
  393. }
  394. }
  395. } else {
  396. error = storage_common_merge(
  397. storage, string_get_cstr(tmp_old_path), string_get_cstr(tmp_new_path));
  398. }
  399. if(error != FSE_OK) break;
  400. }
  401. }
  402. } while(false);
  403. string_clear(tmp_new_path);
  404. string_clear(tmp_old_path);
  405. string_clear(path);
  406. dir_walk_free(dir_walk);
  407. return error;
  408. }
  409. FS_Error storage_common_merge(Storage* storage, const char* old_path, const char* new_path) {
  410. FS_Error error;
  411. const char* new_path_tmp;
  412. string_t new_path_next;
  413. string_init(new_path_next);
  414. FileInfo fileinfo;
  415. error = storage_common_stat(storage, old_path, &fileinfo);
  416. if(error == FSE_OK) {
  417. if(fileinfo.flags & FSF_DIRECTORY) {
  418. error = storage_merge_recursive(storage, old_path, new_path);
  419. } else {
  420. error = storage_common_stat(storage, new_path, &fileinfo);
  421. if(error == FSE_OK) {
  422. string_set_str(new_path_next, new_path);
  423. string_t dir_path;
  424. string_t filename;
  425. char extension[MAX_EXT_LEN];
  426. string_init(dir_path);
  427. string_init(filename);
  428. path_extract_filename(new_path_next, filename, true);
  429. path_extract_dirname(new_path, dir_path);
  430. path_extract_extension(new_path_next, extension, MAX_EXT_LEN);
  431. storage_get_next_filename(
  432. storage,
  433. string_get_cstr(dir_path),
  434. string_get_cstr(filename),
  435. extension,
  436. new_path_next,
  437. 255);
  438. string_cat_printf(dir_path, "/%s%s", string_get_cstr(new_path_next), extension);
  439. string_set(new_path_next, dir_path);
  440. string_clear(dir_path);
  441. string_clear(filename);
  442. new_path_tmp = string_get_cstr(new_path_next);
  443. } else {
  444. new_path_tmp = new_path;
  445. }
  446. Stream* stream_from = file_stream_alloc(storage);
  447. Stream* stream_to = file_stream_alloc(storage);
  448. do {
  449. if(!file_stream_open(stream_from, old_path, FSAM_READ, FSOM_OPEN_EXISTING)) break;
  450. if(!file_stream_open(stream_to, new_path_tmp, FSAM_WRITE, FSOM_CREATE_NEW)) break;
  451. stream_copy_full(stream_from, stream_to);
  452. } while(false);
  453. error = file_stream_get_error(stream_from);
  454. if(error == FSE_OK) {
  455. error = file_stream_get_error(stream_to);
  456. }
  457. stream_free(stream_from);
  458. stream_free(stream_to);
  459. }
  460. }
  461. string_clear(new_path_next);
  462. return error;
  463. }
  464. FS_Error storage_common_mkdir(Storage* storage, const char* path) {
  465. S_API_PROLOGUE;
  466. S_API_DATA_PATH;
  467. S_API_MESSAGE(StorageCommandCommonMkDir);
  468. S_API_EPILOGUE;
  469. return S_RETURN_ERROR;
  470. }
  471. FS_Error storage_common_fs_info(
  472. Storage* storage,
  473. const char* fs_path,
  474. uint64_t* total_space,
  475. uint64_t* free_space) {
  476. S_API_PROLOGUE;
  477. SAData data = {
  478. .cfsinfo = {
  479. .fs_path = fs_path,
  480. .total_space = total_space,
  481. .free_space = free_space,
  482. }};
  483. S_API_MESSAGE(StorageCommandCommonFSInfo);
  484. S_API_EPILOGUE;
  485. return S_RETURN_ERROR;
  486. }
  487. /****************** ERROR ******************/
  488. const char* storage_error_get_desc(FS_Error error_id) {
  489. return filesystem_api_error_get_desc(error_id);
  490. }
  491. FS_Error storage_file_get_error(File* file) {
  492. furi_check(file != NULL);
  493. return file->error_id;
  494. }
  495. int32_t storage_file_get_internal_error(File* file) {
  496. furi_check(file != NULL);
  497. return file->internal_error_id;
  498. }
  499. const char* storage_file_get_error_desc(File* file) {
  500. furi_check(file != NULL);
  501. return filesystem_api_error_get_desc(file->error_id);
  502. }
  503. /****************** Raw SD API ******************/
  504. FS_Error storage_sd_format(Storage* storage) {
  505. S_API_PROLOGUE;
  506. SAData data = {};
  507. S_API_MESSAGE(StorageCommandSDFormat);
  508. S_API_EPILOGUE;
  509. return S_RETURN_ERROR;
  510. }
  511. FS_Error storage_sd_unmount(Storage* storage) {
  512. S_API_PROLOGUE;
  513. SAData data = {};
  514. S_API_MESSAGE(StorageCommandSDUnmount);
  515. S_API_EPILOGUE;
  516. return S_RETURN_ERROR;
  517. }
  518. FS_Error storage_sd_info(Storage* storage, SDInfo* info) {
  519. S_API_PROLOGUE;
  520. SAData data = {
  521. .sdinfo = {
  522. .info = info,
  523. }};
  524. S_API_MESSAGE(StorageCommandSDInfo);
  525. S_API_EPILOGUE;
  526. return S_RETURN_ERROR;
  527. }
  528. FS_Error storage_sd_status(Storage* storage) {
  529. S_API_PROLOGUE;
  530. SAData data = {};
  531. S_API_MESSAGE(StorageCommandSDStatus);
  532. S_API_EPILOGUE;
  533. return S_RETURN_ERROR;
  534. }
  535. File* storage_file_alloc(Storage* storage) {
  536. File* file = malloc(sizeof(File));
  537. file->type = FileTypeClosed;
  538. file->storage = storage;
  539. FURI_LOG_T(TAG, "File/Dir %p alloc", (uint32_t)file - SRAM_BASE);
  540. return file;
  541. }
  542. bool storage_file_is_open(File* file) {
  543. return (file->type != FileTypeClosed);
  544. }
  545. bool storage_file_is_dir(File* file) {
  546. return (file->type == FileTypeOpenDir);
  547. }
  548. void storage_file_free(File* file) {
  549. if(storage_file_is_open(file)) {
  550. if(storage_file_is_dir(file)) {
  551. storage_dir_close(file);
  552. } else {
  553. storage_file_close(file);
  554. }
  555. }
  556. FURI_LOG_T(TAG, "File/Dir %p free", (uint32_t)file - SRAM_BASE);
  557. free(file);
  558. }
  559. FuriPubSub* storage_get_pubsub(Storage* storage) {
  560. return storage->pubsub;
  561. }
  562. bool storage_simply_remove_recursive(Storage* storage, const char* path) {
  563. furi_assert(storage);
  564. furi_assert(path);
  565. FileInfo fileinfo;
  566. bool result = false;
  567. string_t fullname;
  568. string_t cur_dir;
  569. if(storage_simply_remove(storage, path)) {
  570. return true;
  571. }
  572. char* name = malloc(MAX_NAME_LENGTH + 1);
  573. File* dir = storage_file_alloc(storage);
  574. string_init_set_str(cur_dir, path);
  575. bool go_deeper = false;
  576. while(1) {
  577. if(!storage_dir_open(dir, string_get_cstr(cur_dir))) {
  578. storage_dir_close(dir);
  579. break;
  580. }
  581. while(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  582. if(fileinfo.flags & FSF_DIRECTORY) {
  583. string_cat_printf(cur_dir, "/%s", name);
  584. go_deeper = true;
  585. break;
  586. }
  587. string_init_printf(fullname, "%s/%s", string_get_cstr(cur_dir), name);
  588. FS_Error error = storage_common_remove(storage, string_get_cstr(fullname));
  589. furi_check(error == FSE_OK);
  590. string_clear(fullname);
  591. }
  592. storage_dir_close(dir);
  593. if(go_deeper) {
  594. go_deeper = false;
  595. continue;
  596. }
  597. FS_Error error = storage_common_remove(storage, string_get_cstr(cur_dir));
  598. furi_check(error == FSE_OK);
  599. if(string_cmp(cur_dir, path)) {
  600. size_t last_char = string_search_rchar(cur_dir, '/');
  601. furi_assert(last_char != STRING_FAILURE);
  602. string_left(cur_dir, last_char);
  603. } else {
  604. result = true;
  605. break;
  606. }
  607. }
  608. storage_file_free(dir);
  609. string_clear(cur_dir);
  610. free(name);
  611. return result;
  612. }
  613. bool storage_simply_remove(Storage* storage, const char* path) {
  614. FS_Error result;
  615. result = storage_common_remove(storage, path);
  616. return result == FSE_OK || result == FSE_NOT_EXIST;
  617. }
  618. bool storage_simply_mkdir(Storage* storage, const char* path) {
  619. FS_Error result;
  620. result = storage_common_mkdir(storage, path);
  621. return result == FSE_OK || result == FSE_EXIST;
  622. }
  623. void storage_get_next_filename(
  624. Storage* storage,
  625. const char* dirname,
  626. const char* filename,
  627. const char* fileextension,
  628. string_t nextfilename,
  629. uint8_t max_len) {
  630. string_t temp_str;
  631. uint16_t num = 0;
  632. string_init_printf(temp_str, "%s/%s%s", dirname, filename, fileextension);
  633. while(storage_common_stat(storage, string_get_cstr(temp_str), NULL) == FSE_OK) {
  634. num++;
  635. string_printf(temp_str, "%s/%s%d%s", dirname, filename, num, fileextension);
  636. }
  637. if(num && (max_len > strlen(filename))) {
  638. string_printf(nextfilename, "%s%d", filename, num);
  639. } else {
  640. string_printf(nextfilename, "%s", filename);
  641. }
  642. string_clear(temp_str);
  643. }