nkiss

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/nkiss
Log | Files | Refs

commit 8664e65cc612df2f83a431f2b6cb5337b5e54ca1
parent 897a6a99202257ab02f3144d0e285b58c9570c36
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Sat, 20 Feb 2021 17:04:00 -0600

pkg.*: refactor calculating path of source

The path to a source is now calculated in pkg_sources. We also check if
the source is there or not. A new field is added to source_t for each.

Diffstat:
Mkiss.c | 2+-
Mpkg.c | 73++++++++++++++++++++++++++++++++++++-------------------------------------
Mpkg.h | 4+++-
3 files changed, 40 insertions(+), 39 deletions(-)

diff --git a/kiss.c b/kiss.c @@ -30,7 +30,7 @@ kiss_download_names(char *kiss_path, char *names[], int len) do { if (pkg_exists(repo, names[i], path)) { count = pkg_sources(repo, names[i], sources); - pkg_retrieve(repo, names[i], sources, count); + pkg_retrieve( sources, count); break; } } while ((repo = strtok(NULL, ":")) != NULL); diff --git a/pkg.c b/pkg.c @@ -12,38 +12,11 @@ #include "util.h" void -pkg_retrieve(char *repo, char *name, struct source_t *sources, int count) +pkg_retrieve(struct source_t *sources, int count) { - char cachedir[PATH_LEN], path[PATH_LEN], dup[URL_LEN]; - int size; - get_cache_dir(cachedir); - for (int i = 0; i < count; i++) { - if (sources[i].local) { - size = snprintf(path, PATH_LEN, "%s/%s/%s", repo, name, - sources[i].loc); - if (size > PATH_LEN) - die("%s: path %s too long", __func__, path); - - if (access(path, F_OK) != 0) - die("%s: cannot find local source %s for %s", __func__, - sources[i].loc, name); - } else { - strncpy(dup, sources[i].loc, URL_LEN); - size = snprintf(path, PATH_LEN, "%s/sources/%s/%s%s%s", - cachedir, name, - sources[i].extr[0] ? sources[i].extr : "", - sources[i].extr[0] ? "/" : "", - basename(dup)); - - if (size > PATH_LEN) - die("%s: path %s too long", __func__, path); - - strncpy(dup, path, PATH_LEN); - mkdirs(dirname(dup)); - - if (access(path, F_OK) != 0) - http_fetch(sources[i].loc, path); + if (!sources[i].local && !sources[i].present) { + http_fetch(sources[i].loc, sources[i].path); } } } @@ -52,14 +25,16 @@ int pkg_sources(char *repo, char *name, struct source_t *sources) { FILE *sfile; - char path[PATH_LEN]; + char spath[PATH_LEN], cachedir[PATH_LEN], dup[URL_LEN]; char line[512], *p, *end; - int i = 0; + int i = 0, size; - if (snprintf(path, PATH_LEN, "%s/%s/sources", repo, name) > PATH_LEN) - die("%s: path %s too long", __func__, path); + get_cache_dir(cachedir); - if ((sfile = fopen(path, "r")) == NULL) + if (snprintf(spath, PATH_LEN, "%s/%s/sources", repo, name) > PATH_LEN) + die("%s: spath %s too long", __func__, spath); + + if ((sfile = fopen(spath, "r")) == NULL) die("%s: failed to open sources file for %s:", __func__, name); while (fgets(line, 512, sfile)) { @@ -87,14 +62,38 @@ pkg_sources(char *repo, char *name, struct source_t *sources) } /* local source */ - if (strstr(sources[i].loc, "://") == NULL) + if (strstr(sources[i].loc, "://") == NULL) { sources[i].local = 1; + size = snprintf(sources[i].path, PATH_LEN, "%s/%s/%s", repo, name, + sources[i].loc); + + if (size > PATH_LEN) { + fclose(sfile); + die("%s: path %s too long", __func__, sources[i].path); + } + + sources[i].present = access(sources[i].path, F_OK) ? 0 : 1; + } else { + sources[i].local = 0; + strncpy(dup, sources[i].loc, URL_LEN); + size = snprintf(sources[i].path, PATH_LEN, "%s/sources/%s/%s%s%s", + cachedir, name, + sources[i].extr[0] ? sources[i].extr : "", + sources[i].extr[0] ? "/" : "", + basename(dup)); + + if (size > PATH_LEN) { + fclose(sfile); + die("%s: path %s too long", __func__, sources[i].path); + } + + sources[i].present = access(sources[i].path, F_OK) ? 0 : 1; + } if (++i > PKG_SOURCES_MAX) { fclose(sfile); die("%s: too many sources for %s", __func__, name); } - } if (ferror(sfile)) { diff --git a/pkg.h b/pkg.h @@ -9,11 +9,13 @@ struct source_t { char git; char local; + char present; char loc[URL_LEN]; char extr[PATH_LEN]; + char path[PATH_LEN]; }; -void pkg_retrieve(char *repo, char *name, struct source_t *sources, int count); +void pkg_retrieve(struct source_t *sources, int count); int pkg_sources(char *repo, char *name, struct source_t *); void pkg_version(char *repo, char *name, char *ver); void pkg_binpath(char *repo, char *name, char *binpath);