storage_external_api.c 25 KB

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