storage.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. #include "flip_storage/storage.h"
  2. // Forward declaration for use in other functions
  3. static bool load_flip_social_settings(
  4. char *ssid,
  5. size_t ssid_size,
  6. char *password,
  7. size_t password_size,
  8. char *login_username_logged_out,
  9. size_t username_out_size,
  10. char *login_username_logged_in,
  11. size_t username_in_size,
  12. char *login_password_logged_out,
  13. size_t password_out_size,
  14. char *change_password_logged_in,
  15. size_t change_password_size,
  16. char *change_bio_logged_in,
  17. size_t change_bio_size,
  18. char *is_logged_in,
  19. size_t is_logged_in_size);
  20. void save_settings(
  21. const char *wifi_ssid,
  22. const char *wifi_password,
  23. const char *username,
  24. const char *password)
  25. {
  26. // Create the directory for saving settings
  27. char directory_path[256];
  28. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  29. // Create the directory
  30. Storage *storage = furi_record_open(RECORD_STORAGE);
  31. storage_common_mkdir(storage, directory_path);
  32. // Open the settings file
  33. File *file = storage_file_alloc(storage);
  34. if (!storage_file_open(file, SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  35. {
  36. FURI_LOG_E(TAG, "Failed to open settings file for writing: %s", SETTINGS_PATH);
  37. storage_file_free(file);
  38. furi_record_close(RECORD_STORAGE);
  39. return;
  40. }
  41. // Save the wifi_ssid length and data
  42. size_t wifi_ssid_length = strlen(wifi_ssid) + 1; // Include null terminator
  43. if (storage_file_write(file, &wifi_ssid_length, sizeof(size_t)) != sizeof(size_t) ||
  44. storage_file_write(file, wifi_ssid, wifi_ssid_length) != wifi_ssid_length)
  45. {
  46. FURI_LOG_E(TAG, "Failed to write wifi_SSID");
  47. }
  48. // Save the wifi_password length and data
  49. size_t wifi_password_length = strlen(wifi_password) + 1; // Include null terminator
  50. if (storage_file_write(file, &wifi_password_length, sizeof(size_t)) != sizeof(size_t) ||
  51. storage_file_write(file, wifi_password, wifi_password_length) != wifi_password_length)
  52. {
  53. FURI_LOG_E(TAG, "Failed to write wifi_password");
  54. }
  55. // Save the username length and data
  56. size_t username_length = strlen(username) + 1; // Include null terminator
  57. if (storage_file_write(file, &username_length, sizeof(size_t)) != sizeof(size_t) ||
  58. storage_file_write(file, username, username_length) != username_length)
  59. {
  60. FURI_LOG_E(TAG, "Failed to write username");
  61. }
  62. // Save the password length and data
  63. size_t password_length = strlen(password) + 1; // Include null terminator
  64. if (storage_file_write(file, &password_length, sizeof(size_t)) != sizeof(size_t) ||
  65. storage_file_write(file, password, password_length) != password_length)
  66. {
  67. FURI_LOG_E(TAG, "Failed to write password");
  68. }
  69. storage_file_close(file);
  70. storage_file_free(file);
  71. furi_record_close(RECORD_STORAGE);
  72. }
  73. bool load_settings(
  74. char *wifi_ssid,
  75. size_t wifi_ssid_size,
  76. char *wifi_password,
  77. size_t wifi_password_size,
  78. char *username,
  79. size_t username_size,
  80. char *password,
  81. size_t password_size)
  82. {
  83. Storage *storage = furi_record_open(RECORD_STORAGE);
  84. File *file = storage_file_alloc(storage);
  85. if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
  86. {
  87. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
  88. storage_file_free(file);
  89. furi_record_close(RECORD_STORAGE);
  90. return false; // Return false if the file does not exist
  91. }
  92. // Load the wifi_ssid
  93. size_t wifi_ssid_length;
  94. if (storage_file_read(file, &wifi_ssid_length, sizeof(size_t)) != sizeof(size_t) || wifi_ssid_length > wifi_ssid_size ||
  95. storage_file_read(file, wifi_ssid, wifi_ssid_length) != wifi_ssid_length)
  96. {
  97. FURI_LOG_E(TAG, "Failed to read wifi_SSID");
  98. storage_file_close(file);
  99. storage_file_free(file);
  100. furi_record_close(RECORD_STORAGE);
  101. return false;
  102. }
  103. wifi_ssid[wifi_ssid_length - 1] = '\0'; // Ensure null-termination
  104. // Load the wifi_password
  105. size_t wifi_password_length;
  106. if (storage_file_read(file, &wifi_password_length, sizeof(size_t)) != sizeof(size_t) || wifi_password_length > wifi_password_size ||
  107. storage_file_read(file, wifi_password, wifi_password_length) != wifi_password_length)
  108. {
  109. FURI_LOG_E(TAG, "Failed to read wifi_password");
  110. storage_file_close(file);
  111. storage_file_free(file);
  112. furi_record_close(RECORD_STORAGE);
  113. return false;
  114. }
  115. wifi_password[wifi_password_length - 1] = '\0'; // Ensure null-termination
  116. // Load the username
  117. size_t username_length;
  118. if (storage_file_read(file, &username_length, sizeof(size_t)) != sizeof(size_t) || username_length > username_size ||
  119. storage_file_read(file, username, username_length) != username_length)
  120. {
  121. FURI_LOG_E(TAG, "Failed to read username");
  122. storage_file_close(file);
  123. storage_file_free(file);
  124. furi_record_close(RECORD_STORAGE);
  125. return false;
  126. }
  127. username[username_length - 1] = '\0'; // Ensure null-termination
  128. // Load the password
  129. size_t password_length;
  130. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  131. storage_file_read(file, password, password_length) != password_length)
  132. {
  133. FURI_LOG_E(TAG, "Failed to read password");
  134. storage_file_close(file);
  135. storage_file_free(file);
  136. furi_record_close(RECORD_STORAGE);
  137. return false;
  138. }
  139. password[password_length - 1] = '\0'; // Ensure null-termination
  140. storage_file_close(file);
  141. storage_file_free(file);
  142. furi_record_close(RECORD_STORAGE);
  143. return true;
  144. }
  145. bool save_char(
  146. const char *path_name, const char *value)
  147. {
  148. if (!value)
  149. {
  150. return false;
  151. }
  152. // Create the directory for saving settings
  153. char directory_path[256];
  154. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  155. // Create the directory
  156. Storage *storage = furi_record_open(RECORD_STORAGE);
  157. storage_common_mkdir(storage, directory_path);
  158. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data");
  159. storage_common_mkdir(storage, directory_path);
  160. // Open the settings file
  161. File *file = storage_file_alloc(storage);
  162. char file_path[256];
  163. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/%s.txt", path_name);
  164. // Open the file in write mode
  165. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  166. {
  167. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  168. storage_file_free(file);
  169. furi_record_close(RECORD_STORAGE);
  170. return false;
  171. }
  172. // Write the data to the file
  173. size_t data_size = strlen(value) + 1; // Include null terminator
  174. if (storage_file_write(file, value, data_size) != data_size)
  175. {
  176. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  177. storage_file_close(file);
  178. storage_file_free(file);
  179. furi_record_close(RECORD_STORAGE);
  180. return false;
  181. }
  182. storage_file_close(file);
  183. storage_file_free(file);
  184. furi_record_close(RECORD_STORAGE);
  185. return true;
  186. }
  187. bool load_char(
  188. const char *path_name,
  189. char *value,
  190. size_t value_size)
  191. {
  192. if (!value)
  193. {
  194. return false;
  195. }
  196. Storage *storage = furi_record_open(RECORD_STORAGE);
  197. File *file = storage_file_alloc(storage);
  198. char file_path[256];
  199. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data/%s.txt", path_name);
  200. // Open the file for reading
  201. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  202. {
  203. storage_file_free(file);
  204. furi_record_close(RECORD_STORAGE);
  205. return false; // Return false if the file does not exist
  206. }
  207. // Read data into the buffer
  208. size_t read_count = storage_file_read(file, value, value_size);
  209. if (storage_file_get_error(file) != FSE_OK)
  210. {
  211. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  212. storage_file_close(file);
  213. storage_file_free(file);
  214. furi_record_close(RECORD_STORAGE);
  215. return false;
  216. }
  217. // Ensure null-termination
  218. value[read_count - 1] = '\0';
  219. storage_file_close(file);
  220. storage_file_free(file);
  221. furi_record_close(RECORD_STORAGE);
  222. return strlen(value) > 0;
  223. }
  224. FuriString *load_furi_world(
  225. const char *name)
  226. {
  227. Storage *storage = furi_record_open(RECORD_STORAGE);
  228. File *file = storage_file_alloc(storage);
  229. char file_path[128];
  230. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  231. // Open the file for reading
  232. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  233. {
  234. storage_file_free(file);
  235. furi_record_close(RECORD_STORAGE);
  236. return NULL; // Return false if the file does not exist
  237. }
  238. // Allocate a FuriString to hold the received data
  239. FuriString *str_result = furi_string_alloc();
  240. if (!str_result)
  241. {
  242. FURI_LOG_E(HTTP_TAG, "Failed to allocate FuriString");
  243. storage_file_close(file);
  244. storage_file_free(file);
  245. furi_record_close(RECORD_STORAGE);
  246. return NULL;
  247. }
  248. // Reset the FuriString to ensure it's empty before reading
  249. furi_string_reset(str_result);
  250. // Define a buffer to hold the read data
  251. uint8_t *buffer = (uint8_t *)malloc(MAX_FILE_SHOW);
  252. if (!buffer)
  253. {
  254. FURI_LOG_E(HTTP_TAG, "Failed to allocate buffer");
  255. furi_string_free(str_result);
  256. storage_file_close(file);
  257. storage_file_free(file);
  258. furi_record_close(RECORD_STORAGE);
  259. return NULL;
  260. }
  261. // Read data into the buffer
  262. size_t read_count = storage_file_read(file, buffer, MAX_FILE_SHOW);
  263. if (storage_file_get_error(file) != FSE_OK)
  264. {
  265. FURI_LOG_E(HTTP_TAG, "Error reading from file.");
  266. furi_string_free(str_result);
  267. storage_file_close(file);
  268. storage_file_free(file);
  269. furi_record_close(RECORD_STORAGE);
  270. return NULL;
  271. }
  272. // Append each byte to the FuriString
  273. for (size_t i = 0; i < read_count; i++)
  274. {
  275. furi_string_push_back(str_result, buffer[i]);
  276. }
  277. // Check if there is more data beyond the maximum size
  278. char extra_byte;
  279. storage_file_read(file, &extra_byte, 1);
  280. // Clean up
  281. storage_file_close(file);
  282. storage_file_free(file);
  283. furi_record_close(RECORD_STORAGE);
  284. free(buffer);
  285. return str_result;
  286. }
  287. bool world_exists(const char *name)
  288. {
  289. if (!name)
  290. {
  291. FURI_LOG_E(TAG, "Invalid name");
  292. return false;
  293. }
  294. Storage *storage = furi_record_open(RECORD_STORAGE);
  295. if (!storage)
  296. {
  297. FURI_LOG_E(TAG, "Failed to open storage");
  298. return false;
  299. }
  300. char file_path[256];
  301. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", name);
  302. bool does_exist = storage_file_exists(storage, file_path);
  303. // Clean up
  304. furi_record_close(RECORD_STORAGE);
  305. return does_exist;
  306. }
  307. bool save_world_names(const FuriString *json)
  308. {
  309. // Create the directory for saving settings
  310. char directory_path[256];
  311. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  312. // Create the directory
  313. Storage *storage = furi_record_open(RECORD_STORAGE);
  314. storage_common_mkdir(storage, directory_path);
  315. // Open the settings file
  316. File *file = storage_file_alloc(storage);
  317. char file_path[128];
  318. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  319. // Open the file in write mode
  320. if (!storage_file_open(file, file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS))
  321. {
  322. FURI_LOG_E(HTTP_TAG, "Failed to open file for writing: %s", file_path);
  323. storage_file_free(file);
  324. furi_record_close(RECORD_STORAGE);
  325. return false;
  326. }
  327. // Write the data to the file
  328. size_t data_size = furi_string_size(json) + 1; // Include null terminator
  329. if (storage_file_write(file, furi_string_get_cstr(json), data_size) != data_size)
  330. {
  331. FURI_LOG_E(HTTP_TAG, "Failed to append data to file");
  332. storage_file_close(file);
  333. storage_file_free(file);
  334. furi_record_close(RECORD_STORAGE);
  335. return false;
  336. }
  337. storage_file_close(file);
  338. storage_file_free(file);
  339. furi_record_close(RECORD_STORAGE);
  340. return true;
  341. }
  342. static FuriString *flip_social_info(char *key)
  343. {
  344. char ssid[64];
  345. char password[64];
  346. char login_username_logged_out[64];
  347. char login_username_logged_in[64];
  348. char login_password_logged_out[64];
  349. char change_password_logged_in[64];
  350. char change_bio_logged_in[64];
  351. char is_logged_in[64];
  352. FuriString *result = furi_string_alloc();
  353. if (!result)
  354. {
  355. FURI_LOG_E(TAG, "Failed to allocate FuriString");
  356. return NULL;
  357. }
  358. if (!load_flip_social_settings(ssid, sizeof(ssid), password, sizeof(password), login_username_logged_out, sizeof(login_username_logged_out), login_username_logged_in, sizeof(login_username_logged_in), login_password_logged_out, sizeof(login_password_logged_out), change_password_logged_in, sizeof(change_password_logged_in), change_bio_logged_in, sizeof(change_bio_logged_in), is_logged_in, sizeof(is_logged_in)))
  359. {
  360. FURI_LOG_E(TAG, "Failed to load flip social settings");
  361. return NULL;
  362. }
  363. if (strcmp(key, "ssid") == 0)
  364. {
  365. furi_string_set_str(result, ssid);
  366. }
  367. else if (strcmp(key, "password") == 0)
  368. {
  369. furi_string_set_str(result, password);
  370. }
  371. else if (strcmp(key, "login_username_logged_out") == 0)
  372. {
  373. furi_string_set_str(result, login_username_logged_out);
  374. }
  375. else if (strcmp(key, "login_username_logged_in") == 0)
  376. {
  377. furi_string_set_str(result, login_username_logged_in);
  378. }
  379. else if (strcmp(key, "login_password_logged_out") == 0)
  380. {
  381. furi_string_set_str(result, login_password_logged_out);
  382. }
  383. else if (strcmp(key, "change_password_logged_in") == 0)
  384. {
  385. furi_string_set_str(result, change_password_logged_in);
  386. }
  387. else if (strcmp(key, "change_bio_logged_in") == 0)
  388. {
  389. furi_string_set_str(result, change_bio_logged_in);
  390. }
  391. else if (strcmp(key, "is_logged_in") == 0)
  392. {
  393. furi_string_set_str(result, is_logged_in);
  394. }
  395. else
  396. {
  397. FURI_LOG_E(TAG, "Invalid key");
  398. furi_string_free(result);
  399. return NULL;
  400. }
  401. return result;
  402. }
  403. bool is_logged_in_to_flip_social()
  404. {
  405. // load flip social settings and check if logged in
  406. FuriString *is_logged_in = flip_social_info("is_logged_in");
  407. if (!is_logged_in)
  408. {
  409. FURI_LOG_E(TAG, "Failed to load is_logged_in");
  410. return false;
  411. }
  412. if (furi_string_cmp(is_logged_in, "true") == 0)
  413. {
  414. // copy the logged_in FlipSocaial settings to FlipWorld
  415. FuriString *username = flip_social_info("login_username_logged_in");
  416. FuriString *password = flip_social_info("change_password_logged_in");
  417. FuriString *wifi_password = flip_social_info("password");
  418. FuriString *wifi_ssid = flip_social_info("ssid");
  419. if (!username || !password || !wifi_password || !wifi_ssid)
  420. {
  421. furi_string_free(username);
  422. furi_string_free(password);
  423. furi_string_free(wifi_password);
  424. furi_string_free(wifi_ssid);
  425. return false;
  426. }
  427. save_settings(furi_string_get_cstr(wifi_ssid), furi_string_get_cstr(wifi_password), furi_string_get_cstr(username), furi_string_get_cstr(password));
  428. furi_string_free(username);
  429. furi_string_free(password);
  430. furi_string_free(wifi_password);
  431. furi_string_free(wifi_ssid);
  432. furi_string_free(is_logged_in);
  433. return true;
  434. }
  435. furi_string_free(is_logged_in);
  436. return false;
  437. }
  438. bool is_logged_in()
  439. {
  440. char is_logged_in[64];
  441. if (load_char("is_logged_in", is_logged_in, sizeof(is_logged_in)))
  442. {
  443. return strcmp(is_logged_in, "true") == 0;
  444. }
  445. return false;
  446. }
  447. static bool load_flip_social_settings(
  448. char *ssid,
  449. size_t ssid_size,
  450. char *password,
  451. size_t password_size,
  452. char *login_username_logged_out,
  453. size_t username_out_size,
  454. char *login_username_logged_in,
  455. size_t username_in_size,
  456. char *login_password_logged_out,
  457. size_t password_out_size,
  458. char *change_password_logged_in,
  459. size_t change_password_size,
  460. char *change_bio_logged_in,
  461. size_t change_bio_size,
  462. char *is_logged_in,
  463. size_t is_logged_in_size)
  464. {
  465. Storage *storage = furi_record_open(RECORD_STORAGE);
  466. File *file = storage_file_alloc(storage);
  467. // file path from flipsocial
  468. char file_path[128];
  469. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/settings.bin");
  470. if (!storage_file_open(file, file_path, FSAM_READ, FSOM_OPEN_EXISTING))
  471. {
  472. FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", file_path);
  473. storage_file_free(file);
  474. furi_record_close(RECORD_STORAGE);
  475. return false; // Return false if the file does not exist
  476. }
  477. // Load the ssid
  478. size_t ssid_length;
  479. if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
  480. storage_file_read(file, ssid, ssid_length) != ssid_length)
  481. {
  482. FURI_LOG_E(TAG, "Failed to read SSID");
  483. storage_file_close(file);
  484. storage_file_free(file);
  485. furi_record_close(RECORD_STORAGE);
  486. return false;
  487. }
  488. else
  489. {
  490. ssid[ssid_length - 1] = '\0'; // Ensure null-termination
  491. }
  492. // Load the password
  493. size_t password_length;
  494. if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
  495. storage_file_read(file, password, password_length) != password_length)
  496. {
  497. FURI_LOG_E(TAG, "Failed to read password");
  498. storage_file_close(file);
  499. storage_file_free(file);
  500. furi_record_close(RECORD_STORAGE);
  501. return false;
  502. }
  503. else
  504. {
  505. password[password_length - 1] = '\0'; // Ensure null-termination
  506. }
  507. // Load the login_username_logged_out
  508. size_t username_out_length;
  509. if (storage_file_read(file, &username_out_length, sizeof(size_t)) != sizeof(size_t) || username_out_length > username_out_size ||
  510. storage_file_read(file, login_username_logged_out, username_out_length) != username_out_length)
  511. {
  512. FURI_LOG_E(TAG, "Failed to read login_username_logged_out");
  513. // storage_file_close(file);
  514. // storage_file_free(file);
  515. // furi_record_close(RECORD_STORAGE);
  516. // return false;
  517. }
  518. else
  519. {
  520. login_username_logged_out[username_out_length - 1] = '\0'; // Ensure null-termination
  521. }
  522. // Load the login_username_logged_in
  523. size_t username_in_length;
  524. if (storage_file_read(file, &username_in_length, sizeof(size_t)) != sizeof(size_t) || username_in_length > username_in_size ||
  525. storage_file_read(file, login_username_logged_in, username_in_length) != username_in_length)
  526. {
  527. FURI_LOG_E(TAG, "Failed to read login_username_logged_in");
  528. // storage_file_close(file);
  529. // storage_file_free(file);
  530. // furi_record_close(RECORD_STORAGE);
  531. // return false;
  532. }
  533. else
  534. {
  535. login_username_logged_in[username_in_length - 1] = '\0'; // Ensure null-termination
  536. }
  537. // Load the login_password_logged_out
  538. size_t password_out_length;
  539. if (storage_file_read(file, &password_out_length, sizeof(size_t)) != sizeof(size_t) || password_out_length > password_out_size ||
  540. storage_file_read(file, login_password_logged_out, password_out_length) != password_out_length)
  541. {
  542. FURI_LOG_E(TAG, "Failed to read login_password_logged_out");
  543. // storage_file_close(file);
  544. // storage_file_free(file);
  545. // furi_record_close(RECORD_STORAGE);
  546. // return false;
  547. }
  548. else
  549. {
  550. login_password_logged_out[password_out_length - 1] = '\0'; // Ensure null-termination
  551. }
  552. // Load the change_password_logged_in
  553. size_t change_password_length;
  554. if (storage_file_read(file, &change_password_length, sizeof(size_t)) != sizeof(size_t) || change_password_length > change_password_size ||
  555. storage_file_read(file, change_password_logged_in, change_password_length) != change_password_length)
  556. {
  557. FURI_LOG_E(TAG, "Failed to read change_password_logged_in");
  558. // storage_file_close(file);
  559. // storage_file_free(file);
  560. // furi_record_close(RECORD_STORAGE);
  561. // return false;
  562. }
  563. else
  564. {
  565. change_password_logged_in[change_password_length - 1] = '\0'; // Ensure null-termination
  566. }
  567. // Load the is_logged_in
  568. size_t is_logged_in_length;
  569. if (storage_file_read(file, &is_logged_in_length, sizeof(size_t)) != sizeof(size_t) || is_logged_in_length > is_logged_in_size ||
  570. storage_file_read(file, is_logged_in, is_logged_in_length) != is_logged_in_length)
  571. {
  572. FURI_LOG_E(TAG, "Failed to read is_logged_in");
  573. // storage_file_close(file);
  574. // storage_file_free(file);
  575. // furi_record_close(RECORD_STORAGE);
  576. // return false;
  577. }
  578. else
  579. {
  580. is_logged_in[is_logged_in_length - 1] = '\0'; // Ensure null-termination
  581. }
  582. // Load the change_bio_logged_in
  583. size_t change_bio_length;
  584. if (storage_file_read(file, &change_bio_length, sizeof(size_t)) != sizeof(size_t) || change_bio_length > change_bio_size ||
  585. storage_file_read(file, change_bio_logged_in, change_bio_length) != change_bio_length)
  586. {
  587. FURI_LOG_E(TAG, "Failed to read change_bio_logged_in");
  588. // storage_file_close(file);
  589. // storage_file_free(file);
  590. // furi_record_close(RECORD_STORAGE);
  591. // return false;
  592. }
  593. else
  594. {
  595. change_bio_logged_in[change_bio_length - 1] = '\0'; // Ensure null-termination
  596. }
  597. storage_file_close(file);
  598. storage_file_free(file);
  599. furi_record_close(RECORD_STORAGE);
  600. return true;
  601. }