Use item in cache instead of redownloading it - sacc - sacc(omys), simple console gopher client HTML git clone git://bitreich.org/sacc/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/sacc/ DIR Log DIR Files DIR Refs DIR Tags DIR LICENSE --- DIR commit dbc5c018d5773da8514ff933815c9612d20b2677 DIR parent 74347a93e3ab3f90c310b2f4d457fb9717401c65 HTML Author: Quentin Rameau <quinq@fifth.space> Date: Wed, 26 Jul 2017 17:40:10 +0200 Use item in cache instead of redownloading it Diffstat: M sacc.c | 32 +++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) --- DIR diff --git a/sacc.c b/sacc.c @@ -369,12 +369,19 @@ download(Item *item, int dest) { char buf[BUFSIZ]; ssize_t r, w; - int sock; - - sock = connectto(item->host, item->port); - sendselector(sock, item->selector); + int src; + + if (!item->tag) { + src = connectto(item->host, item->port); + sendselector(src, item->selector); + } else if ((src = open(item->tag, O_RDONLY)) < 0) { + printf("Can't open source file %s: %s\n", + item->tag, strerror(errno)); + errno = 0; + return 0; + } - while ((r = read(sock, buf, BUFSIZ)) > 0) { + while ((r = read(src, buf, BUFSIZ)) > 0) { while ((w = write(dest, buf, r)) > 0) r -= w; } @@ -385,7 +392,7 @@ download(Item *item, int dest) errno = 0; } - close(sock); + close(src); return (r == 0 && w == 0); } @@ -393,7 +400,7 @@ download(Item *item, int dest) static void downloaditem(Item *item) { - char *file, *path; + char *file, *path, *tag; mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP; int dest; @@ -410,6 +417,14 @@ downloaditem(Item *item) else path = xstrdup(file); + if (tag = item->tag) { + if (access(tag, R_OK) < 0) { + clear(&item->tag); + } else if (!strcmp(tag, path)) { + goto cleanup; + } + } + if ((dest = open(path, O_WRONLY|O_CREAT|O_EXCL, mode)) < 0) { printf("Can't open destination file %s: %s\n", path, strerror(errno)); @@ -420,7 +435,8 @@ downloaditem(Item *item) if (!download(item, dest)) goto cleanup; - item->tag = path; + if (!item->tag) + item->tag = path; return; cleanup: free(path);