package

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

commit dfed014c83827abc03b889d98a8faf68b5d1b2d4
parent 46a6ecd8153192d1bd922a4ac06f41fc48e91c10
Author: Josuah Demangeon <mail@josuah.net>
Date:   Mon, 14 Jan 2019 22:47:56 +0100

change semantics of pack-get and pack-build

Diffstat:
MMakefile | 29++++++++++++++---------------
Mpack-build.c | 4++--
Mpack-get.c | 70+++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
3 files changed, 69 insertions(+), 34 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,5 +1,5 @@ -bin = pack-build pack-dep pack-add pack-get pack-tree -obj = pack-build.o pack.o buffer.o fmt.o fs.o log.o mem.o str.o stralloc.o forkexec.o tai.o +bin = pack-build pack-dep pack-get pack-tree # pack-add +obj = pack.o buffer.o fmt.o fs.o log.o mem.o str.o stralloc.o forkexec.o tai.o .PHONY: all all: ${bin} @@ -14,21 +14,20 @@ install: all cp -r bin ${PREFIX} cp ${bin} ${PREFIX}/bin -pack-build: ${obj} - ./make-bin $@ ${obj} +pack-build: pack-build.o ${obj} + ./make-bin $@ pack-build.o ${obj} -pack-dep: ${obj} - ./make-bin $@ ${obj} +pack-dep: pack-dep.o ${obj} + ./make-bin $@ pack-dep.o ${obj} -pack-add: ${obj} - ./make-bin $@ ${obj} +pack-add: pack-add.o ${obj} + ./make-bin $@ pack-add.o ${obj} -pack-get: ${obj} - ./make-bin $@ ${obj} +pack-get: pack-get.o ${obj} + ./make-bin $@ pack-get.o ${obj} -pack-tree-o = pack-tree.o pack.o buffer.o fmt.o fs.o log.o mem.o str.o stralloc.o forkexec.o -pack-tree: ${pack-tree-o} - ./make-bin $@ ${pack-tree-o} +pack-tree: pack-tree.o ${obj} + ./make-bin $@ pack-tree.o ${obj} .c.o: ./make-o $< @@ -40,9 +39,9 @@ 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 pack-add.o: pack-add.c buffer.h env.h fs.h genalloc.h log.h pack.h str.h fmt.h open.h -pack-build.o: pack-build.c buffer.h env.h forkexec.h fs.h log.h open.h pack.h str.h stralloc.h +pack-build.o: pack-build.c buffer.h env.h forkexec.h fs.h log.h open.h pack.h str.h stralloc.h tai.h fmt.h pack-dep.o: pack-dep.c buffer.h env.h fs.h genalloc.h log.h pack.h str.h -pack-get.o: pack-get.c buffer.h env.h forkexec.h fs.h log.h open.h pack.h stralloc.h +pack-get.o: pack-get.c buffer.h env.h forkexec.h fs.h log.h open.h pack.h stralloc.h lock.h pack-tree.o: pack-tree.c pack.h stralloc.h open.h buffer.h env.h fs.h log.h pack.o: pack.c buffer.h env.h fmt.h forkexec.h fs.h genalloc.h mem.h open.h pack.h stralloc.h lock.h tai.h str.o: str.c str.h diff --git a/pack-build.c b/pack-build.c @@ -247,7 +247,7 @@ main(int argc, char **argv) tai_now(&t); id[0] = '@'; - id[fmt_ulong(id + 1, tai_approx(&t)) + 1] = '\0'; + id[fmt_xlong(id + 1, tai_approx(&t)) + 1] = '\0'; if (chdir(pkg) == -1) log_diesys2(101, "chdir ", pkg); @@ -258,7 +258,7 @@ main(int argc, char **argv) if (chdir(p.name) == -1) log_diesys4(101, "chdir ", pkg, "/", p.name); if (!pack_version(&p, def)) - log_diesys2(101, "pack_version ", p.name); + log_diesys4(101, "version ", def, "/", p.name); if (mkdir(p.ver, 075) == -1 && errno != EEXIST) log_diesys6(101, "mkdir ", pkg, "/", p.name, "/", p.ver); if (chdir(p.ver) == -1) diff --git a/pack-get.c b/pack-get.c @@ -1,3 +1,6 @@ +#include <unistd.h> +#include <stdio.h> + #include "buffer.h" #include "env.h" #include "forkexec.h" @@ -6,12 +9,16 @@ #include "open.h" #include "pack.h" #include "stralloc.h" +#include "lock.h" int get(pack const *p, char const *cmd, char const *url) { char const *argv[] = { NULL, NULL, NULL, NULL }; char fmt[PACK_FMT]; + stralloc sa; + + stralloc_init(&sa); log_info2("trying mirror ", url); @@ -35,6 +42,13 @@ die_nomem() log_diesys1(100, "malloc"); } +void +usage(char const *argv0, char const *param) +{ + log_err2("missing parameter: ", param); + log_usage(argv0, "-p pkgdir -d defdir package[/version]"); +} + int main(int argc, char **argv) { @@ -42,27 +56,34 @@ main(int argc, char **argv) pack p; buffer b; char buf[1024]; - char *log; - char *def; - char *tar; - char *cmd; + char *def = NULL; + char *pkg = NULL; + char *cmd = NULL; + char **av = argv; int fd = -1; size_t n; size_t r; - if (argc != 2) log_usage(*argv, "package[/version] url"); - ++argv; + (void)argc; + + while (*++av) { + if (str_equal(*av, "-p")) { if (!(pkg = *++av)) break; continue; } + if (str_equal(*av, "-d")) { if (!(def = *++av)) break; continue; } + break; + } - def = env_get("PACK_DEF"); if (!def) def = PACK_DEF; - log = env_get("PACK_LOG"); if (!log) log = PACK_LOG; - tar = env_get("PACK_TAR"); if (!tar) tar = PACK_TAR; + if (!def) usage(*argv, "-d"); + if (!pkg) usage(*argv, "-p"); + if (!*av) usage(*argv, "package[/version]"); - n = pack_scan(&p, *argv); - if ((*argv)[n] != '\0') - log_die2(1, "invalid package format: ", *argv); + if (!fs_isdir(def)) log_diesys1(101, def); + if (!fs_isdir(pkg)) log_diesys1(101, pkg); + n = pack_scan(&p, *av); + if ((*av)[n] != '\0') + log_die2(1, "invalid package format: ", *argv); if (!pack_version(&p, def)) - log_diesys2(101, "getting version of ", p.name); + log_diesys2(101, "pack_version ", p.name); stralloc_init(&sa); if (!stralloc_cats(&sa, def)) die_nomem(); @@ -75,14 +96,24 @@ main(int argc, char **argv) fd = open_read(sa.x); if (fd == -1) die_nomem(); stralloc_zero(&sa); - if (!stralloc_cats(&sa, tar)) die_nomem(); + if (!stralloc_cats(&sa, pkg)) die_nomem(); if (!stralloc_cats(&sa, "/")) die_nomem(); if (!stralloc_cats(&sa, p.name)) die_nomem(); if (!stralloc_cats(&sa, "/")) die_nomem(); if (!stralloc_cats(&sa, p.ver)) die_nomem(); if (!stralloc_cat0(&sa)) die_nomem(); if (!fs_mkdir_r(sa.x, 0750)) - log_diesys2(101, "recursive mkdir:", sa.x); + log_diesys2(101, "mkdir ", sa.x); + if (chdir(sa.x) == -1) + log_diesys2(101, "chdir ", sa.x); + + sa.n--; + if (close(1) == -1 || dup(open_trunc("source.tmp")) == -1) + log_diesys3(100, "redirect tar output to ", sa.x, "/source.tmp"); + if (!lock_exclusive(1)) + log_diesys3(100, "flock ", sa.x, "/source.tmp"); + if (close(2) == -1 || dup(open_trunc("source.log")) == -1) + log_diesys3(100, "redirect err output to ", sa.x, "/source.log"); buffer_init(&b, &read, fd, buf, sizeof buf); while (stralloc_zero(&sa), (r = buffer_getline(&b, &sa)) > 0) @@ -97,9 +128,14 @@ main(int argc, char **argv) if (str_start(sa.x, "ftps://")) cmd = "pack-ftps"; if (str_start(sa.x, "git://")) cmd = "pack-git"; if (str_start(sa.x, "cvs://")) cmd = "pack-cvs"; - if (cmd && get(&p, cmd, sa.x)) break; + if (!cmd) log_warn2("no handler found for downloading ", sa.x); + else if (get(&p, cmd, sa.x)) break; } - if (r < 0 || !cmd) log_errsys1("buffering"); + if (r < 0) log_errsys1("buffering"); + if (!cmd) log_errsys4("no mirror found for ", p.name, "/", p.ver); + + if (rename("source.tmp", "source.tar") == -1) + log_diesys1(100, "rename source.tmp source.tar"); stralloc_free(&sa); close(fd);