package

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

commit 14ed8c644a93e69181a0cbf58633b4ac81bf4f82
parent 3f59d0ea07fb48815d9848d586b3ee481a9cb27f
Author: Josuah Demangeon <mail@josuah.net>
Date:   Sun, 13 Jan 2019 01:00:15 +0100

move all generic functions to pack.c

Diffstat:
Mbin/pack-git | 7+------
Mpack-build.c | 216-------------------------------------------------------------------------------
Mpack.c | 215+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpack.h | 6++++++
4 files changed, 222 insertions(+), 222 deletions(-)

diff --git a/bin/pack-git b/bin/pack-git @@ -2,13 +2,8 @@ PACK=${PACK:-/var/pack} 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 +uri=$2 mkdir -p "$PACK/git/$pack" "$PACK/tar/$pack" git clone --bare "$url" "$PACK/git/$pack/$version" git -C "$PACK/git/$pack/$version" archive master >$PACK/tar/$pack/$version.tar - -trap - INT TERM EXIT diff --git a/pack-build.c b/pack-build.c @@ -14,222 +14,6 @@ #include "str.h" #include "stralloc.h" -static int -pack_envset(char const *name, char const *path) -{ - char buf[NAME_MAX]; - int len; - - for (len = 0; name[len] != '\0'; ++len) - buf[len] = isalnum(name[len]) ? name[len] : '_'; - buf[len] = '\0'; - return env_set(buf, path); -} - -static int -pack_env(pack *p, const char *def, const char *out) -{ - stralloc path = STRALLOC_ZERO; - stralloc line = STRALLOC_ZERO; - buffer b; - char buf[1024]; - size_t r; - size_t n; - size_t m; - int fd = -1; - int ret = 0; - - stralloc_init(&path); - if (!stralloc_cats(&path, out)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - n = path.n; - if (!stralloc_cats(&path, p->name)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - if (!stralloc_cats(&path, p->ver)) goto err; - if (!stralloc_0(&path)) goto err; - pack_envset("PREFIX", path.x); - - errno = 0; - fd = pack_opendepfile(p, def); - if (fd == -1) return (errno == ENOTDIR || errno == ENOENT); - - buffer_init(&b, &read, fd, buf, sizeof buf); - stralloc_init(&line); - while (stralloc_zero(&line), (r = buffer_getline(&b, &line)) > 0) { - pack new; - - if (!stralloc_0(&line)) goto err; - if (line.x[line.n - 1] == '\n') line.x[--(line.n)] = '\0'; - - if (line.x[m] != '\n' && line.x[m] != '\0') goto err; - m = pack_scan(&new, line.x); if (m == 0) goto err; - if (!pack_version(&new, def)) goto err; - - path.n = n; - if (!stralloc_cats(&path, new.name)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - if (!stralloc_cats(&path, new.ver)) goto err; - if (!stralloc_0(&path)) goto err; - pack_envset(new.name, path.x); - } - - ret = (r == 0); - -err: stralloc_free(&path); - stralloc_free(&line); - if (fd >= 0) close(fd); - return ret; -} - -/* - * CWD must be set to the source directory - */ -static int -pack_data(pack const *p, char const *def) -{ - stralloc sa = STRALLOC_ZERO; - size_t n; - int ret = 0; - - stralloc_init(&sa); - if (!stralloc_cats(&sa, def)) goto err; - if (!stralloc_cats(&sa, "/")) goto err; - if (!stralloc_cats(&sa, p->name)) goto err; - if (!stralloc_cats(&sa, "/")) goto err; - n = sa.n; - if (!stralloc_cats(&sa, "default/data")) goto err; - if (!stralloc_0(&sa)) goto err; - - if (fs_isdir(sa.x)) if (!fs_copy_r(sa.x, ".")) goto err; - - sa.n = n; - if (!stralloc_cats(&sa, p->ver)) goto err; - if (!stralloc_cats(&sa, "/data")) goto err; - if (!stralloc_0(&sa)) goto err; - - if (fs_isdir(sa.x)) if (!fs_copy_r(sa.x, ".")) goto err; - - ret = 1; - -err: stralloc_free(&sa); - return ret; -} - -/* - * CWD must be set to the source directory - */ -static int -pack_build(pack const *p, char const *def) -{ - stralloc path; - char *argv[] = { "build", NULL }; - size_t n; - - stralloc_init(&path); - if (!stralloc_cats(&path, def)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - if (!stralloc_cats(&path, p->name)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - n = path.n; - if (!stralloc_cats(&path, p->ver)) goto err; - if (!stralloc_cats(&path, "/build")) goto err; - if (!stralloc_0(&path)) goto err; - - execv(path.x, argv); - if (!(errno == ENOTDIR || errno == ENOENT)) goto err; - - path.n = n; - if (!stralloc_cats(&path, "default/build")) goto err; - if (!stralloc_0(&path)) goto err; - - execv(path.x, argv); - if (!(errno == ENOTDIR || errno == ENOENT)) goto err; - -err: stralloc_free(&path); - return 0; -} - -static int -pack_chdir(pack const *p, char const *src) -{ - stralloc path; - int ret = 0; - - stralloc_init(&path); - if (!stralloc_cats(&path, src)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - if (!stralloc_cats(&path, p->name)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - if (!stralloc_cats(&path, p->ver)) goto err; - if (!stralloc_0(&path)) goto err; - - if (chdir(path.x) == -1) goto err; - - ret = 1; - -err: stralloc_free(&path); - return ret; -} - -/* - * CWD must be set to the source directory - */ -static int -pack_patch(pack const *p, char *def) -{ - char const *argv[] = { "patch", "-N", NULL }; - char const *d[2]; - stralloc path; - size_t n; - size_t m; - DIR *dp = NULL; - struct dirent *de; - int i; - int fd = -1; - int ret = 0; - - stralloc_init(&path); - if (!stralloc_cats(&path, def)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - if (!stralloc_cats(&path, p->name)) goto err; - if (!stralloc_cats(&path, "/")) goto err; - n = path.n; - - d[0] = "default"; - d[1] = p->ver; - for (i = 0; i < 2; ++i) { - path.n = n; - if (!stralloc_cats(&path, d[i])) goto err; - if (!stralloc_cats(&path, "/patch/")) goto err; - m = path.n; - if (!stralloc_0(&path)) goto err; - dp = opendir(path.x); - if (dp == NULL) { - if (errno == ENOTDIR || errno == ENOENT) continue; - goto err; - } - while ((de = readdir(dp)) != NULL) { - if (str_equal(de->d_name, ".")) continue; - if (str_equal(de->d_name, "..")) continue; - - path.n = m; - if (!stralloc_cats(&path, de->d_name)) goto err; - if (!stralloc_0(&path)) goto err; - if (fs_redir_read(0, path.x) == -1) goto err; - if (forkexec_wait(argv) != 0) goto err; - close(fd); - } - closedir(dp); - } - - ret = 1; - -err: stralloc_free(&path); - close(fd); - if (ret == 0) closedir(dp); - return ret; -} - int main(int argc, char **argv) { diff --git a/pack.c b/pack.c @@ -1,6 +1,7 @@ #include <ctype.h> #include <errno.h> #include <unistd.h> +#include <dirent.h> #include "buffer.h" #include "env.h" @@ -236,3 +237,217 @@ err: close(fd); return ret; } + +/* + * CWD must be set to the source directory + */ +int +pack_patch(pack const *p, char *def) +{ + char const *argv[] = { "patch", "-N", NULL }; + char const *d[2]; + stralloc path; + size_t n; + size_t m; + DIR *dp = NULL; + struct dirent *de; + int i; + int fd = -1; + int ret = 0; + + stralloc_init(&path); + if (!stralloc_cats(&path, def)) goto err; + if (!stralloc_cats(&path, "/")) goto err; + if (!stralloc_cats(&path, p->name)) goto err; + if (!stralloc_cats(&path, "/")) goto err; + n = path.n; + + d[0] = "default"; + d[1] = p->ver; + for (i = 0; i < 2; ++i) { + path.n = n; + if (!stralloc_cats(&path, d[i])) goto err; + if (!stralloc_cats(&path, "/patch/")) goto err; + m = path.n; + if (!stralloc_0(&path)) goto err; + dp = opendir(path.x); + if (dp == NULL) { + if (errno == ENOTDIR || errno == ENOENT) continue; + goto err; + } + while ((de = readdir(dp)) != NULL) { + if (str_equal(de->d_name, ".")) continue; + if (str_equal(de->d_name, "..")) continue; + + path.n = m; + if (!stralloc_cats(&path, de->d_name)) goto err; + if (!stralloc_0(&path)) goto err; + if (fs_redir_read(0, path.x) == -1) goto err; + if (forkexec_wait(argv) != 0) goto err; + close(fd); + } + closedir(dp); + } + + ret = 1; + +err: stralloc_free(&path); + close(fd); + if (ret == 0) closedir(dp); + return ret; +} + +int +pack_chdir(pack const *p, char const *src) +{ + stralloc path; + int ret = 0; + + stralloc_init(&path); + if (!stralloc_cats(&path, src)) goto err; + if (!stralloc_cats(&path, "/")) goto err; + if (!stralloc_cats(&path, p->name)) goto err; + if (!stralloc_cats(&path, "/")) goto err; + if (!stralloc_cats(&path, p->ver)) goto err; + if (!stralloc_0(&path)) goto err; + + if (chdir(path.x) == -1) goto err; + + ret = 1; + +err: stralloc_free(&path); + return ret; +} + +/* + * CWD must be set to the source directory + */ +int +pack_build(pack const *p, char const *def) +{ + stralloc path; + char *argv[] = { "build", NULL }; + size_t n; + + stralloc_init(&path); + if (!stralloc_cats(&path, def)) goto err; + if (!stralloc_cats(&path, "/")) goto err; + if (!stralloc_cats(&path, p->name)) goto err; + if (!stralloc_cats(&path, "/")) goto err; + n = path.n; + if (!stralloc_cats(&path, p->ver)) goto err; + if (!stralloc_cats(&path, "/build")) goto err; + if (!stralloc_0(&path)) goto err; + + execv(path.x, argv); + if (!(errno == ENOTDIR || errno == ENOENT)) goto err; + + path.n = n; + if (!stralloc_cats(&path, "default/build")) goto err; + if (!stralloc_0(&path)) goto err; + + execv(path.x, argv); + if (!(errno == ENOTDIR || errno == ENOENT)) goto err; + +err: stralloc_free(&path); + return 0; +} + +/* + * CWD must be set to the source directory + */ +int +pack_data(pack const *p, char const *def) +{ + stralloc sa = STRALLOC_ZERO; + size_t n; + int ret = 0; + + stralloc_init(&sa); + if (!stralloc_cats(&sa, def)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + if (!stralloc_cats(&sa, p->name)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + n = sa.n; + if (!stralloc_cats(&sa, "default/data")) goto err; + if (!stralloc_0(&sa)) goto err; + + if (fs_isdir(sa.x)) if (!fs_copy_r(sa.x, ".")) goto err; + + sa.n = n; + if (!stralloc_cats(&sa, p->ver)) goto err; + if (!stralloc_cats(&sa, "/data")) goto err; + if (!stralloc_0(&sa)) goto err; + + if (fs_isdir(sa.x)) if (!fs_copy_r(sa.x, ".")) goto err; + + ret = 1; + +err: stralloc_free(&sa); + return ret; +} + +int +pack_envset(char const *name, char const *path) +{ + char buf[NAME_MAX]; + int len; + + for (len = 0; name[len] != '\0'; ++len) + buf[len] = isalnum(name[len]) ? name[len] : '_'; + buf[len] = '\0'; + return env_set(buf, path); +} + +int +pack_env(pack *p, const char *def, const char *out) +{ + stralloc path = STRALLOC_ZERO; + stralloc line = STRALLOC_ZERO; + buffer b; + char buf[1024]; + size_t r; + size_t n; + size_t m; + int fd = -1; + int ret = 0; + + stralloc_init(&path); + if (!stralloc_cats(&path, out)) goto err; + if (!stralloc_cats(&path, "/")) goto err; + n = path.n; + if (!stralloc_cats(&path, p->name)) goto err; + if (!stralloc_cats(&path, "/")) goto err; + if (!stralloc_cats(&path, p->ver)) goto err; + if (!stralloc_0(&path)) goto err; + pack_envset("PREFIX", path.x); + + errno = 0; + fd = pack_opendepfile(p, def); + if (fd == -1) return (errno == ENOTDIR || errno == ENOENT); + + buffer_init(&b, &read, fd, buf, sizeof buf); + stralloc_init(&line); + while (stralloc_zero(&line), (r = buffer_getline(&b, &line)) > 0) { + pack new; + + if (!stralloc_0(&line)) goto err; + if (line.x[m] != '\n' && line.x[m] != '\0') goto err; + m = pack_scan(&new, line.x); if (m == 0) goto err; + if (!pack_version(&new, def)) goto err; + + path.n = n; + if (!stralloc_cats(&path, new.name)) goto err; + if (!stralloc_cats(&path, "/")) goto err; + if (!stralloc_cats(&path, new.ver)) goto err; + if (!stralloc_0(&path)) goto err; + pack_envset(new.name, path.x); + } + + ret = (r == 0); + +err: stralloc_free(&path); + stralloc_free(&line); + if (fd >= 0) close(fd); + return ret; +} diff --git a/pack.h b/pack.h @@ -17,9 +17,15 @@ typedef struct { char ver[NAME_MAX]; } pack; +int pack_build(pack const *, char const *); +int pack_chdir(pack const *, char const *); +int pack_data(pack const *, char const *); int pack_dep(pack const *, genalloc *, char const *); +int pack_env(pack *, const char *, const char *); +int pack_envset(char const *, char const *); int pack_hasdep(pack const *, genalloc *); int pack_opendepfile(pack const *, char const *); +int pack_patch(pack const *, char *); int pack_remove(pack const *, char const *); int pack_step(pack const *, char const *, char const *); int pack_version(pack *, char const *);