ratox

FIFO based tox client
git clone git://git.2f30.org/ratox
Log | Files | Refs | README | LICENSE

commit 1ae2de113e476260ccd318ece1c0aa6bed7b452d
parent db1a2f604988c36a9a25cec8218e320f650408ec
Author: FRIGN <dev@frign.de>
Date:   Mon, 22 Sep 2014 16:17:28 +0200

Refactor dataload()

Use fd's instead of fp's and simplify the case-handling for encryption.

Diffstat:
Mratox.c | 61+++++++++++++++++++++++--------------------------------------
1 file changed, 23 insertions(+), 38 deletions(-)

diff --git a/ratox.c b/ratox.c @@ -553,21 +553,19 @@ readpass(const char *prompt) static void dataload(void) { - FILE *fp; - size_t sz; + off_t sz; uint8_t *data; - int r; + int fd; - fp = fopen(DATAFILE, "r"); - if (!fp) { + fd = open(DATAFILE, 0444); + if (fd < 0) { if (encryptdatafile == 1) - while (readpass("New password: ") == -1); + while (readpass("New passphrase: ") == -1); return; } - fseek(fp, 0, SEEK_END); - sz = ftell(fp); - rewind(fp); + sz = lseek(fd, 0, SEEK_END); + lseek(fd, 0, SEEK_SET); data = malloc(sz); if (!data) { @@ -575,44 +573,31 @@ dataload(void) exit(EXIT_FAILURE); } - if (fread(data, 1, sz, fp) != sz || ferror(fp)) { - fprintf(stderr, "failed to read %s\n", DATAFILE); + if (read(fd, data, sz) != sz) { + perror("read"); exit(EXIT_FAILURE); } - if (encryptdatafile == 1) { - if (tox_is_data_encrypted(data) == 1) { - while (readpass("Password: ") == -1 || - tox_encrypted_load(tox, data, sz, passphrase, pplen) < 0) - ; - } else { - printout("%s is not encrypted, forcing encrypted format\n", - DATAFILE); - while (readpass("New password: ") == -1) - ; - r = tox_load(tox, data, sz); - if (r < 0) { - fprintf(stderr, "tox_load() failed\n"); - exit(EXIT_FAILURE); - } + if (tox_is_data_encrypted(data) == 1) { + while (readpass("Passphrase: ") < 0 || + tox_encrypted_load(tox, data, sz, passphrase, pplen) < 0); + if (encryptdatafile == 0) { + printout("%s is encrypted, but saving in plain format\n", DATAFILE); } } else { - if (tox_is_data_encrypted(data) == 0) { - r = tox_load(tox, data, sz); - if (r < 0) { - fprintf(stderr, "tox_load() failed\n"); - exit(EXIT_FAILURE); - } - } else { - printout("%s is encrypted, forcing plain format\n", DATAFILE); - while (readpass("Password: ") == -1 || - tox_encrypted_load(tox, data, sz, passphrase, pplen) < 0) - ; + if (tox_load(tox, data, sz) < 0) { + fprintf(stderr, "tox_load() failed\n"); + exit(EXIT_FAILURE); + } + if (encryptdatafile == 1) { + printout("%s is not encrypted, but saving in encrypted format\n", DATAFILE); + while (readpass("New passphrase: ") < 0); } + } free(data); - fclose(fp); + close(fd); } static void