archive_files.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "archive_files.h"
  2. #include "archive_apps.h"
  3. #include "archive_browser.h"
  4. #define TAG "Archive"
  5. #define ASSETS_DIR "assets"
  6. void archive_set_file_type(ArchiveFile_t* file, const char* path, bool is_folder, bool is_app) {
  7. furi_assert(file);
  8. file->is_app = is_app;
  9. if(is_app) {
  10. file->type = archive_get_app_filetype(archive_get_app_type(path));
  11. } else {
  12. for(size_t i = 0; i < COUNT_OF(known_ext); i++) {
  13. if((known_ext[i][0] == '?') || (known_ext[i][0] == '*')) continue;
  14. if(string_search_str(file->path, known_ext[i], 0) != STRING_FAILURE) {
  15. if(i == ArchiveFileTypeBadUsb) {
  16. if(string_search_str(file->path, archive_get_default_path(ArchiveTabBadUsb)) ==
  17. 0) {
  18. file->type = i;
  19. return; // *.txt file is a BadUSB script only if it is in BadUSB folder
  20. }
  21. } else {
  22. file->type = i;
  23. return;
  24. }
  25. }
  26. }
  27. if(is_folder) {
  28. file->type = ArchiveFileTypeFolder;
  29. } else {
  30. file->type = ArchiveFileTypeUnknown;
  31. }
  32. }
  33. }
  34. bool archive_get_items(void* context, const char* path) {
  35. furi_assert(context);
  36. bool res = false;
  37. ArchiveBrowserView* browser = context;
  38. if(archive_get_tab(browser) == ArchiveTabFavorites) {
  39. res = archive_favorites_read(browser);
  40. } else if(strncmp(path, "/app:", 5) == 0) {
  41. res = archive_app_read_dir(browser, path);
  42. }
  43. return res;
  44. }
  45. void archive_file_append(const char* path, const char* format, ...) {
  46. furi_assert(path);
  47. string_t string;
  48. va_list args;
  49. va_start(args, format);
  50. string_init_vprintf(string, format, args);
  51. va_end(args);
  52. Storage* fs_api = furi_record_open("storage");
  53. File* file = storage_file_alloc(fs_api);
  54. bool res = storage_file_open(file, path, FSAM_WRITE, FSOM_OPEN_APPEND);
  55. if(res) {
  56. storage_file_write(file, string_get_cstr(string), string_size(string));
  57. }
  58. storage_file_close(file);
  59. storage_file_free(file);
  60. furi_record_close("storage");
  61. }
  62. void archive_delete_file(void* context, const char* format, ...) {
  63. furi_assert(context);
  64. string_t filename;
  65. va_list args;
  66. va_start(args, format);
  67. string_init_vprintf(filename, format, args);
  68. va_end(args);
  69. ArchiveBrowserView* browser = context;
  70. Storage* fs_api = furi_record_open("storage");
  71. FileInfo fileinfo;
  72. storage_common_stat(fs_api, string_get_cstr(filename), &fileinfo);
  73. bool res = false;
  74. if(fileinfo.flags & FSF_DIRECTORY) {
  75. res = storage_simply_remove_recursive(fs_api, string_get_cstr(filename));
  76. } else {
  77. res = (storage_common_remove(fs_api, string_get_cstr(filename)) == FSE_OK);
  78. }
  79. furi_record_close("storage");
  80. if(archive_is_favorite("%s", string_get_cstr(filename))) {
  81. archive_favorites_delete("%s", string_get_cstr(filename));
  82. }
  83. if(res) {
  84. archive_file_array_rm_selected(browser);
  85. }
  86. string_clear(filename);
  87. }