evil_portal_storage.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. }
  32. evil_portal_close_storage();
  33. }
  34. void evil_portal_read_ap_name(void *context) {
  35. Evil_PortalApp *app = context;
  36. Storage *storage = evil_portal_open_storage();
  37. FileInfo fi;
  38. if (storage_common_stat(storage, EVIL_PORTAL_AP_SAVE_PATH, &fi) == FSE_OK) {
  39. File *ap_name = storage_file_alloc(storage);
  40. if (storage_file_open(ap_name, EVIL_PORTAL_AP_SAVE_PATH, FSAM_READ,
  41. FSOM_OPEN_EXISTING)) {
  42. app->ap_name = malloc((size_t)fi.size);
  43. uint8_t *buf_ptr = app->ap_name;
  44. size_t read = 0;
  45. while (read < fi.size) {
  46. size_t to_read = fi.size - read;
  47. if (to_read > UINT16_MAX)
  48. to_read = UINT16_MAX;
  49. uint16_t now_read =
  50. storage_file_read(ap_name, buf_ptr, (uint16_t)to_read);
  51. read += now_read;
  52. buf_ptr += now_read;
  53. }
  54. free(buf_ptr);
  55. }
  56. storage_file_close(ap_name);
  57. storage_file_free(ap_name);
  58. }
  59. evil_portal_close_storage();
  60. }
  61. char* sequential_file_resolve_path(
  62. Storage* storage,
  63. const char* dir,
  64. const char* prefix,
  65. const char* extension) {
  66. if(storage == NULL || dir == NULL || prefix == NULL || extension == NULL) {
  67. return NULL;
  68. }
  69. char file_path[256];
  70. int file_index = 0;
  71. do {
  72. if(snprintf(
  73. file_path, sizeof(file_path), "%s/%s_%d.%s", dir, prefix, file_index, extension) <
  74. 0) {
  75. return NULL;
  76. }
  77. file_index++;
  78. } while(storage_file_exists(storage, file_path));
  79. return strdup(file_path);
  80. }
  81. void write_logs(char* portal_logs) {
  82. Storage *storage = evil_portal_open_storage();
  83. char* seq_file_path = sequential_file_resolve_path(storage, EVIL_PORTAL_LOG_SAVE_PATH, "log", "txt");
  84. File* file = storage_file_alloc(storage);
  85. if (storage_file_open(file, seq_file_path, FSAM_WRITE,
  86. FSOM_CREATE_ALWAYS)) {
  87. storage_file_write(file, portal_logs, strlen(portal_logs));
  88. }
  89. storage_file_close(file);
  90. storage_file_free(file);
  91. evil_portal_close_storage();
  92. portal_logs = "";
  93. }