You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

385 lines
13 KiB

  1. #pragma once
  2. #include "hash.hh"
  3. #include "serialise.hh"
  4. #include <string>
  5. #include <map>
  6. #include <memory>
  7. namespace nix {
  8. typedef std::map<Path, Path> Roots;
  9. struct GCOptions
  10. {
  11. /* Garbage collector operation:
  12. - `gcReturnLive': return the set of paths reachable from
  13. (i.e. in the closure of) the roots.
  14. - `gcReturnDead': return the set of paths not reachable from
  15. the roots.
  16. - `gcDeleteDead': actually delete the latter set.
  17. - `gcDeleteSpecific': delete the paths listed in
  18. `pathsToDelete', insofar as they are not reachable.
  19. */
  20. typedef enum {
  21. gcReturnLive,
  22. gcReturnDead,
  23. gcDeleteDead,
  24. gcDeleteSpecific,
  25. } GCAction;
  26. GCAction action;
  27. /* If `ignoreLiveness' is set, then reachability from the roots is
  28. ignored (dangerous!). However, the paths must still be
  29. unreferenced *within* the store (i.e., there can be no other
  30. store paths that depend on them). */
  31. bool ignoreLiveness;
  32. /* For `gcDeleteSpecific', the paths to delete. */
  33. PathSet pathsToDelete;
  34. /* Stop after at least `maxFreed' bytes have been freed. */
  35. unsigned long long maxFreed;
  36. GCOptions();
  37. };
  38. struct GCResults
  39. {
  40. /* Depending on the action, the GC roots, or the paths that would
  41. be or have been deleted. */
  42. PathSet paths;
  43. /* For `gcReturnDead', `gcDeleteDead' and `gcDeleteSpecific', the
  44. number of bytes that would be or was freed. */
  45. unsigned long long bytesFreed;
  46. GCResults()
  47. {
  48. bytesFreed = 0;
  49. }
  50. };
  51. struct SubstitutablePathInfo
  52. {
  53. Path deriver;
  54. PathSet references;
  55. unsigned long long downloadSize; /* 0 = unknown or inapplicable */
  56. unsigned long long narSize; /* 0 = unknown */
  57. };
  58. typedef std::map<Path, SubstitutablePathInfo> SubstitutablePathInfos;
  59. struct ValidPathInfo
  60. {
  61. Path path;
  62. Path deriver;
  63. Hash hash;
  64. PathSet references;
  65. time_t registrationTime = 0;
  66. uint64_t narSize = 0; // 0 = unknown
  67. uint64_t id; // internal use only
  68. bool operator == (const ValidPathInfo & i) const
  69. {
  70. return
  71. path == i.path
  72. && hash == i.hash
  73. && references == i.references;
  74. }
  75. };
  76. typedef list<ValidPathInfo> ValidPathInfos;
  77. enum BuildMode { bmNormal, bmRepair, bmCheck };
  78. struct BuildResult
  79. {
  80. enum Status {
  81. Built = 0,
  82. Substituted,
  83. AlreadyValid,
  84. PermanentFailure,
  85. InputRejected,
  86. OutputRejected,
  87. TransientFailure, // possibly transient
  88. CachedFailure,
  89. TimedOut,
  90. MiscFailure,
  91. DependencyFailed,
  92. LogLimitExceeded,
  93. NotDeterministic,
  94. } status = MiscFailure;
  95. std::string errorMsg;
  96. //time_t startTime = 0, stopTime = 0;
  97. bool success() {
  98. return status == Built || status == Substituted || status == AlreadyValid;
  99. }
  100. };
  101. class StoreAPI
  102. {
  103. public:
  104. virtual ~StoreAPI() { }
  105. /* Check whether a path is valid. */
  106. virtual bool isValidPath(const Path & path) = 0;
  107. /* Query which of the given paths is valid. */
  108. virtual PathSet queryValidPaths(const PathSet & paths) = 0;
  109. /* Query the set of all valid paths. */
  110. virtual PathSet queryAllValidPaths() = 0;
  111. /* Query information about a valid path. */
  112. virtual ValidPathInfo queryPathInfo(const Path & path) = 0;
  113. /* Query the hash of a valid path. */
  114. virtual Hash queryPathHash(const Path & path) = 0;
  115. /* Query the set of outgoing FS references for a store path. The
  116. result is not cleared. */
  117. virtual void queryReferences(const Path & path,
  118. PathSet & references) = 0;
  119. /* Queries the set of incoming FS references for a store path.
  120. The result is not cleared. */
  121. virtual void queryReferrers(const Path & path,
  122. PathSet & referrers) = 0;
  123. /* Query the deriver of a store path. Return the empty string if
  124. no deriver has been set. */
  125. virtual Path queryDeriver(const Path & path) = 0;
  126. /* Return all currently valid derivations that have `path' as an
  127. output. (Note that the result of `queryDeriver()' is the
  128. derivation that was actually used to produce `path', which may
  129. not exist anymore.) */
  130. virtual PathSet queryValidDerivers(const Path & path) = 0;
  131. /* Query the outputs of the derivation denoted by `path'. */
  132. virtual PathSet queryDerivationOutputs(const Path & path) = 0;
  133. /* Query the output names of the derivation denoted by `path'. */
  134. virtual StringSet queryDerivationOutputNames(const Path & path) = 0;
  135. /* Query the full store path given the hash part of a valid store
  136. path, or "" if the path doesn't exist. */
  137. virtual Path queryPathFromHashPart(const string & hashPart) = 0;
  138. /* Query which of the given paths have substitutes. */
  139. virtual PathSet querySubstitutablePaths(const PathSet & paths) = 0;
  140. /* Query substitute info (i.e. references, derivers and download
  141. sizes) of a set of paths. If a path does not have substitute
  142. info, it's omitted from the resulting ‘infos’ map. */
  143. virtual void querySubstitutablePathInfos(const PathSet & paths,
  144. SubstitutablePathInfos & infos) = 0;
  145. /* Copy the contents of a path to the store and register the
  146. validity the resulting path. The resulting path is returned.
  147. The function object `filter' can be used to exclude files (see
  148. libutil/archive.hh). */
  149. virtual Path addToStore(const string & name, const Path & srcPath,
  150. bool recursive = true, HashType hashAlgo = htSHA256,
  151. PathFilter & filter = defaultPathFilter, bool repair = false) = 0;
  152. /* Like addToStore, but the contents written to the output path is
  153. a regular file containing the given string. */
  154. virtual Path addTextToStore(const string & name, const string & s,
  155. const PathSet & references, bool repair = false) = 0;
  156. /* Export a store path, that is, create a NAR dump of the store
  157. path and append its references and its deriver. Optionally, a
  158. cryptographic signature (created by OpenSSL) of the preceding
  159. data is attached. */
  160. virtual void exportPath(const Path & path, bool sign,
  161. Sink & sink) = 0;
  162. /* Import a sequence of NAR dumps created by exportPaths() into
  163. the Nix store. */
  164. virtual Paths importPaths(bool requireSignature, Source & source) = 0;
  165. /* For each path, if it's a derivation, build it. Building a
  166. derivation means ensuring that the output paths are valid. If
  167. they are already valid, this is a no-op. Otherwise, validity
  168. can be reached in two ways. First, if the output paths is
  169. substitutable, then build the path that way. Second, the
  170. output paths can be created by running the builder, after
  171. recursively building any sub-derivations. For inputs that are
  172. not derivations, substitute them. */
  173. virtual void buildPaths(const PathSet & paths, BuildMode buildMode = bmNormal) = 0;
  174. /* Ensure that a path is valid. If it is not currently valid, it
  175. may be made valid by running a substitute (if defined for the
  176. path). */
  177. virtual void ensurePath(const Path & path) = 0;
  178. /* Add a store path as a temporary root of the garbage collector.
  179. The root disappears as soon as we exit. */
  180. virtual void addTempRoot(const Path & path) = 0;
  181. /* Add an indirect root, which is merely a symlink to `path' from
  182. /nix/var/nix/gcroots/auto/<hash of `path'>. `path' is supposed
  183. to be a symlink to a store path. The garbage collector will
  184. automatically remove the indirect root when it finds that
  185. `path' has disappeared. */
  186. virtual void addIndirectRoot(const Path & path) = 0;
  187. /* Acquire the global GC lock, then immediately release it. This
  188. function must be called after registering a new permanent root,
  189. but before exiting. Otherwise, it is possible that a running
  190. garbage collector doesn't see the new root and deletes the
  191. stuff we've just built. By acquiring the lock briefly, we
  192. ensure that either:
  193. - The collector is already running, and so we block until the
  194. collector is finished. The collector will know about our
  195. *temporary* locks, which should include whatever it is we
  196. want to register as a permanent lock.
  197. - The collector isn't running, or it's just started but hasn't
  198. acquired the GC lock yet. In that case we get and release
  199. the lock right away, then exit. The collector scans the
  200. permanent root and sees our's.
  201. In either case the permanent root is seen by the collector. */
  202. virtual void syncWithGC() = 0;
  203. /* Find the roots of the garbage collector. Each root is a pair
  204. (link, storepath) where `link' is the path of the symlink
  205. outside of the Nix store that point to `storePath'. */
  206. virtual Roots findRoots() = 0;
  207. /* Perform a garbage collection. */
  208. virtual void collectGarbage(const GCOptions & options, GCResults & results) = 0;
  209. /* Return the set of paths that have failed to build.*/
  210. virtual PathSet queryFailedPaths() = 0;
  211. /* Clear the "failed" status of the given paths. The special
  212. value `*' causes all failed paths to be cleared. */
  213. virtual void clearFailedPaths(const PathSet & paths) = 0;
  214. /* Return a string representing information about the path that
  215. can be loaded into the database using `nix-store --load-db' or
  216. `nix-store --register-validity'. */
  217. string makeValidityRegistration(const PathSet & paths,
  218. bool showDerivers, bool showHash);
  219. /* Optimise the disk space usage of the Nix store by hard-linking files
  220. with the same contents. */
  221. virtual void optimiseStore() = 0;
  222. /* Check the integrity of the Nix store. Returns true if errors
  223. remain. */
  224. virtual bool verifyStore(bool checkContents, bool repair) = 0;
  225. };
  226. /* !!! These should be part of the store API, I guess. */
  227. /* Throw an exception if `path' is not directly in the Nix store. */
  228. void assertStorePath(const Path & path);
  229. bool isInStore(const Path & path);
  230. bool isStorePath(const Path & path);
  231. /* Extract the name part of the given store path. */
  232. string storePathToName(const Path & path);
  233. void checkStoreName(const string & name);
  234. /* Chop off the parts after the top-level store name, e.g.,
  235. /nix/store/abcd-foo/bar => /nix/store/abcd-foo. */
  236. Path toStorePath(const Path & path);
  237. /* Constructs a unique store path name. */
  238. Path makeStorePath(const string & type,
  239. const Hash & hash, const string & name);
  240. Path makeOutputPath(const string & id,
  241. const Hash & hash, const string & name);
  242. Path makeFixedOutputPath(bool recursive,
  243. HashType hashAlgo, Hash hash, string name);
  244. /* Preparatory part of addTextToStore().
  245. !!! Computation of the path should take the references given to
  246. addTextToStore() into account, otherwise we have a (relatively
  247. minor) security hole: a caller can register a source file with
  248. bogus references. If there are too many references, the path may
  249. not be garbage collected when it has to be (not really a problem,
  250. the caller could create a root anyway), or it may be garbage
  251. collected when it shouldn't be (more serious).
  252. Hashing the references would solve this (bogus references would
  253. simply yield a different store path, so other users wouldn't be
  254. affected), but it has some backwards compatibility issues (the
  255. hashing scheme changes), so I'm not doing that for now. */
  256. Path computeStorePathForText(const string & name, const string & s,
  257. const PathSet & references);
  258. /* Remove the temporary roots file for this process. Any temporary
  259. root becomes garbage after this point unless it has been registered
  260. as a (permanent) root. */
  261. void removeTempRoots();
  262. /* Register a permanent GC root. */
  263. Path addPermRoot(StoreAPI & store, const Path & storePath,
  264. const Path & gcRoot, bool indirect, bool allowOutsideRootsDir = false);
  265. /* Sort a set of paths topologically under the references relation.
  266. If p refers to q, then p preceeds q in this list. */
  267. Paths topoSortPaths(StoreAPI & store, const PathSet & paths);
  268. /* For now, there is a single global store API object, but we'll
  269. purify that in the future. */
  270. extern std::shared_ptr<StoreAPI> store;
  271. /* Factory method: open the Nix database, either through the local or
  272. remote implementation. */
  273. std::shared_ptr<StoreAPI> openStore(bool reserveSpace = true);
  274. /* Display a set of paths in human-readable form (i.e., between quotes
  275. and separated by commas). */
  276. string showPaths(const PathSet & paths);
  277. /* Export multiple paths in the format expected by ‘nix-store
  278. --import’. */
  279. void exportPaths(StoreAPI & store, const Paths & paths,
  280. bool sign, Sink & sink);
  281. MakeError(SubstError, Error)
  282. MakeError(BuildError, Error) /* denotes a permanent build failure */
  283. }