From d512bcb85fbffcf80df666c80759941867001634 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 23 Nov 2019 12:16:57 +1030 Subject: [PATCH] lightningd: automatically move files for existing deployments. Signed-off-by: Rusty Russell --- Makefile | 2 +- lightningd/options.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1d021e5be..2726fe70e 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ endif ifeq ($(COMPAT),1) # We support compatibility with pre-0.6. -COMPAT_CFLAGS=-DCOMPAT_V052=1 -DCOMPAT_V060=1 -DCOMPAT_V061=1 -DCOMPAT_V062=1 -DCOMPAT_V070=1 +COMPAT_CFLAGS=-DCOMPAT_V052=1 -DCOMPAT_V060=1 -DCOMPAT_V061=1 -DCOMPAT_V062=1 -DCOMPAT_V070=1 -DCOMPAT_V073=1 endif # Timeout shortly before the 600 second travis silence timeout diff --git a/lightningd/options.c b/lightningd/options.c index 7eea1585f..d1b37328e 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -848,6 +848,60 @@ static void register_opts(struct lightningd *ld) #endif } +/* We are in ld->config_netdir when this is run! */ +static void promote_missing_files(struct lightningd *ld) +{ +#ifdef COMPAT_V073 + DIR *d_from; + struct dirent *d; + struct stat st; + + /* If hsm_secret already exists, we assume we're ugpraded */ + if (stat("hsm_secret", &st) == 0) + return; + + if (errno != ENOENT) + err(1, "Looking for hsm_secret in lightning dir"); + + /* If hsm doesn't exist in basedir, we've nothing to upgrade. */ + if (stat(path_join(tmpctx, ld->config_basedir, "hsm_secret"), &st) != 0) + return; + + d_from = opendir(ld->config_basedir); + if (!d_from) + err(1, "Opening %s", ld->config_basedir); + + while ((d = readdir(d_from)) != NULL) { + const char *fullname; + + /* Ignore this directory and upper one, and leave config */ + if (streq(d->d_name, ".") + || streq(d->d_name, "..") + || streq(d->d_name, "config")) + continue; + + fullname = path_join(tmpctx, ld->config_basedir, d->d_name); + + /* Ignore any directories. */ + if (lstat(fullname, &st) != 0) + errx(1, "Could not stat %s", fullname); + if ((st.st_mode & S_IFMT) == S_IFDIR) + continue; + + /* Check we don't overwrite something in this dir! */ + if (lstat(d->d_name, &st) != -1) + errx(1, "Refusing to overwrite %s into %s/", + fullname, ld->config_netdir); + log_unusual(ld->log, "Moving %s into %s/", + d->d_name, ld->config_netdir); + if (rename(fullname, d->d_name) != 0) + err(1, "Could not move %s/%s to %s", + ld->config_basedir, d->d_name, ld->config_netdir); + } + closedir(d_from); +#endif /* COMPAT_V073 */ +} + /* Names stolen from https://github.com/ternus/nsaproductgenerator/blob/master/nsa.js */ static const char *codename_adjective[] = { "LOUD", "RED", "BLUE", "GREEN", "YELLOW", "IRATE", "ANGRY", "PEEVED", @@ -948,6 +1002,9 @@ void handle_early_opts(struct lightningd *ld, int argc, char *argv[]) ld->config_netdir, strerror(errno)); } + /*~ We move files from old locations on first upgrade. */ + promote_missing_files(ld); + /*~ The ccan/opt code requires registration then parsing; we * mimic this API here, even though they're on separate lines.*/ register_opts(ld);