evil_portal_storage.c 5.3 KB

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