evil_portal_storage.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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() { furi_record_close(RECORD_STORAGE); }
  6. void evil_portal_read_index_html(void *context) {
  7. Evil_PortalApp *app = context;
  8. Storage *storage = evil_portal_open_storage();
  9. FileInfo fi;
  10. if (storage_common_stat(storage, EVIL_PORTAL_INDEX_SAVE_PATH, &fi) ==
  11. FSE_OK) {
  12. File *index_html = storage_file_alloc(storage);
  13. if (storage_file_open(index_html, EVIL_PORTAL_INDEX_SAVE_PATH, FSAM_READ,
  14. FSOM_OPEN_EXISTING)) {
  15. app->index_html = malloc((size_t)fi.size);
  16. uint8_t *buf_ptr = app->index_html;
  17. size_t read = 0;
  18. while (read < fi.size) {
  19. size_t to_read = fi.size - read;
  20. if (to_read > UINT16_MAX)
  21. to_read = UINT16_MAX;
  22. uint16_t now_read =
  23. storage_file_read(index_html, buf_ptr, (uint16_t)to_read);
  24. read += now_read;
  25. buf_ptr += now_read;
  26. }
  27. free(buf_ptr);
  28. }
  29. storage_file_close(index_html);
  30. storage_file_free(index_html);
  31. } else {
  32. char *html_error =
  33. "<b>Evil portal</b><br>Unable to read the html file.<br>"
  34. "Is the SD Card set up correctly? <br>See instructions @ "
  35. "github.com/bigbrodude6119/flipper-zero-evil-portal<br>"
  36. "Under the 'Install pre-built app on the flipper' section.";
  37. app->index_html = (uint8_t *)html_error;
  38. }
  39. evil_portal_close_storage();
  40. }
  41. void evil_portal_read_ap_name(void *context) {
  42. Evil_PortalApp *app = context;
  43. Storage *storage = evil_portal_open_storage();
  44. FileInfo fi;
  45. if (storage_common_stat(storage, EVIL_PORTAL_AP_SAVE_PATH, &fi) == FSE_OK) {
  46. File *ap_name = storage_file_alloc(storage);
  47. if (storage_file_open(ap_name, EVIL_PORTAL_AP_SAVE_PATH, FSAM_READ,
  48. FSOM_OPEN_EXISTING)) {
  49. app->ap_name = malloc((size_t)fi.size);
  50. uint8_t *buf_ptr = app->ap_name;
  51. size_t read = 0;
  52. while (read < fi.size) {
  53. size_t to_read = fi.size - read;
  54. if (to_read > UINT16_MAX)
  55. to_read = UINT16_MAX;
  56. uint16_t now_read =
  57. storage_file_read(ap_name, buf_ptr, (uint16_t)to_read);
  58. read += now_read;
  59. buf_ptr += now_read;
  60. }
  61. free(buf_ptr);
  62. }
  63. storage_file_close(ap_name);
  64. storage_file_free(ap_name);
  65. } else {
  66. char *app_default = "Evil Portal";
  67. app->ap_name = (uint8_t *)app_default;
  68. }
  69. evil_portal_close_storage();
  70. }
  71. void evil_portal_write_ap_name(void *context) {
  72. Evil_PortalApp *app = context;
  73. UNUSED(app);
  74. }
  75. char *sequential_file_resolve_path(Storage *storage, const char *dir,
  76. const char *prefix, const char *extension) {
  77. if (storage == NULL || dir == NULL || prefix == NULL || extension == NULL) {
  78. return NULL;
  79. }
  80. char file_path[256];
  81. int file_index = 0;
  82. do {
  83. if (snprintf(file_path, sizeof(file_path), "%s/%s_%d.%s", dir, prefix,
  84. file_index, extension) < 0) {
  85. return NULL;
  86. }
  87. file_index++;
  88. } while (storage_file_exists(storage, file_path));
  89. return strdup(file_path);
  90. }
  91. void write_logs(FuriString *portal_logs) {
  92. Storage *storage = evil_portal_open_storage();
  93. if (!storage_file_exists(storage, EVIL_PORTAL_LOG_SAVE_PATH)) {
  94. storage_simply_mkdir(storage, EVIL_PORTAL_LOG_SAVE_PATH);
  95. }
  96. char *seq_file_path = sequential_file_resolve_path(
  97. storage, EVIL_PORTAL_LOG_SAVE_PATH, "log", "txt");
  98. File *file = storage_file_alloc(storage);
  99. if (storage_file_open(file, seq_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  100. storage_file_write(file, furi_string_get_cstr(portal_logs), furi_string_utf8_length(portal_logs));
  101. }
  102. storage_file_close(file);
  103. storage_file_free(file);
  104. evil_portal_close_storage();
  105. }