repo

List, download and sync packs with remote repositories
Log | Files | Refs

commit a3970bdfaa5cd9b7300827205f9875fc29ec736b
parent b7ea949712bf597433985871df74f03857f52e03
Author: z3bra <contactatz3bradotorg>
Date:   Thu Dec 15 17:29:05 +0100

Make remote_sync() implementation simpler

The process of openning a file to write has now been moved OUTSIDE the
remote_sync() function to make the function more generic and thus
simpler to use.

Diffstat:
repo.c | 100++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 58 insertions(+), 42 deletions(-)
diff --git a/repo.c b/repo.c @@ -34,7 +34,7 @@ struct pack *pack_load(char *); struct repo *add_repo(struct repos *, char *); int local_load(struct packs *, char *); int local_list(char *); -int remote_sync(char *, char *, char *); +int remote_sync(char *, FILE *); void usage(char *name) @@ -117,6 +117,7 @@ local_load(struct packs *plist, char *local) TAILQ_INSERT_TAIL(plist, p, entries); p = NULL; } + fclose(list); return 0; } @@ -133,12 +134,10 @@ local_list(char *local) } int -remote_sync(char *remote, char *local, char *file) +remote_sync(char *url, FILE *fd) { - int ret = 0; ssize_t len; - char *url, *enc, *fn; - FILE *list; + char *fn, *base, *encfile, *encurl; CURL *c; CURLcode r; @@ -146,61 +145,51 @@ remote_sync(char *remote, char *local, char *file) if (!c) return -1; - enc = curl_easy_escape(c, file, strlen(file)); - len = strlen(remote) + strlen(enc); - url = malloc(len + 1); - if (!url) { - perror("malloc"); - ret = -1; - goto synccleanup; - } - snprintf(url, len + 1, "%s%s", remote, enc); - url[len] = 0; - len = strlen(local) + strlen(file); - fn = malloc(len + 1); - if (!fn) { + fn = strdup(basename(url)); + base = strdup(dirname(url)); + encfile = curl_easy_escape(c, fn, strlen(fn)); + + len = strlen(base) + strlen(encfile) + 1; + encurl = malloc(len + 1); + if (!encurl) { perror("malloc"); - ret = 1; - goto synccleanup; - } - snprintf(fn, len + 1, "%s%s", local, file); - fn[len] = 0; - list = fopen(fn, "w"); - if (!list) { - perror("fopen"); - ret = -1; - goto synccleanup; + exit(1); } + snprintf(encurl, len + 1, "%s/%s", base, encfile); - curl_easy_setopt(c, CURLOPT_URL, url); - curl_easy_setopt(c, CURLOPT_WRITEDATA, list); + curl_easy_setopt(c, CURLOPT_URL, encurl); + curl_easy_setopt(c, CURLOPT_WRITEDATA, fd); r = curl_easy_perform(c); - if (r != CURLE_OK) - fprintf(stderr, "%s: %s\n", url, curl_easy_strerror(r)); -synccleanup: - if (url) - free(url); - if (fn) - free(fn); - curl_easy_cleanup(c); - if (list) - fclose(list); - return ret; + if (r != CURLE_OK) { + fprintf(stderr, "%s: %s\n", encurl, curl_easy_strerror(r)); + return r; + } + + free(fn); + curl_free(encfile); + + return 0; } int main (int argc, char *argv[]) { + int sflag = 0; char *argv0, *n; + char fn[PATH_MAX], url[PATH_MAX]; + FILE *fd; struct packs plist; + struct repos rlist; struct pack *p = NULL; + struct repo *r = NULL; TAILQ_INIT(&plist); + TAILQ_INIT(&rlist); ARGBEGIN{ case 's': - remote_sync(REMOTEREPO, LOCALREPO, LISTFILE); + sflag = 1; break; case 'l': local_list(LOCALREPO); @@ -210,10 +199,37 @@ main (int argc, char *argv[]) usage(argv0); }ARGEND; + if (TAILQ_EMPTY(&rlist)) { + add_repo(&rlist, REMOTEREPO); + } + + if (sflag) { + snprintf(fn, PATH_MAX, "%s/%s", LOCALREPO, LISTFILE); + fd = fopen(fn, "w"); + if (!fd) { + perror(fn); + exit(1); + } + TAILQ_FOREACH(r, &rlist, entries) { + snprintf(url, PATH_MAX, "%s/%s", r->url, LISTFILE); + remote_sync(url, fd); + } + fclose(fd); + } + local_load(&plist, LOCALREPO); while ((n = *(argv++))) { TAILQ_FOREACH(p, &plist, entries) { if (!strncmp(p->name, n, PATH_MAX)) { + snprintf(fn, PATH_MAX, "%s/%s", LOCALREPO, basename(p->url)); + fd = fopen(fn, "w"); + if (!fd) { + perror(fn); + continue; + } + remote_sync(p->url, fd); + puts(fn); + fclose(fd); break; } }