web_crawler_storage.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. storage_file_close(file);
  220. storage_file_free(file);
  221. furi_record_close(RECORD_STORAGE);
  222. return true;
  223. }
  224. bool delete_received_data()
  225. {
  226. // Open the storage record
  227. Storage *storage = furi_record_open(RECORD_STORAGE);
  228. furi_check(storage, "Failed to open storage record");
  229. if (!storage_simply_remove_recursive(storage, RECEIVED_DATA_PATH "received_data.txt"))
  230. {
  231. FURI_LOG_E(TAG, "Failed to delete main file");
  232. furi_record_close(RECORD_STORAGE);
  233. return false;
  234. }
  235. // Allocate memory for new_path
  236. char *new_path = malloc(256);
  237. if (new_path == NULL)
  238. {
  239. FURI_LOG_E(TAG, "Memory allocation failed for paths");
  240. free(new_path);
  241. return false;
  242. }
  243. char file_type[16];
  244. if (!load_char("file_type", file_type, sizeof(file_type)))
  245. {
  246. snprintf(file_type, sizeof(file_type), ".txt");
  247. }
  248. char file_rename[128];
  249. if (!load_char("file_rename", file_rename, sizeof(file_rename)))
  250. {
  251. snprintf(file_rename, sizeof(file_rename), "received_data");
  252. }
  253. // Format the new_path
  254. int ret_new = snprintf(new_path, 256, "%s%s%s", RECEIVED_DATA_PATH, file_rename, file_type);
  255. if (ret_new < 0 || (size_t)ret_new >= 256)
  256. {
  257. FURI_LOG_E(TAG, "Failed to create new_path");
  258. free(new_path);
  259. return false;
  260. }
  261. if (!storage_simply_remove_recursive(storage, new_path))
  262. {
  263. FURI_LOG_E(TAG, "Failed to delete duplicate file");
  264. furi_record_close(RECORD_STORAGE);
  265. return false;
  266. }
  267. furi_record_close(RECORD_STORAGE);
  268. return true;
  269. }
  270. bool rename_received_data(const char *old_name, const char *new_name, const char *file_type, const char *old_file_type)
  271. {
  272. // Open the storage record
  273. Storage *storage = furi_record_open(RECORD_STORAGE);
  274. if (!storage)
  275. {
  276. FURI_LOG_E(TAG, "Failed to open storage record");
  277. return false;
  278. }
  279. // Allocate memory for old_path and new_path
  280. char *new_path = malloc(256);
  281. char *old_path = malloc(256);
  282. if (new_path == NULL || old_path == NULL)
  283. {
  284. FURI_LOG_E(TAG, "Memory allocation failed for paths");
  285. free(old_path);
  286. free(new_path);
  287. return false;
  288. }
  289. if (file_type == NULL || strlen(file_type) == 0)
  290. {
  291. file_type = ".txt";
  292. }
  293. if (old_file_type == NULL || strlen(old_file_type) == 0)
  294. {
  295. old_file_type = ".txt";
  296. }
  297. // Format the old_path
  298. int ret_old = snprintf(old_path, 256, "%s%s%s", RECEIVED_DATA_PATH, old_name, old_file_type);
  299. if (ret_old < 0 || (size_t)ret_old >= 256)
  300. {
  301. FURI_LOG_E(TAG, "Failed to create old_path");
  302. free(old_path);
  303. free(new_path);
  304. return false;
  305. }
  306. // Format the new_path
  307. int ret_new = snprintf(new_path, 256, "%s%s%s", RECEIVED_DATA_PATH, new_name, file_type);
  308. if (ret_new < 0 || (size_t)ret_new >= 256)
  309. {
  310. FURI_LOG_E(TAG, "Failed to create new_path");
  311. free(old_path);
  312. free(new_path);
  313. return false;
  314. }
  315. // Check if the file exists
  316. if (!storage_file_exists(storage, old_path))
  317. {
  318. if (!storage_file_exists(storage, RECEIVED_DATA_PATH "received_data.txt"))
  319. {
  320. FURI_LOG_E(TAG, "No saved file exists");
  321. free(old_path);
  322. free(new_path);
  323. furi_record_close(RECORD_STORAGE);
  324. return false;
  325. }
  326. else
  327. {
  328. bool renamed = storage_common_copy(storage, RECEIVED_DATA_PATH "received_data.txt", new_path) == FSE_OK;
  329. furi_record_close(RECORD_STORAGE);
  330. return renamed;
  331. }
  332. }
  333. else
  334. {
  335. bool renamed = storage_common_rename(storage, old_path, new_path) == FSE_OK;
  336. storage_simply_remove_recursive(storage, old_path);
  337. furi_record_close(RECORD_STORAGE);
  338. return renamed;
  339. }
  340. }
  341. bool save_char(
  342. const char *path_name, const char *value)
  343. {
  344. if (!value)
  345. {
  346. return false;
  347. }
  348. // Create the directory for saving settings
  349. char directory_path[256];
  350. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/web_crawler");
  351. // Create the directory
  352. Storage *storage = furi_record_open(RECORD_STORAGE);
  353. storage_common_mkdir(storage, directory_path);
  354. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/web_crawler/data");
  355. storage_common_mkdir(storage, directory_path);
  356. // Open the settings file
  357. File *file = storage_file_alloc(storage);
  358. char file_path[256];
  359. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/web_crawler/data/%s.txt", path_name);
  360. // Open the file in write mode
  361. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  362. {
  363. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  364. storage_file_free(file);
  365. furi_record_close(RECORD_STORAGE);
  366. return false;
  367. }
  368. // Write the data to the file
  369. size_t data_size = strlen(value) + 1; // Include null terminator
  370. if (storage_file_write(file, value, data_size) != data_size)
  371. {
  372. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  373. storage_file_close(file);
  374. storage_file_free(file);
  375. furi_record_close(RECORD_STORAGE);
  376. return false;
  377. }
  378. storage_file_close(file);
  379. storage_file_free(file);
  380. furi_record_close(RECORD_STORAGE);
  381. return true;
  382. }
  383. bool load_char(
  384. const char *path_name,
  385. char *value,
  386. size_t value_size)
  387. {
  388. if (!value)
  389. {
  390. return false;
  391. }
  392. Storage *storage = furi_record_open(RECORD_STORAGE);
  393. File *file = storage_file_alloc(storage);
  394. char file_path[256];
  395. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/web_crawler/data/%s.txt", path_name);
  396. // Open the file for reading
  397. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  398. {
  399. storage_file_free(file);
  400. furi_record_close(RECORD_STORAGE);
  401. return false; // Return false if the file does not exist
  402. }
  403. // Read data into the buffer
  404. size_t read_count = storage_file_read(file, value, value_size);
  405. if (storage_file_get_error(file) != FSE_OK)
  406. {
  407. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  408. storage_file_close(file);
  409. storage_file_free(file);
  410. furi_record_close(RECORD_STORAGE);
  411. return false;
  412. }
  413. // Ensure null-termination
  414. value[read_count - 1] = '\0';
  415. storage_file_close(file);
  416. storage_file_free(file);
  417. furi_record_close(RECORD_STORAGE);
  418. return strlen(value) > 0;
  419. }