evil_portal_storage.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "evil_portal_storage.h"
  2. static Storage *evil_portal_open_storage() {
  3. return furi_record_open(RECORD_STORAGE);
  4. }
  5. static void evil_portal_close_storage() { furi_record_close(RECORD_STORAGE); }
  6. void evil_portal_read_index_html(void *context) {
  7. Evil_PortalApp *app = context;
  8. Storage *storage = evil_portal_open_storage();
  9. FileInfo fi;
  10. if (storage_common_stat(storage, EVIL_PORTAL_INDEX_SAVE_PATH, &fi) ==
  11. FSE_OK) {
  12. File *index_html = storage_file_alloc(storage);
  13. if (storage_file_open(index_html, EVIL_PORTAL_INDEX_SAVE_PATH, FSAM_READ,
  14. FSOM_OPEN_EXISTING)) {
  15. app->index_html = malloc((size_t)fi.size);
  16. uint8_t *buf_ptr = app->index_html;
  17. size_t read = 0;
  18. while (read < fi.size) {
  19. size_t to_read = fi.size - read;
  20. if (to_read > UINT16_MAX)
  21. to_read = UINT16_MAX;
  22. uint16_t now_read =
  23. storage_file_read(index_html, buf_ptr, (uint16_t)to_read);
  24. read += now_read;
  25. buf_ptr += now_read;
  26. }
  27. free(buf_ptr);
  28. }
  29. storage_file_close(index_html);
  30. storage_file_free(index_html);
  31. } else {
  32. char *html_error =
  33. "<b>Evil portal</b><br>Unable to read the html file.<br>"
  34. "Is the SD Card set up correctly? <br>See instructions @ "
  35. "github.com/bigbrodude6119/flipper-zero-evil-portal<br>"
  36. "Under the 'Install pre-built app on the flipper' section.";
  37. app->index_html = (uint8_t *)html_error;
  38. }
  39. evil_portal_close_storage();
  40. }
  41. void evil_portal_replace_index_html(FuriString* path) {
  42. Storage *storage = evil_portal_open_storage();
  43. FS_Error error;
  44. error = storage_common_remove(storage, EVIL_PORTAL_INDEX_SAVE_PATH);
  45. if(error != FSE_OK) {
  46. FURI_LOG_D("EVIL PORTAL", "Error removing file");
  47. } else {
  48. FURI_LOG_D("EVIL PORTAL", "Error removed file");
  49. }
  50. error = storage_common_copy(storage, furi_string_get_cstr(path), EVIL_PORTAL_INDEX_SAVE_PATH);
  51. if(error != FSE_OK) {
  52. FURI_LOG_D("EVIL PORTAL", "Error copying file");
  53. }
  54. evil_portal_close_storage();
  55. }
  56. void evil_portal_create_html_folder_if_not_exists() {
  57. Storage *storage = evil_portal_open_storage();
  58. if(storage_common_stat(storage, HTML_FOLDER, NULL) == FSE_NOT_EXIST) {
  59. FURI_LOG_D("Evil Portal", "Directory %s doesn't exist. Will create new.", HTML_FOLDER);
  60. if(!storage_simply_mkdir(storage, HTML_FOLDER)) {
  61. FURI_LOG_E("Evil Portal", "Error creating directory %s", HTML_FOLDER);
  62. }
  63. }
  64. evil_portal_close_storage();
  65. }
  66. void evil_portal_read_ap_name(void *context) {
  67. Evil_PortalApp *app = context;
  68. Storage *storage = evil_portal_open_storage();
  69. FileInfo fi;
  70. if (storage_common_stat(storage, EVIL_PORTAL_AP_SAVE_PATH, &fi) == FSE_OK) {
  71. File *ap_name = storage_file_alloc(storage);
  72. if (storage_file_open(ap_name, EVIL_PORTAL_AP_SAVE_PATH, FSAM_READ,
  73. FSOM_OPEN_EXISTING)) {
  74. app->ap_name = malloc((size_t)fi.size);
  75. uint8_t *buf_ptr = app->ap_name;
  76. size_t read = 0;
  77. while (read < fi.size) {
  78. size_t to_read = fi.size - read;
  79. if (to_read > UINT16_MAX)
  80. to_read = UINT16_MAX;
  81. uint16_t now_read =
  82. storage_file_read(ap_name, buf_ptr, (uint16_t)to_read);
  83. read += now_read;
  84. buf_ptr += now_read;
  85. }
  86. free(buf_ptr);
  87. }
  88. storage_file_close(ap_name);
  89. storage_file_free(ap_name);
  90. } else {
  91. char *app_default = "Evil Portal";
  92. app->ap_name = (uint8_t *)app_default;
  93. }
  94. evil_portal_close_storage();
  95. }
  96. void evil_portal_write_ap_name(void *context) {
  97. Evil_PortalApp *app = context;
  98. Storage *storage = evil_portal_open_storage();
  99. File *ap_name = storage_file_alloc(storage);
  100. if (storage_file_open(ap_name, EVIL_PORTAL_AP_SAVE_PATH, FSAM_WRITE,
  101. FSOM_CREATE_ALWAYS)) {
  102. storage_file_write(ap_name, app->text_store[0], strlen(app->text_store[0]));
  103. }
  104. storage_file_close(ap_name);
  105. storage_file_free(ap_name);
  106. evil_portal_close_storage();
  107. }
  108. char *sequential_file_resolve_path(Storage *storage, const char *dir,
  109. const char *prefix, const char *extension) {
  110. if (storage == NULL || dir == NULL || prefix == NULL || extension == NULL) {
  111. return NULL;
  112. }
  113. char file_path[256];
  114. int file_index = 0;
  115. do {
  116. if (snprintf(file_path, sizeof(file_path), "%s/%s_%d.%s", dir, prefix,
  117. file_index, extension) < 0) {
  118. return NULL;
  119. }
  120. file_index++;
  121. } while (storage_file_exists(storage, file_path));
  122. return strdup(file_path);
  123. }
  124. void write_logs(FuriString *portal_logs) {
  125. Storage *storage = evil_portal_open_storage();
  126. if (!storage_file_exists(storage, EVIL_PORTAL_LOG_SAVE_PATH)) {
  127. storage_simply_mkdir(storage, EVIL_PORTAL_LOG_SAVE_PATH);
  128. }
  129. char *seq_file_path = sequential_file_resolve_path(
  130. storage, EVIL_PORTAL_LOG_SAVE_PATH, "log", "txt");
  131. File *file = storage_file_alloc(storage);
  132. if (storage_file_open(file, seq_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  133. storage_file_write(file, furi_string_get_cstr(portal_logs), furi_string_utf8_length(portal_logs));
  134. }
  135. storage_file_close(file);
  136. storage_file_free(file);
  137. evil_portal_close_storage();
  138. }