From 14a8bdb874f537c98dca940ca55ce51db8c17571 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 7 Nov 2015 21:38:32 -0500 Subject: [PATCH] Store temp data in same directory as original file --- src/friendlist.c | 16 +++++++++++++--- src/toxic.c | 13 +++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/friendlist.c b/src/friendlist.c index c752790..546b6f9 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -125,7 +125,13 @@ void kill_friendlist(void) realloc_friends(0); } -#define TEMP_BLOCKLIST_SAVE_NAME "toxic_blocklist.tmp" +/* Saves the blocklist to path. If there are no items in the blocklist the + * empty file will be removed. + * + * Returns 0 if stored successfully. + * Returns -1 on failure. + */ +#define TEMP_BLOCKLIST_EXT ".tmp" static int save_blocklist(char *path) { if (path == NULL) @@ -165,19 +171,23 @@ static int save_blocklist(char *path) return 0; } - FILE *fp = fopen(TEMP_BLOCKLIST_SAVE_NAME, "wb"); + char temp_path[strlen(path) + strlen(TEMP_BLOCKLIST_EXT) + 1]; + snprintf(temp_path, sizeof(temp_path), "%s%s", path, TEMP_BLOCKLIST_EXT); + + FILE *fp = fopen(temp_path, "wb"); if (fp == NULL) return -1; if (fwrite(data, len, 1, fp) != 1) { + fprintf(stderr, "Failed to write blocklist data.\n"); fclose(fp); return -1; } fclose(fp); - if (rename(TEMP_BLOCKLIST_SAVE_NAME, path) != 0) + if (rename(temp_path, path) != 0) return -1; return 0; diff --git a/src/toxic.c b/src/toxic.c index 2e57183..6fd04d0 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -537,18 +537,21 @@ static void first_time_encrypt(const char *msg) system("clear"); } -/* Store Tox profile data to given location +/* Store Tox profile data to path. * * Return 0 if stored successfully. * Return -1 on error. */ -#define TEMP_PROFILE_SAVE_NAME "toxic_profile.tmp" +#define TEMP_PROFILE_EXT ".tmp" int store_data(Tox *m, const char *path) { if (path == NULL) return -1; - FILE *fp = fopen(TEMP_PROFILE_SAVE_NAME, "wb"); + char temp_path[strlen(path) + strlen(TEMP_PROFILE_EXT) + 1]; + snprintf(temp_path, sizeof(temp_path), "%s%s", path, TEMP_PROFILE_EXT); + + FILE *fp = fopen(temp_path, "wb"); if (fp == NULL) return -1; @@ -573,11 +576,13 @@ int store_data(Tox *m, const char *path) } if (fwrite(enc_data, enc_len, 1, fp) != 1) { + fprintf(stderr, "Failed to write profile data.\n"); fclose(fp); return -1; } } else { /* data will not be encrypted */ if (fwrite(data, data_len, 1, fp) != 1) { + fprintf(stderr, "Failed to write profile data.\n"); fclose(fp); return -1; } @@ -585,7 +590,7 @@ int store_data(Tox *m, const char *path) fclose(fp); - if (rename(TEMP_PROFILE_SAVE_NAME, path) != 0) + if (rename(temp_path, path) != 0) return -1; return 0;