commit 8e06b61f64711c984465c443cb68b527f13706e5
parent 6a04dedb33120780e1f3083540c287c5855497c3
Author: aabacchus <ben@bvnf.space>
Date: Sun, 23 Apr 2023 16:16:53 +0100
rename source.c to pkg.c
Diffstat:
7 files changed, 176 insertions(+), 176 deletions(-)
diff --git a/src/Makefile b/src/Makefile
@@ -2,7 +2,7 @@
XCFLAGS = $(CFLAGS) -Wall -Wextra -Wshadow -pedantic -D_XOPEN_SOURCE=700 -Og -g
-OBJS = utils.o list.o search.o array.o checksum.o source.o
+OBJS = utils.o list.o search.o array.o checksum.o pkg.o
LIBS = -lblake3
all: ckiss
@@ -32,6 +32,6 @@ test.o: ckiss.h
list.o: ckiss.h
search.o: ckiss.h array.h
array.o: ckiss.h array.h
-checksum.o: ckiss.h source.h
-source.o: ckiss.h source.h
+checksum.o: ckiss.h pkg.h
+pkg.o: ckiss.h pkg.h
main.o: ckiss.h
diff --git a/src/checksum.c b/src/checksum.c
@@ -5,7 +5,7 @@
#include <string.h>
#include "ckiss.h"
-#include "source.h"
+#include "pkg.h"
/* reads f and returns the checksum */
static char *
@@ -119,7 +119,7 @@ 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++) {
- struct pkg *p = parse_sources(argv[i], e);
+ struct pkg *p = pkg_parse_sources(argv[i], e);
if (p->n_need_checksums == 0) {
pkg_free(p);
mylog2(argv[i], "No sources needing checksums");
diff --git a/src/ckiss.h b/src/ckiss.h
@@ -38,7 +38,7 @@ struct env {
/* include these now in case they need struct env */
#include "array.h"
-#include "source.h"
+#include "pkg.h"
/* called "mylog" to avoid collision with math.h log function. */
void mylog(const char *s);
diff --git a/src/pkg.c b/src/pkg.c
@@ -0,0 +1,137 @@
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "ckiss.h"
+#include "pkg.h"
+
+/* 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) {
+ assert(s);
+
+ switch (s->type) {
+ case SRC_HTTP:
+ {
+ char *filepart = strrchr(s->remote, '/');
+ if (filepart == NULL)
+ die2(s->remote, "filepart of URL not found");
+ filepart++;
+ char *cache = concat(e->cac_dir, "/sources/", pkg, "/",
+ s->extract_dir ? s->extract_dir : "", filepart, NULL);
+ return cache;
+ }
+ case SRC_FILE:
+ {
+ char *f = concat(pkg_path, "/", s->remote, NULL);
+ return f;
+ }
+ default:
+ return NULL;
+ }
+}
+
+enum pkg_source_types
+pkg_source_type(char *remote, char *pkg_path) {
+ if (strncmp("git+", remote, 4) == 0)
+ return SRC_GIT;
+ if (strncmp("http", remote, 4) == 0)
+ return SRC_HTTP;
+ if (strstr(remote, "://"))
+ die2(remote, "protocol not supported");
+
+ /* XXX: only relative files are supported. */
+ char *f = concat(pkg_path, "/", remote, NULL);
+ int ok = (access(f, F_OK) == 0);
+ free(f);
+ if (ok)
+ return SRC_FILE;
+
+ die2(remote, "invalid source");
+}
+
+struct pkg *
+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");
+ return NULL;
+ } else {
+ die2(pkg, "couldn't open sources file");
+ }
+ }
+
+ char *buf = NULL;
+ size_t bufn = 0;
+ ssize_t n;
+ int lineno = 0, needed = 0;
+ while ((n = getline(&buf, &bufn, f)) != -1) {
+ if (n == 0 || buf[0] == '#' || buf[0] == '\n')
+ continue;
+
+ lineno++;
+
+ struct source *new = calloc(1, sizeof(struct source));
+ if (new == NULL) die_perror("calloc");
+
+ array_t t = split(buf, " \t\n");
+
+ new->remote = t[0];
+ new->extract_dir = t[1];
+ free(t); /* just free t, not the strings in it */
+
+ 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);
+ if (s == NULL)
+ die_perror("realloc");
+ s[lineno-1] = new;
+ }
+ fclose(f);
+ free(buf);
+
+ 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/pkg.h b/src/pkg.h
@@ -0,0 +1,33 @@
+#ifndef _CKISS_SOURCE_H
+#define _CKISS_SOURCE_H
+
+#include "ckiss.h"
+
+enum pkg_source_types {
+ SRC_HTTP,
+ SRC_GIT,
+ SRC_FILE,
+};
+
+struct source {
+ enum pkg_source_types type;
+ char *remote; /* location of file to be downloaded/copied */
+ char *extract_dir; /* optional dir to extract file into */
+ char *cachefile; /* cache location if file downloaded */
+};
+
+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 pkg *pkg_parse_sources(char *pkg, struct env *e);
+
+void pkg_free(struct pkg *p);
+
+#endif
diff --git a/src/source.c b/src/source.c
@@ -1,137 +0,0 @@
-#include <assert.h>
-#include <errno.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-
-#include "ckiss.h"
-#include "source.h"
-
-/* 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) {
- assert(s);
-
- switch (s->type) {
- case SRC_HTTP:
- {
- char *filepart = strrchr(s->remote, '/');
- if (filepart == NULL)
- die2(s->remote, "filepart of URL not found");
- filepart++;
- char *cache = concat(e->cac_dir, "/sources/", pkg, "/",
- s->extract_dir ? s->extract_dir : "", filepart, NULL);
- return cache;
- }
- case SRC_FILE:
- {
- char *f = concat(pkg_path, "/", s->remote, NULL);
- return f;
- }
- default:
- return NULL;
- }
-}
-
-enum pkg_source_types
-pkg_source_type(char *remote, char *pkg_path) {
- if (strncmp("git+", remote, 4) == 0)
- return SRC_GIT;
- if (strncmp("http", remote, 4) == 0)
- return SRC_HTTP;
- if (strstr(remote, "://"))
- die2(remote, "protocol not supported");
-
- /* XXX: only relative files are supported. */
- char *f = concat(pkg_path, "/", remote, NULL);
- int ok = (access(f, F_OK) == 0);
- free(f);
- if (ok)
- return SRC_FILE;
-
- die2(remote, "invalid source");
-}
-
-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");
- return NULL;
- } else {
- die2(pkg, "couldn't open sources file");
- }
- }
-
- char *buf = NULL;
- size_t bufn = 0;
- ssize_t n;
- int lineno = 0, needed = 0;
- while ((n = getline(&buf, &bufn, f)) != -1) {
- if (n == 0 || buf[0] == '#' || buf[0] == '\n')
- continue;
-
- lineno++;
-
- struct source *new = calloc(1, sizeof(struct source));
- if (new == NULL) die_perror("calloc");
-
- array_t t = split(buf, " \t\n");
-
- new->remote = t[0];
- new->extract_dir = t[1];
- free(t); /* just free t, not the strings in it */
-
- 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);
- if (s == NULL)
- die_perror("realloc");
- s[lineno-1] = new;
- }
- fclose(f);
- free(buf);
-
- 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
@@ -1,33 +0,0 @@
-#ifndef _CKISS_SOURCE_H
-#define _CKISS_SOURCE_H
-
-#include "ckiss.h"
-
-enum pkg_source_types {
- SRC_HTTP,
- SRC_GIT,
- SRC_FILE,
-};
-
-struct source {
- enum pkg_source_types type;
- char *remote; /* location of file to be downloaded/copied */
- char *extract_dir; /* optional dir to extract file into */
- char *cachefile; /* cache location if file downloaded */
-};
-
-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 pkg *parse_sources(char *pkg, struct env *e);
-
-void pkg_free(struct pkg *p);
-
-#endif