web_crawler_storage.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #include <flip_storage/web_crawler_storage.h>
  2. // Function to save settings: path, SSID, and password
  3. void save_settings(
  4. const char* path,
  5. const char* ssid,
  6. const char* password,
  7. const char* file_rename,
  8. const char* file_type,
  9. const char* http_method,
  10. const char* headers,
  11. const char* payload) {
  12. // Create the directory for saving settings
  13. char directory_path[256];
  14. snprintf(
  15. directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/web_crawler");
  16. // Create the directory
  17. Storage* storage = furi_record_open(RECORD_STORAGE);
  18. storage_common_mkdir(storage, directory_path);
  19. // Open the settings file
  20. File* file = storage_file_alloc(storage);
  21. if(!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  22. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
  23. storage_file_free(file);
  24. furi_record_close(RECORD_STORAGE);
  25. return;
  26. }
  27. if(file_type == NULL || strlen(file_type) == 0) {
  28. file_type = ".txt";
  29. }
  30. // Save the SSID length and data
  31. size_t ssid_length = strlen(ssid) + 1; // Include null terminator
  32. if(storage_file_write(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  33. storage_file_write(file, ssid, ssid_length) != ssid_length) {
  34. FURI_LOG_E(TAG, "Failed to write SSID");
  35. }
  36. // Save the password length and data
  37. size_t password_length = strlen(password) + 1; // Include null terminator
  38. if(storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  39. storage_file_write(file, password, password_length) != password_length) {
  40. FURI_LOG_E(TAG, "Failed to write password");
  41. }
  42. // Save the path length and data
  43. size_t path_length = strlen(path) + 1; // Include null terminator
  44. if(storage_file_write(file, &path_length, sizeof(size_t)) != sizeof(size_t) ||
  45. storage_file_write(file, path, path_length) != path_length) {
  46. FURI_LOG_E(TAG, "Failed to write path");
  47. }
  48. // Save the file rename length and data
  49. size_t file_rename_length = strlen(file_rename) + 1; // Include null terminator
  50. if(storage_file_write(file, &file_rename_length, sizeof(size_t)) != sizeof(size_t) ||
  51. storage_file_write(file, file_rename, file_rename_length) != file_rename_length) {
  52. FURI_LOG_E(TAG, "Failed to write file rename");
  53. }
  54. // Save the file type length and data
  55. size_t file_type_length = strlen(file_type) + 1; // Include null terminator
  56. if(storage_file_write(file, &file_type_length, sizeof(size_t)) != sizeof(size_t) ||
  57. storage_file_write(file, file_type, file_type_length) != file_type_length) {
  58. FURI_LOG_E(TAG, "Failed to write file type");
  59. }
  60. // Save the http method length and data
  61. size_t http_method_length = strlen(http_method) + 1; // Include null terminator
  62. if(storage_file_write(file, &http_method_length, sizeof(size_t)) != sizeof(size_t) ||
  63. storage_file_write(file, http_method, http_method_length) != http_method_length) {
  64. FURI_LOG_E(TAG, "Failed to write http method");
  65. }
  66. // Save the headers length and data
  67. size_t headers_length = strlen(headers) + 1; // Include null terminator
  68. if(storage_file_write(file, &headers_length, sizeof(size_t)) != sizeof(size_t) ||
  69. storage_file_write(file, headers, headers_length) != headers_length) {
  70. FURI_LOG_E(TAG, "Failed to write headers");
  71. }
  72. // Save the payload length and data
  73. size_t payload_length = strlen(payload) + 1; // Include null terminator
  74. if(storage_file_write(file, &payload_length, sizeof(size_t)) != sizeof(size_t) ||
  75. storage_file_write(file, payload, payload_length) != payload_length) {
  76. FURI_LOG_E(TAG, "Failed to write payload");
  77. }
  78. storage_file_close(file);
  79. storage_file_free(file);
  80. furi_record_close(RECORD_STORAGE);
  81. }
  82. // Function to load settings (the variables must be opened in the order they were saved)
  83. bool load_settings(
  84. char* path,
  85. size_t path_size,
  86. char* ssid,
  87. size_t ssid_size,
  88. char* password,
  89. size_t password_size,
  90. char* file_rename,
  91. size_t file_rename_size,
  92. char* file_type,
  93. size_t file_type_size,
  94. char* http_method,
  95. size_t http_method_size,
  96. char* headers,
  97. size_t headers_size,
  98. char* payload,
  99. size_t payload_size,
  100. WebCrawlerApp* app) {
  101. if(!app) {
  102. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  103. return false;
  104. }
  105. Storage* storage = furi_record_open(RECORD_STORAGE);
  106. File* file = storage_file_alloc(storage);
  107. if(!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING)) {
  108. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  109. storage_file_free(file);
  110. furi_record_close(RECORD_STORAGE);
  111. return false; // Return false if the file does not exist
  112. }
  113. // Load the SSID
  114. size_t ssid_length;
  115. if(storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  116. ssid_length > ssid_size || storage_file_read(file, ssid, ssid_length) != ssid_length) {
  117. FURI_LOG_E(TAG, "Failed to read SSID");
  118. storage_file_close(file);
  119. storage_file_free(file);
  120. furi_record_close(RECORD_STORAGE);
  121. return false;
  122. }
  123. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  124. // Load the password
  125. size_t password_length;
  126. if(storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  127. password_length > password_size ||
  128. storage_file_read(file, password, password_length) != password_length) {
  129. FURI_LOG_E(TAG, "Failed to read password");
  130. storage_file_close(file);
  131. storage_file_free(file);
  132. furi_record_close(RECORD_STORAGE);
  133. return false;
  134. }
  135. password[password_length - 1] = '\0'; // Ensure null-termination
  136. // Load the path
  137. size_t path_length;
  138. if(storage_file_read(file, &path_length, sizeof(size_t)) != sizeof(size_t) ||
  139. path_length > path_size || storage_file_read(file, path, path_length) != path_length) {
  140. FURI_LOG_E(TAG, "Failed to read path");
  141. storage_file_close(file);
  142. storage_file_free(file);
  143. furi_record_close(RECORD_STORAGE);
  144. return false;
  145. }
  146. path[path_length - 1] = '\0'; // Ensure null-termination
  147. // Load the file rename
  148. size_t file_rename_length;
  149. if(storage_file_read(file, &file_rename_length, sizeof(size_t)) != sizeof(size_t) ||
  150. file_rename_length > file_rename_size ||
  151. storage_file_read(file, file_rename, file_rename_length) != file_rename_length) {
  152. FURI_LOG_E(TAG, "Failed to read file rename");
  153. storage_file_close(file);
  154. storage_file_free(file);
  155. furi_record_close(RECORD_STORAGE);
  156. return false;
  157. }
  158. file_rename[file_rename_length - 1] = '\0'; // Ensure null-termination
  159. // Load the file type
  160. size_t file_type_length;
  161. if(storage_file_read(file, &file_type_length, sizeof(size_t)) != sizeof(size_t) ||
  162. file_type_length > file_type_size ||
  163. storage_file_read(file, file_type, file_type_length) != file_type_length) {
  164. FURI_LOG_E(TAG, "Failed to read file type");
  165. storage_file_close(file);
  166. storage_file_free(file);
  167. furi_record_close(RECORD_STORAGE);
  168. return false;
  169. }
  170. file_type[file_type_length - 1] = '\0'; // Ensure null-termination
  171. // Load the http method
  172. size_t http_method_length;
  173. if(storage_file_read(file, &http_method_length, sizeof(size_t)) != sizeof(size_t) ||
  174. http_method_length > http_method_size ||
  175. storage_file_read(file, http_method, http_method_length) != http_method_length) {
  176. FURI_LOG_E(TAG, "Failed to read http method");
  177. storage_file_close(file);
  178. storage_file_free(file);
  179. furi_record_close(RECORD_STORAGE);
  180. return false;
  181. }
  182. // Load the headers
  183. size_t headers_length;
  184. if(storage_file_read(file, &headers_length, sizeof(size_t)) != sizeof(size_t) ||
  185. headers_length > headers_size ||
  186. storage_file_read(file, headers, headers_length) != headers_length) {
  187. FURI_LOG_E(TAG, "Failed to read headers");
  188. storage_file_close(file);
  189. storage_file_free(file);
  190. furi_record_close(RECORD_STORAGE);
  191. return false;
  192. }
  193. // Load the payload
  194. size_t payload_length;
  195. if(storage_file_read(file, &payload_length, sizeof(size_t)) != sizeof(size_t) ||
  196. payload_length > payload_size ||
  197. storage_file_read(file, payload, payload_length) != payload_length) {
  198. FURI_LOG_E(TAG, "Failed to read payload");
  199. storage_file_close(file);
  200. storage_file_free(file);
  201. furi_record_close(RECORD_STORAGE);
  202. return false;
  203. }
  204. // set the path, ssid, and password
  205. strncpy(app->path, path, path_size);
  206. strncpy(app->ssid, ssid, ssid_size);
  207. strncpy(app->password, password, password_size);
  208. strncpy(app->file_rename, file_rename, file_rename_size);
  209. strncpy(app->file_type, file_type, file_type_size);
  210. strncpy(app->http_method, http_method, http_method_size);
  211. strncpy(app->headers, headers, headers_size);
  212. strncpy(app->payload, payload, payload_size);
  213. storage_file_close(file);
  214. storage_file_free(file);
  215. furi_record_close(RECORD_STORAGE);
  216. return true;
  217. }
  218. bool delete_received_data(WebCrawlerApp* app) {
  219. if(app == NULL) {
  220. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  221. return false;
  222. }
  223. // Open the storage record
  224. Storage* storage = furi_record_open(RECORD_STORAGE);
  225. if(!storage) {
  226. FURI_LOG_E(TAG, "Failed to open storage record");
  227. return false;
  228. }
  229. if(!storage_simply_remove_recursive(storage, RECEIVED_DATA_PATH "received_data.txt")) {
  230. FURI_LOG_E(TAG, "Failed to delete main file");
  231. furi_record_close(RECORD_STORAGE);
  232. return false;
  233. }
  234. // Allocate memory for new_path
  235. char* new_path = malloc(256);
  236. if(new_path == NULL) {
  237. FURI_LOG_E(TAG, "Memory allocation failed for paths");
  238. free(new_path);
  239. return false;
  240. }
  241. if(app->file_type == NULL || strlen(app->file_type) == 0) {
  242. app->file_type = ".txt";
  243. }
  244. // Format the new_path
  245. int ret_new =
  246. snprintf(new_path, 256, "%s%s%s", RECEIVED_DATA_PATH, app->file_rename, app->file_type);
  247. if(ret_new < 0 || (size_t)ret_new >= 256) {
  248. FURI_LOG_E(TAG, "Failed to create new_path");
  249. free(new_path);
  250. return false;
  251. }
  252. if(!storage_simply_remove_recursive(storage, new_path)) {
  253. FURI_LOG_E(TAG, "Failed to delete duplicate file");
  254. furi_record_close(RECORD_STORAGE);
  255. return false;
  256. }
  257. furi_record_close(RECORD_STORAGE);
  258. return true;
  259. }
  260. bool rename_received_data(
  261. const char* old_name,
  262. const char* new_name,
  263. const char* file_type,
  264. const char* old_file_type) {
  265. // Open the storage record
  266. Storage* storage = furi_record_open(RECORD_STORAGE);
  267. if(!storage) {
  268. FURI_LOG_E(TAG, "Failed to open storage record");
  269. return false;
  270. }
  271. // Allocate memory for old_path and new_path
  272. char* new_path = malloc(256);
  273. char* old_path = malloc(256);
  274. if(new_path == NULL || old_path == NULL) {
  275. FURI_LOG_E(TAG, "Memory allocation failed for paths");
  276. free(old_path);
  277. free(new_path);
  278. return false;
  279. }
  280. if(file_type == NULL || strlen(file_type) == 0) {
  281. file_type = ".txt";
  282. }
  283. if(old_file_type == NULL || strlen(old_file_type) == 0) {
  284. old_file_type = ".txt";
  285. }
  286. // Format the old_path
  287. int ret_old = snprintf(old_path, 256, "%s%s%s", RECEIVED_DATA_PATH, old_name, old_file_type);
  288. if(ret_old < 0 || (size_t)ret_old >= 256) {
  289. FURI_LOG_E(TAG, "Failed to create old_path");
  290. free(old_path);
  291. free(new_path);
  292. return false;
  293. }
  294. // Format the new_path
  295. int ret_new = snprintf(new_path, 256, "%s%s%s", RECEIVED_DATA_PATH, new_name, file_type);
  296. if(ret_new < 0 || (size_t)ret_new >= 256) {
  297. FURI_LOG_E(TAG, "Failed to create new_path");
  298. free(old_path);
  299. free(new_path);
  300. return false;
  301. }
  302. // Check if the file exists
  303. if(!storage_file_exists(storage, old_path)) {
  304. if(!storage_file_exists(storage, RECEIVED_DATA_PATH "received_data.txt")) {
  305. FURI_LOG_E(TAG, "No saved file exists");
  306. free(old_path);
  307. free(new_path);
  308. furi_record_close(RECORD_STORAGE);
  309. return false;
  310. } else {
  311. bool renamed =
  312. storage_common_copy(storage, RECEIVED_DATA_PATH "received_data.txt", new_path) ==
  313. FSE_OK;
  314. furi_record_close(RECORD_STORAGE);
  315. return renamed;
  316. }
  317. } else {
  318. bool renamed = storage_common_rename(storage, old_path, new_path) == FSE_OK;
  319. storage_simply_remove_recursive(storage, old_path);
  320. furi_record_close(RECORD_STORAGE);
  321. return renamed;
  322. }
  323. }