We are in ld->config_netdir when this is run! */
| 1718 | |
| 1719 | /* We are in ld->config_netdir when this is run! */ |
| 1720 | static void promote_missing_files(struct lightningd *ld) |
| 1721 | { |
| 1722 | #ifdef COMPAT_V073 |
| 1723 | DIR *d_from; |
| 1724 | struct dirent *d; |
| 1725 | struct stat st; |
| 1726 | |
| 1727 | /* If hsm_secret already exists, we assume we're ugpraded */ |
| 1728 | if (stat("hsm_secret", &st) == 0) |
| 1729 | return; |
| 1730 | |
| 1731 | if (errno != ENOENT) |
| 1732 | err(1, "Looking for hsm_secret in lightning dir"); |
| 1733 | |
| 1734 | /* If hsm doesn't exist in basedir, we've nothing to upgrade. */ |
| 1735 | if (stat(path_join(tmpctx, ld->config_basedir, "hsm_secret"), &st) != 0) |
| 1736 | return; |
| 1737 | |
| 1738 | d_from = opendir(ld->config_basedir); |
| 1739 | if (!d_from) |
| 1740 | err(1, "Opening %s", ld->config_basedir); |
| 1741 | |
| 1742 | while ((d = readdir(d_from)) != NULL) { |
| 1743 | const char *fullname; |
| 1744 | |
| 1745 | /* Ignore this directory and upper one, and leave |
| 1746 | * config and pid files */ |
| 1747 | if (streq(d->d_name, ".") |
| 1748 | || streq(d->d_name, "..") |
| 1749 | || streq(d->d_name, "config") |
| 1750 | || strends(d->d_name, ".pid")) |
| 1751 | continue; |
| 1752 | |
| 1753 | fullname = path_join(tmpctx, ld->config_basedir, d->d_name); |
| 1754 | |
| 1755 | /* Simply remove rpc file: if they use --rpc-file to place it |
| 1756 | * here explicitly it will get recreated, but moving it would |
| 1757 | * be confusing as it would be unused. */ |
| 1758 | if (streq(d->d_name, "lightning-rpc")) { |
| 1759 | if (unlink(fullname) != 0) |
| 1760 | log_unusual(ld->log, "Could not unlink %s: %s", |
| 1761 | fullname, strerror(errno)); |
| 1762 | continue; |
| 1763 | } |
| 1764 | |
| 1765 | /* Ignore any directories. */ |
| 1766 | if (lstat(fullname, &st) != 0) |
| 1767 | errx(1, "Could not stat %s", fullname); |
| 1768 | if ((st.st_mode & S_IFMT) == S_IFDIR) |
| 1769 | continue; |
| 1770 | |
| 1771 | /* Check we don't overwrite something in this dir! */ |
| 1772 | if (lstat(d->d_name, &st) != -1) |
| 1773 | errx(1, "Refusing to overwrite %s into %s/", |
| 1774 | fullname, ld->config_netdir); |
| 1775 | log_unusual(ld->log, "Moving %s into %s/", |
| 1776 | d->d_name, ld->config_netdir); |
| 1777 | if (rename(fullname, d->d_name) != 0) |