aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2025-12-20 11:04:11 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2025-12-20 11:04:11 +0000
commite63674eed9e59448118a5bd74e04578f4042fe01 (patch)
tree7ee102c5d34b3f16981008ed268e477752727126 /src
parent3be4cf90beaf874f9c116696a8c0a17ec396843b (diff)
downloads6-rc-e63674eed9e59448118a5bd74e04578f4042fe01.tar.gz
Add s6-rc-set-fix
Diffstat (limited to 'src')
-rw-r--r--src/repo/deps-exe/s6-rc-set-fix3
-rw-r--r--src/repo/s6-rc-set-copy.c2
-rw-r--r--src/repo/s6-rc-set-fix.c95
-rw-r--r--src/repo/s6rc_repo_fixset.c57
-rw-r--r--src/repo/s6rc_repo_listsub.c5
-rw-r--r--src/repo/s6rc_repo_makedefbundle.c76
-rw-r--r--src/repo/s6rc_repo_makesetbundles.c29
-rw-r--r--src/repo/s6rc_repo_moveservices.c6
8 files changed, 202 insertions, 71 deletions
diff --git a/src/repo/deps-exe/s6-rc-set-fix b/src/repo/deps-exe/s6-rc-set-fix
new file mode 100644
index 0000000..9fe69bb
--- /dev/null
+++ b/src/repo/deps-exe/s6-rc-set-fix
@@ -0,0 +1,3 @@
+${LIBS6RCREPO}
+-lskarnet
+${SPAWN_LIB}
diff --git a/src/repo/s6-rc-set-copy.c b/src/repo/s6-rc-set-copy.c
index 8a5992e..72ffd9f 100644
--- a/src/repo/s6-rc-set-copy.c
+++ b/src/repo/s6-rc-set-copy.c
@@ -119,6 +119,8 @@ int main (int argc, char const *const *argv)
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 (!strcmp(argv[0], argv[1]))
+ strerr_dief1x(100, "source and destination sets are the same") ;
s6rc_repo_sanitize_setname(argv[0]) ;
s6rc_repo_sanitize_setname(argv[1]) ;
diff --git a/src/repo/s6-rc-set-fix.c b/src/repo/s6-rc-set-fix.c
new file mode 100644
index 0000000..cb9c1e8
--- /dev/null
+++ b/src/repo/s6-rc-set-fix.c
@@ -0,0 +1,95 @@
+/* ISC license. */
+
+#include <unistd.h>
+
+#include <skalibs/gccattributes.h>
+#include <skalibs/uint64.h>
+#include <skalibs/envexec.h>
+#include <skalibs/tai.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/genalloc.h>
+
+#include <s6-rc/config.h>
+#include <s6-rc/repo.h>
+
+enum golb_e
+{
+ GOLB_FIXUP = 0x01,
+ GOLB_FORCE_ESSENTIALS = 0x02,
+ GOLB_DRYRUN = 0x04,
+} ;
+
+enum gola_e
+{
+ GOLA_VERBOSITY,
+ GOLA_REPODIR,
+ GOLA_N
+} ;
+
+static int fixall (char const *repo, unsigned int options, unsigned int verbosity, stralloc *sa, genalloc *svlist, stralloc *badga)
+{
+ stralloc setnames = STRALLOC_ZERO ;
+ genalloc setindices = GENALLOC_ZERO ; /* size_t */
+ int n = s6rc_repo_list_sets(repo, &setnames, &setindices) ;
+ if (n == -1) { strerr_warnfu2sys("list sets at ", repo) ; return 111 ; }
+ for (unsigned int i = 0 ; i < n ; i++)
+ {
+ int e = s6rc_repo_fixset(repo, setnames.s + genalloc_s(size_t, &setindices)[i], options, verbosity, sa, svlist, badga) ;
+ if (e) return e ;
+ }
+ // genalloc_free(size_t, &setindices) ;
+ // stralloc_free(&setnames) ;
+ return 0 ;
+}
+
+int main (int argc, char const **argv)
+{
+ static gol_bool const rgolb[] =
+ {
+ { .so = 'u', .lo = "fix-up", .clear = 0, .set = GOLB_FIXUP },
+ { .so = 'd', .lo = "fix-down", .clear = GOLB_FIXUP, .set = 0 },
+ { .so = 'E', .lo = "no-force-essentials", .clear = GOLB_FORCE_ESSENTIALS, .set = 0 },
+ { .so = 'e', .lo = "force-essentials", .clear = 0, .set = GOLB_FORCE_ESSENTIALS },
+ { .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 }
+ } ;
+ stralloc sa = STRALLOC_ZERO ;
+ genalloc svlist = GENALLOC_ZERO ;
+ genalloc badga = GENALLOC_ZERO ;
+ unsigned int verbosity = 1 ;
+ int fdlock ;
+ uint64_t wgolb = 0 ;
+ char const *wgola[GOLA_N] = { 0 } ;
+ unsigned int golc ;
+
+ PROG = "s6-rc-set-fix" ;
+ wgola[GOLA_REPODIR] = S6RC_REPODIR ;
+
+ 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") ;
+ for (unsigned int i = 0 ; i < argc ; i++) s6rc_repo_sanitize_setname(argv[i]) ;
+
+ tain_now_g() ;
+ fdlock = s6rc_repo_lock(wgola[GOLA_REPODIR], 1) ;
+ if (fdlock == -1) strerr_diefu2sys(111, "lock ", wgola[GOLA_REPODIR]) ;
+
+ if (!argc) _exit(fixall(wgola[GOLA_REPODIR], wgolb & 7, verbosity, &sa, &svlist, &badga)) ;
+
+ for (unsigned int i = 0 ; i < argc ; i++)
+ {
+ int e = s6rc_repo_checkset(wgola[GOLA_REPODIR], argv[i]) ;
+ if (e) _exit(e) ;
+ }
+ for (unsigned int i = 0 ; i < argc ; i++)
+ {
+ int e = s6rc_repo_fixset(wgola[GOLA_REPODIR], argv[i], wgolb & 7, verbosity, &sa, &svlist, &badga) ;
+ if (e) _exit(e) ;
+ }
+ _exit(0) ;
+}
diff --git a/src/repo/s6rc_repo_fixset.c b/src/repo/s6rc_repo_fixset.c
index 8ad64bc..4a42992 100644
--- a/src/repo/s6rc_repo_fixset.c
+++ b/src/repo/s6rc_repo_fixset.c
@@ -17,47 +17,52 @@ static inline int s6rc_repo_fixsub (char const *repo, char const *set, uint8_t s
uint32_t gabase = genalloc_len(uint32_t, badga) ;
uint32_t const *bads ;
uint32_t badn ;
- if (!s6rc_repo_badsub(repo, set, bysub + subind[sub], subn[sub], sub, options & 1 ? 2 : 1, byname, ntot, sa, badga)) return 0 ;
+ int e = 0 ;
+ if (!s6rc_repo_badsub(repo, set, bysub + subind[sub], subn[sub], sub, options & 1 ? 2 : 1, byname, ntot, sa, badga)) return 111 ;
bads = genalloc_s(uint32_t, badga) ;
badn = genalloc_len(uint32_t, badga) ;
- if (!badn) return 1 ;
+ if (!badn) return 0 ;
s6rc_repo_sv tomove[badn] ;
for (uint32_t j = 0 ; j < badn ; j++)
{
tomove[j] = byname[bads[j]] ;
if (verbosity >= 2)
- strerr_warni("in set ", set, " of repository ", repo, ": ", options & 1 ? "up" : "down", "fixing service ", sa->s + tomove[j].pos, " from ", s6rc_repo_subnames[tomove[j].sub], " to ", s6rc_repo_subnames[sub]) ;
+ strerr_warni(options & 4 ? "(dry run) " : "", "in set ", set, " of repository ", repo, ": need to ", options & 1 ? "up" : "down", "fix service ", sa->s + tomove[j].pos, " from ", s6rc_repo_subnames[tomove[j].sub], " to ", s6rc_repo_subnames[sub]) ;
if (tomove[j].sub == 0 && verbosity)
- strerr_warnw("service ", sa->s + tomove[j].pos, " is being automatically unmasked by an upfix to ", s6rc_repo_subnames[sub]) ;
+ strerr_warnw(options & 4 ? "(dry run) " : "", "service ", sa->s + tomove[j].pos, " will automatically be unmasked by an upfix to ", s6rc_repo_subnames[sub]) ;
if (tomove[j].sub == 3)
{
if (!(options & 2))
{
- strerr_warnf("in set ", set, " of repository ", repo, ": service ", sa->s + tomove[j].pos, " is marked as essential and cannot be downfixed. If you are sure of yourself, try --force-essential") ;
- goto err ;
+ if (options & 4)
+ strerr_warnw(options & 4 ? "(dry run) " : "", "in set ", set, " of repository ", repo, ": service ", sa->s + tomove[j].pos, " is marked as essential and cannot be downfixed. You will need --force-essential") ;
+ else
+ {
+ strerr_warnf(options & 4 ? "(dry run) " : "", "in set ", set, " of repository ", repo, ": service ", sa->s + tomove[j].pos, " is marked as essential and cannot be downfixed. If you are sure of yourself, try --force-essential") ;
+ e = 1 ;
+ goto err ;
+ }
}
- if (verbosity)
- strerr_warnw("service ", sa->s + tomove[j].pos, " is being automatically marked non-essential by a downfix to ", s6rc_repo_subnames[sub]) ;
+ else if (verbosity)
+ strerr_warnw(options & 4 ? "(dry run) " : "", "service ", sa->s + tomove[j].pos, " will automatically be marked non-essential by a downfix to ", s6rc_repo_subnames[sub]) ;
}
}
- if (!s6rc_repo_moveservices(repo, set, tomove, badn, sub, sa->s, verbosity)) goto err ;
- for (uint32_t j = 0 ; j < badn ; j++) byname[bads[j]].sub = sub ;
- genalloc_setlen(uint32_t, badga, gabase) ;
- sa->len = sabase ;
- return 1 ;
+ if (!(options & 4))
+ {
+ if (!s6rc_repo_moveservices(repo, set, tomove, badn, sub, sa->s, verbosity)) { e = 111 ; goto err ; }
+ for (uint32_t j = 0 ; j < badn ; j++) byname[bads[j]].sub = sub ;
+ }
err:
genalloc_setlen(uint32_t, badga, gabase) ;
sa->len = sabase ;
- return 0 ;
+ return e ;
}
int s6rc_repo_fixset (char const *repo, char const *set, uint32_t options, unsigned int verbosity, stralloc *sa, genalloc *svlist, genalloc *badga)
{
- int sawasnull = !sa->s ;
- int svwasnull = !genalloc_s(s6rc_repo_sv, svlist) ;
- int gawasnull = !genalloc_s(uint32_t, badga) ;
+ int e = 0 ;
size_t sabase = sa->len ;
uint32_t svbase = genalloc_len(s6rc_repo_sv, svlist) ;
uint32_t gabase = genalloc_len(uint32_t, badga) ;
@@ -68,8 +73,8 @@ int s6rc_repo_fixset (char const *repo, char const *set, uint32_t options, unsig
if (!s6rc_repo_makesvlist(repo, set, sa, svlist, subind)) return 0 ;
byname = genalloc_s(s6rc_repo_sv, svlist) ;
ntot = genalloc_len(s6rc_repo_sv, svlist) ;
- if (!ntot) return 1 ;
- for (uint8_t i = 0 ; i < 4 ; i++) sublen[i] = (i == 3 ? ntot : subind[i+1]) - subind[i] ;
+ if (!ntot) return 0 ;
+ for (uint8_t sub = 0 ; sub < 4 ; sub++) sublen[sub] = (sub == 3 ? ntot : subind[sub+1]) - subind[sub] ;
char const *bysub[ntot] ;
char bysub_storage[sa->len - sabase] ;
@@ -77,20 +82,14 @@ int s6rc_repo_fixset (char const *repo, char const *set, uint32_t options, unsig
for (uint32_t i = 0 ; i < ntot ; i++) bysub[i] = bysub_storage + byname[i].pos - sabase ;
qsortr(byname, ntot, sizeof(s6rc_repo_sv), &s6rc_repo_sv_cmpr, sa->s) ;
- for (uint8_t i = 0 ; i < 4 ; i++) if (sublen[i])
+ for (uint8_t sub = 0 ; sub < 4 ; sub++) if (sublen[sub])
{
- if (!s6rc_repo_fixsub(repo, set, i, byname, bysub, ntot, options, subind, sublen, verbosity, sa, badga))
- goto err ;
+ e = s6rc_repo_fixsub(repo, set, sub, byname, bysub, ntot, options, subind, sublen, verbosity, sa, badga) ;
+ if (e) break ;
}
genalloc_setlen(uint32_t, badga, gabase) ;
genalloc_setlen(s6rc_repo_sv, svlist, svbase) ;
sa->len = sabase ;
- return 1 ;
-
- err:
- if (gawasnull) genalloc_free(uint32_t, badga) ; else genalloc_setlen(uint32_t, badga, gabase) ;
- if (svwasnull) genalloc_free(s6rc_repo_sv, svlist) ; else genalloc_setlen(s6rc_repo_sv, svlist, svbase) ;
- if (sawasnull) stralloc_free(sa) ; else sa->len = sabase ;
- return 0 ;
+ return e ;
}
diff --git a/src/repo/s6rc_repo_listsub.c b/src/repo/s6rc_repo_listsub.c
index 1ee23bf..f6fa7c5 100644
--- a/src/repo/s6rc_repo_listsub.c
+++ b/src/repo/s6rc_repo_listsub.c
@@ -9,11 +9,12 @@ int s6rc_repo_listsub (char const *repo, char const *set, char const *sub, stral
size_t repolen = strlen(repo) ;
size_t setlen = strlen(set) ;
size_t sublen = strlen(sub) ;
- char fn[repolen + setlen + 10 + sublen] ;
+ char fn[repolen + setlen + 11 + sublen] ;
memcpy(fn, repo, repolen) ;
memcpy(fn + repolen, "/sources/", 9) ;
memcpy(fn + repolen + 9, set, setlen) ;
- memcpy(fn + repolen + 9 + setlen, sub, sublen + 1) ;
+ fn[repolen + 9 + setlen] = '/' ;
+ memcpy(fn + repolen + 10 + setlen, sub, sublen + 1) ;
return s6rc_repo_ls(fn, sa, ga) ;
}
diff --git a/src/repo/s6rc_repo_makedefbundle.c b/src/repo/s6rc_repo_makedefbundle.c
index 5c67917..d0d5d32 100644
--- a/src/repo/s6rc_repo_makedefbundle.c
+++ b/src/repo/s6rc_repo_makedefbundle.c
@@ -21,6 +21,7 @@ int s6rc_repo_makedefbundle (char const *repo, char const *set, char const *bund
size_t setlen = strlen(set) ;
size_t bundlelen = strlen(bundle) ;
size_t bfnlen = repolen + setlen + bundlelen + 28 ;
+ mode_t m ;
DIR *dir ;
char bfn[bfnlen + 1] ;
char subfn[repolen + setlen + 17] ;
@@ -46,60 +47,67 @@ int s6rc_repo_makedefbundle (char const *repo, char const *set, char const *bund
memcpy(bfn + repolen + 9, set, setlen) ;
memcpy(bfn + repolen + 9 + setlen, "/bundle/", 8) ;
memcpy(bfn + repolen + 17 + setlen, bundle, bundlelen + 1) ;
+ m = umask(0) ;
if (mkdir(bfn, 02755) == -1)
{
if (errno != EEXIST) strerr_warnfu2sys("mkdir ", bfn) ;
else strerr_warnf6x("bundle ", bundle, " is already defined in set ", set, " of repository ", repo) ;
+ umask(m) ;
return 0 ;
}
- memcpy(bfn + repolen + setlen + 17 + bundlelen, "/type", 6) ;
- if (!openwritenclose_unsafe(bfn, "bundle\n", 7))
+ memcpy(bfn + repolen + setlen + 17 + bundlelen, "/contents.d", 12) ;
+ if (mkdir(bfn, 02755) == -1)
{
- strerr_warnfu2sys("write to ", bfn) ;
+ strerr_warnfu2sys("mkdir ", bfn) ;
+ umask(m) ;
return 0 ;
}
- memcpy(bfn + repolen + setlen + 18 + bundlelen, "contents.d", 11) ;
- if (mkdir(bfn, 02755) == -1)
+ umask(m) ;
+ memcpy(bfn + repolen + setlen + 18 + bundlelen, "type", 5) ;
+ if (!openwritenclose_unsafe(bfn, "bundle\n", 7))
{
- strerr_warnfu2sys("mkdir ", bfn) ;
+ strerr_warnfu2sys("write to ", bfn) ;
return 0 ;
}
bfn[bfnlen] = '/' ;
memcpy(subfn, bfn, repolen + 10 + setlen) ;
- memcpy(subfn + repolen + 10 + setlen, "onboot", 7) ;
- dir = opendir(subfn) ;
- if (!dir)
+ for (uint8_t sub = 2 ; sub < 4 ; sub++)
{
- strerr_warnfu2sys("opendir ", subfn) ;
- return 0 ;
- }
+ memcpy(subfn + repolen + 10 + setlen, s6rc_repo_subnames[sub], 7) ;
+ dir = opendir(subfn) ;
+ if (!dir)
+ {
+ strerr_warnfu2sys("opendir ", subfn) ;
+ return 0 ;
+ }
- for (;;)
- {
- size_t len ;
- direntry *d ;
- errno = 0 ;
- d = readdir(dir) ;
- if (!d) break ;
- if (d->d_name[0] == '.') continue ;
- len = strlen(d->d_name) ;
- char fn[bfnlen + len + 2] ;
- memcpy(fn, bfn, bfnlen + 1) ;
- memcpy(fn + bfnlen + 1, d->d_name, len + 1) ;
- if (!openwritenclose_unsafe(fn, "", 0))
+ for (;;)
{
- strerr_warnfu2sys("write to ", fn) ;
- dir_close(dir) ;
+ size_t len ;
+ direntry *d ;
+ errno = 0 ;
+ d = readdir(dir) ;
+ if (!d) break ;
+ if (d->d_name[0] == '.') continue ;
+ len = strlen(d->d_name) ;
+ char fn[bfnlen + len + 2] ;
+ memcpy(fn, bfn, bfnlen + 1) ;
+ memcpy(fn + bfnlen + 1, d->d_name, len + 1) ;
+ if (!openwritenclose_unsafe(fn, "", 0))
+ {
+ strerr_warnfu2sys("write to ", fn) ;
+ dir_close(dir) ;
+ return 0 ;
+ }
+ }
+ dir_close(dir) ;
+ if (errno)
+ {
+ strerr_warnfu2sys("readdir ", subfn) ;
return 0 ;
}
}
- dir_close(dir) ;
- if (errno)
- {
- strerr_warnfu2sys("readdir ", subfn) ;
- return 0 ;
- }
return 1 ;
@@ -117,5 +125,5 @@ int s6rc_repo_makedefbundle (char const *repo, char const *set, char const *bund
}
strerr_warnf4x("bundle ", bundle, " is already defined at ", satmp.s) ;
satmp.len = 0 ;
- return (errno = EINVAL, 0) ;
+ return 0 ;
}
diff --git a/src/repo/s6rc_repo_makesetbundles.c b/src/repo/s6rc_repo_makesetbundles.c
index 9cf73ac..5bb72a6 100644
--- a/src/repo/s6rc_repo_makesetbundles.c
+++ b/src/repo/s6rc_repo_makesetbundles.c
@@ -24,14 +24,14 @@ int s6rc_repo_makesetbundles (char const *repo, char const *set, unsigned int ve
stralloc sa = STRALLOC_ZERO ;
genalloc ga = GENALLOC_ZERO ;
int n ;
- char src[repolen + 19] ;
+ char src[repolen + 18] ;
char dst[repolen + 17 + setlen] ;
memcpy(dst, repo, repolen) ;
memcpy(dst + repolen, "/sources/", 9) ;
memcpy(dst + repolen + 9, set, setlen) ;
memcpy(dst + repolen + 9 + setlen, "/bundle", 8) ;
- memcpy(src, dst, repolen + 10) ;
- memcpy(src + repolen + 10, ".bundles", 9) ;
+ memcpy(src, dst, repolen + 9) ;
+ memcpy(src + repolen + 9, ".bundles", 9) ;
rm_rf_tmp(dst, &sa) ;
n = s6rc_repo_listsub(repo, set, s6rc_repo_subnames[0], &sa, &ga) ;
@@ -114,10 +114,27 @@ int s6rc_repo_makesetbundles (char const *repo, char const *set, unsigned int ve
goto err0 ;
}
}
- else if (!hiercopy_tmp(src, dst, &sa))
+ else
{
- strerr_warnfu4sys("recursively copy ", src, " to ", dst) ;
- goto err0 ;
+ int r ;
+ char realsrc[repolen + 28] ;
+ memcpy(realsrc, src, repolen + 9) ;
+ r = readlink(src, realsrc + repolen + 9, 17) ;
+ if (r == -1)
+ {
+ strerr_warnfu2sys("readlink ", src) ;
+ goto err0 ;
+ }
+ if (r != 16)
+ {
+ strerr_warnf2x("invalid symlink for ", src) ;
+ goto err0 ;
+ }
+ if (!hiercopy_tmp(realsrc, dst, &sa))
+ {
+ strerr_warnfu4sys("recursively copy ", realsrc, " to ", dst) ;
+ goto err0 ;
+ }
}
stralloc_free(&sa) ;
diff --git a/src/repo/s6rc_repo_moveservices.c b/src/repo/s6rc_repo_moveservices.c
index 00d1ec8..cac2052 100644
--- a/src/repo/s6rc_repo_moveservices.c
+++ b/src/repo/s6rc_repo_moveservices.c
@@ -5,6 +5,7 @@
#include <errno.h>
#include <stdio.h>
+#include <skalibs/posixplz.h>
#include <skalibs/strerr.h>
#include <s6-rc/repo.h>
@@ -56,5 +57,10 @@ int s6rc_repo_moveservices (char const *repo, char const *set, s6rc_repo_sv cons
strerr_warnt10x("repository ", repo, ", set ", set, ", from subset ", s6rc_repo_subnames[services[i].sub], " to subset ", s6rc_repo_subnames[newsub], ": successfully moved service ", storage + services[i].pos) ;
}
}
+ if (n)
+ {
+ memcpy(oldfn + repolen + 10 + setlen, ".stamp", 7) ;
+ if (!touch(oldfn)) strerr_warnwu2sys("touch ", oldfn) ;
+ }
return 1 ;
}