From db1a2f604988c36a9a25cec8218e320f650408ec Mon Sep 17 00:00:00 2001 From: FRIGN Date: Mon, 22 Sep 2014 15:16:28 +0200 Subject: Refactor fifoflush() After the previous simplification of return-values of fiforead() (exit if there's a more serious issue), fifoflush() can easily be replaced with a oneliner calling fiforead until it returns 0. In case 0 is returned, the underlying fifo will have already been reopened. --- ratox.c | 49 ++++++++++--------------------------------------- 1 file changed, 10 insertions(+), 39 deletions(-) (limited to 'ratox.c') diff --git a/ratox.c b/ratox.c index 74edfa6..cd9aee2 100644 --- a/ratox.c +++ b/ratox.c @@ -165,12 +165,12 @@ static Tox *tox; static Tox_Options toxopt; static uint8_t *passphrase; static uint32_t pplen; +static uint8_t toilet[BUFSIZ]; static int running = 1; static int ipv6; static void printrat(void); static void printout(const char *, ...); -static void fifoflush(int); static ssize_t fiforead(int, int *, struct file, void *, size_t); static void cbconnstatus(Tox *, int32_t, uint8_t, void *); static void cbfriendmessage(Tox *, int32_t, const uint8_t *, uint16_t, void *); @@ -253,26 +253,6 @@ again: return r; } -static void -fifoflush(int fd) -{ - char buf[BUFSIZ]; - ssize_t n; - - /* Flush the FIFO */ - while (1) { - n = read(fd, buf, sizeof(buf)); - if (n < 0) { - if (errno == EINTR || errno == EWOULDBLOCK) - continue; - perror("read"); - exit(EXIT_FAILURE); - } - if (n == 0) - break; - } -} - static void cbconnstatus(Tox *m, int32_t fid, uint8_t status, void *udata) { @@ -437,7 +417,6 @@ cbfilecontrol(Tox *m, int32_t fid, uint8_t rec_sen, uint8_t fnum, uint8_t ctrlty const uint8_t *data, uint16_t len, void *udata) { struct friend *f; - int r; TAILQ_FOREACH(f, &friendhead, entry) if (f->fid == fid) @@ -480,14 +459,10 @@ cbfilecontrol(Tox *m, int32_t fid, uint8_t rec_sen, uint8_t fnum, uint8_t ctrlty f->t.state = TRANSFER_NONE; free(f->t.buf); f->t.buf = NULL; - fifoflush(f->fd[FFILE_IN]); - close(f->fd[FFILE_IN]); - r = openat(f->dirfd, ffiles[FFILE_IN].name, ffiles[FFILE_IN].flags, 0644); - if (r < 0) { - perror("open"); - exit(EXIT_FAILURE); - } - f->fd[FFILE_IN] = r; + + /* flush the FIFO */ + while (fiforead(f->dirfd, &f->fd[FFILE_IN], ffiles[FFILE_IN], + toilet, sizeof(toilet))); } break; case TOX_FILECONTROL_FINISHED: @@ -1026,7 +1001,7 @@ loop(void) struct request *req, *rtmp; time_t t0, t1; int connected = 0; - int i, n, r; + int i, n; int fdmax; char c; fd_set rfds; @@ -1102,14 +1077,10 @@ loop(void) f->t.state = TRANSFER_NONE; free(f->t.buf); f->t.buf = NULL; - fifoflush(f->fd[FFILE_IN]); - close(f->fd[FFILE_IN]); - r = openat(f->dirfd, ffiles[FFILE_IN].name, ffiles[FFILE_IN].flags, 0644); - if (r < 0) { - perror("open"); - exit(EXIT_FAILURE); - } - f->fd[FFILE_IN] = r; + + /* flush the FIFO */ + while (fiforead(f->dirfd, &f->fd[FFILE_IN], ffiles[FFILE_IN], + toilet, sizeof(toilet))); } } } -- cgit v1.2.3