archive.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #define _XOPEN_SOURCE 600
  2. #include "config.h"
  3. #include <cerrno>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <map>
  7. #include <strings.h> // for strcasecmp
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <unistd.h>
  11. #include <dirent.h>
  12. #include <fcntl.h>
  13. #include "archive.hh"
  14. #include "util.hh"
  15. namespace nix {
  16. bool useCaseHack =
  17. #if __APPLE__
  18. true;
  19. #else
  20. false;
  21. #endif
  22. static string archiveVersion1 = "nix-archive-1";
  23. static string caseHackSuffix = "~nix~case~hack~";
  24. PathFilter defaultPathFilter;
  25. static void dumpContents(const Path & path, size_t size,
  26. Sink & sink)
  27. {
  28. writeString("contents", sink);
  29. writeLongLong(size, sink);
  30. AutoCloseFD fd = open(path.c_str(), O_RDONLY);
  31. if (fd == -1) throw SysError(format("opening file `%1%'") % path);
  32. unsigned char buf[65536];
  33. size_t left = size;
  34. while (left > 0) {
  35. size_t n = left > sizeof(buf) ? sizeof(buf) : left;
  36. readFull(fd, buf, n);
  37. left -= n;
  38. sink(buf, n);
  39. }
  40. writePadding(size, sink);
  41. }
  42. static void dump(const Path & path, Sink & sink, PathFilter & filter)
  43. {
  44. struct stat st;
  45. if (lstat(path.c_str(), &st))
  46. throw SysError(format("getting attributes of path `%1%'") % path);
  47. writeString("(", sink);
  48. if (S_ISREG(st.st_mode)) {
  49. writeString("type", sink);
  50. writeString("regular", sink);
  51. if (st.st_mode & S_IXUSR) {
  52. writeString("executable", sink);
  53. writeString("", sink);
  54. }
  55. dumpContents(path, (size_t) st.st_size, sink);
  56. }
  57. else if (S_ISDIR(st.st_mode)) {
  58. writeString("type", sink);
  59. writeString("directory", sink);
  60. /* If we're on a case-insensitive system like Mac OS X, undo
  61. the case hack applied by restorePath(). */
  62. std::map<string, string> unhacked;
  63. for (auto & i : readDirectory(path))
  64. if (useCaseHack) {
  65. string name(i.name);
  66. size_t pos = i.name.find(caseHackSuffix);
  67. if (pos != string::npos) {
  68. printMsg(lvlDebug, format("removing case hack suffix from `%1%'") % (path + "/" + i.name));
  69. name.erase(pos);
  70. }
  71. if (unhacked.find(name) != unhacked.end())
  72. throw Error(format("file name collision in between `%1%' and `%2%'")
  73. % (path + "/" + unhacked[name]) % (path + "/" + i.name));
  74. unhacked[name] = i.name;
  75. } else
  76. unhacked[i.name] = i.name;
  77. for (auto & i : unhacked)
  78. if (filter(path + "/" + i.first)) {
  79. writeString("entry", sink);
  80. writeString("(", sink);
  81. writeString("name", sink);
  82. writeString(i.first, sink);
  83. writeString("node", sink);
  84. dump(path + "/" + i.second, sink, filter);
  85. writeString(")", sink);
  86. }
  87. }
  88. else if (S_ISLNK(st.st_mode)) {
  89. writeString("type", sink);
  90. writeString("symlink", sink);
  91. writeString("target", sink);
  92. writeString(readLink(path), sink);
  93. }
  94. else throw Error(format("file `%1%' has an unsupported type") % path);
  95. writeString(")", sink);
  96. }
  97. void dumpPath(const Path & path, Sink & sink, PathFilter & filter)
  98. {
  99. writeString(archiveVersion1, sink);
  100. dump(path, sink, filter);
  101. }
  102. static SerialisationError badArchive(string s)
  103. {
  104. return SerialisationError("bad archive: " + s);
  105. }
  106. #if 0
  107. static void skipGeneric(Source & source)
  108. {
  109. if (readString(source) == "(") {
  110. while (readString(source) != ")")
  111. skipGeneric(source);
  112. }
  113. }
  114. #endif
  115. static void parseContents(ParseSink & sink, Source & source, const Path & path)
  116. {
  117. unsigned long long size = readLongLong(source);
  118. sink.preallocateContents(size);
  119. unsigned long long left = size;
  120. unsigned char buf[65536];
  121. while (left) {
  122. checkInterrupt();
  123. unsigned int n = sizeof(buf);
  124. if ((unsigned long long) n > left) n = left;
  125. source(buf, n);
  126. sink.receiveContents(buf, n);
  127. left -= n;
  128. }
  129. readPadding(size, source);
  130. }
  131. struct CaseInsensitiveCompare
  132. {
  133. bool operator() (const string & a, const string & b) const
  134. {
  135. return strcasecmp(a.c_str(), b.c_str()) < 0;
  136. }
  137. };
  138. static void parse(ParseSink & sink, Source & source, const Path & path)
  139. {
  140. string s;
  141. s = readString(source);
  142. if (s != "(") throw badArchive("expected open tag");
  143. enum { tpUnknown, tpRegular, tpDirectory, tpSymlink } type = tpUnknown;
  144. std::map<Path, int, CaseInsensitiveCompare> names;
  145. while (1) {
  146. checkInterrupt();
  147. s = readString(source);
  148. if (s == ")") {
  149. break;
  150. }
  151. else if (s == "type") {
  152. if (type != tpUnknown)
  153. throw badArchive("multiple type fields");
  154. string t = readString(source);
  155. if (t == "regular") {
  156. type = tpRegular;
  157. sink.createRegularFile(path);
  158. }
  159. else if (t == "directory") {
  160. sink.createDirectory(path);
  161. type = tpDirectory;
  162. }
  163. else if (t == "symlink") {
  164. type = tpSymlink;
  165. }
  166. else throw badArchive("unknown file type " + t);
  167. }
  168. else if (s == "contents" && type == tpRegular) {
  169. parseContents(sink, source, path);
  170. }
  171. else if (s == "executable" && type == tpRegular) {
  172. readString(source);
  173. sink.isExecutable();
  174. }
  175. else if (s == "entry" && type == tpDirectory) {
  176. string name, prevName;
  177. s = readString(source);
  178. if (s != "(") throw badArchive("expected open tag");
  179. while (1) {
  180. checkInterrupt();
  181. s = readString(source);
  182. if (s == ")") {
  183. break;
  184. } else if (s == "name") {
  185. name = readString(source);
  186. if (name.empty() || name == "." || name == ".." || name.find('/') != string::npos || name.find((char) 0) != string::npos)
  187. throw Error(format("NAR contains invalid file name `%1%'") % name);
  188. if (name <= prevName)
  189. throw Error("NAR directory is not sorted");
  190. prevName = name;
  191. if (useCaseHack) {
  192. auto i = names.find(name);
  193. if (i != names.end()) {
  194. printMsg(lvlDebug, format("case collision between `%1%' and `%2%'") % i->first % name);
  195. name += caseHackSuffix;
  196. name += std::to_string(++i->second);
  197. } else
  198. names[name] = 0;
  199. }
  200. } else if (s == "node") {
  201. if (s.empty()) throw badArchive("entry name missing");
  202. parse(sink, source, path + "/" + name);
  203. } else
  204. throw badArchive("unknown field " + s);
  205. }
  206. }
  207. else if (s == "target" && type == tpSymlink) {
  208. string target = readString(source);
  209. sink.createSymlink(path, target);
  210. }
  211. else
  212. throw badArchive("unknown field " + s);
  213. }
  214. }
  215. void parseDump(ParseSink & sink, Source & source)
  216. {
  217. string version;
  218. try {
  219. version = readString(source);
  220. } catch (SerialisationError & e) {
  221. /* This generally means the integer at the start couldn't be
  222. decoded. Ignore and throw the exception below. */
  223. }
  224. if (version != archiveVersion1)
  225. throw badArchive("input doesn't look like a Nix archive");
  226. parse(sink, source, "");
  227. }
  228. struct RestoreSink : ParseSink
  229. {
  230. Path dstPath;
  231. AutoCloseFD fd;
  232. void createDirectory(const Path & path)
  233. {
  234. Path p = dstPath + path;
  235. if (mkdir(p.c_str(), 0777) == -1)
  236. throw SysError(format("creating directory `%1%'") % p);
  237. };
  238. void createRegularFile(const Path & path)
  239. {
  240. Path p = dstPath + path;
  241. fd.close();
  242. fd = open(p.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666);
  243. if (fd == -1) throw SysError(format("creating file `%1%'") % p);
  244. }
  245. void isExecutable()
  246. {
  247. struct stat st;
  248. if (fstat(fd, &st) == -1)
  249. throw SysError("fstat");
  250. if (fchmod(fd, st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1)
  251. throw SysError("fchmod");
  252. }
  253. void preallocateContents(unsigned long long len)
  254. {
  255. #if HAVE_POSIX_FALLOCATE
  256. if (len) {
  257. errno = posix_fallocate(fd, 0, len);
  258. /* Note that EINVAL may indicate that the underlying
  259. filesystem doesn't support preallocation (e.g. on
  260. OpenSolaris). Since preallocation is just an
  261. optimisation, ignore it. */
  262. if (errno && errno != EINVAL)
  263. throw SysError(format("preallocating file of %1% bytes") % len);
  264. }
  265. #endif
  266. }
  267. void receiveContents(unsigned char * data, unsigned int len)
  268. {
  269. writeFull(fd, data, len);
  270. }
  271. void createSymlink(const Path & path, const string & target)
  272. {
  273. Path p = dstPath + path;
  274. nix::createSymlink(target, p);
  275. }
  276. };
  277. void restorePath(const Path & path, Source & source)
  278. {
  279. RestoreSink sink;
  280. sink.dstPath = path;
  281. parseDump(sink, source);
  282. }
  283. }