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