storage_external_api.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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, file_basename, tmp_new_path;
  365. FileInfo fileinfo;
  366. string_init(path);
  367. string_init(file_basename);
  368. string_init(tmp_new_path);
  369. do {
  370. if((error != FSE_OK) && (error != FSE_EXIST)) break;
  371. dir_walk_set_recursive(dir_walk, false);
  372. if(!dir_walk_open(dir_walk, old_path)) {
  373. error = dir_walk_get_error(dir_walk);
  374. break;
  375. }
  376. while(1) {
  377. DirWalkResult res = dir_walk_read(dir_walk, path, &fileinfo);
  378. if(res == DirWalkError) {
  379. error = dir_walk_get_error(dir_walk);
  380. break;
  381. } else if(res == DirWalkLast) {
  382. break;
  383. } else {
  384. path_extract_basename(string_get_cstr(path), file_basename);
  385. path_concat(new_path, string_get_cstr(file_basename), tmp_new_path);
  386. if(fileinfo.flags & FSF_DIRECTORY) {
  387. if(storage_common_stat(storage, string_get_cstr(tmp_new_path), &fileinfo) ==
  388. FSE_OK) {
  389. if(fileinfo.flags & FSF_DIRECTORY) {
  390. error = storage_common_mkdir(storage, string_get_cstr(tmp_new_path));
  391. if(error != FSE_OK) {
  392. break;
  393. }
  394. }
  395. }
  396. }
  397. error = storage_common_merge(
  398. storage, string_get_cstr(path), string_get_cstr(tmp_new_path));
  399. if(error != FSE_OK) {
  400. break;
  401. }
  402. }
  403. }
  404. } while(false);
  405. string_clear(tmp_new_path);
  406. string_clear(file_basename);
  407. string_clear(path);
  408. dir_walk_free(dir_walk);
  409. return error;
  410. }
  411. FS_Error storage_common_merge(Storage* storage, const char* old_path, const char* new_path) {
  412. FS_Error error;
  413. const char* new_path_tmp;
  414. string_t new_path_next;
  415. string_init(new_path_next);
  416. FileInfo fileinfo;
  417. error = storage_common_stat(storage, old_path, &fileinfo);
  418. if(error == FSE_OK) {
  419. if(fileinfo.flags & FSF_DIRECTORY) {
  420. error = storage_merge_recursive(storage, old_path, new_path);
  421. } else {
  422. error = storage_common_stat(storage, new_path, &fileinfo);
  423. if(error == FSE_OK) {
  424. string_set_str(new_path_next, new_path);
  425. string_t dir_path;
  426. string_t filename;
  427. char extension[MAX_EXT_LEN];
  428. string_init(dir_path);
  429. string_init(filename);
  430. path_extract_filename(new_path_next, filename, true);
  431. path_extract_dirname(new_path, dir_path);
  432. path_extract_extension(new_path_next, extension, MAX_EXT_LEN);
  433. storage_get_next_filename(
  434. storage,
  435. string_get_cstr(dir_path),
  436. string_get_cstr(filename),
  437. extension,
  438. new_path_next,
  439. 255);
  440. string_cat_printf(dir_path, "/%s%s", string_get_cstr(new_path_next), extension);
  441. string_set(new_path_next, dir_path);
  442. string_clear(dir_path);
  443. string_clear(filename);
  444. new_path_tmp = string_get_cstr(new_path_next);
  445. } else {
  446. new_path_tmp = new_path;
  447. }
  448. Stream* stream_from = file_stream_alloc(storage);
  449. Stream* stream_to = file_stream_alloc(storage);
  450. do {
  451. if(!file_stream_open(stream_from, old_path, FSAM_READ, FSOM_OPEN_EXISTING)) break;
  452. if(!file_stream_open(stream_to, new_path_tmp, FSAM_WRITE, FSOM_CREATE_NEW)) break;
  453. stream_copy_full(stream_from, stream_to);
  454. } while(false);
  455. error = file_stream_get_error(stream_from);
  456. if(error == FSE_OK) {
  457. error = file_stream_get_error(stream_to);
  458. }
  459. stream_free(stream_from);
  460. stream_free(stream_to);
  461. }
  462. }
  463. string_clear(new_path_next);
  464. return error;
  465. }
  466. FS_Error storage_common_mkdir(Storage* storage, const char* path) {
  467. S_API_PROLOGUE;
  468. S_API_DATA_PATH;
  469. S_API_MESSAGE(StorageCommandCommonMkDir);
  470. S_API_EPILOGUE;
  471. return S_RETURN_ERROR;
  472. }
  473. FS_Error storage_common_fs_info(
  474. Storage* storage,
  475. const char* fs_path,
  476. uint64_t* total_space,
  477. uint64_t* free_space) {
  478. S_API_PROLOGUE;
  479. SAData data = {
  480. .cfsinfo = {
  481. .fs_path = fs_path,
  482. .total_space = total_space,
  483. .free_space = free_space,
  484. }};
  485. S_API_MESSAGE(StorageCommandCommonFSInfo);
  486. S_API_EPILOGUE;
  487. return S_RETURN_ERROR;
  488. }
  489. /****************** ERROR ******************/
  490. const char* storage_error_get_desc(FS_Error error_id) {
  491. return filesystem_api_error_get_desc(error_id);
  492. }
  493. FS_Error storage_file_get_error(File* file) {
  494. furi_check(file != NULL);
  495. return file->error_id;
  496. }
  497. int32_t storage_file_get_internal_error(File* file) {
  498. furi_check(file != NULL);
  499. return file->internal_error_id;
  500. }
  501. const char* storage_file_get_error_desc(File* file) {
  502. furi_check(file != NULL);
  503. return filesystem_api_error_get_desc(file->error_id);
  504. }
  505. /****************** Raw SD API ******************/
  506. FS_Error storage_sd_format(Storage* storage) {
  507. S_API_PROLOGUE;
  508. SAData data = {};
  509. S_API_MESSAGE(StorageCommandSDFormat);
  510. S_API_EPILOGUE;
  511. return S_RETURN_ERROR;
  512. }
  513. FS_Error storage_sd_unmount(Storage* storage) {
  514. S_API_PROLOGUE;
  515. SAData data = {};
  516. S_API_MESSAGE(StorageCommandSDUnmount);
  517. S_API_EPILOGUE;
  518. return S_RETURN_ERROR;
  519. }
  520. FS_Error storage_sd_info(Storage* storage, SDInfo* info) {
  521. S_API_PROLOGUE;
  522. SAData data = {
  523. .sdinfo = {
  524. .info = info,
  525. }};
  526. S_API_MESSAGE(StorageCommandSDInfo);
  527. S_API_EPILOGUE;
  528. return S_RETURN_ERROR;
  529. }
  530. FS_Error storage_sd_status(Storage* storage) {
  531. S_API_PROLOGUE;
  532. SAData data = {};
  533. S_API_MESSAGE(StorageCommandSDStatus);
  534. S_API_EPILOGUE;
  535. return S_RETURN_ERROR;
  536. }
  537. File* storage_file_alloc(Storage* storage) {
  538. File* file = malloc(sizeof(File));
  539. file->type = FileTypeClosed;
  540. file->storage = storage;
  541. FURI_LOG_T(TAG, "File/Dir %p alloc", (uint32_t)file - SRAM_BASE);
  542. return file;
  543. }
  544. bool storage_file_is_open(File* file) {
  545. return (file->type != FileTypeClosed);
  546. }
  547. bool storage_file_is_dir(File* file) {
  548. return (file->type == FileTypeOpenDir);
  549. }
  550. void storage_file_free(File* file) {
  551. if(storage_file_is_open(file)) {
  552. if(storage_file_is_dir(file)) {
  553. storage_dir_close(file);
  554. } else {
  555. storage_file_close(file);
  556. }
  557. }
  558. FURI_LOG_T(TAG, "File/Dir %p free", (uint32_t)file - SRAM_BASE);
  559. free(file);
  560. }
  561. FuriPubSub* storage_get_pubsub(Storage* storage) {
  562. return storage->pubsub;
  563. }
  564. bool storage_simply_remove_recursive(Storage* storage, const char* path) {
  565. furi_assert(storage);
  566. furi_assert(path);
  567. FileInfo fileinfo;
  568. bool result = false;
  569. string_t fullname;
  570. string_t cur_dir;
  571. if(storage_simply_remove(storage, path)) {
  572. return true;
  573. }
  574. char* name = malloc(MAX_NAME_LENGTH + 1);
  575. File* dir = storage_file_alloc(storage);
  576. string_init_set_str(cur_dir, path);
  577. bool go_deeper = false;
  578. while(1) {
  579. if(!storage_dir_open(dir, string_get_cstr(cur_dir))) {
  580. storage_dir_close(dir);
  581. break;
  582. }
  583. while(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  584. if(fileinfo.flags & FSF_DIRECTORY) {
  585. string_cat_printf(cur_dir, "/%s", name);
  586. go_deeper = true;
  587. break;
  588. }
  589. string_init_printf(fullname, "%s/%s", string_get_cstr(cur_dir), name);
  590. FS_Error error = storage_common_remove(storage, string_get_cstr(fullname));
  591. furi_check(error == FSE_OK);
  592. string_clear(fullname);
  593. }
  594. storage_dir_close(dir);
  595. if(go_deeper) {
  596. go_deeper = false;
  597. continue;
  598. }
  599. FS_Error error = storage_common_remove(storage, string_get_cstr(cur_dir));
  600. furi_check(error == FSE_OK);
  601. if(string_cmp(cur_dir, path)) {
  602. size_t last_char = string_search_rchar(cur_dir, '/');
  603. furi_assert(last_char != STRING_FAILURE);
  604. string_left(cur_dir, last_char);
  605. } else {
  606. result = true;
  607. break;
  608. }
  609. }
  610. storage_file_free(dir);
  611. string_clear(cur_dir);
  612. free(name);
  613. return result;
  614. }
  615. bool storage_simply_remove(Storage* storage, const char* path) {
  616. FS_Error result;
  617. result = storage_common_remove(storage, path);
  618. return result == FSE_OK || result == FSE_NOT_EXIST;
  619. }
  620. bool storage_simply_mkdir(Storage* storage, const char* path) {
  621. FS_Error result;
  622. result = storage_common_mkdir(storage, path);
  623. return result == FSE_OK || result == FSE_EXIST;
  624. }
  625. void storage_get_next_filename(
  626. Storage* storage,
  627. const char* dirname,
  628. const char* filename,
  629. const char* fileextension,
  630. string_t nextfilename,
  631. uint8_t max_len) {
  632. string_t temp_str;
  633. uint16_t num = 0;
  634. string_init_printf(temp_str, "%s/%s%s", dirname, filename, fileextension);
  635. while(storage_common_stat(storage, string_get_cstr(temp_str), NULL) == FSE_OK) {
  636. num++;
  637. string_printf(temp_str, "%s/%s%d%s", dirname, filename, num, fileextension);
  638. }
  639. if(num && (max_len > strlen(filename))) {
  640. string_printf(nextfilename, "%s%d", filename, num);
  641. } else {
  642. string_printf(nextfilename, "%s", filename);
  643. }
  644. string_clear(temp_str);
  645. }