diff options
author | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2012-10-02T19·04-0400 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2012-10-02T19·12-0400 |
commit | 8e3a7bd71253f02eb1a9fbb996166727b1283887 (patch) | |
tree | 622c2de46ea29a6d42defc50b65112c5b6022138 /src/libstore | |
parent | 9958bd6992e2b3e7bacb493a372d17d5a5b95d90 (diff) |
nix-store --verify: Add an option ‘--repair’ to repair all missing/corrupt paths
Also, return a non-zero exit code if errors remain after verifying/repairing.
Diffstat (limited to 'src/libstore')
-rw-r--r-- | src/libstore/local-store.cc | 25 | ||||
-rw-r--r-- | src/libstore/local-store.hh | 7 |
2 files changed, 24 insertions, 8 deletions
diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index 6882e9328b6e..e038cd4b29d3 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -1544,10 +1544,12 @@ void LocalStore::invalidatePathChecked(const Path & path) } -void LocalStore::verifyStore(bool checkContents) +bool LocalStore::verifyStore(bool checkContents, bool repair) { printMsg(lvlError, format("reading the Nix store...")); + bool errors = false; + /* Acquire the global GC lock to prevent a garbage collection. */ AutoCloseFD fdGCLock = openGCLock(ltWrite); @@ -1560,7 +1562,7 @@ void LocalStore::verifyStore(bool checkContents) PathSet validPaths2 = queryAllValidPaths(), validPaths, done; foreach (PathSet::iterator, i, validPaths2) - verifyPath(*i, store, done, validPaths); + verifyPath(*i, store, done, validPaths, repair, errors); /* Release the GC lock so that checking content hashes (which can take ages) doesn't block the GC or builds. */ @@ -1584,6 +1586,7 @@ void LocalStore::verifyStore(bool checkContents) printMsg(lvlError, format("path `%1%' was modified! " "expected hash `%2%', got `%3%'") % *i % printHash(info.hash) % printHash(current.first)); + if (repair) repairPath(*i); else errors = true; } else { bool update = false; @@ -1611,14 +1614,17 @@ void LocalStore::verifyStore(bool checkContents) errors on invalid paths. */ if (isValidPath(*i)) throw; printMsg(lvlError, format("warning: %1%") % e.msg()); + errors = true; } } } + + return errors; } void LocalStore::verifyPath(const Path & path, const PathSet & store, - PathSet & done, PathSet & validPaths) + PathSet & done, PathSet & validPaths, bool repair, bool & errors) { checkInterrupt(); @@ -1638,7 +1644,7 @@ void LocalStore::verifyPath(const Path & path, const PathSet & store, PathSet referrers; queryReferrers(path, referrers); foreach (PathSet::iterator, i, referrers) if (*i != path) { - verifyPath(*i, store, done, validPaths); + verifyPath(*i, store, done, validPaths, repair, errors); if (validPaths.find(*i) != validPaths.end()) canInvalidate = false; } @@ -1646,8 +1652,17 @@ void LocalStore::verifyPath(const Path & path, const PathSet & store, if (canInvalidate) { printMsg(lvlError, format("path `%1%' disappeared, removing from database...") % path); invalidatePath(path); - } else + } else { printMsg(lvlError, format("path `%1%' disappeared, but it still has valid referrers!") % path); + if (repair) + try { + repairPath(path); + } catch (Error & e) { + printMsg(lvlError, format("warning: %1%") % e.msg()); + errors = true; + } + else errors = true; + } return; } diff --git a/src/libstore/local-store.hh b/src/libstore/local-store.hh index 80db10de10d0..9f1c8280a8de 100644 --- a/src/libstore/local-store.hh +++ b/src/libstore/local-store.hh @@ -171,8 +171,9 @@ public: /* Optimise a single store path. */ void optimisePath(const Path & path); - /* Check the integrity of the Nix store. */ - void verifyStore(bool checkContents); + /* Check the integrity of the Nix store. Returns true if errors + remain. */ + bool verifyStore(bool checkContents, bool repair); /* Register the validity of a path, i.e., that `path' exists, that the paths referenced by it exists, and in the case of an output @@ -250,7 +251,7 @@ private: void invalidatePathChecked(const Path & path); void verifyPath(const Path & path, const PathSet & store, - PathSet & done, PathSet & validPaths); + PathSet & done, PathSet & validPaths, bool repair, bool & errors); void updatePathInfo(const ValidPathInfo & info); |