storage_external_api.c 23 KB

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