evil_portal_storage.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 + 1);
  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. *buf_ptr = '\0';
  87. buf_ptr++;
  88. free(buf_ptr);
  89. }
  90. storage_file_close(ap_name);
  91. storage_file_free(ap_name);
  92. } else {
  93. char* app_default = "Evil Portal";
  94. app->ap_name = (uint8_t*)app_default;
  95. }
  96. evil_portal_close_storage();
  97. }
  98. void evil_portal_write_ap_name(void* context) {
  99. Evil_PortalApp* app = context;
  100. Storage* storage = evil_portal_open_storage();
  101. File* ap_name = storage_file_alloc(storage);
  102. if(storage_file_open(ap_name, EVIL_PORTAL_AP_SAVE_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  103. storage_file_write(ap_name, app->text_store[0], strlen(app->text_store[0]));
  104. }
  105. storage_file_close(ap_name);
  106. storage_file_free(ap_name);
  107. evil_portal_close_storage();
  108. }
  109. char* sequential_file_resolve_path(
  110. Storage* storage,
  111. const char* dir,
  112. const char* prefix,
  113. const char* extension) {
  114. if(storage == NULL || dir == NULL || prefix == NULL || extension == NULL) {
  115. return NULL;
  116. }
  117. char file_path[256];
  118. int file_index = 0;
  119. do {
  120. if(snprintf(
  121. file_path, sizeof(file_path), "%s/%s_%d.%s", dir, prefix, file_index, extension) <
  122. 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 =
  135. sequential_file_resolve_path(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_size(portal_logs));
  139. }
  140. storage_file_close(file);
  141. storage_file_free(file);
  142. evil_portal_close_storage();
  143. }