commit d13b760aa41cf2b2ef105e5a5d6492351a377c1a
parent 2e1d543685a517d0fa26a843f8786ff425e2bedc
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Sat, 20 Feb 2021 20:31:08 -0600
pkg.*: add pkg_checksums_load
This loads the checksums file into the source_t struct
Diffstat:
3 files changed, 39 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
@@ -1,2 +1,2 @@
all:
- c99 -Wall -Werror http.c kiss.c pkg.c util.c -ltls
+ c99 -Wall -Werror http.c kiss.c pkg.c sha256.c util.c -ltls
diff --git a/pkg.c b/pkg.c
@@ -3,12 +3,14 @@
#include <fnmatch.h>
#include <libgen.h>
#include <stdlib.h>
+#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "http.h"
#include "pkg.h"
+#include "sha256.h"
#include "util.h"
void
@@ -106,6 +108,39 @@ pkg_sources(char *repo, char *name, struct source_t *sources)
return i;
}
+void pkg_checksums_load(char *repo, char *name, struct source_t *sources) {
+ char cpath[PATH_LEN], line[512], *p;
+ int i = 0;
+ FILE *cfile;
+
+ if (snprintf(cpath, PATH_LEN, "%s/%s/checksums", repo, name) > PATH_LEN)
+ die("%s: path %s too long", __func__, cpath);
+
+ if ((cfile = fopen(cpath, "r")) == NULL)
+ die("%s: failed to open checksums file for %s:", __func__, name);
+
+ while (fgets(line, 512, cfile) && sources[i].path[0] != '\0') {
+ if ((p = strchr(line, '\n')) != line + 2*HASH_LEN) {
+ fclose(cfile);
+ die("%s: line %d in checksums for %s is malformatted", __func__,
+ i+1, name);
+ }
+
+ *p = '\0';
+
+ sha256_fromstr(sources[i].checksum, line);
+ i++;
+ }
+
+ if (fgets(line, 2, cfile) || sources[i].path[0] != '\0') {
+ fclose(cfile);
+ die("%s: line count mismatch between sources and checksums for %s",
+ __func__, name);
+ }
+
+ fclose(cfile);
+}
+
void
pkg_version(char *repo, char *name, char *ver)
{
diff --git a/pkg.h b/pkg.h
@@ -1,3 +1,4 @@
+#define HASH_LEN 32
#define KISS_INSTALLED "/var/db/kiss/installed"
#define PKG_VERSION_MAX 256
#define PKG_SOURCES_MAX 64
@@ -13,10 +14,12 @@ struct source_t {
char loc[URL_LEN];
char extr[PATH_LEN];
char path[PATH_LEN];
+ char checksum[HASH_LEN];
};
void pkg_retrieve(struct source_t *sources, int count);
int pkg_sources(char *repo, char *name, struct source_t *);
+void pkg_checksums_load(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);
int pkg_match(char *repo, char pkgs[][64], const char *pattern);