storage_external_api.c 23 KB

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