evil_portal_storage.c 5.6 KB

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