We are in ld->config_netdir when this is run! */
| 1678 | |
| 1679 | /* We are in ld->config_netdir when this is run! */ |
| 1680 | static void promote_missing_files(struct lightningd *ld) |
| 1681 | { |
| 1682 | #ifdef COMPAT_V073 |
| 1683 | DIR *d_from; |
| 1684 | struct dirent *d; |
| 1685 | struct stat st; |
| 1686 | |
| 1687 | /* If hsm_secret already exists, we assume we're ugpraded */ |
| 1688 | if (stat("hsm_secret", &st) == 0) |
| 1689 | return; |
| 1690 | |
| 1691 | if (errno != ENOENT) |
| 1692 | err(1, "Looking for hsm_secret in lightning dir"); |
| 1693 | |
| 1694 | /* If hsm doesn't exist in basedir, we've nothing to upgrade. */ |
| 1695 | if (stat(path_join(tmpctx, ld->config_basedir, "hsm_secret"), &st) != 0) |
| 1696 | return; |
| 1697 | |
| 1698 | d_from = opendir(ld->config_basedir); |
| 1699 | if (!d_from) |
| 1700 | err(1, "Opening %s", ld->config_basedir); |
| 1701 | |
| 1702 | while ((d = readdir(d_from)) != NULL) { |
| 1703 | const char *fullname; |
| 1704 | |
| 1705 | /* Ignore this directory and upper one, and leave |
| 1706 | * config and pid files */ |
| 1707 | if (streq(d->d_name, ".") |
| 1708 | || streq(d->d_name, "..") |
| 1709 | || streq(d->d_name, "config") |
| 1710 | || strends(d->d_name, ".pid")) |
| 1711 | continue; |
| 1712 | |
| 1713 | fullname = path_join(tmpctx, ld->config_basedir, d->d_name); |
| 1714 | |
| 1715 | /* Simply remove rpc file: if they use --rpc-file to place it |
| 1716 | * here explicitly it will get recreated, but moving it would |
| 1717 | * be confusing as it would be unused. */ |
| 1718 | if (streq(d->d_name, "lightning-rpc")) { |
| 1719 | if (unlink(fullname) != 0) |
| 1720 | log_unusual(ld->log, "Could not unlink %s: %s", |
| 1721 | fullname, strerror(errno)); |
| 1722 | continue; |
| 1723 | } |
| 1724 | |
| 1725 | /* Ignore any directories. */ |
| 1726 | if (lstat(fullname, &st) != 0) |
| 1727 | errx(1, "Could not stat %s", fullname); |
| 1728 | if ((st.st_mode & S_IFMT) == S_IFDIR) |
| 1729 | continue; |
| 1730 | |
| 1731 | /* Check we don't overwrite something in this dir! */ |
| 1732 | if (lstat(d->d_name, &st) != -1) |
| 1733 | errx(1, "Refusing to overwrite %s into %s/", |
| 1734 | fullname, ld->config_netdir); |
| 1735 | log_unusual(ld->log, "Moving %s into %s/", |
| 1736 | d->d_name, ld->config_netdir); |
| 1737 | if (rename(fullname, d->d_name) != 0) |