storage_external_api.c 15 KB

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