storage_external_api.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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_RETURN_BOOL (return_data.bool_value);
  35. #define S_RETURN_UINT16 (return_data.uint16_value);
  36. #define S_RETURN_UINT64 (return_data.uint64_value);
  37. #define S_RETURN_ERROR (return_data.error_value);
  38. #define S_RETURN_CSTRING (return_data.cstring_value);
  39. typedef enum {
  40. StorageEventFlagFileClose = (1 << 0),
  41. } StorageEventFlag;
  42. /****************** FILE ******************/
  43. static bool storage_file_open_internal(
  44. File* file,
  45. const char* path,
  46. FS_AccessMode access_mode,
  47. FS_OpenMode open_mode) {
  48. S_FILE_API_PROLOGUE;
  49. S_API_PROLOGUE;
  50. SAData data = {
  51. .fopen = {
  52. .file = file,
  53. .path = path,
  54. .access_mode = access_mode,
  55. .open_mode = open_mode,
  56. .thread_id = furi_thread_get_current_id(),
  57. }};
  58. file->type = FileTypeOpenFile;
  59. S_API_MESSAGE(StorageCommandFileOpen);
  60. S_API_EPILOGUE;
  61. return S_RETURN_BOOL;
  62. }
  63. static void storage_file_close_callback(const void* message, void* context) {
  64. const StorageEvent* storage_event = message;
  65. if(storage_event->type == StorageEventTypeFileClose ||
  66. storage_event->type == StorageEventTypeDirClose) {
  67. furi_assert(context);
  68. FuriEventFlag* event = context;
  69. furi_event_flag_set(event, StorageEventFlagFileClose);
  70. }
  71. }
  72. bool storage_file_open(
  73. File* file,
  74. const char* path,
  75. FS_AccessMode access_mode,
  76. FS_OpenMode open_mode) {
  77. bool result;
  78. FuriEventFlag* event = furi_event_flag_alloc();
  79. FuriPubSubSubscription* subscription = furi_pubsub_subscribe(
  80. storage_get_pubsub(file->storage), storage_file_close_callback, event);
  81. do {
  82. result = storage_file_open_internal(file, path, access_mode, open_mode);
  83. if(!result && file->error_id == FSE_ALREADY_OPEN) {
  84. furi_event_flag_wait(
  85. event, StorageEventFlagFileClose, FuriFlagWaitAny, FuriWaitForever);
  86. } else {
  87. break;
  88. }
  89. } while(true);
  90. furi_pubsub_unsubscribe(storage_get_pubsub(file->storage), subscription);
  91. furi_event_flag_free(event);
  92. FURI_LOG_T(
  93. TAG,
  94. "File %p - %p open (%s)",
  95. (void*)((uint32_t)file - SRAM_BASE),
  96. (void*)(file->file_id - SRAM_BASE),
  97. path);
  98. return result;
  99. }
  100. bool storage_file_close(File* file) {
  101. S_FILE_API_PROLOGUE;
  102. S_API_PROLOGUE;
  103. S_API_DATA_FILE;
  104. S_API_MESSAGE(StorageCommandFileClose);
  105. S_API_EPILOGUE;
  106. FURI_LOG_T(
  107. TAG,
  108. "File %p - %p closed",
  109. (void*)((uint32_t)file - SRAM_BASE),
  110. (void*)(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. bool storage_file_exists(Storage* storage, const char* path) {
  200. bool exist = false;
  201. FileInfo fileinfo;
  202. FS_Error error = storage_common_stat(storage, path, &fileinfo);
  203. if(error == FSE_OK && !file_info_is_dir(&fileinfo)) {
  204. exist = true;
  205. }
  206. return exist;
  207. }
  208. /****************** DIR ******************/
  209. static bool storage_dir_open_internal(File* file, const char* path) {
  210. S_FILE_API_PROLOGUE;
  211. S_API_PROLOGUE;
  212. SAData data = {
  213. .dopen = {
  214. .file = file,
  215. .path = path,
  216. .thread_id = furi_thread_get_current_id(),
  217. }};
  218. file->type = FileTypeOpenDir;
  219. S_API_MESSAGE(StorageCommandDirOpen);
  220. S_API_EPILOGUE;
  221. return S_RETURN_BOOL;
  222. }
  223. bool storage_dir_open(File* file, const char* path) {
  224. bool result;
  225. FuriEventFlag* event = furi_event_flag_alloc();
  226. FuriPubSubSubscription* subscription = furi_pubsub_subscribe(
  227. storage_get_pubsub(file->storage), storage_file_close_callback, event);
  228. do {
  229. result = storage_dir_open_internal(file, path);
  230. if(!result && file->error_id == FSE_ALREADY_OPEN) {
  231. furi_event_flag_wait(
  232. event, StorageEventFlagFileClose, FuriFlagWaitAny, FuriWaitForever);
  233. } else {
  234. break;
  235. }
  236. } while(true);
  237. furi_pubsub_unsubscribe(storage_get_pubsub(file->storage), subscription);
  238. furi_event_flag_free(event);
  239. FURI_LOG_T(
  240. TAG,
  241. "Dir %p - %p open (%s)",
  242. (void*)((uint32_t)file - SRAM_BASE),
  243. (void*)(file->file_id - SRAM_BASE),
  244. path);
  245. return result;
  246. }
  247. bool storage_dir_close(File* file) {
  248. S_FILE_API_PROLOGUE;
  249. S_API_PROLOGUE;
  250. S_API_DATA_FILE;
  251. S_API_MESSAGE(StorageCommandDirClose);
  252. S_API_EPILOGUE;
  253. FURI_LOG_T(
  254. TAG,
  255. "Dir %p - %p closed",
  256. (void*)((uint32_t)file - SRAM_BASE),
  257. (void*)(file->file_id - SRAM_BASE));
  258. file->type = FileTypeClosed;
  259. return S_RETURN_BOOL;
  260. }
  261. bool storage_dir_read(File* file, FileInfo* fileinfo, char* name, uint16_t name_length) {
  262. S_FILE_API_PROLOGUE;
  263. S_API_PROLOGUE;
  264. SAData data = {
  265. .dread = {
  266. .file = file,
  267. .fileinfo = fileinfo,
  268. .name = name,
  269. .name_length = name_length,
  270. }};
  271. S_API_MESSAGE(StorageCommandDirRead);
  272. S_API_EPILOGUE;
  273. return S_RETURN_BOOL;
  274. }
  275. bool storage_dir_rewind(File* file) {
  276. S_FILE_API_PROLOGUE;
  277. S_API_PROLOGUE;
  278. S_API_DATA_FILE;
  279. S_API_MESSAGE(StorageCommandDirRewind);
  280. S_API_EPILOGUE;
  281. return S_RETURN_BOOL;
  282. }
  283. bool storage_dir_exists(Storage* storage, const char* path) {
  284. bool exist = false;
  285. FileInfo fileinfo;
  286. FS_Error error = storage_common_stat(storage, path, &fileinfo);
  287. if(error == FSE_OK && file_info_is_dir(&fileinfo)) {
  288. exist = true;
  289. }
  290. return exist;
  291. }
  292. /****************** COMMON ******************/
  293. FS_Error storage_common_timestamp(Storage* storage, const char* path, uint32_t* timestamp) {
  294. S_API_PROLOGUE;
  295. SAData data = {
  296. .ctimestamp = {
  297. .path = path,
  298. .timestamp = timestamp,
  299. .thread_id = furi_thread_get_current_id(),
  300. }};
  301. S_API_MESSAGE(StorageCommandCommonTimestamp);
  302. S_API_EPILOGUE;
  303. return S_RETURN_ERROR;
  304. }
  305. FS_Error storage_common_stat(Storage* storage, const char* path, FileInfo* fileinfo) {
  306. S_API_PROLOGUE;
  307. SAData data = {
  308. .cstat = {
  309. .path = path,
  310. .fileinfo = fileinfo,
  311. .thread_id = furi_thread_get_current_id(),
  312. }};
  313. S_API_MESSAGE(StorageCommandCommonStat);
  314. S_API_EPILOGUE;
  315. return S_RETURN_ERROR;
  316. }
  317. FS_Error storage_common_remove(Storage* storage, const char* path) {
  318. S_API_PROLOGUE;
  319. SAData data = {
  320. .path = {
  321. .path = path,
  322. .thread_id = furi_thread_get_current_id(),
  323. }};
  324. S_API_MESSAGE(StorageCommandCommonRemove);
  325. S_API_EPILOGUE;
  326. return S_RETURN_ERROR;
  327. }
  328. FS_Error storage_common_rename(Storage* storage, const char* old_path, const char* new_path) {
  329. FS_Error error = storage_common_copy(storage, old_path, new_path);
  330. if(error == FSE_OK) {
  331. if(!storage_simply_remove_recursive(storage, old_path)) {
  332. error = FSE_INTERNAL;
  333. }
  334. }
  335. return error;
  336. }
  337. static FS_Error
  338. storage_copy_recursive(Storage* storage, const char* old_path, const char* new_path) {
  339. FS_Error error = storage_common_mkdir(storage, new_path);
  340. DirWalk* dir_walk = dir_walk_alloc(storage);
  341. FuriString* path;
  342. FuriString* tmp_new_path;
  343. FuriString* tmp_old_path;
  344. FileInfo fileinfo;
  345. path = furi_string_alloc();
  346. tmp_new_path = furi_string_alloc();
  347. tmp_old_path = furi_string_alloc();
  348. do {
  349. if(error != FSE_OK) break;
  350. if(!dir_walk_open(dir_walk, old_path)) {
  351. error = dir_walk_get_error(dir_walk);
  352. break;
  353. }
  354. while(1) {
  355. DirWalkResult res = dir_walk_read(dir_walk, path, &fileinfo);
  356. if(res == DirWalkError) {
  357. error = dir_walk_get_error(dir_walk);
  358. break;
  359. } else if(res == DirWalkLast) {
  360. break;
  361. } else {
  362. furi_string_set(tmp_old_path, path);
  363. furi_string_right(path, strlen(old_path));
  364. furi_string_printf(tmp_new_path, "%s%s", new_path, furi_string_get_cstr(path));
  365. if(file_info_is_dir(&fileinfo)) {
  366. error = storage_common_mkdir(storage, furi_string_get_cstr(tmp_new_path));
  367. } else {
  368. error = storage_common_copy(
  369. storage,
  370. furi_string_get_cstr(tmp_old_path),
  371. furi_string_get_cstr(tmp_new_path));
  372. }
  373. if(error != FSE_OK) break;
  374. }
  375. }
  376. } while(false);
  377. furi_string_free(tmp_new_path);
  378. furi_string_free(tmp_old_path);
  379. furi_string_free(path);
  380. dir_walk_free(dir_walk);
  381. return error;
  382. }
  383. FS_Error storage_common_copy(Storage* storage, const char* old_path, const char* new_path) {
  384. FS_Error error;
  385. FileInfo fileinfo;
  386. error = storage_common_stat(storage, old_path, &fileinfo);
  387. if(error == FSE_OK) {
  388. if(file_info_is_dir(&fileinfo)) {
  389. error = storage_copy_recursive(storage, old_path, new_path);
  390. } else {
  391. Stream* stream_from = file_stream_alloc(storage);
  392. Stream* stream_to = file_stream_alloc(storage);
  393. do {
  394. if(!file_stream_open(stream_from, old_path, FSAM_READ, FSOM_OPEN_EXISTING)) break;
  395. if(!file_stream_open(stream_to, new_path, FSAM_WRITE, FSOM_CREATE_NEW)) break;
  396. stream_copy_full(stream_from, stream_to);
  397. } while(false);
  398. error = file_stream_get_error(stream_from);
  399. if(error == FSE_OK) {
  400. error = file_stream_get_error(stream_to);
  401. }
  402. stream_free(stream_from);
  403. stream_free(stream_to);
  404. }
  405. }
  406. return error;
  407. }
  408. static FS_Error
  409. storage_merge_recursive(Storage* storage, const char* old_path, const char* new_path) {
  410. FS_Error error = FSE_OK;
  411. DirWalk* dir_walk = dir_walk_alloc(storage);
  412. FuriString *path, *file_basename, *tmp_new_path;
  413. FileInfo fileinfo;
  414. path = furi_string_alloc();
  415. file_basename = furi_string_alloc();
  416. tmp_new_path = furi_string_alloc();
  417. do {
  418. if(!storage_simply_mkdir(storage, new_path)) break;
  419. dir_walk_set_recursive(dir_walk, false);
  420. if(!dir_walk_open(dir_walk, old_path)) {
  421. error = dir_walk_get_error(dir_walk);
  422. break;
  423. }
  424. while(1) {
  425. DirWalkResult res = dir_walk_read(dir_walk, path, &fileinfo);
  426. if(res == DirWalkError) {
  427. error = dir_walk_get_error(dir_walk);
  428. break;
  429. } else if(res == DirWalkLast) {
  430. break;
  431. } else {
  432. path_extract_basename(furi_string_get_cstr(path), file_basename);
  433. path_concat(new_path, furi_string_get_cstr(file_basename), tmp_new_path);
  434. if(file_info_is_dir(&fileinfo)) {
  435. if(storage_common_stat(
  436. storage, furi_string_get_cstr(tmp_new_path), &fileinfo) == FSE_OK) {
  437. if(file_info_is_dir(&fileinfo)) {
  438. error =
  439. storage_common_mkdir(storage, furi_string_get_cstr(tmp_new_path));
  440. if(error != FSE_OK && error != FSE_EXIST) {
  441. break;
  442. }
  443. }
  444. }
  445. }
  446. error = storage_common_merge(
  447. storage, furi_string_get_cstr(path), furi_string_get_cstr(tmp_new_path));
  448. if(error != FSE_OK) {
  449. break;
  450. }
  451. }
  452. }
  453. } while(false);
  454. furi_string_free(tmp_new_path);
  455. furi_string_free(file_basename);
  456. furi_string_free(path);
  457. dir_walk_free(dir_walk);
  458. return error;
  459. }
  460. FS_Error storage_common_merge(Storage* storage, const char* old_path, const char* new_path) {
  461. FS_Error error;
  462. const char* new_path_tmp = NULL;
  463. FuriString* new_path_next = NULL;
  464. new_path_next = furi_string_alloc();
  465. FileInfo fileinfo;
  466. error = storage_common_stat(storage, old_path, &fileinfo);
  467. if(error == FSE_OK) {
  468. if(file_info_is_dir(&fileinfo)) {
  469. error = storage_merge_recursive(storage, old_path, new_path);
  470. } else {
  471. error = storage_common_stat(storage, new_path, &fileinfo);
  472. if(error == FSE_OK) {
  473. furi_string_set(new_path_next, new_path);
  474. FuriString* dir_path;
  475. FuriString* filename;
  476. char extension[MAX_EXT_LEN] = {0};
  477. dir_path = furi_string_alloc();
  478. filename = furi_string_alloc();
  479. path_extract_filename(new_path_next, filename, true);
  480. path_extract_dirname(new_path, dir_path);
  481. path_extract_extension(new_path_next, extension, MAX_EXT_LEN);
  482. storage_get_next_filename(
  483. storage,
  484. furi_string_get_cstr(dir_path),
  485. furi_string_get_cstr(filename),
  486. extension,
  487. new_path_next,
  488. 255);
  489. furi_string_cat_printf(
  490. dir_path, "/%s%s", furi_string_get_cstr(new_path_next), extension);
  491. furi_string_set(new_path_next, dir_path);
  492. furi_string_free(dir_path);
  493. furi_string_free(filename);
  494. new_path_tmp = furi_string_get_cstr(new_path_next);
  495. } else {
  496. new_path_tmp = new_path;
  497. }
  498. Stream* stream_from = file_stream_alloc(storage);
  499. Stream* stream_to = file_stream_alloc(storage);
  500. do {
  501. if(!file_stream_open(stream_from, old_path, FSAM_READ, FSOM_OPEN_EXISTING)) break;
  502. if(!file_stream_open(stream_to, new_path_tmp, FSAM_WRITE, FSOM_CREATE_NEW)) break;
  503. stream_copy_full(stream_from, stream_to);
  504. } while(false);
  505. error = file_stream_get_error(stream_from);
  506. if(error == FSE_OK) {
  507. error = file_stream_get_error(stream_to);
  508. }
  509. stream_free(stream_from);
  510. stream_free(stream_to);
  511. }
  512. }
  513. furi_string_free(new_path_next);
  514. return error;
  515. }
  516. FS_Error storage_common_mkdir(Storage* storage, const char* path) {
  517. S_API_PROLOGUE;
  518. SAData data = {
  519. .path = {
  520. .path = path,
  521. .thread_id = furi_thread_get_current_id(),
  522. }};
  523. S_API_MESSAGE(StorageCommandCommonMkDir);
  524. S_API_EPILOGUE;
  525. return S_RETURN_ERROR;
  526. }
  527. FS_Error storage_common_fs_info(
  528. Storage* storage,
  529. const char* fs_path,
  530. uint64_t* total_space,
  531. uint64_t* free_space) {
  532. S_API_PROLOGUE;
  533. SAData data = {
  534. .cfsinfo = {
  535. .fs_path = fs_path,
  536. .total_space = total_space,
  537. .free_space = free_space,
  538. .thread_id = furi_thread_get_current_id(),
  539. }};
  540. S_API_MESSAGE(StorageCommandCommonFSInfo);
  541. S_API_EPILOGUE;
  542. return S_RETURN_ERROR;
  543. }
  544. void storage_common_resolve_path_and_ensure_app_directory(Storage* storage, FuriString* path) {
  545. S_API_PROLOGUE;
  546. SAData data = {
  547. .cresolvepath = {
  548. .path = path,
  549. .thread_id = furi_thread_get_current_id(),
  550. }};
  551. S_API_MESSAGE(StorageCommandCommonResolvePath);
  552. S_API_EPILOGUE;
  553. }
  554. FS_Error storage_common_migrate(Storage* storage, const char* source, const char* dest) {
  555. if(!storage_common_exists(storage, source)) {
  556. return FSE_OK;
  557. }
  558. FS_Error error = storage_common_merge(storage, source, dest);
  559. if(error == FSE_OK) {
  560. storage_simply_remove_recursive(storage, source);
  561. }
  562. return error;
  563. }
  564. bool storage_common_exists(Storage* storage, const char* path) {
  565. FileInfo file_info;
  566. return storage_common_stat(storage, path, &file_info) == FSE_OK;
  567. }
  568. /****************** ERROR ******************/
  569. const char* storage_error_get_desc(FS_Error error_id) {
  570. return filesystem_api_error_get_desc(error_id);
  571. }
  572. FS_Error storage_file_get_error(File* file) {
  573. furi_check(file != NULL);
  574. return file->error_id;
  575. }
  576. int32_t storage_file_get_internal_error(File* file) {
  577. furi_check(file != NULL);
  578. return file->internal_error_id;
  579. }
  580. const char* storage_file_get_error_desc(File* file) {
  581. furi_check(file != NULL);
  582. return filesystem_api_error_get_desc(file->error_id);
  583. }
  584. /****************** Raw SD API ******************/
  585. FS_Error storage_sd_format(Storage* storage) {
  586. S_API_PROLOGUE;
  587. SAData data = {};
  588. S_API_MESSAGE(StorageCommandSDFormat);
  589. S_API_EPILOGUE;
  590. return S_RETURN_ERROR;
  591. }
  592. FS_Error storage_sd_unmount(Storage* storage) {
  593. S_API_PROLOGUE;
  594. SAData data = {};
  595. S_API_MESSAGE(StorageCommandSDUnmount);
  596. S_API_EPILOGUE;
  597. return S_RETURN_ERROR;
  598. }
  599. FS_Error storage_sd_info(Storage* storage, SDInfo* info) {
  600. S_API_PROLOGUE;
  601. SAData data = {
  602. .sdinfo = {
  603. .info = info,
  604. }};
  605. S_API_MESSAGE(StorageCommandSDInfo);
  606. S_API_EPILOGUE;
  607. return S_RETURN_ERROR;
  608. }
  609. FS_Error storage_sd_status(Storage* storage) {
  610. S_API_PROLOGUE;
  611. SAData data = {};
  612. S_API_MESSAGE(StorageCommandSDStatus);
  613. S_API_EPILOGUE;
  614. return S_RETURN_ERROR;
  615. }
  616. File* storage_file_alloc(Storage* storage) {
  617. File* file = malloc(sizeof(File));
  618. file->type = FileTypeClosed;
  619. file->storage = storage;
  620. FURI_LOG_T(TAG, "File/Dir %p alloc", (void*)((uint32_t)file - SRAM_BASE));
  621. return file;
  622. }
  623. bool storage_file_is_open(File* file) {
  624. return (file->type != FileTypeClosed);
  625. }
  626. bool storage_file_is_dir(File* file) {
  627. return (file->type == FileTypeOpenDir);
  628. }
  629. void storage_file_free(File* file) {
  630. if(storage_file_is_open(file)) {
  631. if(storage_file_is_dir(file)) {
  632. storage_dir_close(file);
  633. } else {
  634. storage_file_close(file);
  635. }
  636. }
  637. FURI_LOG_T(TAG, "File/Dir %p free", (void*)((uint32_t)file - SRAM_BASE));
  638. free(file);
  639. }
  640. FuriPubSub* storage_get_pubsub(Storage* storage) {
  641. return storage->pubsub;
  642. }
  643. bool storage_simply_remove_recursive(Storage* storage, const char* path) {
  644. furi_assert(storage);
  645. furi_assert(path);
  646. FileInfo fileinfo;
  647. bool result = false;
  648. FuriString* fullname;
  649. FuriString* cur_dir;
  650. if(storage_simply_remove(storage, path)) {
  651. return true;
  652. }
  653. char* name = malloc(MAX_NAME_LENGTH + 1); //-V799
  654. File* dir = storage_file_alloc(storage);
  655. cur_dir = furi_string_alloc_set(path);
  656. bool go_deeper = false;
  657. while(1) {
  658. if(!storage_dir_open(dir, furi_string_get_cstr(cur_dir))) {
  659. storage_dir_close(dir);
  660. break;
  661. }
  662. while(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  663. if(file_info_is_dir(&fileinfo)) {
  664. furi_string_cat_printf(cur_dir, "/%s", name);
  665. go_deeper = true;
  666. break;
  667. }
  668. fullname = furi_string_alloc_printf("%s/%s", furi_string_get_cstr(cur_dir), name);
  669. FS_Error error = storage_common_remove(storage, furi_string_get_cstr(fullname));
  670. furi_check(error == FSE_OK);
  671. furi_string_free(fullname);
  672. }
  673. storage_dir_close(dir);
  674. if(go_deeper) {
  675. go_deeper = false;
  676. continue;
  677. }
  678. FS_Error error = storage_common_remove(storage, furi_string_get_cstr(cur_dir));
  679. furi_check(error == FSE_OK);
  680. if(furi_string_cmp(cur_dir, path)) {
  681. size_t last_char = furi_string_search_rchar(cur_dir, '/');
  682. furi_assert(last_char != FURI_STRING_FAILURE);
  683. furi_string_left(cur_dir, last_char);
  684. } else {
  685. result = true;
  686. break;
  687. }
  688. }
  689. storage_file_free(dir);
  690. furi_string_free(cur_dir);
  691. free(name);
  692. return result;
  693. } //-V773
  694. bool storage_simply_remove(Storage* storage, const char* path) {
  695. FS_Error result;
  696. result = storage_common_remove(storage, path);
  697. return result == FSE_OK || result == FSE_NOT_EXIST;
  698. }
  699. bool storage_simply_mkdir(Storage* storage, const char* path) {
  700. FS_Error result;
  701. result = storage_common_mkdir(storage, path);
  702. return result == FSE_OK || result == FSE_EXIST;
  703. }
  704. void storage_get_next_filename(
  705. Storage* storage,
  706. const char* dirname,
  707. const char* filename,
  708. const char* fileextension,
  709. FuriString* nextfilename,
  710. uint8_t max_len) {
  711. FuriString* temp_str;
  712. uint16_t num = 0;
  713. temp_str = furi_string_alloc_printf("%s/%s%s", dirname, filename, fileextension);
  714. while(storage_common_stat(storage, furi_string_get_cstr(temp_str), NULL) == FSE_OK) {
  715. num++;
  716. furi_string_printf(temp_str, "%s/%s%d%s", dirname, filename, num, fileextension);
  717. }
  718. if(num && (max_len > strlen(filename))) {
  719. furi_string_printf(nextfilename, "%s%d", filename, num);
  720. } else {
  721. furi_string_printf(nextfilename, "%s", filename);
  722. }
  723. furi_string_free(temp_str);
  724. }