From 0ea45c6552010c36e4718542db759ec6cdfce58a Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Mon, 6 Oct 2025 02:27:38 +0000 Subject: Add s6-rc-set-install, repodefs page --- src/include/s6-rc/repo.h | 2 + src/libs6rc/deps-lib/s6rc | 2 + src/libs6rc/s6rc_repo_checkset.c | 40 ++++++ src/libs6rc/s6rc_repo_compile.c | 2 +- src/libs6rc/s6rc_repo_setuptodate.c | 45 +++++++ src/repo/deps-exe/s6-rc-set-change | 3 + src/repo/deps-exe/s6-rc-set-changestate | 3 - src/repo/deps-exe/s6-rc-set-install | 3 + src/repo/s6-rc-set-change.c | 217 ++++++++++++++++++++++++++++++++ src/repo/s6-rc-set-changestate.c | 217 -------------------------------- src/repo/s6-rc-set-commit.c | 64 ++-------- src/repo/s6-rc-set-install.c | 205 ++++++++++++++++++++++++++++++ 12 files changed, 529 insertions(+), 274 deletions(-) create mode 100644 src/libs6rc/s6rc_repo_checkset.c create mode 100644 src/libs6rc/s6rc_repo_setuptodate.c create mode 100644 src/repo/deps-exe/s6-rc-set-change delete mode 100644 src/repo/deps-exe/s6-rc-set-changestate create mode 100644 src/repo/deps-exe/s6-rc-set-install create mode 100644 src/repo/s6-rc-set-change.c delete mode 100644 src/repo/s6-rc-set-changestate.c create mode 100644 src/repo/s6-rc-set-install.c (limited to 'src') diff --git a/src/include/s6-rc/repo.h b/src/include/s6-rc/repo.h index 5d0426a..f611059 100644 --- a/src/include/s6-rc/repo.h +++ b/src/include/s6-rc/repo.h @@ -32,6 +32,8 @@ extern int s6rc_repo_syncset_tmp (char const *, char const *, stralloc *, genall extern int s6rc_repo_lock (char const *, int) ; extern int s6rc_repo_touch (char const *) ; extern int s6rc_repo_touchset (char const *, char const *) ; +extern int s6rc_repo_checkset (char const *, char const *) ; +extern int s6rc_repo_setuptodate (char const *, char const *) ; extern int s6rc_repo_makesetbundles (char const *, char const *, unsigned int) ; extern int s6rc_repo_makedefbundle (char const *, char const *, char const *) ; diff --git a/src/libs6rc/deps-lib/s6rc b/src/libs6rc/deps-lib/s6rc index 79c2909..02797a8 100644 --- a/src/libs6rc/deps-lib/s6rc +++ b/src/libs6rc/deps-lib/s6rc @@ -16,6 +16,7 @@ s6rc_ls.o s6rc_nlto0.o s6rc_read_uint.o s6rc_repo_badsub.o +s6rc_repo_checkset.o s6rc_repo_cleanup.o s6rc_repo_compile.o s6rc_repo_fillset.o @@ -36,6 +37,7 @@ s6rc_repo_makesvlist_byname.o s6rc_repo_moveservices.o s6rc_repo_refcompile.o s6rc_repo_setcompile.o +s6rc_repo_setuptodate.o s6rc_repo_subnames.o s6rc_repo_sv_bcmpr.o s6rc_repo_sv_cmpr.o diff --git a/src/libs6rc/s6rc_repo_checkset.c b/src/libs6rc/s6rc_repo_checkset.c new file mode 100644 index 0000000..adf8674 --- /dev/null +++ b/src/libs6rc/s6rc_repo_checkset.c @@ -0,0 +1,40 @@ +/* ISC license. */ + +#include +#include + +#include +#include + +#include +#include + +int s6rc_repo_checkset (char const *repo, char const *set) +{ + struct stat st ; + size_t repolen = strlen(repo) ; + size_t setlen = strlen(set) ; + char fn[repolen + 10 + setlen] ; + memcpy(fn, repo, repolen) ; + memcpy(fn + repolen, "/sources/", 9) ; + memcpy(fn + repolen + 9, set, setlen + 1) ; + if (stat(fn, &st) == -1) + { + if (errno == ENOENT) + { + strerr_warnf4x("set ", set, " does not exist in repository ", repo) ; + return 3 ; + } + else + { + strerr_warnfu2sys("stat ", fn) ; + return 111 ; + } + } + if (!S_ISDIR(st.st_mode)) + { + strerr_warnf3x("file ", fn, " is not a directory") ; + return 102 ; + } + return 0 ; +} diff --git a/src/libs6rc/s6rc_repo_compile.c b/src/libs6rc/s6rc_repo_compile.c index b31a981..ac4207d 100644 --- a/src/libs6rc/s6rc_repo_compile.c +++ b/src/libs6rc/s6rc_repo_compile.c @@ -93,7 +93,7 @@ int s6rc_repo_compile (char const *repo, char const *set, char const *const *sub char fn[repolen + setlen + 11] ; memcpy(fn, newc, repolen + 10) ; memcpy(fn + repolen + 10, set, setlen + 1) ; - if (!atomic_symlink4(newc + repolen + 10, fn, oldc + repolen + 10, repolen + setlen + 45)) + if (!atomic_symlink4(newc + repolen + 10, fn, oldc + repolen + 10, setlen + 35)) { int e = errno ; rm_rf(newc) ; diff --git a/src/libs6rc/s6rc_repo_setuptodate.c b/src/libs6rc/s6rc_repo_setuptodate.c new file mode 100644 index 0000000..9e9fe58 --- /dev/null +++ b/src/libs6rc/s6rc_repo_setuptodate.c @@ -0,0 +1,45 @@ +/* ISC license. */ + +#include + +#include +#include + +#include +#include + +#include +#include + +int s6rc_repo_setuptodate (char const *repo, char const *set) +{ + struct stat stsource ; + struct stat stcompiled ; + size_t repolen = strlen(repo) ; + size_t setlen = strlen(set) ; + char srcfn[repolen + 17 + setlen] ; + char dstfn[repolen + 11 + setlen] ; + memcpy(srcfn, repo, repolen) ; + memcpy(srcfn + repolen, "/sources/", 9) ; + memcpy(srcfn + repolen + 9, set, setlen) ; + memcpy(srcfn + repolen + 9 + setlen, "/.stamp", 8) ; + memcpy(dstfn, repo, repolen) ; + memcpy(dstfn + repolen, "/compiled/", 10) ; + memcpy(dstfn + repolen + 10, set, setlen + 1) ; + if (stat(srcfn, &stsource) == -1) + { + strerr_warnfu2sys("stat ", srcfn) ; + return -1 ; + } + if (stat(dstfn, &stcompiled) == -1) + { + if (errno == ENOENT) return 0 ; + strerr_warnfu2sys("stat ", dstfn) ; + return -1 ; + } + return + stsource.st_atim.tv_sec < stcompiled.st_atim.tv_sec ? 1 : + stsource.st_atim.tv_sec > stcompiled.st_atim.tv_sec ? 0 : + stsource.st_atim.tv_nsec < stcompiled.st_atim.tv_nsec ; +} + diff --git a/src/repo/deps-exe/s6-rc-set-change b/src/repo/deps-exe/s6-rc-set-change new file mode 100644 index 0000000..6dd7af7 --- /dev/null +++ b/src/repo/deps-exe/s6-rc-set-change @@ -0,0 +1,3 @@ +${LIBS6RC} +-lskarnet +${SYSCLOCK_LIB} diff --git a/src/repo/deps-exe/s6-rc-set-changestate b/src/repo/deps-exe/s6-rc-set-changestate deleted file mode 100644 index 6dd7af7..0000000 --- a/src/repo/deps-exe/s6-rc-set-changestate +++ /dev/null @@ -1,3 +0,0 @@ -${LIBS6RC} --lskarnet -${SYSCLOCK_LIB} diff --git a/src/repo/deps-exe/s6-rc-set-install b/src/repo/deps-exe/s6-rc-set-install new file mode 100644 index 0000000..6dd7af7 --- /dev/null +++ b/src/repo/deps-exe/s6-rc-set-install @@ -0,0 +1,3 @@ +${LIBS6RC} +-lskarnet +${SYSCLOCK_LIB} diff --git a/src/repo/s6-rc-set-change.c b/src/repo/s6-rc-set-change.c new file mode 100644 index 0000000..c659471 --- /dev/null +++ b/src/repo/s6-rc-set-change.c @@ -0,0 +1,217 @@ +/* ISC license. */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define USAGE "s6-rc-set-change [ -v verbosity ] [ -r repo ] [ -E ] [ -f | -I fail|pull|warn ] [ -n ] set newstate services..." +#define dieusage() strerr_dieusage(100, USAGE) + +enum golb_e +{ + GOLB_FORCE_ESSENTIAL = 0x01, + GOLB_IGNORE_DEPENDENCIES = 0x02, + GOLB_DRYRUN = 0x04 +} ; + +enum gola_e +{ + GOLA_VERBOSITY, + GOLA_REPODIR, + GOLA_FORCELEVEL, + GOLA_N +} ; + +struct subname_s +{ + char const *name ; + uint8_t sub ; +} ; + +static gol_bool const rgolb[] = +{ + { .so = 'E', .lo = "force-essential", .clear = 0, .set = GOLB_FORCE_ESSENTIAL }, + { .so = 'f', .lo = "ignore-dependencies", .clear = 0, .set = GOLB_IGNORE_DEPENDENCIES }, + { .so = 'n', .lo = "dry-run", .clear = 0, .set = GOLB_DRYRUN } +} ; + +static gol_arg const rgola[] = +{ + { .so = 'v', .lo = "verbosity", .i = GOLA_VERBOSITY }, + { .so = 'r', .lo = "repodir", .i = GOLA_REPODIR }, + { .so = 'I', .lo = "if-dependencies-found", .i = GOLA_FORCELEVEL } +} ; + +static struct subname_s const accepted_forcelevels[] = +{ + { .name = "fail", .sub = 0 }, + { .name = "pull", .sub = 2 }, + { .name = "warn", .sub = 1 } +} ; + +static uint64_t wgolb = 0 ; + +static struct subname_s const accepted_subs[] = +{ + { .name = "active", .sub = 1 }, + { .name = "always", .sub = 3 }, + { .name = "disable", .sub = 1 }, + { .name = "disabled", .sub = 1 }, + { .name = "enable", .sub = 2 }, + { .name = "enabled", .sub = 2 }, + { .name = "essential", .sub = 3 }, + { .name = "make-essential", .sub = 3 }, + { .name = "mask", .sub = 0 }, + { .name = "masked", .sub = 0 }, + { .name = "onboot", .sub = 2 }, + { .name = "unmask", .sub = 1 }, + { .name = "unmasked", .sub = 1 } +} ; + +static int subname_cmp (void const *a, void const *b) +{ + return strcmp((char const *)a, ((struct subname_s const *)b)->name) ; +} + +int main (int argc, char const *const *argv) +{ + stralloc storage = STRALLOC_ZERO ; + genalloc svlist = GENALLOC_ZERO ; /* s6rc_repo_sv */ + genalloc indices = GENALLOC_ZERO ; /* size_t then uint32_t */ + int fdlock ; + unsigned int verbosity = 1 ; + unsigned int forcelevel = 1 ; + char const *wgola[GOLA_N] = { 0 } ; + unsigned int golc ; + struct subname_s *newsub ; + size_t max = 0, sabase ; + s6rc_repo_sv *list ; + uint32_t listn, n ; + + PROG = "s6-rc-set-change" ; + wgola[GOLA_REPODIR] = S6RC_REPO_BASE ; + + golc = GOL_main(argc, argv, rgolb, rgola, &wgolb, wgola) ; + argc -= golc ; argv += golc ; + if (wgola[GOLA_VERBOSITY] && !uint0_scan(wgola[GOLA_VERBOSITY], &verbosity)) + strerr_dief1x(100, "verbosity needs to be an unsigned integer") ; + if (wgola[GOLA_FORCELEVEL]) + { + struct subname_s *p = bsearch(wgola[GOLA_FORCELEVEL], accepted_forcelevels, sizeof(accepted_forcelevels)/sizeof(struct subname_s), sizeof(struct subname_s), &subname_cmp) ; + if (!p) strerr_dief1x(100, "if-dependencies-found needs to be fail, warn or pull") ; + forcelevel = p->sub ; + } + if (argc < 3) dieusage() ; + if (strchr(argv[0], '/') || strchr(argv[0], '\n')) + strerr_dief1x(100, "set names cannot contain / or newlines") ; + + newsub = bsearch(argv[1], accepted_subs, sizeof(accepted_subs)/sizeof(struct subname_s), sizeof(struct subname_s), &subname_cmp) ; + if (!newsub) strerr_dief2x(100, "unrecognized state change directive:", argv[1]) ; + if (newsub->sub == 3 && !(wgolb & GOLB_FORCE_ESSENTIAL)) + strerr_dief1x(100, " artificially mark a service as essential without --force-essential") ; + + fdlock = s6rc_repo_lock(wgola[GOLA_REPODIR], 1) ; + if (fdlock == -1) strerr_diefu2sys(111, "lock ", wgola[GOLA_REPODIR]) ; + + if (!s6rc_repo_makesvlist_byname(wgola[GOLA_REPODIR], argv[0], &storage, &svlist)) _exit(111) ; + list = genalloc_s(s6rc_repo_sv, &svlist) ; + listn = genalloc_len(s6rc_repo_sv, &svlist) ; + sabase = storage.len ; + { + int r = s6rc_repo_flattenservices(wgola[GOLA_REPODIR], argv + 2, argc - 2, &storage, &indices) ; + if (r) _exit(r) ; + } + n = genalloc_len(size_t, &indices) ; + if (!n) strerr_dief1x(101, "can't happen: 0 services in flattened list!") ; + + s6rc_repo_sv starting[n] ; + uint32_t ind[n] ; + + for (uint32_t i = 0 ; i < n ; i++) + { + char const *s = storage.s + genalloc_s(size_t, &indices)[i] ; + s6rc_repo_sv *p = bsearchr(s, list, listn, sizeof(s6rc_repo_sv), &s6rc_repo_sv_bcmpr, storage.s) ; + if (!p) strerr_dief7x(102, "inconsistent view in set ", argv[0], " of repository ", wgola[GOLA_REPODIR], ": service ", s, " is defined in the reference database but not in the textual representation of the set") ; + starting[i] = *p ; + ind[i] = p - list ; + max += strlen(s) + 1 ; + } + + storage.len = sabase ; + genalloc_setlen(size_t, &indices, 0) ; + + if (!(wgolb & GOLB_IGNORE_DEPENDENCIES)) + { + size_t m = 0 ; + char const *tmpstart[n] ; + char tmpstore[max] ; + + for (uint32_t i = 0 ; i < n ; i++) + { + size_t len ; + list[ind[i]].sub = newsub->sub ; + tmpstart[i] = tmpstore + m ; + len = strlen(storage.s + list[ind[i]].pos) + 1 ; + memcpy(tmpstore + m, storage.s + list[ind[i]].pos, len) ; + m += len ; + } + + if (!s6rc_repo_badsub(wgola[GOLA_REPODIR], argv[0], tmpstart, n, newsub->sub, 3, list, listn, &storage, &indices)) _exit(111) ; + if (genalloc_len(uint32_t, &indices)) + { + uint32_t const *bads = genalloc_s(uint32_t, &indices) ; + uint32_t badn = genalloc_len(uint32_t, &indices) ; + if (verbosity) + { + char const *arg[10 + (badn << 1)] ; + arg[0] = PROG ; + arg[1] = ": " ; + arg[2] = !forcelevel ? "fatal" : "warning" ; + arg[3] = ": the following services (" ; + arg[4] = newsub->sub >= 2 ? "dependencies of" : "depending on" ; + arg[5] = " the ones given as arguments) " ; + arg[6] = forcelevel == 2 ? "are also being" : "also need to be" ; + arg[7] = " changed to \"" ; + arg[8] = s6rc_repo_subnames[newsub->sub] ; + arg[9] = "\": " ; + for (uint32_t i = 0 ; i < badn ; i++) + { + arg[10 + (i << 1)] = storage.s + list[bads[i]].pos ; + arg[11 + (i << 1)] = i < badn ? " " : "" ; + } + strerr_warnv(arg, 10 + (badn << 1)) ; + } + if (!forcelevel) _exit(1) ; + if (wgolb & GOLB_DRYRUN) _exit(0) ; + if (forcelevel == 2) + { + s6rc_repo_sv full[n + badn] ; + for (uint32_t i = 0 ; i < n ; i++) full[i] = starting[i] ; + for (uint32_t i = 0 ; i < badn ; i++) full[n + i] = list[bads[i]] ; + if (!s6rc_repo_moveservices(wgola[GOLA_REPODIR], argv[0], full, n + badn, newsub->sub, storage.s, verbosity)) _exit(111) ; + _exit(0) ; + } + } + } + genalloc_free(uint32_t, &indices) ; + stralloc_free(&storage) ; + + if (!(wgolb & GOLB_DRYRUN)) + { + if (!s6rc_repo_moveservices(wgola[GOLA_REPODIR], argv[0], starting, n, newsub->sub, storage.s, verbosity)) _exit(111) ; + } + _exit(0) ; +} diff --git a/src/repo/s6-rc-set-changestate.c b/src/repo/s6-rc-set-changestate.c deleted file mode 100644 index 1064051..0000000 --- a/src/repo/s6-rc-set-changestate.c +++ /dev/null @@ -1,217 +0,0 @@ -/* ISC license. */ - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#define USAGE "s6-rc-set-changestate [ -v verbosity ] [ -r repo ] [ -E ] [ -f | -I fail|pull|warn ] [ -n ] set newstate services..." -#define dieusage() strerr_dieusage(100, USAGE) - -enum golb_e -{ - GOLB_FORCE_ESSENTIAL = 0x01, - GOLB_IGNORE_DEPENDENCIES = 0x02, - GOLB_DRYRUN = 0x04 -} ; - -enum gola_e -{ - GOLA_VERBOSITY, - GOLA_REPODIR, - GOLA_FORCELEVEL, - GOLA_N -} ; - -struct subname_s -{ - char const *name ; - uint8_t sub ; -} ; - -static gol_bool const rgolb[] = -{ - { .so = 'E', .lo = "force-essential", .clear = 0, .set = GOLB_FORCE_ESSENTIAL }, - { .so = 'f', .lo = "ignore-dependencies", .clear = 0, .set = GOLB_IGNORE_DEPENDENCIES }, - { .so = 'n', .lo = "dry-run", .clear = 0, .set = GOLB_DRYRUN } -} ; - -static gol_arg const rgola[] = -{ - { .so = 'v', .lo = "verbosity", .i = GOLA_VERBOSITY }, - { .so = 'r', .lo = "repodir", .i = GOLA_REPODIR }, - { .so = 'I', .lo = "if-dependencies-found", .i = GOLA_FORCELEVEL } -} ; - -static struct subname_s const accepted_forcelevels[] = -{ - { .name = "fail", .sub = 0 }, - { .name = "pull", .sub = 2 }, - { .name = "warn", .sub = 1 } -} ; - -static uint64_t wgolb = 0 ; - -static struct subname_s const accepted_subs[] = -{ - { .name = "active", .sub = 1 }, - { .name = "always", .sub = 3 }, - { .name = "disable", .sub = 1 }, - { .name = "disabled", .sub = 1 }, - { .name = "enable", .sub = 2 }, - { .name = "enabled", .sub = 2 }, - { .name = "essential", .sub = 3 }, - { .name = "make-essential", .sub = 3 }, - { .name = "mask", .sub = 0 }, - { .name = "masked", .sub = 0 }, - { .name = "onboot", .sub = 2 }, - { .name = "unmask", .sub = 1 }, - { .name = "unmasked", .sub = 1 } -} ; - -static int subname_cmp (void const *a, void const *b) -{ - return strcmp((char const *)a, ((struct subname_s const *)b)->name) ; -} - -int main (int argc, char const *const *argv) -{ - stralloc storage = STRALLOC_ZERO ; - genalloc svlist = GENALLOC_ZERO ; /* s6rc_repo_sv */ - genalloc indices = GENALLOC_ZERO ; /* size_t then uint32_t */ - int fdlock ; - unsigned int verbosity = 1 ; - unsigned int forcelevel = 1 ; - char const *wgola[GOLA_N] = { 0 } ; - unsigned int golc ; - struct subname_s *newsub ; - size_t max = 0, sabase ; - s6rc_repo_sv *list ; - uint32_t listn, n ; - - PROG = "s6-rc-set-changestate" ; - wgola[GOLA_REPODIR] = S6RC_REPO_BASE ; - - golc = GOL_main(argc, argv, rgolb, rgola, &wgolb, wgola) ; - argc -= golc ; argv += golc ; - if (wgola[GOLA_VERBOSITY] && !uint0_scan(wgola[GOLA_VERBOSITY], &verbosity)) - strerr_dief1x(100, "verbosity needs to be an unsigned integer") ; - if (wgola[GOLA_FORCELEVEL]) - { - struct subname_s *p = bsearch(wgola[GOLA_FORCELEVEL], accepted_forcelevels, sizeof(accepted_forcelevels)/sizeof(struct subname_s), sizeof(struct subname_s), &subname_cmp) ; - if (!p) strerr_dief1x(100, "if-dependencies-found needs to be fail, warn or pull") ; - forcelevel = p->sub ; - } - if (argc < 3) dieusage() ; - if (strchr(argv[0], '/') || strchr(argv[0], '\n')) - strerr_dief1x(100, "set names cannot contain / or newlines") ; - - newsub = bsearch(argv[1], accepted_subs, sizeof(accepted_subs)/sizeof(struct subname_s), sizeof(struct subname_s), &subname_cmp) ; - if (!newsub) strerr_dief2x(100, "unrecognized state change directive:", argv[1]) ; - if (newsub->sub == 3 && !(wgolb & GOLB_FORCE_ESSENTIAL)) - strerr_dief1x(100, " artificially mark a service as essential without --force-essential") ; - - fdlock = s6rc_repo_lock(wgola[GOLA_REPODIR], 1) ; - if (fdlock == -1) strerr_diefu2sys(111, "lock ", wgola[GOLA_REPODIR]) ; - - if (!s6rc_repo_makesvlist_byname(wgola[GOLA_REPODIR], argv[0], &storage, &svlist)) _exit(111) ; - list = genalloc_s(s6rc_repo_sv, &svlist) ; - listn = genalloc_len(s6rc_repo_sv, &svlist) ; - sabase = storage.len ; - { - int r = s6rc_repo_flattenservices(wgola[GOLA_REPODIR], argv + 2, argc - 2, &storage, &indices) ; - if (r) _exit(r) ; - } - n = genalloc_len(size_t, &indices) ; - if (!n) strerr_dief1x(101, "can't happen: 0 services in flattened list!") ; - - s6rc_repo_sv starting[n] ; - uint32_t ind[n] ; - - for (uint32_t i = 0 ; i < n ; i++) - { - char const *s = storage.s + genalloc_s(size_t, &indices)[i] ; - s6rc_repo_sv *p = bsearchr(s, list, listn, sizeof(s6rc_repo_sv), &s6rc_repo_sv_bcmpr, storage.s) ; - if (!p) strerr_dief7x(102, "inconsistent view in set ", argv[0], " of repository ", wgola[GOLA_REPODIR], ": service ", s, " is defined in the reference database but not in the textual representation of the set") ; - starting[i] = *p ; - ind[i] = p - list ; - max += strlen(s) + 1 ; - } - - storage.len = sabase ; - genalloc_setlen(size_t, &indices, 0) ; - - if (!(wgolb & GOLB_IGNORE_DEPENDENCIES)) - { - size_t m = 0 ; - char const *tmpstart[n] ; - char tmpstore[max] ; - - for (uint32_t i = 0 ; i < n ; i++) - { - size_t len ; - list[ind[i]].sub = newsub->sub ; - tmpstart[i] = tmpstore + m ; - len = strlen(storage.s + list[ind[i]].pos) + 1 ; - memcpy(tmpstore + m, storage.s + list[ind[i]].pos, len) ; - m += len ; - } - - if (!s6rc_repo_badsub(wgola[GOLA_REPODIR], argv[0], tmpstart, n, newsub->sub, 3, list, listn, &storage, &indices)) _exit(111) ; - if (genalloc_len(uint32_t, &indices)) - { - uint32_t const *bads = genalloc_s(uint32_t, &indices) ; - uint32_t badn = genalloc_len(uint32_t, &indices) ; - if (verbosity) - { - char const *arg[10 + (badn << 1)] ; - arg[0] = PROG ; - arg[1] = ": " ; - arg[2] = !forcelevel ? "fatal" : "warning" ; - arg[3] = ": the following services (" ; - arg[4] = newsub->sub >= 2 ? "dependencies of" : "depending on" ; - arg[5] = " the ones given as arguments) " ; - arg[6] = forcelevel == 2 ? "are also being" : "also need to be" ; - arg[7] = " changed to \"" ; - arg[8] = s6rc_repo_subnames[newsub->sub] ; - arg[9] = "\": " ; - for (uint32_t i = 0 ; i < badn ; i++) - { - arg[10 + (i << 1)] = storage.s + list[bads[i]].pos ; - arg[11 + (i << 1)] = i < badn ? " " : "" ; - } - strerr_warnv(arg, 10 + (badn << 1)) ; - } - if (!forcelevel) _exit(1) ; - if (wgolb & GOLB_DRYRUN) _exit(0) ; - if (forcelevel == 2) - { - s6rc_repo_sv full[n + badn] ; - for (uint32_t i = 0 ; i < n ; i++) full[i] = starting[i] ; - for (uint32_t i = 0 ; i < badn ; i++) full[n + i] = list[bads[i]] ; - if (!s6rc_repo_moveservices(wgola[GOLA_REPODIR], argv[0], full, n + badn, newsub->sub, storage.s, verbosity)) _exit(111) ; - _exit(0) ; - } - } - } - genalloc_free(uint32_t, &indices) ; - stralloc_free(&storage) ; - - if (!(wgolb & GOLB_DRYRUN)) - { - if (!s6rc_repo_moveservices(wgola[GOLA_REPODIR], argv[0], starting, n, newsub->sub, storage.s, verbosity)) _exit(111) ; - } - _exit(0) ; -} diff --git a/src/repo/s6-rc-set-commit.c b/src/repo/s6-rc-set-commit.c index 908f307..efdb2a9 100644 --- a/src/repo/s6-rc-set-commit.c +++ b/src/repo/s6-rc-set-commit.c @@ -51,60 +51,12 @@ static gol_arg const rgola[] = { .so = 'h', .lo = "fdholder-user", .i = GOLA_FDHUSER } } ; -static uint64_t wgolb = 0 ; - -static inline void check_set (char const *repo, char const *set) -{ - struct stat st ; - size_t repolen = strlen(repo) ; - size_t setlen = strlen(set) ; - char fn[repolen + 10 + setlen] ; - memcpy(fn, repo, repolen) ; - memcpy(fn + repolen, "/sources/", 9) ; - memcpy(fn + repolen + 9, set, setlen + 1) ; - if (stat(fn, &st) == -1) - { - if (errno == ENOENT) - strerr_dief4x(3, "set ", set, " does not exist in repository ", repo) ; - else strerr_diefu2sys(111, "stat ", fn) ; - } - if (!S_ISDIR(st.st_mode)) - strerr_dief3x(102, "file ", fn, " is not a directory") ; -} - -static inline int uptodate (char const *repo, char const *set) -{ - struct stat stsource ; - struct stat stcompiled ; - size_t repolen = strlen(repo) ; - size_t setlen = strlen(set) ; - char srcfn[repolen + 17 + setlen] ; - char dstfn[repolen + 11 + setlen] ; - memcpy(srcfn, repo, repolen) ; - memcpy(srcfn + repolen, "/sources/", 9) ; - memcpy(srcfn + repolen + 9, set, setlen) ; - memcpy(srcfn + repolen + 9 + setlen, "/.stamp", 8) ; - memcpy(dstfn, repo, repolen) ; - memcpy(dstfn + repolen, "/compiled/", 10) ; - memcpy(dstfn + repolen + 10, set, setlen + 1) ; - if (stat(srcfn, &stsource) == -1) - strerr_diefu2sys(111, "stat ", srcfn) ; - if (stat(dstfn, &stcompiled) == -1) - { - if (errno == ENOENT) return 0 ; - else strerr_diefu2sys(111, "stat ", dstfn) ; - } - return - stsource.st_atim.tv_sec < stcompiled.st_atim.tv_sec ? 1 : - stsource.st_atim.tv_sec > stcompiled.st_atim.tv_sec ? 0 : - stsource.st_atim.tv_nsec < stcompiled.st_atim.tv_nsec ; -} - int main (int argc, char const *const *argv) { int fdlock ; unsigned int verbosity = 1 ; char const *wgola[GOLA_N] = { 0 } ; + uint64_t wgolb = 0 ; unsigned int golc ; int r ; @@ -122,11 +74,17 @@ int main (int argc, char const *const *argv) fdlock = s6rc_repo_lock(wgola[GOLA_REPODIR], 1) ; if (fdlock == -1) strerr_diefu2sys(111, "lock ", wgola[GOLA_REPODIR]) ; - check_set(wgola[GOLA_REPODIR], argv[0]) ; - if (!(wgolb & GOLB_FORCE) && uptodate(wgola[GOLA_REPODIR], argv[0])) + r = s6rc_repo_checkset(wgola[GOLA_REPODIR], argv[0]) ; + if (r) _exit(r) ; + if (!(wgolb & GOLB_FORCE)) { - if (verbosity >= 2) strerr_warni3x("set ", argv[0], " is already up-to-date.") ; - _exit(0) ; + r = s6rc_repo_setuptodate(wgola[GOLA_REPODIR], argv[0]) ; + if (r == -1) _exit(111) ; + if (r) + { + if (verbosity >= 2) strerr_warni3x("set ", argv[0], " is already up-to-date.") ; + _exit(0) ; + } } size_t oldclen = S6RC_REPO_COMPILE_BUFLEN(strlen(wgola[GOLA_REPODIR]), strlen(argv[0])) ; diff --git a/src/repo/s6-rc-set-install.c b/src/repo/s6-rc-set-install.c new file mode 100644 index 0000000..2e0b7a0 --- /dev/null +++ b/src/repo/s6-rc-set-install.c @@ -0,0 +1,205 @@ +/* ISC license. */ + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define USAGE "s6-rc-set-install [ -v verbosity ] [ -l livedir ] [ -r repo ] [ -f convfile ] [ -b ] [ -K ] set bootdbdir" +#define dieusage() strerr_dieusage(100, USAGE) + +enum golb_e +{ + GOLB_BLOCK = 0x01, + GOLB_KEEPOLD = 0x02 +} ; + +enum gola_e +{ + GOLA_VERBOSITY, + GOLA_LIVEDIR, + GOLA_REPODIR, + GOLA_CONVFILE, + GOLA_N +} ; + +static gol_bool const rgolb[] = +{ + { .so = 'b', .lo = "block", .clear = 0, .set = GOLB_BLOCK }, + { .so = 'K', .lo = "keep-old", .clear = 0, .set = GOLB_KEEPOLD } +} ; + +static gol_arg const rgola[] = +{ + { .so = 'v', .lo = "verbosity", .i = GOLA_VERBOSITY }, + { .so = 'l', .lo = "livedir", .i = GOLA_LIVEDIR }, + { .so = 'r', .lo = "repodir", .i = GOLA_REPODIR }, + { .so = 'f', .lo = "conversion-file", .i = GOLA_CONVFILE } +} ; + +int main (int argc, char const *const *argv) +{ + int fdlock ; + unsigned int verbosity = 1 ; + char const *wgola[GOLA_N] = { 0 } ; + uint64_t wgolb = 0 ; + unsigned int golc ; + int r ; + + PROG = "s6-rc-set-install" ; + wgola[GOLA_LIVEDIR] = S6RC_LIVE_BASE ; + wgola[GOLA_REPODIR] = S6RC_REPO_BASE ; + + golc = GOL_main(argc, argv, rgolb, rgola, &wgolb, wgola) ; + argc -= golc ; argv += golc ; + if (wgola[GOLA_VERBOSITY] && !uint0_scan(wgola[GOLA_VERBOSITY], &verbosity)) + strerr_dief1x(100, "verbosity needs to be an unsigned integer") ; + if (argc < 2) dieusage() ; + if (strchr(argv[0], '/') || strchr(argv[0], '\n')) + strerr_dief1x(100, "set names cannot contain / or newlines") ; + + fdlock = s6rc_repo_lock(wgola[GOLA_REPODIR], 0) ; + if (fdlock == -1) strerr_diefu2sys(111, "lock ", wgola[GOLA_REPODIR]) ; + r = s6rc_repo_checkset(wgola[GOLA_REPODIR], argv[0]) ; + if (r) _exit(r) ; + + r = s6rc_repo_setuptodate(wgola[GOLA_REPODIR], argv[0]) ; + if (r == -1) _exit(111) ; + if (!r) strerr_dief3x(1, "set ", argv[0], " is not up-to-date; commit it first") ; + + { + size_t repolen = strlen(wgola[GOLA_REPODIR]) ; + size_t setlen = strlen(argv[0]) ; + size_t clen = S6RC_REPO_COMPILE_BUFLEN(repolen, setlen) ; + size_t dstlen = strlen(argv[1]) ; + ssize_t l ; + pid_t pid ; + int wstat ; + char *olddb = 0 ; + char const *uargv[11] ; + char fmtv[UINT_FMT] ; + char clink[repolen + setlen + 11] ; + char cfull[clen] ; + char dstfn[dstlen + setlen + 36] ; + fmtv[uint_fmt(fmtv, verbosity)] = 0 ; + memcpy(clink, wgola[GOLA_REPODIR], repolen) ; + memcpy(clink + repolen, "/compiled/", 10) ; + memcpy(clink + repolen + 10, argv[0], setlen + 1) ; + memcpy(cfull, clink, repolen + 10) ; + memcpy(dstfn, argv[1], dstlen) ; + dstfn[dstlen] = '/' ; + l = readlink(clink, cfull + repolen + 10, setlen + 35) ; + if (l == -1) strerr_diefu2sys(111, "readlink ", clink) ; + if (l >= setlen + 35) strerr_diefu2x(102, "incorrect/unexpected link contents for ", clink) ; + cfull[repolen + 10 + l] = 0 ; + memcpy(dstfn + dstlen + 1, cfull + repolen + 10, l+1) ; + l = 0 ; + { + struct stat st ; + if (stat(argv[1], &st) == -1) + strerr_diefu2sys(111, "stat ", argv[1]) ; + if (!S_ISDIR(st.st_mode)) + strerr_dief2x(100, argv[1], " is not a directory") ; + if (stat(dstfn, &st) == -1) + { + if (errno != ENOENT) + strerr_diefu2sys(111, "stat ", dstfn) ; + } + else strerr_dief2x(102, dstfn, " already exists!?") ; + } + { + size_t llen = strlen(wgola[GOLA_LIVEDIR]) ; + char cfn[llen + 10] ; + memcpy(cfn, wgola[GOLA_LIVEDIR], llen) ; + memcpy(cfn + llen, "/compiled", 10) ; + olddb = realpath(cfn, 0) ; + if (!olddb && errno != ENOENT) strerr_diefu2sys(111, "realpath ", cfn) ; + } + + if (!hiercopy(cfull, dstfn)) + { + int e = errno ; + rm_rf(dstfn) ; + errno = e ; + strerr_diefu4sys(111, "recursively copy ", cfull, " to ", dstfn) ; + } + + uargv[l++] = S6RC_BINPREFIX "s6-rc-update" ; + if (wgolb & GOLB_BLOCK) uargv[l++] = "-b" ; + uargv[l++] = "-v" ; + uargv[l++] = fmtv ; + uargv[l++] = "-l" ; + uargv[l++] = wgola[GOLA_LIVEDIR] ; + if (wgola[GOLA_CONVFILE]) + { + uargv[l++] = "-f" ; + uargv[l++] = wgola[GOLA_CONVFILE] ; + } + uargv[l++] = "--" ; + uargv[l++] = dstfn ; + uargv[l++] = 0 ; + + pid = cspawn(uargv[0], uargv, (char const *const *)environ, 0, 0, 0) ; + if (!pid) + { + int e = errno ; + rm_rf(dstfn) ; + errno = e ; + strerr_diefu2sys(111, "spawn ", uargv[0]) ; + } + r = wait_pid(pid, &wstat) ; + if (r == -1) strerr_diefu3sys(111, "wait for the ", uargv[0], " process (updating may yet succeed)") ; + if (WIFSIGNALED(wstat)) + { + char fmt[INT_FMT] ; + fmt[int_fmt(fmt, WTERMSIG(wstat))] = 0 ; + strerr_dief3x(wait_estatus(wstat), uargv[0], " crashed with signal ", fmt) ; + } + r = WEXITSTATUS(wstat) ; + if (r == 111) + strerr_dief2x(111, uargv[0], " exited 111, unable to know the state of the live db or clean up") ; + if ((r >= 3 && r <= 10) || r == 100) + { + int e = errno ; + char fmt[INT_FMT] ; + fmt[int_fmt(fmt, r)] = 0 ; + rm_rf(dstfn) ; + errno = e ; + strerr_dief4x(r, uargv[0], " exited with code ", fmt, ": live database switch NOT performed") ; + } + if (olddb) + { + if (wgolb & GOLB_KEEPOLD) + { + if (buffer_puts(buffer_1small, olddb) == -1 + || !buffer_putflush(buffer_1small, "\n", 1)) + strerr_diefu1sys(111, "write to stdout (live database switch performed)") ; + } + else rm_rf(olddb) ; + } + if (r) + { + char fmt[INT_FMT] ; + fmt[int_fmt(fmt, r)] = 0 ; + strerr_dief4x(r, uargv[0], " exited with code ", fmt, ", but the live database switch was performed") ; + } + } + _exit(0) ; +} -- cgit v1.3.1