storage_external_api.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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_INTERNAL;
  316. }
  317. }
  318. return error;
  319. }
  320. static FS_Error
  321. storage_copy_recursive(Storage* storage, const char* old_path, const char* new_path) {
  322. FS_Error error = storage_common_mkdir(storage, new_path);
  323. DirWalk* dir_walk = dir_walk_alloc(storage);
  324. FuriString* path;
  325. FuriString* tmp_new_path;
  326. FuriString* tmp_old_path;
  327. FileInfo fileinfo;
  328. path = furi_string_alloc();
  329. tmp_new_path = furi_string_alloc();
  330. tmp_old_path = furi_string_alloc();
  331. do {
  332. if(error != FSE_OK) break;
  333. if(!dir_walk_open(dir_walk, old_path)) {
  334. error = dir_walk_get_error(dir_walk);
  335. break;
  336. }
  337. while(1) {
  338. DirWalkResult res = dir_walk_read(dir_walk, path, &fileinfo);
  339. if(res == DirWalkError) {
  340. error = dir_walk_get_error(dir_walk);
  341. break;
  342. } else if(res == DirWalkLast) {
  343. break;
  344. } else {
  345. furi_string_set(tmp_old_path, path);
  346. furi_string_right(path, strlen(old_path));
  347. furi_string_printf(tmp_new_path, "%s%s", new_path, furi_string_get_cstr(path));
  348. if(fileinfo.flags & FSF_DIRECTORY) {
  349. error = storage_common_mkdir(storage, furi_string_get_cstr(tmp_new_path));
  350. } else {
  351. error = storage_common_copy(
  352. storage,
  353. furi_string_get_cstr(tmp_old_path),
  354. furi_string_get_cstr(tmp_new_path));
  355. }
  356. if(error != FSE_OK) break;
  357. }
  358. }
  359. } while(false);
  360. furi_string_free(tmp_new_path);
  361. furi_string_free(tmp_old_path);
  362. furi_string_free(path);
  363. dir_walk_free(dir_walk);
  364. return error;
  365. }
  366. FS_Error storage_common_copy(Storage* storage, const char* old_path, const char* new_path) {
  367. FS_Error error;
  368. FileInfo fileinfo;
  369. error = storage_common_stat(storage, old_path, &fileinfo);
  370. if(error == FSE_OK) {
  371. if(fileinfo.flags & FSF_DIRECTORY) {
  372. error = storage_copy_recursive(storage, old_path, new_path);
  373. } else {
  374. Stream* stream_from = file_stream_alloc(storage);
  375. Stream* stream_to = file_stream_alloc(storage);
  376. do {
  377. if(!file_stream_open(stream_from, old_path, FSAM_READ, FSOM_OPEN_EXISTING)) break;
  378. if(!file_stream_open(stream_to, new_path, FSAM_WRITE, FSOM_CREATE_NEW)) break;
  379. stream_copy_full(stream_from, stream_to);
  380. } while(false);
  381. error = file_stream_get_error(stream_from);
  382. if(error == FSE_OK) {
  383. error = file_stream_get_error(stream_to);
  384. }
  385. stream_free(stream_from);
  386. stream_free(stream_to);
  387. }
  388. }
  389. return error;
  390. }
  391. static FS_Error
  392. storage_merge_recursive(Storage* storage, const char* old_path, const char* new_path) {
  393. FS_Error error = storage_common_mkdir(storage, new_path);
  394. DirWalk* dir_walk = dir_walk_alloc(storage);
  395. FuriString *path, *file_basename, *tmp_new_path;
  396. FileInfo fileinfo;
  397. path = furi_string_alloc();
  398. file_basename = furi_string_alloc();
  399. tmp_new_path = furi_string_alloc();
  400. do {
  401. if((error != FSE_OK) && (error != FSE_EXIST)) break;
  402. dir_walk_set_recursive(dir_walk, false);
  403. if(!dir_walk_open(dir_walk, old_path)) {
  404. error = dir_walk_get_error(dir_walk);
  405. break;
  406. }
  407. while(1) {
  408. DirWalkResult res = dir_walk_read(dir_walk, path, &fileinfo);
  409. if(res == DirWalkError) {
  410. error = dir_walk_get_error(dir_walk);
  411. break;
  412. } else if(res == DirWalkLast) {
  413. break;
  414. } else {
  415. path_extract_basename(furi_string_get_cstr(path), file_basename);
  416. path_concat(new_path, furi_string_get_cstr(file_basename), tmp_new_path);
  417. if(fileinfo.flags & FSF_DIRECTORY) {
  418. if(storage_common_stat(
  419. storage, furi_string_get_cstr(tmp_new_path), &fileinfo) == FSE_OK) {
  420. if(fileinfo.flags & FSF_DIRECTORY) {
  421. error =
  422. storage_common_mkdir(storage, furi_string_get_cstr(tmp_new_path));
  423. if(error != FSE_OK) {
  424. break;
  425. }
  426. }
  427. }
  428. }
  429. error = storage_common_merge(
  430. storage, furi_string_get_cstr(path), furi_string_get_cstr(tmp_new_path));
  431. if(error != FSE_OK) {
  432. break;
  433. }
  434. }
  435. }
  436. } while(false);
  437. furi_string_free(tmp_new_path);
  438. furi_string_free(file_basename);
  439. furi_string_free(path);
  440. dir_walk_free(dir_walk);
  441. return error;
  442. }
  443. FS_Error storage_common_merge(Storage* storage, const char* old_path, const char* new_path) {
  444. FS_Error error;
  445. const char* new_path_tmp = NULL;
  446. FuriString* new_path_next = NULL;
  447. new_path_next = furi_string_alloc();
  448. FileInfo fileinfo;
  449. error = storage_common_stat(storage, old_path, &fileinfo);
  450. if(error == FSE_OK) {
  451. if(fileinfo.flags & FSF_DIRECTORY) {
  452. error = storage_merge_recursive(storage, old_path, new_path);
  453. } else {
  454. error = storage_common_stat(storage, new_path, &fileinfo);
  455. if(error == FSE_OK) {
  456. furi_string_set(new_path_next, new_path);
  457. FuriString* dir_path;
  458. FuriString* filename;
  459. char extension[MAX_EXT_LEN];
  460. dir_path = furi_string_alloc();
  461. filename = furi_string_alloc();
  462. path_extract_filename(new_path_next, filename, true);
  463. path_extract_dirname(new_path, dir_path);
  464. path_extract_extension(new_path_next, extension, MAX_EXT_LEN);
  465. storage_get_next_filename(
  466. storage,
  467. furi_string_get_cstr(dir_path),
  468. furi_string_get_cstr(filename),
  469. extension,
  470. new_path_next,
  471. 255);
  472. furi_string_cat_printf(
  473. dir_path, "/%s%s", furi_string_get_cstr(new_path_next), extension);
  474. furi_string_set(new_path_next, dir_path);
  475. furi_string_free(dir_path);
  476. furi_string_free(filename);
  477. new_path_tmp = furi_string_get_cstr(new_path_next);
  478. } else {
  479. new_path_tmp = new_path;
  480. }
  481. Stream* stream_from = file_stream_alloc(storage);
  482. Stream* stream_to = file_stream_alloc(storage);
  483. do {
  484. if(!file_stream_open(stream_from, old_path, FSAM_READ, FSOM_OPEN_EXISTING)) break;
  485. if(!file_stream_open(stream_to, new_path_tmp, FSAM_WRITE, FSOM_CREATE_NEW)) break;
  486. stream_copy_full(stream_from, stream_to);
  487. } while(false);
  488. error = file_stream_get_error(stream_from);
  489. if(error == FSE_OK) {
  490. error = file_stream_get_error(stream_to);
  491. }
  492. stream_free(stream_from);
  493. stream_free(stream_to);
  494. }
  495. }
  496. furi_string_free(new_path_next);
  497. return error;
  498. }
  499. FS_Error storage_common_mkdir(Storage* storage, const char* path) {
  500. S_API_PROLOGUE;
  501. S_API_DATA_PATH;
  502. S_API_MESSAGE(StorageCommandCommonMkDir);
  503. S_API_EPILOGUE;
  504. return S_RETURN_ERROR;
  505. }
  506. FS_Error storage_common_fs_info(
  507. Storage* storage,
  508. const char* fs_path,
  509. uint64_t* total_space,
  510. uint64_t* free_space) {
  511. S_API_PROLOGUE;
  512. SAData data = {
  513. .cfsinfo = {
  514. .fs_path = fs_path,
  515. .total_space = total_space,
  516. .free_space = free_space,
  517. }};
  518. S_API_MESSAGE(StorageCommandCommonFSInfo);
  519. S_API_EPILOGUE;
  520. return S_RETURN_ERROR;
  521. }
  522. /****************** ERROR ******************/
  523. const char* storage_error_get_desc(FS_Error error_id) {
  524. return filesystem_api_error_get_desc(error_id);
  525. }
  526. FS_Error storage_file_get_error(File* file) {
  527. furi_check(file != NULL);
  528. return file->error_id;
  529. }
  530. int32_t storage_file_get_internal_error(File* file) {
  531. furi_check(file != NULL);
  532. return file->internal_error_id;
  533. }
  534. const char* storage_file_get_error_desc(File* file) {
  535. furi_check(file != NULL);
  536. return filesystem_api_error_get_desc(file->error_id);
  537. }
  538. /****************** Raw SD API ******************/
  539. FS_Error storage_sd_format(Storage* storage) {
  540. S_API_PROLOGUE;
  541. SAData data = {};
  542. S_API_MESSAGE(StorageCommandSDFormat);
  543. S_API_EPILOGUE;
  544. return S_RETURN_ERROR;
  545. }
  546. FS_Error storage_sd_unmount(Storage* storage) {
  547. S_API_PROLOGUE;
  548. SAData data = {};
  549. S_API_MESSAGE(StorageCommandSDUnmount);
  550. S_API_EPILOGUE;
  551. return S_RETURN_ERROR;
  552. }
  553. FS_Error storage_sd_info(Storage* storage, SDInfo* info) {
  554. S_API_PROLOGUE;
  555. SAData data = {
  556. .sdinfo = {
  557. .info = info,
  558. }};
  559. S_API_MESSAGE(StorageCommandSDInfo);
  560. S_API_EPILOGUE;
  561. return S_RETURN_ERROR;
  562. }
  563. FS_Error storage_sd_status(Storage* storage) {
  564. S_API_PROLOGUE;
  565. SAData data = {};
  566. S_API_MESSAGE(StorageCommandSDStatus);
  567. S_API_EPILOGUE;
  568. return S_RETURN_ERROR;
  569. }
  570. File* storage_file_alloc(Storage* storage) {
  571. File* file = malloc(sizeof(File));
  572. file->type = FileTypeClosed;
  573. file->storage = storage;
  574. FURI_LOG_T(TAG, "File/Dir %p alloc", (void*)((uint32_t)file - SRAM_BASE));
  575. return file;
  576. }
  577. bool storage_file_is_open(File* file) {
  578. return (file->type != FileTypeClosed);
  579. }
  580. bool storage_file_is_dir(File* file) {
  581. return (file->type == FileTypeOpenDir);
  582. }
  583. void storage_file_free(File* file) {
  584. if(storage_file_is_open(file)) {
  585. if(storage_file_is_dir(file)) {
  586. storage_dir_close(file);
  587. } else {
  588. storage_file_close(file);
  589. }
  590. }
  591. FURI_LOG_T(TAG, "File/Dir %p free", (void*)((uint32_t)file - SRAM_BASE));
  592. free(file);
  593. }
  594. FuriPubSub* storage_get_pubsub(Storage* storage) {
  595. return storage->pubsub;
  596. }
  597. bool storage_simply_remove_recursive(Storage* storage, const char* path) {
  598. furi_assert(storage);
  599. furi_assert(path);
  600. FileInfo fileinfo;
  601. bool result = false;
  602. FuriString* fullname;
  603. FuriString* cur_dir;
  604. if(storage_simply_remove(storage, path)) {
  605. return true;
  606. }
  607. char* name = malloc(MAX_NAME_LENGTH + 1); //-V799
  608. File* dir = storage_file_alloc(storage);
  609. cur_dir = furi_string_alloc_set(path);
  610. bool go_deeper = false;
  611. while(1) {
  612. if(!storage_dir_open(dir, furi_string_get_cstr(cur_dir))) {
  613. storage_dir_close(dir);
  614. break;
  615. }
  616. while(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  617. if(fileinfo.flags & FSF_DIRECTORY) {
  618. furi_string_cat_printf(cur_dir, "/%s", name);
  619. go_deeper = true;
  620. break;
  621. }
  622. fullname = furi_string_alloc_printf("%s/%s", furi_string_get_cstr(cur_dir), name);
  623. FS_Error error = storage_common_remove(storage, furi_string_get_cstr(fullname));
  624. furi_check(error == FSE_OK);
  625. furi_string_free(fullname);
  626. }
  627. storage_dir_close(dir);
  628. if(go_deeper) {
  629. go_deeper = false;
  630. continue;
  631. }
  632. FS_Error error = storage_common_remove(storage, furi_string_get_cstr(cur_dir));
  633. furi_check(error == FSE_OK);
  634. if(furi_string_cmp(cur_dir, path)) {
  635. size_t last_char = furi_string_search_rchar(cur_dir, '/');
  636. furi_assert(last_char != FURI_STRING_FAILURE);
  637. furi_string_left(cur_dir, last_char);
  638. } else {
  639. result = true;
  640. break;
  641. }
  642. }
  643. storage_file_free(dir);
  644. furi_string_free(cur_dir);
  645. free(name);
  646. return result;
  647. } //-V773
  648. bool storage_simply_remove(Storage* storage, const char* path) {
  649. FS_Error result;
  650. result = storage_common_remove(storage, path);
  651. return result == FSE_OK || result == FSE_NOT_EXIST;
  652. }
  653. bool storage_simply_mkdir(Storage* storage, const char* path) {
  654. FS_Error result;
  655. result = storage_common_mkdir(storage, path);
  656. return result == FSE_OK || result == FSE_EXIST;
  657. }
  658. void storage_get_next_filename(
  659. Storage* storage,
  660. const char* dirname,
  661. const char* filename,
  662. const char* fileextension,
  663. FuriString* nextfilename,
  664. uint8_t max_len) {
  665. FuriString* temp_str;
  666. uint16_t num = 0;
  667. temp_str = furi_string_alloc_printf("%s/%s%s", dirname, filename, fileextension);
  668. while(storage_common_stat(storage, furi_string_get_cstr(temp_str), NULL) == FSE_OK) {
  669. num++;
  670. furi_string_printf(temp_str, "%s/%s%d%s", dirname, filename, num, fileextension);
  671. }
  672. if(num && (max_len > strlen(filename))) {
  673. furi_string_printf(nextfilename, "%s%d", filename, num);
  674. } else {
  675. furi_string_printf(nextfilename, "%s", filename);
  676. }
  677. furi_string_free(temp_str);
  678. }