evil_portal_storage.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 + 1);
  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. *buf_ptr = '\0';
  32. buf_ptr++;
  33. free(buf_ptr);
  34. }
  35. storage_file_close(index_html);
  36. storage_file_free(index_html);
  37. } else {
  38. char* html_error = "<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, FSOM_OPEN_EXISTING)) {
  78. app->ap_name = malloc((size_t)fi.size + 1);
  79. uint8_t* buf_ptr = app->ap_name;
  80. size_t read = 0;
  81. while(read < fi.size) {
  82. size_t to_read = fi.size - read;
  83. if(to_read > UINT16_MAX) to_read = UINT16_MAX;
  84. uint16_t now_read = storage_file_read(ap_name, buf_ptr, (uint16_t)to_read);
  85. read += now_read;
  86. buf_ptr += now_read;
  87. }
  88. *buf_ptr = '\0';
  89. buf_ptr++;
  90. free(buf_ptr);
  91. }
  92. storage_file_close(ap_name);
  93. storage_file_free(ap_name);
  94. } else {
  95. char* app_default = "Evil Portal";
  96. app->ap_name = (uint8_t*)app_default;
  97. }
  98. evil_portal_close_storage();
  99. }
  100. void evil_portal_write_ap_name(void* context) {
  101. Evil_PortalApp* app = context;
  102. Storage* storage = evil_portal_open_storage();
  103. File* ap_name = storage_file_alloc(storage);
  104. if(storage_file_open(ap_name, EVIL_PORTAL_AP_SAVE_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  105. storage_file_write(ap_name, app->text_store[0], strlen(app->text_store[0]));
  106. }
  107. storage_file_close(ap_name);
  108. storage_file_free(ap_name);
  109. evil_portal_close_storage();
  110. }
  111. char* sequential_file_resolve_path(
  112. Storage* storage,
  113. const char* dir,
  114. const char* prefix,
  115. const char* extension) {
  116. if(storage == NULL || dir == NULL || prefix == NULL || extension == NULL) {
  117. return NULL;
  118. }
  119. char file_path[256];
  120. int file_index = 0;
  121. do {
  122. if(snprintf(
  123. file_path, sizeof(file_path), "%s/%s_%d.%s", dir, prefix, file_index, extension) <
  124. 0) {
  125. return NULL;
  126. }
  127. file_index++;
  128. } while(storage_file_exists(storage, file_path));
  129. return strdup(file_path);
  130. }
  131. void write_logs(FuriString* portal_logs) {
  132. Storage* storage = evil_portal_open_storage();
  133. if(!storage_file_exists(storage, EVIL_PORTAL_LOG_SAVE_PATH)) {
  134. storage_simply_mkdir(storage, EVIL_PORTAL_LOG_SAVE_PATH);
  135. }
  136. char* seq_file_path =
  137. sequential_file_resolve_path(storage, EVIL_PORTAL_LOG_SAVE_PATH, "log", "txt");
  138. File* file = storage_file_alloc(storage);
  139. if(storage_file_open(file, seq_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  140. storage_file_write(file, furi_string_get_cstr(portal_logs), furi_string_size(portal_logs));
  141. }
  142. storage_file_close(file);
  143. storage_file_free(file);
  144. evil_portal_close_storage();
  145. }