evil_portal_storage.c 5.4 KB

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