package

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

commit 8f593641f43b35ba8706b454a899647ff7534bef
parent 00c399a475e18c1e0b55ce0755c962ef6bda80c3
Author: Josuah Demangeon <mail@josuah.net>
Date:   Wed,  2 Jan 2019 21:15:25 +0100

go fetch the version

Diffstat:
Mpack.c | 40+++++++++++++++++++++++++++++++++++-----
Mpack.h | 2+-
2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/pack.c b/pack.c @@ -23,8 +23,6 @@ pack_fmt(pack const *p, char *buf) return i; } -#include <stdio.h> - size_t pack_scan(pack *p, char const *s) { @@ -54,11 +52,41 @@ end: p->name[n] = '\0'; } int -pack_version(pack const *p) +pack_version(pack *p, char const *def) { + stralloc path; + stralloc line; + buffer b; + char buf[1024]; + int fd; + int ret = 0; + if (*p->ver) return 1; - return 1; + stralloc_init(&path); + stralloc_copys(&path, def); + stralloc_append(&path, '/'); + stralloc_cats(&path, p->name); + stralloc_cats(&path, "/default/version"); + stralloc_append(&path, '\0'); + + fd = open_read(path.x); if (fd == -1) goto err; + + stralloc_init(&line); + buffer_init(&b, &read, fd, buf, sizeof buf); + if (buffer_getline(&b, &line) <= 0) goto err; + if (line.x[line.n - 1] == '\n') line.n--; + if (line.n >= sizeof p->ver) goto err; + + mem_copy(p->ver, line.x, line.n); + p->ver[line.n] = '\0'; + + ret = 1; + +err: stralloc_free(&path); + stralloc_free(&line); + close(fd); + return ret; } static int @@ -122,7 +150,7 @@ pack_dep_recurse(pack const *p, genalloc *packs, char const *def) stralloc_append(&line, '\0'); n = pack_scan(&new, line.x); if (n == 0) goto err; - pack_version(&new); + pack_version(&new, def); if (pack_dep_has(&new, packs)) continue; if (!genalloc_append(packs, new)) goto err; @@ -132,6 +160,7 @@ pack_dep_recurse(pack const *p, genalloc *packs, char const *def) ret = (r == 0); err: stralloc_free(&line); + stralloc_free(&path); close(fd); return ret; } @@ -144,6 +173,7 @@ pack_dep(genalloc *packs, char const *def) for (i = 0; i < n; ++i) { pack *p = genalloc_get(packs, pack, i); + pack_version(p, def); if (!pack_dep_recurse(p, packs, def)) return 0; } diff --git a/pack.h b/pack.h @@ -18,7 +18,7 @@ typedef struct { int pack_data(pack const *, char const *, char const *); int pack_dep(genalloc *, char const *); int pack_dep_recurse(pack const *, genalloc *, char const *); -int pack_version(pack const *); +int pack_version(pack *, char const *); size_t pack_fmt(pack const *, char *); size_t pack_scan(pack *, char const *);