storage_external_api.c 12 KB

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