misc.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "misc.hh"
  2. #include "store-api.hh"
  3. #include "local-store.hh"
  4. #include "globals.hh"
  5. namespace nix {
  6. Derivation derivationFromPath(StoreAPI & store, const Path & drvPath)
  7. {
  8. assertStorePath(drvPath);
  9. store.ensurePath(drvPath);
  10. return readDerivation(drvPath);
  11. }
  12. void computeFSClosure(StoreAPI & store, const Path & path,
  13. PathSet & paths, bool flipDirection, bool includeOutputs, bool includeDerivers)
  14. {
  15. if (paths.find(path) != paths.end()) return;
  16. paths.insert(path);
  17. PathSet edges;
  18. if (flipDirection) {
  19. store.queryReferrers(path, edges);
  20. if (includeOutputs) {
  21. PathSet derivers = store.queryValidDerivers(path);
  22. foreach (PathSet::iterator, i, derivers)
  23. edges.insert(*i);
  24. }
  25. if (includeDerivers && isDerivation(path)) {
  26. PathSet outputs = store.queryDerivationOutputs(path);
  27. foreach (PathSet::iterator, i, outputs)
  28. if (store.isValidPath(*i) && store.queryDeriver(*i) == path)
  29. edges.insert(*i);
  30. }
  31. } else {
  32. store.queryReferences(path, edges);
  33. if (includeOutputs && isDerivation(path)) {
  34. PathSet outputs = store.queryDerivationOutputs(path);
  35. foreach (PathSet::iterator, i, outputs)
  36. if (store.isValidPath(*i)) edges.insert(*i);
  37. }
  38. if (includeDerivers) {
  39. Path deriver = store.queryDeriver(path);
  40. if (store.isValidPath(deriver)) edges.insert(deriver);
  41. }
  42. }
  43. foreach (PathSet::iterator, i, edges)
  44. computeFSClosure(store, *i, paths, flipDirection, includeOutputs, includeDerivers);
  45. }
  46. Path findOutput(const Derivation & drv, string id)
  47. {
  48. foreach (DerivationOutputs::const_iterator, i, drv.outputs)
  49. if (i->first == id) return i->second.path;
  50. throw Error(format("derivation has no output `%1%'") % id);
  51. }
  52. void queryMissing(StoreAPI & store, const PathSet & targets,
  53. PathSet & willBuild, PathSet & willSubstitute, PathSet & unknown,
  54. unsigned long long & downloadSize, unsigned long long & narSize)
  55. {
  56. downloadSize = narSize = 0;
  57. PathSet todo(targets.begin(), targets.end()), done;
  58. /* Getting substitute info has high latency when using the binary
  59. cache substituter. Thus it's essential to do substitute
  60. queries in parallel as much as possible. To accomplish this
  61. we do the following:
  62. - For all paths still to be processed (‘todo’), we add all
  63. paths for which we need info to the set ‘query’. For an
  64. unbuilt derivation this is the output paths; otherwise, it's
  65. the path itself.
  66. - We get info about all paths in ‘query’ in parallel.
  67. - We process the results and add new items to ‘todo’ if
  68. necessary. E.g. if a path is substitutable, then we need to
  69. get info on its references.
  70. - Repeat until ‘todo’ is empty.
  71. */
  72. while (!todo.empty()) {
  73. PathSet query, todoDrv, todoNonDrv;
  74. foreach (PathSet::iterator, i, todo) {
  75. if (done.find(*i) != done.end()) continue;
  76. done.insert(*i);
  77. DrvPathWithOutputs i2 = parseDrvPathWithOutputs(*i);
  78. if (isDerivation(i2.first)) {
  79. if (!store.isValidPath(i2.first)) {
  80. // FIXME: we could try to substitute p.
  81. unknown.insert(*i);
  82. continue;
  83. }
  84. Derivation drv = derivationFromPath(store, i2.first);
  85. PathSet invalid;
  86. foreach (DerivationOutputs::iterator, j, drv.outputs)
  87. if (wantOutput(j->first, i2.second)
  88. && !store.isValidPath(j->second.path))
  89. invalid.insert(j->second.path);
  90. if (invalid.empty()) continue;
  91. todoDrv.insert(*i);
  92. if (settings.useSubstitutes && substitutesAllowed(drv))
  93. query.insert(invalid.begin(), invalid.end());
  94. }
  95. else {
  96. if (store.isValidPath(*i)) continue;
  97. query.insert(*i);
  98. todoNonDrv.insert(*i);
  99. }
  100. }
  101. todo.clear();
  102. SubstitutablePathInfos infos;
  103. store.querySubstitutablePathInfos(query, infos);
  104. foreach (PathSet::iterator, i, todoDrv) {
  105. DrvPathWithOutputs i2 = parseDrvPathWithOutputs(*i);
  106. // FIXME: cache this
  107. Derivation drv = derivationFromPath(store, i2.first);
  108. PathSet outputs;
  109. bool mustBuild = false;
  110. if (settings.useSubstitutes && substitutesAllowed(drv)) {
  111. foreach (DerivationOutputs::iterator, j, drv.outputs) {
  112. if (!wantOutput(j->first, i2.second)) continue;
  113. if (!store.isValidPath(j->second.path)) {
  114. if (infos.find(j->second.path) == infos.end())
  115. mustBuild = true;
  116. else
  117. outputs.insert(j->second.path);
  118. }
  119. }
  120. } else
  121. mustBuild = true;
  122. if (mustBuild) {
  123. willBuild.insert(i2.first);
  124. todo.insert(drv.inputSrcs.begin(), drv.inputSrcs.end());
  125. foreach (DerivationInputs::iterator, j, drv.inputDrvs)
  126. todo.insert(makeDrvPathWithOutputs(j->first, j->second));
  127. } else
  128. todoNonDrv.insert(outputs.begin(), outputs.end());
  129. }
  130. foreach (PathSet::iterator, i, todoNonDrv) {
  131. done.insert(*i);
  132. SubstitutablePathInfos::iterator info = infos.find(*i);
  133. if (info != infos.end()) {
  134. willSubstitute.insert(*i);
  135. downloadSize += info->second.downloadSize;
  136. narSize += info->second.narSize;
  137. todo.insert(info->second.references.begin(), info->second.references.end());
  138. } else
  139. unknown.insert(*i);
  140. }
  141. }
  142. }
  143. static void dfsVisit(StoreAPI & store, const PathSet & paths,
  144. const Path & path, PathSet & visited, Paths & sorted,
  145. PathSet & parents)
  146. {
  147. if (parents.find(path) != parents.end())
  148. throw BuildError(format("cycle detected in the references of `%1%'") % path);
  149. if (visited.find(path) != visited.end()) return;
  150. visited.insert(path);
  151. parents.insert(path);
  152. PathSet references;
  153. if (store.isValidPath(path))
  154. store.queryReferences(path, references);
  155. foreach (PathSet::iterator, i, references)
  156. /* Don't traverse into paths that don't exist. That can
  157. happen due to substitutes for non-existent paths. */
  158. if (*i != path && paths.find(*i) != paths.end())
  159. dfsVisit(store, paths, *i, visited, sorted, parents);
  160. sorted.push_front(path);
  161. parents.erase(path);
  162. }
  163. Paths topoSortPaths(StoreAPI & store, const PathSet & paths)
  164. {
  165. Paths sorted;
  166. PathSet visited, parents;
  167. foreach (PathSet::const_iterator, i, paths)
  168. dfsVisit(store, paths, *i, visited, sorted, parents);
  169. return sorted;
  170. }
  171. }