storage_external_api.c 14 KB

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