package

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

commit 0fa9b7bcf545a957b73abf96bf3b0aa9483a9e2c
parent 44e1251700b687990acff226a6044044979b007f
Author: Josuah Demangeon <mail@josuah.net>
Date:   Sat, 12 Jan 2019 19:31:26 +0100

first half-working draft of pack-add

Diffstat:
MMakefile | 2+-
Mbin/pack-cvs | 6+++---
Mbin/pack-ftp | 6+++---
Mbin/pack-get | 2+-
Mbin/pack-git | 6+++---
Mbin/pack-http | 4+++-
Mbin/pack-install | 4++--
Mbin/pack-tar | 2+-
Mforkexec.c | 17+++++++++--------
Mforkexec.h | 4++--
Mfs.c | 53+++++++++++++++++++++++++++++++++--------------------
Mfs.h | 1+
Aktrace.out | 0
Mlog.c | 2+-
Mpack-add.c | 69++++++++++++++++++++++++++++++++++-----------------------------------
Mpack-build.c | 2+-
16 files changed, 98 insertions(+), 82 deletions(-)

diff --git a/Makefile b/Makefile @@ -9,7 +9,7 @@ clean: install: all mkdir -p ${PREFIX} cp -r bin ${PREFIX} - cp pack-build pack-dep ${PREFIX}/bin + cp pack-build pack-dep pack-add ${PREFIX}/bin pack-build-o = pack-build.o pack.o buffer.o fmt.o fs.o log.o mem.o str.o stralloc.o forkexec.o pack-build: ${pack-build-o} diff --git a/bin/pack-cvs b/bin/pack-cvs @@ -1,9 +1,9 @@ #!/bin/sh -e PACK=${PACK:-/var/pack} IFS=/; set -- $1; IFS= -pack=$1 -version=${2:-$(sed q "/etc/pack/$1/default/version")} -url=$3 +pack=${1%/*} +version=${1#*/} +url=$2 trap "rm -rf '$PACK/cvs/$pack/$version' '$PACK/tar/$pack/$version.tar'" INT TERM EXIT diff --git a/bin/pack-ftp b/bin/pack-ftp @@ -1,8 +1,8 @@ #!/bin/sh -e PACK=${PACK:-/var/pack} -pack=$1 -version=${2:-$(sed q "/etc/pack/$1/version")} -url=$3 +pack=${1%/*} +version=${1#*/} +url=$2 trap "rm -f '$PACK/tar/$pack/$version.tar'" INT TERM EXIT diff --git a/bin/pack-get b/bin/pack-get @@ -2,7 +2,7 @@ PACK=${PACK:-/var/pack} IFS=/; set -- $1; IFS= pack=${1%/*} -version=${2#*/} +version=${1#*/} if [ -d "$PACK/src/$pack/$version" ]; then exit 0; fi mkdir -p "$PACK/log/$pack/$version" diff --git a/bin/pack-git b/bin/pack-git @@ -1,8 +1,8 @@ #!/bin/sh -e PACK=${PACK:-/var/pack} -pack=$1 -version=$2 -uri=$3 +pack=${1%/*} +version=${1#*/} +uri=$1 if [ -d "$PACK/git/$pack/$version" ]; then exit 0; fi trap "rm -rf '$PACK/git/$pack/$version' '$PACK/tar/$pack/$version.tar'" INT TERM EXIT diff --git a/bin/pack-http b/bin/pack-http @@ -1,6 +1,8 @@ #!/bin/sh -e PACK=${PACK:-/var/pack} -pack=$1 version=${2:-$(sed q "/etc/pack/$1/version")} url=$3 +pack=${1%/*} +version=${1#*/} +url=$2 trap "rm -f '/etc/pack/tar/$pack/$version.tar'" INT TERM EXIT diff --git a/bin/pack-install b/bin/pack-install @@ -1,7 +1,7 @@ #!/bin/sh -e PACK=${PACK:-/var/pack} -pack=$1 -version=$2 +pack=${1%/*} +version=${1#*/} mkdir -p "$PACK/bin" mkdir -p "$PACK/man" diff --git a/bin/pack-tar b/bin/pack-tar @@ -2,7 +2,7 @@ PACK_SRC=${PACK_SRC:-/var/pack/src} PACK_TAR=${PACK_TAR:-/var/pack/tar} pack=${1%/*} -version=${2#*/} +version=${1#*/} trap "rm -rf '$PACK/src/$pack/$version'" EXIT TERM INT diff --git a/forkexec.c b/forkexec.c @@ -2,11 +2,11 @@ #include <unistd.h> #include <sys/wait.h> +#include "open.h" #include "forkexec.h" -#include "fs.h" int -forkexec_wait(char *const argv[]) +forkexec_wait(char const *argv[]) { pid_t pid; int st; @@ -15,7 +15,7 @@ forkexec_wait(char *const argv[]) case -1: return -1; case 0: - execvp(*argv, argv); + execvp(*argv, (char *const *)argv); exit(127); } if (waitpid(pid, &st, 0) == -1) return -1; @@ -24,19 +24,20 @@ forkexec_wait(char *const argv[]) } int -forkexec_wait_redir(char *const argv[], char const *path) +forkexec_wait_redir(char const *argv[], char const *path) { pid_t pid = 0; int st; + int fd; switch ((pid = fork())) { case -1: return -1; case 0: - if (dup2(1, 0) == -1) exit(125); - dprintf(1, "%s ", path); - if (!fs_redir_write(0, path)) exit(126); - execvp(*argv, argv); + fd = open_trunc(path); if (fd == -1) exit(125); + close(1); if (dup(fd) != 1) exit(126); + close(2); if (dup(fd) != 2) exit(126); + execvp(*argv, (char *const *) argv); exit(127); } diff --git a/forkexec.h b/forkexec.h @@ -1,7 +1,7 @@ #ifndef FORKEXEC_H #define FORKEXEC_H -int forkexec_wait(char *const argv[]); -int forkexec_wait_redir(char *const argv[], char const *); +int forkexec_wait(char const *argv[]); +int forkexec_wait_redir(char const *argv[], char const *); #endif diff --git a/fs.c b/fs.c @@ -61,16 +61,28 @@ fs_redir_append(int fd, char const *path) } int +fs_redir_trunc(int fd, char const *path) +{ + int newfd = open_trunc(path); + if (newfd == -1) return 0; + if (dup2(fd, newfd) == -1) return 0; + return 1; +} + +int fs_mkdir_r(char *dir, unsigned int mode) { - char *d = dir + (*dir == '/'); - size_t n = 0; - - while (d[n] != '\0') { - d += n = str_chr(d, '/'); - *d = '\0'; - if (mkdir(dir, mode) == -1 && errno != EEXIST) return 0; - *d = '/'; + size_t n; + + for (n = (dir[0] == '/') ;; ++n) { + if (dir[n] == '/') { + dir[n] = '\0'; + if (mkdir(dir, mode) == -1 && errno != EEXIST) return 0; + dir[n] = '/'; + } else if (dir[n] == '\0') { + if (mkdir(dir, mode) == -1 && errno != EEXIST) return 0; + break; + } } return 1; @@ -99,33 +111,34 @@ err: close(fd_from); } static int -fs_remove_r_recurse(stralloc *path) +fs_remove_r_recurse(stralloc *sa) { DIR *dp; struct dirent *de; struct stat st; - size_t n = path->n; + size_t n = sa->n; - dp = opendir(path->x); if (dp == NULL) return 0; + dp = opendir(sa->x); if (dp == NULL) return 0; - if (!stralloc_cats(path, "/")) return 0; - n = path->n; + if (!stralloc_cats(sa, "/")) return 0; + n = sa->n; errno = 0; while ((de = readdir(dp))) { if (str_equal(de->d_name, ".")) continue; if (str_equal(de->d_name, "..")) continue; - path->n = n; - if (!stralloc_cats(path, de->d_name)) break; - if (!stralloc_0(path)) break; - if (lstat(path->x, &st) == -1) break; + sa->n = n; + if (!stralloc_cats(sa, de->d_name)) break; + if (!stralloc_0(sa)) break; + if (lstat(sa->x, &st) == -1) break; + sa->n--; if (S_ISDIR(st.st_mode)) { - if (!fs_remove_r_recurse(path)) break; - if (rmdir(path->x) == -1) break; + if (!fs_remove_r_recurse(sa)) break; + if (rmdir(sa->x) == -1) break; } else { - if (unlink(path->x) == -1) break; + if (unlink(sa->x) == -1) break; } } diff --git a/fs.h b/fs.h @@ -10,6 +10,7 @@ int fs_islink(char const *); int fs_mkdir_r(char *, unsigned int); int fs_redir_append(int, char const *); int fs_redir_read(int, char const *); +int fs_redir_trunc(int, char const *); int fs_redir_write(int, char const *); int fs_remove_r(char const *); diff --git a/ktrace.out b/ktrace.out Binary files differ. diff --git a/log.c b/log.c @@ -5,7 +5,7 @@ #include "buffer.h" #include "log.h" -int log_level = 3; +int log_level = 0; void log_init(int level) diff --git a/pack-add.c b/pack-add.c @@ -1,7 +1,6 @@ #include <sys/stat.h> #include <errno.h> #include <unistd.h> -#include <errno.h> #include "buffer.h" #include "env.h" @@ -22,31 +21,27 @@ die_nomem(void) } void -die_rm(char *path, pack *p) +rm(char const *dir, pack const *p) { stralloc sa; stralloc_init(&sa); - if (!stralloc_cats(&sa, path)) goto err; - if (!stralloc_cats(&sa, "/")) goto err; - if (!stralloc_cats(&sa, path)) goto err; - if (!stralloc_cats(&sa, "/")) goto err; - if (!stralloc_cats(&sa, p->name)) goto err; - if (!stralloc_0(&sa)) goto err; - if (!fs_remove_r(sa.x) && errno != ENOTDIR && errno != ENOENT) goto err; - exit(100); - -err: log_diesys6(100, "could not delete ", path, "/", p->name, "/", p->ver); + if (!stralloc_cats(&sa, dir)) die_nomem(); + if (!stralloc_cats(&sa, "/")) die_nomem(); + if (!stralloc_cats(&sa, p->name)) die_nomem(); + if (!stralloc_0(&sa)) die_nomem(); + if (!fs_remove_r(sa.x) && errno != ENOTDIR && errno != ENOENT) + log_errsys2("could not delete ", sa.x); + stralloc_free(&sa); } -int -step(pack *p, char *step, char *log) +void +step(pack const *p, char const *step, char const *log, char const *dir) { char fmt[PACK_FMT]; - char *argv[] = { NULL, NULL, NULL }; + char const *argv[] = { NULL, NULL, NULL }; stralloc sa; int x; - int ret = 0; buffer_puts(buffer_2, " "); buffer_puts(buffer_2, step + (str_start(step, "pack-") ? 5 : 0)); @@ -54,16 +49,17 @@ step(pack *p, char *step, char *log) buffer_flush(buffer_2); stralloc_init(&sa); - if (!stralloc_cats(&sa, log)) { nl(); goto err; } - if (!stralloc_cats(&sa, "/")) { nl(); goto err; } - if (!stralloc_cats(&sa, p->name)) { nl(); goto err; } - if (!stralloc_cats(&sa, "/")) { nl(); goto err; } - if (!stralloc_cats(&sa, p->ver)) { nl(); goto err; } - if (!stralloc_0(&sa)) { nl(); goto err; } - if (!fs_mkdir_r(sa.x, 0640)) { nl(); goto err; } - if (!stralloc_cats(&sa, "/")) { nl(); goto err; } - if (!stralloc_cats(&sa, step)) { nl(); goto err; } - if (!stralloc_0(&sa)) { nl(); goto err; } + if (!stralloc_cats(&sa, log)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + if (!stralloc_cats(&sa, p->name)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + if (!stralloc_cats(&sa, p->ver)) goto err; + if (!stralloc_0(&sa)) goto err; + if (!fs_mkdir_r(sa.x, 0640)) goto err; + sa.n--; + if (!stralloc_cats(&sa, "/")) goto err; + if (!stralloc_cats(&sa, step)) goto err; + if (!stralloc_0(&sa)) goto err; argv[0] = step; pack_fmt(p, fmt); @@ -74,14 +70,17 @@ step(pack *p, char *step, char *log) fmt_long(fmt, x); buffer_puts(buffer_2, "< exit "); buffer_puts(buffer_2, fmt); + buffer_puts(buffer_2, "\n"); buffer_flush(buffer_2); - return 0; + rm(dir, p); + exit(100); } - ret = 1; -err: stralloc_free(&sa); + stralloc_free(&sa); + return; - return ret; +err: nl(); rm(dir, p); + log_diesys3(100, step, ": ", sa.x); } int @@ -138,11 +137,11 @@ main(int argc, char **argv) buffer_puts(buffer_2, " "); buffer_flush(buffer_2); - if (!step(p, "pack-get", log)) nl(),die_rm(tar, p); - if (!step(p, "pack-tar", log)) nl(),die_rm(src, p); - if (!step(p, "pack-build", log)) nl(),die_rm(out, p); - if (!step(p, "pack-install", log)) nl(),log_die4(100, "could not install ", p->name, "/", p->ver); - nl(); buffer_flush(buffer_2); + step(p, "pack-get", log, tar); + step(p, "pack-tar", log, src); + step(p, "pack-build", log, out); + step(p, "pack-install", log, NULL); + buffer_flush(buffer_2); nl(); } buffer_flush(buffer_1); diff --git a/pack-build.c b/pack-build.c @@ -173,7 +173,7 @@ err: stralloc_free(&path); static int pack_patch(pack const *p, char *def) { - char *const argv[] = { "patch", "-N", NULL }; + char const *argv[] = { "patch", "-N", NULL }; char const *d[2]; stralloc path; size_t n;