diff --git a/src/init.cpp b/src/init.cpp index 77a496853..778e1a3c9 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -151,6 +151,33 @@ static leveldb::Status ProbeLevelDBLock(const boost::filesystem::path& dbdir) return status; } +/** + * Does hold any LevelDB data, i.e. a non-empty table (.ldb/.sst) or write-ahead log (.log)? + * CURRENT, MANIFEST-*, LOCK and the LOG/LOG.old info logs carry no records. When in doubt (the + * directory cannot be listed) say yes, so the caller keeps a copy. + */ +static bool LevelDBDirHasData(const boost::filesystem::path& dbdir) +{ + boost::system::error_code ec; + if (!boost::filesystem::exists(dbdir, ec)) + return ec ? true : false; + boost::filesystem::directory_iterator it(dbdir, ec), end; + if (ec) + return true; + for (; it != end; it.increment(ec)) { + if (ec) + return true; + const std::string ext = it->path().extension().string(); + if (ext != ".ldb" && ext != ".sst" && ext != ".log") + continue; + boost::system::error_code sizeEc; + const uintmax_t size = boost::filesystem::file_size(it->path(), sizeEc); + if (sizeEc || size > 0) + return true; + } + return false; +} + /** * Open the notarizations (dPoW) DB. It is non-essential and node-regenerable, so nothing here may fail * startup (previously any open failure surfaced as a spurious "Error opening block database"): @@ -159,7 +186,9 @@ static leveldb::Status ProbeLevelDBLock(const boost::filesystem::path& dbdir) * in-memory DB and leave the on-disk one alone. * - otherwise the DB is unreadable (seen: a torn snapshot, 0-byte log -> leveldb "Database I/O error"): * move it aside under a timestamped notarizations.corrupt-* name, never deleting an earlier copy, and - * create a fresh one. If even that fails, fall back to an in-memory DB. + * create a fresh one. A directory holding no data is recreated in place instead, so a persistent + * failure (e.g. disk full) does not add another empty copy on every start. If even that fails, fall + * back to an in-memory DB. * The lock is probed only after an open fails, so a holder that exits between the failed open and the * probe makes the probe report "free" for a DB that is fine. The DB is therefore treated as corrupt only * when an open fails although the probe just before that open found the lock free. @@ -210,6 +239,16 @@ static NotarizationDB* OpenNotarizationDB(bool fWipe) fProbedFree = true; } + if (!LevelDBDirHasData(ndir)) { + LogPrintf("%s: notarizations DB failed to open (%s) and holds no data; recreating it in place (non-fatal)\n", __func__, strError); + try { + return new NotarizationDB(NOTARIZATION_DB_CACHE_BYTES, false, true); + } catch (const std::exception& e) { + LogPrintf("%s: could not recreate the notarizations DB (%s); using an in-memory notarizations DB for this run\n", __func__, e.what()); + return new NotarizationDB(NOTARIZATION_DB_CACHE_BYTES, true, false); + } + } + LogPrintf("%s: notarizations DB failed to open (%s); moving aside and regenerating (non-fatal)\n", __func__, strError); // Move (do NOT delete) the old DB aside, so a transient open failure (fd exhaustion, disk full, // permissions) cannot permanently destroy notarization history. Earlier copies are kept too.