1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-14 23:03:02 +01: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
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
2 changed files with 32 additions and 31 deletions

View File

@ -26,6 +26,7 @@
#include <time.h> #include <time.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <assert.h> #include <assert.h>
#include <errno.h>
#include <tox/tox.h> #include <tox/tox.h>
@ -124,26 +125,20 @@ void kill_friendlist(void)
realloc_friends(0); realloc_friends(0);
} }
#define TEMP_BLOCKLIST_SAVE_NAME "toxic_blocklist.tmp"
static int save_blocklist(char *path) static int save_blocklist(char *path)
{ {
if (path == NULL) if (path == NULL)
return -1; return -1;
int len = sizeof(BlockedFriend) * Blocked.num_blocked; int len = sizeof(BlockedFriend) * Blocked.num_blocked;
char *data = malloc(len); char data[len];
if (data == NULL) int i, count = 0;
exit_toxic_err("Failed in save_blocklist", FATALERR_MEMORY);
int i;
int count = 0;
for (i = 0; i < Blocked.max_idx; ++i) { for (i = 0; i < Blocked.max_idx; ++i) {
if (count > Blocked.num_blocked) { if (count > Blocked.num_blocked)
free(data);
return -1; return -1;
}
if (Blocked.list[i].active) { if (Blocked.list[i].active) {
BlockedFriend tmp; BlockedFriend tmp;
@ -162,21 +157,29 @@ static int save_blocklist(char *path)
} }
} }
FILE *fp = fopen(path, "wb"); /* Blocklist is empty, we can remove the empty file */
if (count == 0) {
if (fp == NULL) { if (remove(path) != 0)
free(data);
return -1; return -1;
return 0;
} }
FILE *fp = fopen(TEMP_BLOCKLIST_SAVE_NAME, "wb");
if (fp == NULL)
return -1;
if (fwrite(data, len, 1, fp) != 1) { if (fwrite(data, len, 1, fp) != 1) {
fclose(fp); fclose(fp);
free(data);
return -1; return -1;
} }
fclose(fp); fclose(fp);
free(data);
if (rename(TEMP_BLOCKLIST_SAVE_NAME, path) != 0)
return -1;
return 0; return 0;
} }
@ -199,22 +202,15 @@ int load_blocklist(char *path)
return -1; return -1;
} }
char *data = malloc(len); char data[len];
if (data == NULL) {
fclose(fp);
exit_toxic_err("Failed in load_blocklist", FATALERR_MEMORY);
}
if (fread(data, len, 1, fp) != 1) { if (fread(data, len, 1, fp) != 1) {
fclose(fp); fclose(fp);
free(data);
return -1; return -1;
} }
if (len % sizeof(BlockedFriend) != 0) { if (len % sizeof(BlockedFriend) != 0) {
fclose(fp); fclose(fp);
free(data);
return -1; return -1;
} }
@ -244,7 +240,6 @@ int load_blocklist(char *path)
++Blocked.num_blocked; ++Blocked.num_blocked;
} }
free(data);
fclose(fp); fclose(fp);
sort_blocklist_index(); sort_blocklist_index();

View File

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