tUse item in cache instead of redownloading it - sacc - sacc (saccomys): simple… | |
Log | |
Files | |
Refs | |
LICENSE | |
--- | |
commit dbc5c018d5773da8514ff933815c9612d20b2677 | |
parent 74347a93e3ab3f90c310b2f4d457fb9717401c65 | |
Author: Quentin Rameau <[email protected]> | |
Date: Wed, 26 Jul 2017 17:40:10 +0200 | |
Use item in cache instead of redownloading it | |
Diffstat: | |
sacc.c | 32 +++++++++++++++++++++++-------- | |
1 file changed, 24 insertions(+), 8 deletions(-) | |
--- | |
diff --git a/sacc.c b/sacc.c | |
t@@ -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; | |
} | |
t@@ -385,7 +392,7 @@ download(Item *item, int dest) | |
errno = 0; | |
} | |
- close(sock); | |
+ close(src); | |
return (r == 0 && w == 0); | |
} | |
t@@ -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; | |
t@@ -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)); | |
t@@ -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); |