evil_portal_storage.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 = "<b>Evil portal</b>\nUnable to read the html file.\n"
  33. "Is the SD Card set up correctly? See instructions\n"
  34. "github.com/bigbrodude6119/flipper-zero-evil-portal\n"
  35. "'Install pre-built app on the flipper'";
  36. app->index_html = (uint8_t *)html_error;
  37. }
  38. evil_portal_close_storage();
  39. }
  40. void evil_portal_read_ap_name(void *context) {
  41. Evil_PortalApp *app = context;
  42. Storage *storage = evil_portal_open_storage();
  43. FileInfo fi;
  44. if (storage_common_stat(storage, EVIL_PORTAL_AP_SAVE_PATH, &fi) == FSE_OK) {
  45. File *ap_name = storage_file_alloc(storage);
  46. if (storage_file_open(ap_name, EVIL_PORTAL_AP_SAVE_PATH, FSAM_READ,
  47. FSOM_OPEN_EXISTING)) {
  48. app->ap_name = malloc((size_t)fi.size);
  49. uint8_t *buf_ptr = app->ap_name;
  50. size_t read = 0;
  51. while (read < fi.size) {
  52. size_t to_read = fi.size - read;
  53. if (to_read > UINT16_MAX)
  54. to_read = UINT16_MAX;
  55. uint16_t now_read =
  56. storage_file_read(ap_name, buf_ptr, (uint16_t)to_read);
  57. read += now_read;
  58. buf_ptr += now_read;
  59. }
  60. free(buf_ptr);
  61. }
  62. storage_file_close(ap_name);
  63. storage_file_free(ap_name);
  64. } else {
  65. char * app_default = "Evil Portal";
  66. app->ap_name = (uint8_t *)app_default;
  67. }
  68. evil_portal_close_storage();
  69. }
  70. char *sequential_file_resolve_path(Storage *storage, const char *dir,
  71. const char *prefix, const char *extension) {
  72. if (storage == NULL || dir == NULL || prefix == NULL || extension == NULL) {
  73. return NULL;
  74. }
  75. char file_path[256];
  76. int file_index = 0;
  77. do {
  78. if (snprintf(file_path, sizeof(file_path), "%s/%s_%d.%s", dir, prefix,
  79. file_index, extension) < 0) {
  80. return NULL;
  81. }
  82. file_index++;
  83. } while (storage_file_exists(storage, file_path));
  84. return strdup(file_path);
  85. }
  86. void write_logs(char *portal_logs) {
  87. Storage *storage = evil_portal_open_storage();
  88. if(!storage_file_exists(storage, EVIL_PORTAL_LOG_SAVE_PATH)) {
  89. storage_simply_mkdir(storage, EVIL_PORTAL_LOG_SAVE_PATH);
  90. }
  91. char *seq_file_path = sequential_file_resolve_path(
  92. storage, EVIL_PORTAL_LOG_SAVE_PATH, "log", "txt");
  93. File *file = storage_file_alloc(storage);
  94. if (storage_file_open(file, seq_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  95. storage_file_write(file, portal_logs, strlen(portal_logs));
  96. }
  97. storage_file_close(file);
  98. storage_file_free(file);
  99. evil_portal_close_storage();
  100. portal_logs = "";
  101. }