ckiss

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 1de90aa4cf92803490a661d82f59b1c40f756fec
parent 0bea474c49ff0f84ca74c5636fb604522b4b9f0b
Author: aabacchus <ben@bvnf.space>
Date:   Sun, 23 Apr 2023 16:01:03 +0100

add a struct pkg

Diffstat:
Msrc/checksum.c | 56++++++++++++++++++++++++++++----------------------------
Msrc/ckiss.h | 2+-
Msrc/source.c | 53+++++++++++++++++++++++++++++++++++++++++------------
Msrc/source.h | 14++++++++++----
4 files changed, 80 insertions(+), 45 deletions(-)

diff --git a/src/checksum.c b/src/checksum.c @@ -69,40 +69,42 @@ source_generate_checksum(struct source *s) { /* returns 1 if all good, 0 if there is a checksum mismatch. */ int -verify_checksums(char *pkg, char *pkg_path, struct source **s) { - if (s == NULL) { - mylog2(pkg, "No sources"); +verify_checksums(struct pkg *p) { + if (p == NULL) { + mylog("No sources"); return 1; } - FILE *f = pkg_open_file(pkg_path, "checksums", "r"); + FILE *f = pkg_open_file(p->pkg_path, "checksums", "r"); if (f == NULL) { - /* TODO check if any sources need sums first */ - die_perror("no checksums"); + if (p->n_need_checksums == 0) + return 1; + else + die2(p->pkg, "checksums needed but no checksum file"); } char *buf = NULL; size_t bufn = 0; ssize_t n; - for (int i = 0; s[i] != NULL; i++) { - if (s[i]->type != SRC_HTTP && s[i]->type != SRC_FILE) + for (size_t i = 0; i < p->n; i++) { + if (p->s[i]->type != SRC_HTTP && p->s[i]->type != SRC_FILE) continue; if ((n = getline(&buf, &bufn, f)) == -1) { free(buf); fclose(f); perror(NULL); - die2(s[i]->remote, "checksums missing"); + die2(p->s[i]->remote, "checksums missing"); } if (buf[n - 1] == '\n') buf[--n] = '\0'; - char *sum = source_generate_checksum(s[i]); + char *sum = source_generate_checksum(p->s[i]); if (strcmp(buf, sum) != 0) { free(sum); free(buf); fclose(f); - mylog2(s[i]->cachefile, "checksum mismatch"); + mylog2(p->s[i]->cachefile, "checksum mismatch"); return 0; } free(sum); @@ -119,34 +121,32 @@ checksum(int argc, char **argv, struct env *e) { die2("checksum", "need a package name(s)"); /* TODO: crux-like */ for (int i = 1; i < argc; i++) { - bool needed = false; - char *pkg_path = find_pkg(argv[i], e); - if (pkg_path == NULL) - die2(argv[i], "not found"); + struct pkg *p = parse_sources(argv[i], e); + if (p->n_need_checksums == 0) { + pkg_free(p); + mylog2(argv[i], "No sources needing checksums"); + continue; + } + + /* for testing */ + mylog2(argv[i], verify_checksums(p) ? "checksums good" : "checksums bad"); - FILE *f = pkg_open_file(pkg_path, "checksums", "w"); + FILE *f = pkg_open_file(p->pkg_path, "checksums", "w"); if (f == NULL) die_perror("couldn't open checksums file for writing"); - struct source **s = parse_sources(argv[i], pkg_path, e); - - for (int j = 0; s[j] != NULL; j++) { - char *sum = source_generate_checksum(s[j]); + for (size_t j = 0; j < p->n; j++) { + char *sum = source_generate_checksum(p->s[j]); if (sum) { fprintf(f, "%s\n", sum); printf("%s\n", sum); - needed = true; } free(sum); - - free(s[j]->remote); - free(s[j]->extract_dir); - free(s[j]->cachefile); - free(s[j]); } - free(s); fclose(f); - mylog2(argv[i], needed ? "Generated checksums" : "No sources needing checksums"); + pkg_free(p); + + mylog2(argv[i], "Generated checksums"); } return ret; } diff --git a/src/ckiss.h b/src/ckiss.h @@ -86,7 +86,7 @@ void destroy_env(struct env *e); char *source_generate_checksum(struct source *s); /* returns 1 if all good, 0 if there is a checksum mismatch. */ -int verify_checksums(char *pkg, char *pkg_path, struct source **s); +int verify_checksums(struct pkg *p); int list(int argc, char **argv, struct env *e); int search(int argc, char **argv, struct env *e); diff --git a/src/source.c b/src/source.c @@ -6,7 +6,9 @@ #include "ckiss.h" #include "source.h" -char * +/* returns the location of the cache file for the source for pkg, or NULL if not + * needed (eg git sources or local files in repo) */ +static char * source_get_cache(char *pkg, char *pkg_path, struct source *s, struct env *e) { if (s == NULL || s->type == SRC_INVAL) die("source struct not initialised"); @@ -51,18 +53,22 @@ pkg_source_type(char *remote, char *pkg_path) { die2(remote, "invalid source"); } -struct source ** -parse_sources(char *pkg, char *pkg_path, struct env *e) { +struct pkg * +parse_sources(char *pkg, struct env *e) { struct source **s = NULL; + char *pkg_path = find_pkg(pkg, e); + if (pkg_path == NULL) + die2(pkg, "not found"); mylog2(pkg, "Reading sources"); FILE *f = pkg_open_file(pkg_path, "sources", "r"); if (f == NULL) { + free(pkg_path); if (errno == ENOENT) { mylog2(pkg, "no sources file, skipping"); - goto sources_ret; + return NULL; } else { die2(pkg, "couldn't open sources file"); } @@ -71,7 +77,7 @@ parse_sources(char *pkg, char *pkg_path, struct env *e) { char *buf = NULL; size_t bufn = 0; ssize_t n; - int lineno = 0; + int lineno = 0, needed = 0; while ((n = getline(&buf, &bufn, f)) != -1) { if (n == 0 || buf[0] == '#' || buf[0] == '\n') continue; @@ -90,19 +96,42 @@ parse_sources(char *pkg, char *pkg_path, struct env *e) { new->type = pkg_source_type(new->remote, pkg_path); new->cachefile = source_get_cache(pkg, pkg_path, new, e); + if (new->type == SRC_FILE || new->type == SRC_HTTP) + needed++; + /* add new to the list */ - s = realloc(s, sizeof(struct source *) * (lineno + 1)); + s = realloc(s, sizeof(struct source *) * lineno); if (s == NULL) die_perror("realloc"); s[lineno-1] = new; - - /* add a final NULL */ - s[lineno] = NULL; } fclose(f); free(buf); -sources_ret: - free(pkg_path); - return s; + struct pkg *p = calloc(1, sizeof(struct pkg)); + if (p == NULL) + die_perror("calloc"); + + p->pkg = pkg; + p->pkg_path = pkg_path; + p->n = lineno; + p->n_need_checksums = needed; + p->s = s; + + return p; +} + +void +pkg_free(struct pkg *p) { + if (p == NULL) + return; + for (size_t i = 0; i < p->n; i++) { + free(p->s[i]->remote); + free(p->s[i]->extract_dir); + free(p->s[i]->cachefile); + free(p->s[i]); + } + free(p->s); + free(p->pkg_path); + free(p); } diff --git a/src/source.h b/src/source.h @@ -17,12 +17,18 @@ struct source { char *cachefile; /* cache location if file downloaded */ }; -/* returns the location of the cache file for the source for pkg, or NULL if not - * needed (eg git sources or local files in repo) */ -char *source_get_cache(char *pkg, char *pkg_path, struct source *s, struct env *e); +struct pkg { + char *pkg; + char *pkg_path; + size_t n; + size_t n_need_checksums; + struct source **s; +}; enum pkg_source_types pkg_source_type(char *remote, char *pkg_path); -struct source **parse_sources(char *pkg, char *pkg_path, struct env *e); +struct pkg *parse_sources(char *pkg, struct env *e); + +void pkg_free(struct pkg *p); #endif