archive.cc 8.2 KB

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