storage_external_api.c 17 KB

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