storage_external_api.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. #include <furi/record.h>
  2. #include <m-string.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. #define MAX_NAME_LENGTH 256
  9. #define TAG "StorageAPI"
  10. #define S_API_PROLOGUE \
  11. osSemaphoreId_t semaphore = osSemaphoreNew(1, 0, NULL); \
  12. furi_check(semaphore != NULL);
  13. #define S_FILE_API_PROLOGUE \
  14. Storage* storage = file->storage; \
  15. furi_assert(storage);
  16. #define S_API_EPILOGUE \
  17. furi_check(osMessageQueuePut(storage->message_queue, &message, 0, osWaitForever) == osOK); \
  18. osSemaphoreAcquire(semaphore, osWaitForever); \
  19. osSemaphoreDelete(semaphore);
  20. #define S_API_MESSAGE(_command) \
  21. SAReturn return_data; \
  22. StorageMessage message = { \
  23. .semaphore = semaphore, \
  24. .command = _command, \
  25. .data = &data, \
  26. .return_data = &return_data, \
  27. };
  28. #define S_API_DATA_FILE \
  29. SAData data = { \
  30. .file = { \
  31. .file = file, \
  32. }};
  33. #define S_API_DATA_PATH \
  34. SAData data = { \
  35. .path = { \
  36. .path = path, \
  37. }};
  38. #define S_RETURN_BOOL (return_data.bool_value);
  39. #define S_RETURN_UINT16 (return_data.uint16_value);
  40. #define S_RETURN_UINT64 (return_data.uint64_value);
  41. #define S_RETURN_ERROR (return_data.error_value);
  42. #define S_RETURN_CSTRING (return_data.cstring_value);
  43. typedef enum {
  44. StorageEventFlagFileClose = (1 << 0),
  45. } StorageEventFlag;
  46. /****************** FILE ******************/
  47. static bool storage_file_open_internal(
  48. File* file,
  49. const char* path,
  50. FS_AccessMode access_mode,
  51. FS_OpenMode open_mode) {
  52. S_FILE_API_PROLOGUE;
  53. S_API_PROLOGUE;
  54. SAData data = {
  55. .fopen = {
  56. .file = file,
  57. .path = path,
  58. .access_mode = access_mode,
  59. .open_mode = open_mode,
  60. }};
  61. file->type = FileTypeOpenFile;
  62. S_API_MESSAGE(StorageCommandFileOpen);
  63. S_API_EPILOGUE;
  64. return S_RETURN_BOOL;
  65. }
  66. static void storage_file_close_callback(const void* message, void* context) {
  67. const StorageEvent* storage_event = message;
  68. if(storage_event->type == StorageEventTypeFileClose ||
  69. storage_event->type == StorageEventTypeDirClose) {
  70. furi_assert(context);
  71. osEventFlagsId_t event = context;
  72. osEventFlagsSet(event, StorageEventFlagFileClose);
  73. }
  74. }
  75. bool storage_file_open(
  76. File* file,
  77. const char* path,
  78. FS_AccessMode access_mode,
  79. FS_OpenMode open_mode) {
  80. bool result;
  81. osEventFlagsId_t event = osEventFlagsNew(NULL);
  82. FuriPubSubSubscription* subscription = furi_pubsub_subscribe(
  83. storage_get_pubsub(file->storage), storage_file_close_callback, event);
  84. do {
  85. result = storage_file_open_internal(file, path, access_mode, open_mode);
  86. if(!result && file->error_id == FSE_ALREADY_OPEN) {
  87. osEventFlagsWait(event, StorageEventFlagFileClose, osFlagsWaitAny, osWaitForever);
  88. } else {
  89. break;
  90. }
  91. } while(true);
  92. furi_pubsub_unsubscribe(storage_get_pubsub(file->storage), subscription);
  93. osEventFlagsDelete(event);
  94. FURI_LOG_T(
  95. TAG, "File %p - %p open (%s)", (uint32_t)file - SRAM_BASE, file->file_id - SRAM_BASE, path);
  96. return result;
  97. }
  98. bool storage_file_close(File* file) {
  99. S_FILE_API_PROLOGUE;
  100. S_API_PROLOGUE;
  101. S_API_DATA_FILE;
  102. S_API_MESSAGE(StorageCommandFileClose);
  103. S_API_EPILOGUE;
  104. FURI_LOG_T(TAG, "File %p - %p closed", (uint32_t)file - SRAM_BASE, file->file_id - SRAM_BASE);
  105. file->type = FileTypeClosed;
  106. return S_RETURN_BOOL;
  107. }
  108. uint16_t storage_file_read(File* file, void* buff, uint16_t bytes_to_read) {
  109. if(bytes_to_read == 0) {
  110. return 0;
  111. }
  112. S_FILE_API_PROLOGUE;
  113. S_API_PROLOGUE;
  114. SAData data = {
  115. .fread = {
  116. .file = file,
  117. .buff = buff,
  118. .bytes_to_read = bytes_to_read,
  119. }};
  120. S_API_MESSAGE(StorageCommandFileRead);
  121. S_API_EPILOGUE;
  122. return S_RETURN_UINT16;
  123. }
  124. uint16_t storage_file_write(File* file, const void* buff, uint16_t bytes_to_write) {
  125. if(bytes_to_write == 0) {
  126. return 0;
  127. }
  128. S_FILE_API_PROLOGUE;
  129. S_API_PROLOGUE;
  130. SAData data = {
  131. .fwrite = {
  132. .file = file,
  133. .buff = buff,
  134. .bytes_to_write = bytes_to_write,
  135. }};
  136. S_API_MESSAGE(StorageCommandFileWrite);
  137. S_API_EPILOGUE;
  138. return S_RETURN_UINT16;
  139. }
  140. bool storage_file_seek(File* file, uint32_t offset, bool from_start) {
  141. S_FILE_API_PROLOGUE;
  142. S_API_PROLOGUE;
  143. SAData data = {
  144. .fseek = {
  145. .file = file,
  146. .offset = offset,
  147. .from_start = from_start,
  148. }};
  149. S_API_MESSAGE(StorageCommandFileSeek);
  150. S_API_EPILOGUE;
  151. return S_RETURN_BOOL;
  152. }
  153. uint64_t storage_file_tell(File* file) {
  154. S_FILE_API_PROLOGUE;
  155. S_API_PROLOGUE;
  156. S_API_DATA_FILE;
  157. S_API_MESSAGE(StorageCommandFileTell);
  158. S_API_EPILOGUE;
  159. return S_RETURN_UINT64;
  160. }
  161. bool storage_file_truncate(File* file) {
  162. S_FILE_API_PROLOGUE;
  163. S_API_PROLOGUE;
  164. S_API_DATA_FILE;
  165. S_API_MESSAGE(StorageCommandFileTruncate);
  166. S_API_EPILOGUE;
  167. return S_RETURN_BOOL;
  168. }
  169. uint64_t storage_file_size(File* file) {
  170. S_FILE_API_PROLOGUE;
  171. S_API_PROLOGUE;
  172. S_API_DATA_FILE;
  173. S_API_MESSAGE(StorageCommandFileSize);
  174. S_API_EPILOGUE;
  175. return S_RETURN_UINT64;
  176. }
  177. bool storage_file_sync(File* file) {
  178. S_FILE_API_PROLOGUE;
  179. S_API_PROLOGUE;
  180. S_API_DATA_FILE;
  181. S_API_MESSAGE(StorageCommandFileSync);
  182. S_API_EPILOGUE;
  183. return S_RETURN_BOOL;
  184. }
  185. bool storage_file_eof(File* file) {
  186. S_FILE_API_PROLOGUE;
  187. S_API_PROLOGUE;
  188. S_API_DATA_FILE;
  189. S_API_MESSAGE(StorageCommandFileEof);
  190. S_API_EPILOGUE;
  191. return S_RETURN_BOOL;
  192. }
  193. /****************** DIR ******************/
  194. static bool storage_dir_open_internal(File* file, const char* path) {
  195. S_FILE_API_PROLOGUE;
  196. S_API_PROLOGUE;
  197. SAData data = {
  198. .dopen = {
  199. .file = file,
  200. .path = path,
  201. }};
  202. file->type = FileTypeOpenDir;
  203. S_API_MESSAGE(StorageCommandDirOpen);
  204. S_API_EPILOGUE;
  205. return S_RETURN_BOOL;
  206. }
  207. bool storage_dir_open(File* file, const char* path) {
  208. bool result;
  209. osEventFlagsId_t event = osEventFlagsNew(NULL);
  210. FuriPubSubSubscription* subscription = furi_pubsub_subscribe(
  211. storage_get_pubsub(file->storage), storage_file_close_callback, event);
  212. do {
  213. result = storage_dir_open_internal(file, path);
  214. if(!result && file->error_id == FSE_ALREADY_OPEN) {
  215. osEventFlagsWait(event, StorageEventFlagFileClose, osFlagsWaitAny, osWaitForever);
  216. } else {
  217. break;
  218. }
  219. } while(true);
  220. furi_pubsub_unsubscribe(storage_get_pubsub(file->storage), subscription);
  221. osEventFlagsDelete(event);
  222. FURI_LOG_T(
  223. TAG, "Dir %p - %p open (%s)", (uint32_t)file - SRAM_BASE, file->file_id - SRAM_BASE, path);
  224. return result;
  225. }
  226. bool storage_dir_close(File* file) {
  227. S_FILE_API_PROLOGUE;
  228. S_API_PROLOGUE;
  229. S_API_DATA_FILE;
  230. S_API_MESSAGE(StorageCommandDirClose);
  231. S_API_EPILOGUE;
  232. FURI_LOG_T(TAG, "Dir %p - %p closed", (uint32_t)file - SRAM_BASE, file->file_id - SRAM_BASE);
  233. file->type = FileTypeClosed;
  234. return S_RETURN_BOOL;
  235. }
  236. bool storage_dir_read(File* file, FileInfo* fileinfo, char* name, uint16_t name_length) {
  237. S_FILE_API_PROLOGUE;
  238. S_API_PROLOGUE;
  239. SAData data = {
  240. .dread = {
  241. .file = file,
  242. .fileinfo = fileinfo,
  243. .name = name,
  244. .name_length = name_length,
  245. }};
  246. S_API_MESSAGE(StorageCommandDirRead);
  247. S_API_EPILOGUE;
  248. return S_RETURN_BOOL;
  249. }
  250. bool storage_dir_rewind(File* file) {
  251. S_FILE_API_PROLOGUE;
  252. S_API_PROLOGUE;
  253. S_API_DATA_FILE;
  254. S_API_MESSAGE(StorageCommandDirRewind);
  255. S_API_EPILOGUE;
  256. return S_RETURN_BOOL;
  257. }
  258. /****************** COMMON ******************/
  259. FS_Error storage_common_stat(Storage* storage, const char* path, FileInfo* fileinfo) {
  260. S_API_PROLOGUE;
  261. SAData data = {.cstat = {.path = path, .fileinfo = fileinfo}};
  262. S_API_MESSAGE(StorageCommandCommonStat);
  263. S_API_EPILOGUE;
  264. return S_RETURN_ERROR;
  265. }
  266. FS_Error storage_common_remove(Storage* storage, const char* path) {
  267. S_API_PROLOGUE;
  268. S_API_DATA_PATH;
  269. S_API_MESSAGE(StorageCommandCommonRemove);
  270. S_API_EPILOGUE;
  271. return S_RETURN_ERROR;
  272. }
  273. FS_Error storage_common_rename(Storage* storage, const char* old_path, const char* new_path) {
  274. FS_Error error = storage_common_copy(storage, old_path, new_path);
  275. if(error == FSE_OK) {
  276. if(storage_simply_remove_recursive(storage, old_path)) {
  277. error = FSE_OK;
  278. } else {
  279. error = FSE_INTERNAL;
  280. }
  281. }
  282. return error;
  283. }
  284. static FS_Error
  285. storage_copy_recursive(Storage* storage, const char* old_path, const char* new_path) {
  286. FS_Error error = storage_common_mkdir(storage, new_path);
  287. DirWalk* dir_walk = dir_walk_alloc(storage);
  288. string_t path;
  289. string_t tmp_new_path;
  290. string_t tmp_old_path;
  291. FileInfo fileinfo;
  292. string_init(path);
  293. string_init(tmp_new_path);
  294. string_init(tmp_old_path);
  295. do {
  296. if(error != FSE_OK) break;
  297. if(!dir_walk_open(dir_walk, old_path)) {
  298. error = dir_walk_get_error(dir_walk);
  299. break;
  300. }
  301. while(1) {
  302. DirWalkResult res = dir_walk_read(dir_walk, path, &fileinfo);
  303. if(res == DirWalkError) {
  304. error = dir_walk_get_error(dir_walk);
  305. break;
  306. } else if(res == DirWalkLast) {
  307. break;
  308. } else {
  309. string_set(tmp_old_path, path);
  310. string_right(path, strlen(old_path));
  311. string_printf(tmp_new_path, "%s%s", new_path, string_get_cstr(path));
  312. if(fileinfo.flags & FSF_DIRECTORY) {
  313. error = storage_common_mkdir(storage, string_get_cstr(tmp_new_path));
  314. } else {
  315. error = storage_common_copy(
  316. storage, string_get_cstr(tmp_old_path), string_get_cstr(tmp_new_path));
  317. }
  318. if(error != FSE_OK) break;
  319. }
  320. }
  321. } while(false);
  322. string_clear(tmp_new_path);
  323. string_clear(tmp_old_path);
  324. string_clear(path);
  325. dir_walk_free(dir_walk);
  326. return error;
  327. }
  328. FS_Error storage_common_copy(Storage* storage, const char* old_path, const char* new_path) {
  329. FS_Error error;
  330. FileInfo fileinfo;
  331. error = storage_common_stat(storage, old_path, &fileinfo);
  332. if(error == FSE_OK) {
  333. if(fileinfo.flags & FSF_DIRECTORY) {
  334. error = storage_copy_recursive(storage, old_path, new_path);
  335. } else {
  336. Stream* stream_from = file_stream_alloc(storage);
  337. Stream* stream_to = file_stream_alloc(storage);
  338. do {
  339. if(!file_stream_open(stream_from, old_path, FSAM_READ, FSOM_OPEN_EXISTING)) break;
  340. if(!file_stream_open(stream_to, new_path, FSAM_WRITE, FSOM_CREATE_NEW)) break;
  341. stream_copy_full(stream_from, stream_to);
  342. } while(false);
  343. error = file_stream_get_error(stream_from);
  344. if(error == FSE_OK) {
  345. error = file_stream_get_error(stream_to);
  346. }
  347. stream_free(stream_from);
  348. stream_free(stream_to);
  349. }
  350. }
  351. return error;
  352. }
  353. FS_Error storage_common_mkdir(Storage* storage, const char* path) {
  354. S_API_PROLOGUE;
  355. S_API_DATA_PATH;
  356. S_API_MESSAGE(StorageCommandCommonMkDir);
  357. S_API_EPILOGUE;
  358. return S_RETURN_ERROR;
  359. }
  360. FS_Error storage_common_fs_info(
  361. Storage* storage,
  362. const char* fs_path,
  363. uint64_t* total_space,
  364. uint64_t* free_space) {
  365. S_API_PROLOGUE;
  366. SAData data = {
  367. .cfsinfo = {
  368. .fs_path = fs_path,
  369. .total_space = total_space,
  370. .free_space = free_space,
  371. }};
  372. S_API_MESSAGE(StorageCommandCommonFSInfo);
  373. S_API_EPILOGUE;
  374. return S_RETURN_ERROR;
  375. }
  376. /****************** ERROR ******************/
  377. const char* storage_error_get_desc(FS_Error error_id) {
  378. return filesystem_api_error_get_desc(error_id);
  379. }
  380. FS_Error storage_file_get_error(File* file) {
  381. furi_check(file != NULL);
  382. return file->error_id;
  383. }
  384. int32_t storage_file_get_internal_error(File* file) {
  385. furi_check(file != NULL);
  386. return file->internal_error_id;
  387. }
  388. const char* storage_file_get_error_desc(File* file) {
  389. furi_check(file != NULL);
  390. return filesystem_api_error_get_desc(file->error_id);
  391. }
  392. /****************** Raw SD API ******************/
  393. FS_Error storage_sd_format(Storage* storage) {
  394. S_API_PROLOGUE;
  395. SAData data = {};
  396. S_API_MESSAGE(StorageCommandSDFormat);
  397. S_API_EPILOGUE;
  398. return S_RETURN_ERROR;
  399. }
  400. FS_Error storage_sd_unmount(Storage* storage) {
  401. S_API_PROLOGUE;
  402. SAData data = {};
  403. S_API_MESSAGE(StorageCommandSDUnmount);
  404. S_API_EPILOGUE;
  405. return S_RETURN_ERROR;
  406. }
  407. FS_Error storage_sd_info(Storage* storage, SDInfo* info) {
  408. S_API_PROLOGUE;
  409. SAData data = {
  410. .sdinfo = {
  411. .info = info,
  412. }};
  413. S_API_MESSAGE(StorageCommandSDInfo);
  414. S_API_EPILOGUE;
  415. return S_RETURN_ERROR;
  416. }
  417. FS_Error storage_sd_status(Storage* storage) {
  418. S_API_PROLOGUE;
  419. SAData data = {};
  420. S_API_MESSAGE(StorageCommandSDStatus);
  421. S_API_EPILOGUE;
  422. return S_RETURN_ERROR;
  423. }
  424. File* storage_file_alloc(Storage* storage) {
  425. File* file = malloc(sizeof(File));
  426. file->type = FileTypeClosed;
  427. file->storage = storage;
  428. FURI_LOG_T(TAG, "File/Dir %p alloc", (uint32_t)file - SRAM_BASE);
  429. return file;
  430. }
  431. bool storage_file_is_open(File* file) {
  432. return (file->type != FileTypeClosed);
  433. }
  434. bool storage_file_is_dir(File* file) {
  435. return (file->type == FileTypeOpenDir);
  436. }
  437. void storage_file_free(File* file) {
  438. if(storage_file_is_open(file)) {
  439. if(storage_file_is_dir(file)) {
  440. storage_dir_close(file);
  441. } else {
  442. storage_file_close(file);
  443. }
  444. }
  445. FURI_LOG_T(TAG, "File/Dir %p free", (uint32_t)file - SRAM_BASE);
  446. free(file);
  447. }
  448. FuriPubSub* storage_get_pubsub(Storage* storage) {
  449. return storage->pubsub;
  450. }
  451. bool storage_simply_remove_recursive(Storage* storage, const char* path) {
  452. furi_assert(storage);
  453. furi_assert(path);
  454. FileInfo fileinfo;
  455. bool result = false;
  456. string_t fullname;
  457. string_t cur_dir;
  458. if(storage_simply_remove(storage, path)) {
  459. return true;
  460. }
  461. char* name = malloc(MAX_NAME_LENGTH + 1);
  462. File* dir = storage_file_alloc(storage);
  463. string_init_set_str(cur_dir, path);
  464. bool go_deeper = false;
  465. while(1) {
  466. if(!storage_dir_open(dir, string_get_cstr(cur_dir))) {
  467. storage_dir_close(dir);
  468. break;
  469. }
  470. while(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  471. if(fileinfo.flags & FSF_DIRECTORY) {
  472. string_cat_printf(cur_dir, "/%s", name);
  473. go_deeper = true;
  474. break;
  475. }
  476. string_init_printf(fullname, "%s/%s", string_get_cstr(cur_dir), name);
  477. FS_Error error = storage_common_remove(storage, string_get_cstr(fullname));
  478. furi_check(error == FSE_OK);
  479. string_clear(fullname);
  480. }
  481. storage_dir_close(dir);
  482. if(go_deeper) {
  483. go_deeper = false;
  484. continue;
  485. }
  486. FS_Error error = storage_common_remove(storage, string_get_cstr(cur_dir));
  487. furi_check(error == FSE_OK);
  488. if(string_cmp(cur_dir, path)) {
  489. size_t last_char = string_search_rchar(cur_dir, '/');
  490. furi_assert(last_char != STRING_FAILURE);
  491. string_left(cur_dir, last_char);
  492. } else {
  493. result = true;
  494. break;
  495. }
  496. }
  497. storage_file_free(dir);
  498. string_clear(cur_dir);
  499. free(name);
  500. return result;
  501. }
  502. bool storage_simply_remove(Storage* storage, const char* path) {
  503. FS_Error result;
  504. result = storage_common_remove(storage, path);
  505. return result == FSE_OK || result == FSE_NOT_EXIST;
  506. }
  507. bool storage_simply_mkdir(Storage* storage, const char* path) {
  508. FS_Error result;
  509. result = storage_common_mkdir(storage, path);
  510. return result == FSE_OK || result == FSE_EXIST;
  511. }
  512. void storage_get_next_filename(
  513. Storage* storage,
  514. const char* dirname,
  515. const char* filename,
  516. const char* fileextension,
  517. string_t nextfilename,
  518. uint8_t max_len) {
  519. string_t temp_str;
  520. uint16_t num = 0;
  521. string_init_printf(temp_str, "%s/%s%s", dirname, filename, fileextension);
  522. while(storage_common_stat(storage, string_get_cstr(temp_str), NULL) == FSE_OK) {
  523. num++;
  524. string_printf(temp_str, "%s/%s%d%s", dirname, filename, num, fileextension);
  525. }
  526. if(num && (max_len > strlen(filename))) {
  527. string_printf(nextfilename, "%s%d", filename, num);
  528. } else {
  529. string_printf(nextfilename, "%s", filename);
  530. }
  531. string_clear(temp_str);
  532. }