5 #include <unordered_set>
7 #include "pathlocks.hh"
8 #include "store-api.hh"
15 /* Nix store and database schema version. Version 1 (or 0) was Nix <=
16 0.7. Version 2 was Nix 0.8 and 0.9. Version 3 is Nix 0.10.
17 Version 4 is Nix 0.11. Version 5 is Nix 0.12-0.16. Version 6 is
18 Nix 1.0. Version 7 is Nix 1.3. Guix has always used version 7. */
19 const int nixSchemaVersion
= 7;
22 extern string drvsLogDir
;
30 unsigned long filesLinked
;
31 unsigned long long bytesFreed
;
32 unsigned long long blocksFreed
;
36 bytesFreed
= blocksFreed
= 0;
41 class LocalStore
: public StoreAPI
44 /* The currently running substituter or empty. */
45 std::shared_ptr
<Agent
> runningSubstituter
;
47 /* Ensure the substituter is running and return it. */
48 std::shared_ptr
<Agent
> substituter();
54 /* Initialise the local store, upgrading the schema if
56 LocalStore(bool reserveSpace
= true);
60 /* Implementations of abstract store API methods. */
62 bool isValidPath(const Path
& path
);
64 PathSet
queryValidPaths(const PathSet
& paths
);
66 PathSet
queryAllValidPaths();
68 ValidPathInfo
queryPathInfo(const Path
& path
);
70 Hash
queryPathHash(const Path
& path
);
72 void queryReferences(const Path
& path
, PathSet
& references
);
74 void queryReferrers(const Path
& path
, PathSet
& referrers
);
76 Path
queryDeriver(const Path
& path
);
78 PathSet
queryValidDerivers(const Path
& path
);
80 PathSet
queryDerivationOutputs(const Path
& path
);
82 StringSet
queryDerivationOutputNames(const Path
& path
);
84 Path
queryPathFromHashPart(const string
& hashPart
);
86 PathSet
querySubstitutablePaths(const PathSet
& paths
);
88 void querySubstitutablePathInfos(PathSet
& paths
,
89 SubstitutablePathInfos
& infos
);
91 void querySubstitutablePathInfos(const PathSet
& paths
,
92 SubstitutablePathInfos
& infos
);
94 Path
addToStore(const string
& name
, const Path
& srcPath
,
95 bool recursive
= true, HashType hashAlgo
= htSHA256
,
96 PathFilter
& filter
= defaultPathFilter
, bool repair
= false);
98 /* Like addToStore(), but the contents of the path are contained
99 in `dump', which is either a NAR serialisation (if recursive ==
100 true) or simply the contents of a regular file (if recursive ==
102 Path
addToStoreFromDump(const string
& dump
, const string
& name
,
103 bool recursive
= true, HashType hashAlgo
= htSHA256
, bool repair
= false);
105 Path
addTextToStore(const string
& name
, const string
& s
,
106 const PathSet
& references
, bool repair
= false);
108 void exportPath(const Path
& path
, bool sign
,
111 Paths
importPaths(bool requireSignature
, Source
& source
);
113 void buildPaths(const PathSet
& paths
, BuildMode buildMode
);
115 void ensurePath(const Path
& path
);
117 void addTempRoot(const Path
& path
);
119 void addIndirectRoot(const Path
& path
);
125 void collectGarbage(const GCOptions
& options
, GCResults
& results
);
127 /* Optimise the disk space usage of the Nix store by hard-linking
128 files with the same contents. */
129 void optimiseStore(OptimiseStats
& stats
);
131 /* Generic variant of the above method. */
132 void optimiseStore();
134 /* Optimise a single store path. */
135 void optimisePath(const Path
& path
);
137 /* Check the integrity of the Nix store. Returns true if errors
139 bool verifyStore(bool checkContents
, bool repair
);
141 /* Register the validity of a path, i.e., that `path' exists, that
142 the paths referenced by it exists, and in the case of an output
143 path of a derivation, that it has been produced by a successful
144 execution of the derivation (or something equivalent). Also
145 register the hash of the file system contents of the path. The
146 hash must be a SHA-256 hash. */
147 void registerValidPath(const ValidPathInfo
& info
);
149 void registerValidPaths(const ValidPathInfos
& infos
);
151 /* Register that the build of a derivation with output `path' has
153 void registerFailedPath(const Path
& path
);
155 /* Query whether `path' previously failed to build. */
156 bool hasPathFailed(const Path
& path
);
158 PathSet
queryFailedPaths();
160 void clearFailedPaths(const PathSet
& paths
);
164 /* Repair the contents of the given path by redownloading it using
165 a substituter (if available). */
166 void repairPath(const Path
& path
);
168 /* Check whether the given valid path exists and has the right
170 bool pathContentsGood(const Path
& path
);
172 void markContentsGood(const Path
& path
);
174 void createUser(const std::string
& userName
, uid_t userId
);
180 /* Lock file used for upgrading. */
181 AutoCloseFD globalLock
;
183 /* The SQLite database object. */
186 /* Some precompiled SQLite statements. */
187 SQLiteStmt stmtRegisterValidPath
;
188 SQLiteStmt stmtUpdatePathInfo
;
189 SQLiteStmt stmtAddReference
;
190 SQLiteStmt stmtQueryPathInfo
;
191 SQLiteStmt stmtQueryReferences
;
192 SQLiteStmt stmtQueryReferrers
;
193 SQLiteStmt stmtInvalidatePath
;
194 SQLiteStmt stmtRegisterFailedPath
;
195 SQLiteStmt stmtHasPathFailed
;
196 SQLiteStmt stmtQueryFailedPaths
;
197 SQLiteStmt stmtClearFailedPath
;
198 SQLiteStmt stmtAddDerivationOutput
;
199 SQLiteStmt stmtQueryValidDerivers
;
200 SQLiteStmt stmtQueryDerivationOutputs
;
201 SQLiteStmt stmtQueryPathFromHashPart
;
202 SQLiteStmt stmtQueryValidPaths
;
204 /* Cache for pathContentsGood(). */
205 std::map
<Path
, bool> pathContentsGoodCache
;
207 /* The file to which we write our temporary roots. */
209 AutoCloseFD fdTempRoots
;
213 void openDB(bool create
);
215 void makeStoreWritable();
217 uint64_t queryValidPathId(const Path
& path
);
219 uint64_t addValidPath(const ValidPathInfo
& info
, bool checkOutputs
= true);
221 void addReference(uint64_t referrer
, uint64_t reference
);
223 void appendReferrer(const Path
& from
, const Path
& to
, bool lock
);
225 void rewriteReferrers(const Path
& path
, bool purge
, PathSet referrers
);
227 void invalidatePath(const Path
& path
);
229 /* Delete a path from the Nix store. */
230 void invalidatePathChecked(const Path
& path
);
232 void verifyPath(const Path
& path
, const PathSet
& store
,
233 PathSet
& done
, PathSet
& validPaths
, bool repair
, bool & errors
);
235 void updatePathInfo(const ValidPathInfo
& info
);
239 void deleteGarbage(GCState
& state
, const Path
& path
);
241 void tryToDelete(GCState
& state
, const Path
& path
);
243 bool canReachRoot(GCState
& state
, PathSet
& visited
, const Path
& path
);
245 void deletePathRecursive(GCState
& state
, const Path
& path
);
247 bool isActiveTempFile(const GCState
& state
,
248 const Path
& path
, const string
& suffix
);
250 int openGCLock(LockType lockType
);
252 void removeUnusedLinks(const GCState
& state
);
254 string
getLineFromSubstituter(Agent
& run
);
256 template<class T
> T
getIntLineFromSubstituter(Agent
& run
);
258 Path
createTempDirInStore();
260 Path
importPath(bool requireSignature
, Source
& source
);
262 void checkDerivationOutputs(const Path
& drvPath
, const Derivation
& drv
);
264 typedef std::unordered_set
<ino_t
> InodeHash
;
266 InodeHash
loadInodeHash();
267 Strings
readDirectoryIgnoringInodes(const Path
& path
, const InodeHash
& inodeHash
);
268 void optimisePath_(OptimiseStats
& stats
, const Path
& path
, InodeHash
& inodeHash
);
270 // Internal versions that are not wrapped in retry_sqlite.
271 bool isValidPath_(const Path
& path
);
272 void queryReferrers_(const Path
& path
, PathSet
& referrers
);
276 typedef std::pair
<dev_t
, ino_t
> Inode
;
277 typedef set
<Inode
> InodesSeen
;
280 /* "Fix", or canonicalise, the meta-data of the files in a store path
281 after it has been built. In particular:
282 - the last modification date on each file is set to 1 (i.e.,
283 00:00:01 1/1/1970 UTC)
284 - the permissions are set of 444 or 555 (i.e., read-only with or
285 without execute permission; setuid bits etc. are cleared)
286 - the owner and group are set to the Nix user and group, if we're
288 void canonicalisePathMetaData(const Path
& path
, uid_t fromUid
, InodesSeen
& inodesSeen
);
289 void canonicalisePathMetaData(const Path
& path
, uid_t fromUid
);
291 void canonicaliseTimestampAndPermissions(const Path
& path
);
293 MakeError(PathInUse
, Error
);