package

simple and robust package manager
Log | Files | Refs | README

commit 00c399a475e18c1e0b55ce0755c962ef6bda80c3
parent c850526893338588d5b0fc2ed8546a5955e41746
Author: Josuah Demangeon <mail@josuah.net>
Date:   Wed,  2 Jan 2019 01:37:46 +0100

successfully resolve dependencies

Diffstat:
M.gitignore | 2++
MMakefile | 10++++++++--
AREADME | 14++++++++++++++
Mbuffer.c | 2+-
Apack-dep.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpack.c | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
Mpack.h | 13++++++++++---
7 files changed, 145 insertions(+), 31 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,2 +1,4 @@ *.o +*.core pack-build +pack-dep diff --git a/Makefile b/Makefile @@ -1,5 +1,5 @@ .PHONY: all -all: pack-build +all: pack-build pack-dep .PHONY: clean clean: @@ -9,6 +9,10 @@ pack-build-o = pack-build.o pack.o buffer.o fmt.o fs.o log.o mem.o openreadclose pack-build: ${pack-build-o} ./make-bin $@ ${pack-build-o} +pack-dep-o = pack-dep.o pack.o buffer.o fmt.o fs.o log.o mem.o openreadclose.o str.o stralloc.o +pack-dep: ${pack-dep-o} + ./make-bin $@ ${pack-dep-o} + .c.o: ./make-o $< @@ -18,6 +22,8 @@ fs.o: fs.c fs.h mem.h open.h str.h stralloc.h log.o: log.c buffer.h log.h mem.o: mem.c mem.h openreadclose.o: openreadclose.c stralloc.h open.h -pack.o: pack.c pack.h stralloc.h fs.h mem.h +pack-build.o: pack-build.c env.h log.h pack.h str.h +pack-dep.o: pack-dep.c env.h log.h pack.h str.h +pack.o: pack.c buffer.h fs.h mem.h open.h pack.h stralloc.h str.o: str.c str.h stralloc.o: stralloc.c mem.h fmt.h str.h stralloc.h diff --git a/README b/README @@ -0,0 +1,14 @@ +import PACK_LOG +import PACK_DEF +import PACK_OUT + +export PREFIX +export one variable per dependency with all non-alnum replaced by '_'. + +copy ${PACK_DEF}/${pack}/${version}/data and ${PACK_DEF}/${pack}/default/data + to ${PACK_OUT}/ + +read ${PACK_DEF}/${pack}/default/version + +read ${PACK_LOG}/${pack}/${version}/dependencies +read ${PACK_LOG}/${pack}/default/dependencies diff --git a/buffer.c b/buffer.c @@ -166,7 +166,7 @@ buffer_gettoken(buffer *b, stralloc *sa, char x) if (c == x) return n; ++n; } - return r; + return (r == 0) ? n : r; } size_t diff --git a/pack-dep.c b/pack-dep.c @@ -0,0 +1,52 @@ +#include <sys/stat.h> +#include <errno.h> +#include <unistd.h> + +#include "genalloc.h" +#include "env.h" +#include "log.h" +#include "pack.h" +#include "str.h" +#include "buffer.h" + +int +main(int argc, char **argv) +{ + (void)argc; + (void)argv; + char *def; + struct stat st; + genalloc packs; + size_t i; + + def = env_get("PACK_DEF"); if (!def) def = PACK_DEF; + + if (stat(def, &st) == -1) log_diesys1(101, def); + if (!S_ISDIR(st.st_mode)) { errno = ENOTDIR; log_diesys1(101, def); } + + genalloc_init(&packs); + + while (*++argv) { + pack p; + size_t n = pack_scan(&p, *argv); + + if (n == 0 || (*argv)[n] != '\0') + log_die2(1, "invalid package name: ", *argv); + genalloc_append(&packs, p); + } + + if (!pack_dep(&packs, def)) + log_diesys1(103, "could not resolve package dependencies"); + + for (i = 0; i < genalloc_len(&packs, pack); i++) { + char fmt[PACK_FMT]; + pack *p = genalloc_get(&packs, pack, i); + + pack_fmt(p, fmt); + buffer_puts(buffer_1, fmt); + buffer_putc(buffer_1, '\n'); + } + buffer_flush(buffer_1); + + return 0; +} diff --git a/pack.c b/pack.c @@ -11,35 +11,50 @@ #include "stralloc.h" size_t +pack_fmt(pack const *p, char *buf) +{ + size_t i; + + i = str_copy(buf, p->name, PACK_FMT); + if (p->ver[0] == '\0') return i; + i += str_copy(buf + i, "/", PACK_FMT); + i += str_copy(buf + i, p->ver, PACK_FMT); + + return i; +} + +#include <stdio.h> + +size_t pack_scan(pack *p, char const *s) { size_t i; - size_t j; + size_t n = 0; + size_t v = 0; if (s[0] == '/') return 0; - for (i = 0 ;; ++i) { - if (!isprint(s[i])) { *p->ver = '\0'; return i; } - if (s[i] == ' ') return 0; + for (n = 0, i = 0 ;; ++n, ++i) { + if (!isprint(s[i]) || s[i] == ' ') goto end; if (s[i] == '/') break; if (i >= sizeof p->name) return 0; + p->name[n] = s[i]; } - for (j = i ;; ++j) { - if (!isprint(s[j])) break; - if (s[j] == ' ') break; - if (s[j] == '/') break; - if (j > sizeof p->name) return 0; + for (v = 0, ++i ;; ++i, ++v) { + if (!isprint(s[i]) || s[i] == ' ') goto end; + if (s[i] == '/') break; + if (i > sizeof p->ver) return 0; + p->ver[v] = s[i]; } - mem_copy(p->name, s, i); - mem_copy(p->ver, s + i + 1, j - i - 1); - - return j; +end: p->name[n] = '\0'; + p->ver[v] = '\0'; + return i; } int -pack_version(pack *p) +pack_version(pack const *p) { if (*p->ver) return 1; @@ -53,7 +68,9 @@ pack_dep_has(pack const *p, genalloc *packs) for (i = 0; i < genalloc_len(packs, pack); ++i) { pack *x = genalloc_get(packs, pack, i); - if (str_equal(x->name, p->name) && str_equal(x->ver, p->ver)) return 1; + if (str_equal(x->name, p->name)) + if (str_equal(x->ver, p->ver)) + return 1; } return 0; @@ -66,6 +83,7 @@ pack_dep_recurse(pack const *p, genalloc *packs, char const *def) stralloc line; buffer b; char buf[1024]; + ssize_t r; int fd = -1; int ret = 0; @@ -78,29 +96,44 @@ pack_dep_recurse(pack const *p, genalloc *packs, char const *def) if (!stralloc_cats(&path, "/dependencies")) goto err; if (!stralloc_append(&path, '\0')) goto err; + errno = 0; + fd = open_read(path.x); + + if (errno == ENOTDIR || errno == ENOENT) { + stralloc_zero(&path); + if (!stralloc_copys(&path, def)) goto err; + if (!stralloc_append(&path, '/')) goto err; + if (!stralloc_cats(&path, p->name)) goto err; + if (!stralloc_cats(&path, "/default/dependencies")) goto err; + if (!stralloc_append(&path, '\0')) goto err; + } + + errno = 0; fd = open_read(path.x); stralloc_free(&path); + if (errno == ENOTDIR || errno == ENOENT) return 1; if (fd == -1) return 0; - errno = 0; buffer_init(&b, &read, fd, buf, sizeof buf); stralloc_init(&line); - while ((buffer_getline(&b, &line))) { + while (stralloc_zero(&line), (r = buffer_getline(&b, &line))) { pack new; + size_t n; - genalloc_get(packs, pack, genalloc_len(packs, pack)); - pack_scan(&new, line.x); + stralloc_append(&line, '\0'); + n = pack_scan(&new, line.x); if (n == 0) goto err; pack_version(&new); - if (pack_dep_has(p, packs)) continue; + if (pack_dep_has(&new, packs)) continue; if (!genalloc_append(packs, new)) goto err; + if (!pack_dep_recurse(&new, packs, def)) goto err; } - stralloc_free(&line); - ret = 1; + ret = (r == 0); -err: close(fd); - return ret && (errno == 0); +err: stralloc_free(&line); + close(fd); + return ret; } int @@ -118,7 +151,7 @@ pack_dep(genalloc *packs, char const *def) } int -pack_data(pack *p, char const *def, char const *out) +pack_data(pack const *p, char const *def, char const *out) { stralloc sa_def = STRALLOC_ZERO; stralloc sa_out = STRALLOC_ZERO; diff --git a/pack.h b/pack.h @@ -2,17 +2,24 @@ #define PACK_H #include <limits.h> - #include "genalloc.h" +#define PACK_DEF "/etc/pack" +#define PACK_OUT "/var/pack" +#define PACK_LOG "/var/log/pack" + +#define PACK_FMT (NAME_MAX * 2 + 1) + typedef struct { char name[NAME_MAX]; char ver[NAME_MAX]; } pack; -int pack_data(pack *, char const *, char const *); +int pack_data(pack const *, char const *, char const *); int pack_dep(genalloc *, char const *); -int pack_version(pack *); +int pack_dep_recurse(pack const *, genalloc *, char const *); +int pack_version(pack const *); +size_t pack_fmt(pack const *, char *); size_t pack_scan(pack *, char const *); #endif