1
0
mirror of https://github.com/Tha14/toxic.git synced 2025-07-03 12:16:44 +02:00

Save data in a safer manner

By saving to a temp file then renaming it we avoid the possibility of data corruption
due to an interrupt (hard reboot, power outage etc.)
This commit is contained in:
Jfreegman
2015-11-05 17:46:00 -05:00
parent a95fc7824c
commit dfff777283
2 changed files with 32 additions and 31 deletions

View File

@ -84,7 +84,7 @@ ToxWindow *prompt = NULL;
#define DATANAME "toxic_profile.tox"
#define BLOCKNAME "toxic_blocklist"
#define AUTOSAVE_FREQ 60
#define AUTOSAVE_FREQ 600
#define MIN_PASSWORD_LEN 6
#define MAX_PASSWORD_LEN 64
@ -537,17 +537,18 @@ static void first_time_encrypt(const char *msg)
system("clear");
}
/* Store Tox data to given location
/* Store Tox profile data to given location
*
* Return 0 if stored successfully or ignoring data file.
* Return -1 on error
* Return 0 if stored successfully.
* Return -1 on error.
*/
#define TEMP_PROFILE_SAVE_NAME "toxic_profile.tmp"
int store_data(Tox *m, const char *path)
{
if (path == NULL)
return -1;
FILE *fp = fopen(path, "wb");
FILE *fp = fopen(TEMP_PROFILE_SAVE_NAME, "wb");
if (fp == NULL)
return -1;
@ -583,6 +584,10 @@ int store_data(Tox *m, const char *path)
}
fclose(fp);
if (rename(TEMP_PROFILE_SAVE_NAME, path) != 0)
return -1;
return 0;
}
@ -1325,6 +1330,7 @@ int main(int argc, char **argv)
while (true) {
do_toxic(m, prompt);
uint64_t cur_time = get_unix_time();
if (timed_out(last_save, AUTOSAVE_FREQ)) {