4 #include "serialise.hh"
14 typedef std::map
<Path
, Path
> Roots
;
19 /* Garbage collector operation:
21 - `gcReturnLive': return the set of paths reachable from
22 (i.e. in the closure of) the roots.
24 - `gcReturnDead': return the set of paths not reachable from
27 - `gcDeleteDead': actually delete the latter set.
29 - `gcDeleteSpecific': delete the paths listed in
30 `pathsToDelete', insofar as they are not reachable.
41 /* If `ignoreLiveness' is set, then reachability from the roots is
42 ignored (dangerous!). However, the paths must still be
43 unreferenced *within* the store (i.e., there can be no other
44 store paths that depend on them). */
47 /* For `gcDeleteSpecific', the paths to delete. */
48 PathSet pathsToDelete
;
50 /* Stop after at least `maxFreed' bytes have been freed. */
51 unsigned long long maxFreed
;
59 /* Depending on the action, the GC roots, or the paths that would
60 be or have been deleted. */
63 /* For `gcReturnDead', `gcDeleteDead' and `gcDeleteSpecific', the
64 number of bytes that would be or was freed. */
65 unsigned long long bytesFreed
;
74 struct SubstitutablePathInfo
78 unsigned long long downloadSize
; /* 0 = unknown or inapplicable */
79 unsigned long long narSize
; /* 0 = unknown */
82 typedef std::map
<Path
, SubstitutablePathInfo
> SubstitutablePathInfos
;
91 time_t registrationTime
;
92 unsigned long long narSize
; // 0 = unknown
93 unsigned long long id
; // internal use only
94 ValidPathInfo() : registrationTime(0), narSize(0) { }
97 typedef list
<ValidPathInfo
> ValidPathInfos
;
100 enum BuildMode
{ bmNormal
, bmRepair
, bmCheck
};
107 virtual ~StoreAPI() { }
109 /* Check whether a path is valid. */
110 virtual bool isValidPath(const Path
& path
) = 0;
112 /* Query which of the given paths is valid. */
113 virtual PathSet
queryValidPaths(const PathSet
& paths
) = 0;
115 /* Query the set of all valid paths. */
116 virtual PathSet
queryAllValidPaths() = 0;
118 /* Query information about a valid path. */
119 virtual ValidPathInfo
queryPathInfo(const Path
& path
) = 0;
121 /* Query the hash of a valid path. */
122 virtual Hash
queryPathHash(const Path
& path
) = 0;
124 /* Query the set of outgoing FS references for a store path. The
125 result is not cleared. */
126 virtual void queryReferences(const Path
& path
,
127 PathSet
& references
) = 0;
129 /* Queries the set of incoming FS references for a store path.
130 The result is not cleared. */
131 virtual void queryReferrers(const Path
& path
,
132 PathSet
& referrers
) = 0;
134 /* Query the deriver of a store path. Return the empty string if
135 no deriver has been set. */
136 virtual Path
queryDeriver(const Path
& path
) = 0;
138 /* Return all currently valid derivations that have `path' as an
139 output. (Note that the result of `queryDeriver()' is the
140 derivation that was actually used to produce `path', which may
141 not exist anymore.) */
142 virtual PathSet
queryValidDerivers(const Path
& path
) = 0;
144 /* Query the outputs of the derivation denoted by `path'. */
145 virtual PathSet
queryDerivationOutputs(const Path
& path
) = 0;
147 /* Query the output names of the derivation denoted by `path'. */
148 virtual StringSet
queryDerivationOutputNames(const Path
& path
) = 0;
150 /* Query the full store path given the hash part of a valid store
151 path, or "" if the path doesn't exist. */
152 virtual Path
queryPathFromHashPart(const string
& hashPart
) = 0;
154 /* Query which of the given paths have substitutes. */
155 virtual PathSet
querySubstitutablePaths(const PathSet
& paths
) = 0;
157 /* Query substitute info (i.e. references, derivers and download
158 sizes) of a set of paths. If a path does not have substitute
159 info, it's omitted from the resulting ‘infos’ map. */
160 virtual void querySubstitutablePathInfos(const PathSet
& paths
,
161 SubstitutablePathInfos
& infos
) = 0;
163 /* Copy the contents of a path to the store and register the
164 validity the resulting path. The resulting path is returned.
165 The function object `filter' can be used to exclude files (see
166 libutil/archive.hh). */
167 virtual Path
addToStore(const string
& name
, const Path
& srcPath
,
168 bool recursive
= true, HashType hashAlgo
= htSHA256
,
169 PathFilter
& filter
= defaultPathFilter
, bool repair
= false) = 0;
171 /* Like addToStore, but the contents written to the output path is
172 a regular file containing the given string. */
173 virtual Path
addTextToStore(const string
& name
, const string
& s
,
174 const PathSet
& references
, bool repair
= false) = 0;
176 /* Export a store path, that is, create a NAR dump of the store
177 path and append its references and its deriver. Optionally, a
178 cryptographic signature (created by OpenSSL) of the preceding
180 virtual void exportPath(const Path
& path
, bool sign
,
183 /* Import a sequence of NAR dumps created by exportPaths() into
185 virtual Paths
importPaths(bool requireSignature
, Source
& source
) = 0;
187 /* For each path, if it's a derivation, build it. Building a
188 derivation means ensuring that the output paths are valid. If
189 they are already valid, this is a no-op. Otherwise, validity
190 can be reached in two ways. First, if the output paths is
191 substitutable, then build the path that way. Second, the
192 output paths can be created by running the builder, after
193 recursively building any sub-derivations. For inputs that are
194 not derivations, substitute them. */
195 virtual void buildPaths(const PathSet
& paths
, BuildMode buildMode
= bmNormal
) = 0;
197 /* Ensure that a path is valid. If it is not currently valid, it
198 may be made valid by running a substitute (if defined for the
200 virtual void ensurePath(const Path
& path
) = 0;
202 /* Add a store path as a temporary root of the garbage collector.
203 The root disappears as soon as we exit. */
204 virtual void addTempRoot(const Path
& path
) = 0;
206 /* Add an indirect root, which is merely a symlink to `path' from
207 /nix/var/nix/gcroots/auto/<hash of `path'>. `path' is supposed
208 to be a symlink to a store path. The garbage collector will
209 automatically remove the indirect root when it finds that
210 `path' has disappeared. */
211 virtual void addIndirectRoot(const Path
& path
) = 0;
213 /* Acquire the global GC lock, then immediately release it. This
214 function must be called after registering a new permanent root,
215 but before exiting. Otherwise, it is possible that a running
216 garbage collector doesn't see the new root and deletes the
217 stuff we've just built. By acquiring the lock briefly, we
220 - The collector is already running, and so we block until the
221 collector is finished. The collector will know about our
222 *temporary* locks, which should include whatever it is we
223 want to register as a permanent lock.
225 - The collector isn't running, or it's just started but hasn't
226 acquired the GC lock yet. In that case we get and release
227 the lock right away, then exit. The collector scans the
228 permanent root and sees our's.
230 In either case the permanent root is seen by the collector. */
231 virtual void syncWithGC() = 0;
233 /* Find the roots of the garbage collector. Each root is a pair
234 (link, storepath) where `link' is the path of the symlink
235 outside of the Nix store that point to `storePath'. */
236 virtual Roots
findRoots() = 0;
238 /* Perform a garbage collection. */
239 virtual void collectGarbage(const GCOptions
& options
, GCResults
& results
) = 0;
241 /* Return the set of paths that have failed to build.*/
242 virtual PathSet
queryFailedPaths() = 0;
244 /* Clear the "failed" status of the given paths. The special
245 value `*' causes all failed paths to be cleared. */
246 virtual void clearFailedPaths(const PathSet
& paths
) = 0;
248 /* Return a string representing information about the path that
249 can be loaded into the database using `nix-store --load-db' or
250 `nix-store --register-validity'. */
251 string
makeValidityRegistration(const PathSet
& paths
,
252 bool showDerivers
, bool showHash
);
254 /* Optimise the disk space usage of the Nix store by hard-linking files
255 with the same contents. */
256 virtual void optimiseStore() = 0;
258 /* Check the integrity of the Nix store. Returns true if errors
260 virtual bool verifyStore(bool checkContents
, bool repair
) = 0;
264 /* !!! These should be part of the store API, I guess. */
266 /* Throw an exception if `path' is not directly in the Nix store. */
267 void assertStorePath(const Path
& path
);
269 bool isInStore(const Path
& path
);
270 bool isStorePath(const Path
& path
);
272 /* Extract the name part of the given store path. */
273 string
storePathToName(const Path
& path
);
275 void checkStoreName(const string
& name
);
278 /* Chop off the parts after the top-level store name, e.g.,
279 /nix/store/abcd-foo/bar => /nix/store/abcd-foo. */
280 Path
toStorePath(const Path
& path
);
283 /* Follow symlinks until we end up with a path in the Nix store. */
284 Path
followLinksToStore(const Path
& path
);
287 /* Same as followLinksToStore(), but apply toStorePath() to the
289 Path
followLinksToStorePath(const Path
& path
);
292 /* Constructs a unique store path name. */
293 Path
makeStorePath(const string
& type
,
294 const Hash
& hash
, const string
& name
);
296 Path
makeOutputPath(const string
& id
,
297 const Hash
& hash
, const string
& name
);
299 Path
makeFixedOutputPath(bool recursive
,
300 HashType hashAlgo
, Hash hash
, string name
);
303 /* This is the preparatory part of addToStore() and addToStoreFixed();
304 it computes the store path to which srcPath is to be copied.
305 Returns the store path and the cryptographic hash of the
306 contents of srcPath. */
307 std::pair
<Path
, Hash
> computeStorePathForPath(const Path
& srcPath
,
308 bool recursive
= true, HashType hashAlgo
= htSHA256
,
309 PathFilter
& filter
= defaultPathFilter
);
311 /* Preparatory part of addTextToStore().
313 !!! Computation of the path should take the references given to
314 addTextToStore() into account, otherwise we have a (relatively
315 minor) security hole: a caller can register a source file with
316 bogus references. If there are too many references, the path may
317 not be garbage collected when it has to be (not really a problem,
318 the caller could create a root anyway), or it may be garbage
319 collected when it shouldn't be (more serious).
321 Hashing the references would solve this (bogus references would
322 simply yield a different store path, so other users wouldn't be
323 affected), but it has some backwards compatibility issues (the
324 hashing scheme changes), so I'm not doing that for now. */
325 Path
computeStorePathForText(const string
& name
, const string
& s
,
326 const PathSet
& references
);
329 /* Remove the temporary roots file for this process. Any temporary
330 root becomes garbage after this point unless it has been registered
331 as a (permanent) root. */
332 void removeTempRoots();
335 /* Register a permanent GC root. */
336 Path
addPermRoot(StoreAPI
& store
, const Path
& storePath
,
337 const Path
& gcRoot
, bool indirect
, bool allowOutsideRootsDir
= false);
340 /* Sort a set of paths topologically under the references relation.
341 If p refers to q, then p preceeds q in this list. */
342 Paths
topoSortPaths(StoreAPI
& store
, const PathSet
& paths
);
345 /* For now, there is a single global store API object, but we'll
346 purify that in the future. */
347 extern std::shared_ptr
<StoreAPI
> store
;
350 /* Factory method: open the Nix database, either through the local or
351 remote implementation. */
352 std::shared_ptr
<StoreAPI
> openStore(bool reserveSpace
= true);
355 /* Display a set of paths in human-readable form (i.e., between quotes
356 and separated by commas). */
357 string
showPaths(const PathSet
& paths
);
360 ValidPathInfo
decodeValidPathInfo(std::istream
& str
,
361 bool hashGiven
= false);
364 /* Export multiple paths in the format expected by ‘nix-store
366 void exportPaths(StoreAPI
& store
, const Paths
& paths
,
367 bool sign
, Sink
& sink
);
370 MakeError(SubstError
, Error
)
371 MakeError(BuildError
, Error
) /* denotes a permanent build failure */