4 #include <unordered_set>
6 #include "store-api.hh"
8 #include "pathlocks.hh"
18 /* Nix store and database schema version. Version 1 (or 0) was Nix <=
19 0.7. Version 2 was Nix 0.8 and 0.9. Version 3 is Nix 0.10.
20 Version 4 is Nix 0.11. Version 5 is Nix 0.12-0.16. Version 6 is
21 Nix 1.0. Version 7 is Nix 1.3. */
22 const int nixSchemaVersion
= 7;
25 extern string drvsLogDir
;
33 unsigned long filesLinked
;
34 unsigned long long bytesFreed
;
35 unsigned long long blocksFreed
;
39 bytesFreed
= blocksFreed
= 0;
44 struct RunningSubstituter
48 AutoCloseFD to
, from
, error
;
51 RunningSubstituter() : disabled(false) { };
55 /* Wrapper object to close the SQLite database automatically. */
61 operator sqlite3
* () { return db
; }
65 /* Wrapper object to create and destroy SQLite prepared statements. */
71 SQLiteStmt() { stmt
= 0; }
72 void create(sqlite3
* db
, const string
& s
);
75 operator sqlite3_stmt
* () { return stmt
; }
76 void bind(const string
& value
);
78 void bind64(long long value
);
83 class LocalStore
: public StoreAPI
86 typedef std::map
<Path
, RunningSubstituter
> RunningSubstituters
;
87 RunningSubstituters runningSubstituters
;
93 /* Initialise the local store, upgrading the schema if
95 LocalStore(bool reserveSpace
= true);
99 /* Implementations of abstract store API methods. */
101 bool isValidPath(const Path
& path
);
103 PathSet
queryValidPaths(const PathSet
& paths
);
105 PathSet
queryAllValidPaths();
107 ValidPathInfo
queryPathInfo(const Path
& path
);
109 Hash
queryPathHash(const Path
& path
);
111 void queryReferences(const Path
& path
, PathSet
& references
);
113 void queryReferrers(const Path
& path
, PathSet
& referrers
);
115 Path
queryDeriver(const Path
& path
);
117 PathSet
queryValidDerivers(const Path
& path
);
119 PathSet
queryDerivationOutputs(const Path
& path
);
121 StringSet
queryDerivationOutputNames(const Path
& path
);
123 Path
queryPathFromHashPart(const string
& hashPart
);
125 PathSet
querySubstitutablePaths(const PathSet
& paths
);
127 void querySubstitutablePathInfos(const Path
& substituter
,
128 PathSet
& paths
, SubstitutablePathInfos
& infos
);
130 void querySubstitutablePathInfos(const PathSet
& paths
,
131 SubstitutablePathInfos
& infos
);
133 Path
addToStore(const string
& name
, const Path
& srcPath
,
134 bool recursive
= true, HashType hashAlgo
= htSHA256
,
135 PathFilter
& filter
= defaultPathFilter
, bool repair
= false);
137 /* Like addToStore(), but the contents of the path are contained
138 in `dump', which is either a NAR serialisation (if recursive ==
139 true) or simply the contents of a regular file (if recursive ==
141 Path
addToStoreFromDump(const string
& dump
, const string
& name
,
142 bool recursive
= true, HashType hashAlgo
= htSHA256
, bool repair
= false);
144 Path
addTextToStore(const string
& name
, const string
& s
,
145 const PathSet
& references
, bool repair
= false);
147 void exportPath(const Path
& path
, bool sign
,
150 Paths
importPaths(bool requireSignature
, Source
& source
);
152 void buildPaths(const PathSet
& paths
, BuildMode buildMode
);
154 void ensurePath(const Path
& path
);
156 void addTempRoot(const Path
& path
);
158 void addIndirectRoot(const Path
& path
);
164 void collectGarbage(const GCOptions
& options
, GCResults
& results
);
166 /* Optimise the disk space usage of the Nix store by hard-linking
167 files with the same contents. */
168 void optimiseStore(OptimiseStats
& stats
);
170 /* Generic variant of the above method. */
171 void optimiseStore();
173 /* Optimise a single store path. */
174 void optimisePath(const Path
& path
);
176 /* Check the integrity of the Nix store. Returns true if errors
178 bool verifyStore(bool checkContents
, bool repair
);
180 /* Register the validity of a path, i.e., that `path' exists, that
181 the paths referenced by it exists, and in the case of an output
182 path of a derivation, that it has been produced by a successful
183 execution of the derivation (or something equivalent). Also
184 register the hash of the file system contents of the path. The
185 hash must be a SHA-256 hash. */
186 void registerValidPath(const ValidPathInfo
& info
);
188 void registerValidPaths(const ValidPathInfos
& infos
);
190 /* Register that the build of a derivation with output `path' has
192 void registerFailedPath(const Path
& path
);
194 /* Query whether `path' previously failed to build. */
195 bool hasPathFailed(const Path
& path
);
197 PathSet
queryFailedPaths();
199 void clearFailedPaths(const PathSet
& paths
);
203 /* Repair the contents of the given path by redownloading it using
204 a substituter (if available). */
205 void repairPath(const Path
& path
);
207 /* Check whether the given valid path exists and has the right
209 bool pathContentsGood(const Path
& path
);
211 void markContentsGood(const Path
& path
);
213 void setSubstituterEnv();
219 /* Lock file used for upgrading. */
220 AutoCloseFD globalLock
;
222 /* The SQLite database object. */
225 /* Some precompiled SQLite statements. */
226 SQLiteStmt stmtRegisterValidPath
;
227 SQLiteStmt stmtUpdatePathInfo
;
228 SQLiteStmt stmtAddReference
;
229 SQLiteStmt stmtQueryPathInfo
;
230 SQLiteStmt stmtQueryReferences
;
231 SQLiteStmt stmtQueryReferrers
;
232 SQLiteStmt stmtInvalidatePath
;
233 SQLiteStmt stmtRegisterFailedPath
;
234 SQLiteStmt stmtHasPathFailed
;
235 SQLiteStmt stmtQueryFailedPaths
;
236 SQLiteStmt stmtClearFailedPath
;
237 SQLiteStmt stmtAddDerivationOutput
;
238 SQLiteStmt stmtQueryValidDerivers
;
239 SQLiteStmt stmtQueryDerivationOutputs
;
240 SQLiteStmt stmtQueryPathFromHashPart
;
242 /* Cache for pathContentsGood(). */
243 std::map
<Path
, bool> pathContentsGoodCache
;
245 bool didSetSubstituterEnv
;
247 /* The file to which we write our temporary roots. */
249 AutoCloseFD fdTempRoots
;
253 void openDB(bool create
);
255 void makeStoreWritable();
257 unsigned long long queryValidPathId(const Path
& path
);
259 unsigned long long addValidPath(const ValidPathInfo
& info
, bool checkOutputs
= true);
261 void addReference(unsigned long long referrer
, unsigned long long reference
);
263 void appendReferrer(const Path
& from
, const Path
& to
, bool lock
);
265 void rewriteReferrers(const Path
& path
, bool purge
, PathSet referrers
);
267 void invalidatePath(const Path
& path
);
269 /* Delete a path from the Nix store. */
270 void invalidatePathChecked(const Path
& path
);
272 void verifyPath(const Path
& path
, const PathSet
& store
,
273 PathSet
& done
, PathSet
& validPaths
, bool repair
, bool & errors
);
275 void updatePathInfo(const ValidPathInfo
& info
);
277 void upgradeStore6();
278 void upgradeStore7();
279 PathSet
queryValidPathsOld();
280 ValidPathInfo
queryPathInfoOld(const Path
& path
);
284 void deleteGarbage(GCState
& state
, const Path
& path
);
286 void tryToDelete(GCState
& state
, const Path
& path
);
288 bool canReachRoot(GCState
& state
, PathSet
& visited
, const Path
& path
);
290 void deletePathRecursive(GCState
& state
, const Path
& path
);
292 bool isActiveTempFile(const GCState
& state
,
293 const Path
& path
, const string
& suffix
);
295 int openGCLock(LockType lockType
);
297 void removeUnusedLinks(const GCState
& state
);
299 void startSubstituter(const Path
& substituter
,
300 RunningSubstituter
& runningSubstituter
);
302 string
getLineFromSubstituter(RunningSubstituter
& run
);
304 template<class T
> T
getIntLineFromSubstituter(RunningSubstituter
& run
);
306 Path
createTempDirInStore();
308 Path
importPath(bool requireSignature
, Source
& source
);
310 void checkDerivationOutputs(const Path
& drvPath
, const Derivation
& drv
);
312 typedef std::unordered_set
<ino_t
> InodeHash
;
314 InodeHash
loadInodeHash();
315 Strings
readDirectoryIgnoringInodes(const Path
& path
, const InodeHash
& inodeHash
);
316 void optimisePath_(OptimiseStats
& stats
, const Path
& path
, InodeHash
& inodeHash
);
318 // Internal versions that are not wrapped in retry_sqlite.
319 bool isValidPath_(const Path
& path
);
320 void queryReferrers_(const Path
& path
, PathSet
& referrers
);
324 typedef std::pair
<dev_t
, ino_t
> Inode
;
325 typedef set
<Inode
> InodesSeen
;
328 /* "Fix", or canonicalise, the meta-data of the files in a store path
329 after it has been built. In particular:
330 - the last modification date on each file is set to 1 (i.e.,
331 00:00:01 1/1/1970 UTC)
332 - the permissions are set of 444 or 555 (i.e., read-only with or
333 without execute permission; setuid bits etc. are cleared)
334 - the owner and group are set to the Nix user and group, if we're
336 void canonicalisePathMetaData(const Path
& path
, uid_t fromUid
, InodesSeen
& inodesSeen
);
337 void canonicalisePathMetaData(const Path
& path
, uid_t fromUid
);
339 void canonicaliseTimestampAndPermissions(const Path
& path
);
341 MakeError(PathInUse
, Error
);