archive_apps.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "archive_files.h"
  2. #include "archive_apps.h"
  3. #include "archive_browser.h"
  4. static const char* known_apps[] = {
  5. [ArchiveAppTypeU2f] = "u2f",
  6. };
  7. ArchiveAppTypeEnum archive_get_app_type(const char* path) {
  8. const char* app_name = strchr(path, ':');
  9. if(app_name == NULL) {
  10. return ArchiveAppTypeUnknown;
  11. }
  12. app_name++;
  13. for(size_t i = 0; i < COUNT_OF(known_apps); i++) {
  14. if(strncmp(app_name, known_apps[i], strlen(known_apps[i])) == 0) {
  15. return i;
  16. }
  17. }
  18. return ArchiveAppTypeUnknown;
  19. }
  20. bool archive_app_is_available(void* context, const char* path) {
  21. UNUSED(context);
  22. furi_assert(path);
  23. ArchiveAppTypeEnum app = archive_get_app_type(path);
  24. if(app == ArchiveAppTypeU2f) {
  25. bool file_exists = false;
  26. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  27. File* file = storage_file_alloc(fs_api);
  28. file_exists =
  29. storage_file_open(file, ANY_PATH("u2f/key.u2f"), FSAM_READ, FSOM_OPEN_EXISTING);
  30. if(file_exists) {
  31. storage_file_close(file);
  32. file_exists =
  33. storage_file_open(file, ANY_PATH("u2f/cnt.u2f"), FSAM_READ, FSOM_OPEN_EXISTING);
  34. if(file_exists) {
  35. storage_file_close(file);
  36. }
  37. }
  38. storage_file_free(file);
  39. furi_record_close(RECORD_STORAGE);
  40. return file_exists;
  41. } else {
  42. return false;
  43. }
  44. }
  45. bool archive_app_read_dir(void* context, const char* path) {
  46. furi_assert(context);
  47. furi_assert(path);
  48. ArchiveBrowserView* browser = context;
  49. archive_file_array_rm_all(browser);
  50. ArchiveAppTypeEnum app = archive_get_app_type(path);
  51. if(app == ArchiveAppTypeU2f) {
  52. archive_add_app_item(browser, "/app:u2f/U2F Token");
  53. return true;
  54. } else {
  55. return false;
  56. }
  57. }
  58. void archive_app_delete_file(void* context, const char* path) {
  59. furi_assert(context);
  60. furi_assert(path);
  61. ArchiveBrowserView* browser = context;
  62. ArchiveAppTypeEnum app = archive_get_app_type(path);
  63. bool res = false;
  64. if(app == ArchiveAppTypeU2f) {
  65. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  66. res = (storage_common_remove(fs_api, ANY_PATH("u2f/key.u2f")) == FSE_OK);
  67. res |= (storage_common_remove(fs_api, ANY_PATH("u2f/cnt.u2f")) == FSE_OK);
  68. furi_record_close(RECORD_STORAGE);
  69. if(archive_is_favorite("/app:u2f/U2F Token")) {
  70. archive_favorites_delete("/app:u2f/U2F Token");
  71. }
  72. }
  73. if(res) {
  74. archive_file_array_rm_selected(browser);
  75. }
  76. }