mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-22 16:03:03 +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:
parent
a95fc7824c
commit
dfff777283
@ -26,6 +26,7 @@
|
||||
#include <time.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <tox/tox.h>
|
||||
|
||||
@ -124,26 +125,20 @@ void kill_friendlist(void)
|
||||
realloc_friends(0);
|
||||
}
|
||||
|
||||
#define TEMP_BLOCKLIST_SAVE_NAME "toxic_blocklist.tmp"
|
||||
static int save_blocklist(char *path)
|
||||
{
|
||||
if (path == NULL)
|
||||
return -1;
|
||||
|
||||
int len = sizeof(BlockedFriend) * Blocked.num_blocked;
|
||||
char *data = malloc(len);
|
||||
char data[len];
|
||||
|
||||
if (data == NULL)
|
||||
exit_toxic_err("Failed in save_blocklist", FATALERR_MEMORY);
|
||||
|
||||
int i;
|
||||
|
||||
int count = 0;
|
||||
int i, count = 0;
|
||||
|
||||
for (i = 0; i < Blocked.max_idx; ++i) {
|
||||
if (count > Blocked.num_blocked) {
|
||||
free(data);
|
||||
if (count > Blocked.num_blocked)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (Blocked.list[i].active) {
|
||||
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 (remove(path) != 0)
|
||||
return -1;
|
||||
|
||||
if (fp == NULL) {
|
||||
free(data);
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
FILE *fp = fopen(TEMP_BLOCKLIST_SAVE_NAME, "wb");
|
||||
|
||||
if (fp == NULL)
|
||||
return -1;
|
||||
|
||||
if (fwrite(data, len, 1, fp) != 1) {
|
||||
fclose(fp);
|
||||
free(data);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
free(data);
|
||||
|
||||
if (rename(TEMP_BLOCKLIST_SAVE_NAME, path) != 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -199,22 +202,15 @@ int load_blocklist(char *path)
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *data = malloc(len);
|
||||
|
||||
if (data == NULL) {
|
||||
fclose(fp);
|
||||
exit_toxic_err("Failed in load_blocklist", FATALERR_MEMORY);
|
||||
}
|
||||
char data[len];
|
||||
|
||||
if (fread(data, len, 1, fp) != 1) {
|
||||
fclose(fp);
|
||||
free(data);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (len % sizeof(BlockedFriend) != 0) {
|
||||
fclose(fp);
|
||||
free(data);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -244,7 +240,6 @@ int load_blocklist(char *path)
|
||||
++Blocked.num_blocked;
|
||||
}
|
||||
|
||||
free(data);
|
||||
fclose(fp);
|
||||
sort_blocklist_index();
|
||||
|
||||
@ -1098,7 +1093,7 @@ static void friendlist_onAV(ToxWindow *self, ToxAV *av, uint32_t friend_number,
|
||||
|
||||
assert(0);
|
||||
Tox *m = toxav_get_tox(av);
|
||||
|
||||
|
||||
if (Friends.list[friend_number].chatwin == -1) {
|
||||
if (get_num_active_windows() < MAX_WINDOWS_NUM) {
|
||||
if(state != TOXAV_FRIEND_CALL_STATE_FINISHED) {
|
||||
|
16
src/toxic.c
16
src/toxic.c
@ -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)) {
|
||||
|
Loading…
Reference in New Issue
Block a user