1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 16:43:27 +01:00

Store temp data in same directory as original file

This commit is contained in:
Jfreegman 2015-11-07 21:38:32 -05:00
parent 93a73cbef2
commit 14a8bdb874
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
2 changed files with 22 additions and 7 deletions

View File

@ -125,7 +125,13 @@ void kill_friendlist(void)
realloc_friends(0); 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) static int save_blocklist(char *path)
{ {
if (path == NULL) if (path == NULL)
@ -165,19 +171,23 @@ static int save_blocklist(char *path)
return 0; 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) if (fp == NULL)
return -1; return -1;
if (fwrite(data, len, 1, fp) != 1) { if (fwrite(data, len, 1, fp) != 1) {
fprintf(stderr, "Failed to write blocklist data.\n");
fclose(fp); fclose(fp);
return -1; return -1;
} }
fclose(fp); fclose(fp);
if (rename(TEMP_BLOCKLIST_SAVE_NAME, path) != 0) if (rename(temp_path, path) != 0)
return -1; return -1;
return 0; return 0;

View File

@ -537,18 +537,21 @@ static void first_time_encrypt(const char *msg)
system("clear"); system("clear");
} }
/* Store Tox profile data to given location /* Store Tox profile data to path.
* *
* Return 0 if stored successfully. * Return 0 if stored successfully.
* Return -1 on error. * 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) int store_data(Tox *m, const char *path)
{ {
if (path == NULL) if (path == NULL)
return -1; 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) if (fp == NULL)
return -1; return -1;
@ -573,11 +576,13 @@ int store_data(Tox *m, const char *path)
} }
if (fwrite(enc_data, enc_len, 1, fp) != 1) { if (fwrite(enc_data, enc_len, 1, fp) != 1) {
fprintf(stderr, "Failed to write profile data.\n");
fclose(fp); fclose(fp);
return -1; return -1;
} }
} else { /* data will not be encrypted */ } else { /* data will not be encrypted */
if (fwrite(data, data_len, 1, fp) != 1) { if (fwrite(data, data_len, 1, fp) != 1) {
fprintf(stderr, "Failed to write profile data.\n");
fclose(fp); fclose(fp);
return -1; return -1;
} }
@ -585,7 +590,7 @@ int store_data(Tox *m, const char *path)
fclose(fp); fclose(fp);
if (rename(TEMP_PROFILE_SAVE_NAME, path) != 0) if (rename(temp_path, path) != 0)
return -1; return -1;
return 0; return 0;