store-api.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "store-api.hh"
  2. #include "globals.hh"
  3. #include "util.hh"
  4. #include <climits>
  5. namespace nix {
  6. GCOptions::GCOptions()
  7. {
  8. action = gcDeleteDead;
  9. ignoreLiveness = false;
  10. maxFreed = ULLONG_MAX;
  11. }
  12. bool isInStore(const Path & path)
  13. {
  14. return isInDir(path, settings.nixStore);
  15. }
  16. bool isStorePath(const Path & path)
  17. {
  18. return isInStore(path)
  19. && path.find('/', settings.nixStore.size() + 1) == Path::npos;
  20. }
  21. void assertStorePath(const Path & path)
  22. {
  23. if (!isStorePath(path))
  24. throw Error(format("path `%1%' is not in the store") % path);
  25. }
  26. Path toStorePath(const Path & path)
  27. {
  28. if (!isInStore(path))
  29. throw Error(format("path `%1%' is not in the store") % path);
  30. Path::size_type slash = path.find('/', settings.nixStore.size() + 1);
  31. if (slash == Path::npos)
  32. return path;
  33. else
  34. return Path(path, 0, slash);
  35. }
  36. string storePathToName(const Path & path)
  37. {
  38. assertStorePath(path);
  39. return string(path, settings.nixStore.size() + 34);
  40. }
  41. void checkStoreName(const string & name)
  42. {
  43. string validChars = "+-._?=";
  44. /* Disallow names starting with a dot for possible security
  45. reasons (e.g., "." and ".."). */
  46. if (string(name, 0, 1) == ".")
  47. throw Error(format("invalid name: `%1%'") % name);
  48. foreach (string::const_iterator, i, name)
  49. if (!((*i >= 'A' && *i <= 'Z') ||
  50. (*i >= 'a' && *i <= 'z') ||
  51. (*i >= '0' && *i <= '9') ||
  52. validChars.find(*i) != string::npos))
  53. {
  54. throw Error(format("invalid character `%1%' in name `%2%'")
  55. % *i % name);
  56. }
  57. }
  58. /* Store paths have the following form:
  59. <store>/<h>-<name>
  60. where
  61. <store> = the location of the store, usually /gnu/store
  62. <name> = a human readable name for the path, typically obtained
  63. from the name attribute of the derivation, or the name of the
  64. source file from which the store path is created. For derivation
  65. outputs other than the default "out" output, the string "-<id>"
  66. is suffixed to <name>.
  67. <h> = base-32 representation of the first 160 bits of a SHA-256
  68. hash of <s>; the hash part of the store name
  69. <s> = the string "<type>:sha256:<h2>:<store>:<name>";
  70. note that it includes the location of the store as well as the
  71. name to make sure that changes to either of those are reflected
  72. in the hash (e.g. you won't get /nix/store/<h>-name1 and
  73. /nix/store/<h>-name2 with equal hash parts).
  74. <type> = one of:
  75. "text:<r1>:<r2>:...<rN>"
  76. for plain text files written to the store using
  77. addTextToStore(); <r1> ... <rN> are the references of the
  78. path.
  79. "source"
  80. for paths copied to the store using addToStore() when recursive
  81. = true and hashAlgo = "sha256"
  82. "output:<id>"
  83. for either the outputs created by derivations, OR paths copied
  84. to the store using addToStore() with recursive != true or
  85. hashAlgo != "sha256" (in that case "source" is used; it's
  86. silly, but it's done that way for compatibility). <id> is the
  87. name of the output (usually, "out").
  88. <h2> = base-16 representation of a SHA-256 hash of:
  89. if <type> = "text:...":
  90. the string written to the resulting store path
  91. if <type> = "source":
  92. the serialisation of the path from which this store path is
  93. copied, as returned by hashPath()
  94. if <type> = "output:out":
  95. for non-fixed derivation outputs:
  96. the derivation (see hashDerivationModulo() in
  97. primops.cc)
  98. for paths copied by addToStore() or produced by fixed-output
  99. derivations:
  100. the string "fixed:out:<rec><algo>:<hash>:", where
  101. <rec> = "r:" for recursive (path) hashes, or "" or flat
  102. (file) hashes
  103. <algo> = "md5", "sha1" or "sha256"
  104. <hash> = base-16 representation of the path or flat hash of
  105. the contents of the path (or expected contents of the
  106. path for fixed-output derivations)
  107. It would have been nicer to handle fixed-output derivations under
  108. "source", e.g. have something like "source:<rec><algo>", but we're
  109. stuck with this for now...
  110. The main reason for this way of computing names is to prevent name
  111. collisions (for security). For instance, it shouldn't be feasible
  112. to come up with a derivation whose output path collides with the
  113. path for a copied source. The former would have a <s> starting with
  114. "output:out:", while the latter would have a <2> starting with
  115. "source:".
  116. */
  117. Path makeStorePath(const string & type,
  118. const Hash & hash, const string & name)
  119. {
  120. /* e.g., "source:sha256:1abc...:/nix/store:foo.tar.gz" */
  121. string s = type + ":sha256:" + printHash(hash) + ":"
  122. + settings.nixStore + ":" + name;
  123. checkStoreName(name);
  124. return settings.nixStore + "/"
  125. + printHash32(compressHash(hashString(htSHA256, s), 20))
  126. + "-" + name;
  127. }
  128. Path makeOutputPath(const string & id,
  129. const Hash & hash, const string & name)
  130. {
  131. return makeStorePath("output:" + id, hash,
  132. name + (id == "out" ? "" : "-" + id));
  133. }
  134. Path makeFixedOutputPath(bool recursive,
  135. HashType hashAlgo, Hash hash, string name)
  136. {
  137. return hashAlgo == htSHA256 && recursive
  138. ? makeStorePath("source", hash, name)
  139. : makeStorePath("output:out", hashString(htSHA256,
  140. "fixed:out:" + (recursive ? (string) "r:" : "") +
  141. printHashType(hashAlgo) + ":" + printHash(hash) + ":"),
  142. name);
  143. }
  144. Path computeStorePathForText(const string & name, const string & s,
  145. const PathSet & references)
  146. {
  147. Hash hash = hashString(htSHA256, s);
  148. /* Stuff the references (if any) into the type. This is a bit
  149. hacky, but we can't put them in `s' since that would be
  150. ambiguous. */
  151. string type = "text";
  152. foreach (PathSet::const_iterator, i, references) {
  153. type += ":";
  154. type += *i;
  155. }
  156. return makeStorePath(type, hash, name);
  157. }
  158. /* Return a string accepted by decodeValidPathInfo() that
  159. registers the specified paths as valid. Note: it's the
  160. responsibility of the caller to provide a closure. */
  161. string StoreAPI::makeValidityRegistration(const PathSet & paths,
  162. bool showDerivers, bool showHash)
  163. {
  164. string s = "";
  165. foreach (PathSet::iterator, i, paths) {
  166. s += *i + "\n";
  167. ValidPathInfo info = queryPathInfo(*i);
  168. if (showHash) {
  169. s += printHash(info.hash) + "\n";
  170. s += (format("%1%\n") % info.narSize).str();
  171. }
  172. Path deriver = showDerivers ? info.deriver : "";
  173. s += deriver + "\n";
  174. s += (format("%1%\n") % info.references.size()).str();
  175. foreach (PathSet::iterator, j, info.references)
  176. s += *j + "\n";
  177. }
  178. return s;
  179. }
  180. string showPaths(const PathSet & paths)
  181. {
  182. string s;
  183. foreach (PathSet::const_iterator, i, paths) {
  184. if (s.size() != 0) s += ", ";
  185. s += "`" + *i + "'";
  186. }
  187. return s;
  188. }
  189. Path readStorePath(Source & from)
  190. {
  191. Path path = readString(from);
  192. assertStorePath(path);
  193. return path;
  194. }
  195. template<class T> T readStorePaths(Source & from)
  196. {
  197. T paths = readStrings<T>(from);
  198. foreach (typename T::iterator, i, paths) assertStorePath(*i);
  199. return paths;
  200. }
  201. template PathSet readStorePaths(Source & from);
  202. }
  203. #include "local-store.hh"
  204. #include "serialise.hh"
  205. namespace nix {
  206. std::shared_ptr<StoreAPI> store;
  207. }