From 949964fa25c8b4cd088f34ccad636e185a878da5 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Wed, 8 Feb 2023 05:51:44 +0000 Subject: Add instance support to s6-rc-update Signed-off-by: Laurent Bercot --- src/libs6rc/s6rc_servicedir_copy_online.c | 60 ++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) (limited to 'src/libs6rc') diff --git a/src/libs6rc/s6rc_servicedir_copy_online.c b/src/libs6rc/s6rc_servicedir_copy_online.c index 62d7c87..a3fa1f7 100644 --- a/src/libs6rc/s6rc_servicedir_copy_online.c +++ b/src/libs6rc/s6rc_servicedir_copy_online.c @@ -6,12 +6,14 @@ #include #include +#include #include + #include #include -#include "s6rc-servicedir-internal.h" #include +#include "s6rc-servicedir-internal.h" int s6rc_servicedir_copy_online (char const *src, char const *dst) { @@ -21,6 +23,8 @@ int s6rc_servicedir_copy_online (char const *src, char const *dst) unsigned int i = 0 ; int wantup = 0 ; int e ; + struct stat st ; + DIR *dir ; char srcfn[srclen + S6_SERVICEDIR_FILE_MAXLEN + 6] ; char dstfn[dstlen + S6_SERVICEDIR_FILE_MAXLEN + 6] ; char oldfn[dstlen + S6_SERVICEDIR_FILE_MAXLEN + 6] ; @@ -63,6 +67,51 @@ int s6rc_servicedir_copy_online (char const *src, char const *dst) } oldfn[dstlen + 4] = 0 ; rm_rf(oldfn) ; + + /* New files copied, now let's update the instances with the new template */ + + memcpy(oldfn + dstlen + 1, "template", 9) ; + if (stat(oldfn, &st) == 0) + { + if (!S_ISDIR(st.st_mode)) { errno = EINVAL ; goto erri ; } + memcpy(dstfn + dstlen + 1, "instances", 10) ; + dir = opendir(dstfn) ; + if (!dir) + { + mode_t m ; + if (errno != ENOENT) goto erri ; + m = umask(0) ; + if (mkdir(dstfn, 0755) == -1) { umask(m) ; goto erri ; } + dstfn[dstlen + 9] = 0 ; + if (mkdir(dstfn, 0755) == -1) { umask(m) ; goto erri ; } + umask(m) ; + } + else + { + dstfn[dstlen + 10] = '/' ; + for (;;) + { + direntry *d ; + errno = 0 ; + d = readdir(dir) ; + if (!d) break ; + if (d->d_name[0] == '.') + if (((d->d_name[1] == '.') && !d->d_name[2]) || !d->d_name[1]) + continue ; + { + size_t len = strlen(d->d_name) ; + char ifn[dstlen + 12 + len] ; + memcpy(ifn, dstfn, dstlen + 11) ; + memcpy(ifn + dstlen + 11, d->d_name, len + 1) ; + if (!s6rc_servicedir_copy_online(oldfn, ifn)) goto errd ; + } + } + dir_close(dir) ; + if (errno) goto erri ; + } + } + else if (errno != ENOENT) goto erri ; + s6rc_servicedir_unblock(dst, wantup) ; return 1 ; @@ -80,9 +129,18 @@ int s6rc_servicedir_copy_online (char const *src, char const *dst) strcpy(oldfn + dstlen + 5, s6rc_servicedir_file_list[i].name) ; rename(oldfn, dstfn) ; } + s6rc_servicedir_unblock(dst, wantup) ; errdir: oldfn[dstlen + 4] = 0 ; rmdir(oldfn) ; errno = e ; return 0 ; + + errd: + dir_close(dir) ; + erri: + e = errno ; + s6rc_servicedir_unblock(dst, wantup) ; + errno = e ; + return 0 ; } -- cgit v1.2.3