web_crawler_storage.c 13 KB

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