archive.hh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include "types.hh"
  3. #include "serialise.hh"
  4. namespace nix {
  5. /* dumpPath creates a Nix archive of the specified path. The format
  6. is as follows:
  7. IF path points to a REGULAR FILE:
  8. dump(path) = attrs(
  9. [ ("type", "regular")
  10. , ("contents", contents(path))
  11. ])
  12. IF path points to a DIRECTORY:
  13. dump(path) = attrs(
  14. [ ("type", "directory")
  15. , ("entries", concat(map(f, sort(entries(path)))))
  16. ])
  17. where f(fn) = attrs(
  18. [ ("name", fn)
  19. , ("file", dump(path + "/" + fn))
  20. ])
  21. where:
  22. attrs(as) = concat(map(attr, as)) + encN(0)
  23. attrs((a, b)) = encS(a) + encS(b)
  24. encS(s) = encN(len(s)) + s + (padding until next 64-bit boundary)
  25. encN(n) = 64-bit little-endian encoding of n.
  26. contents(path) = the contents of a regular file.
  27. sort(strings) = lexicographic sort by 8-bit value (strcmp).
  28. entries(path) = the entries of a directory, without `.' and
  29. `..'.
  30. `+' denotes string concatenation. */
  31. struct PathFilter
  32. {
  33. virtual ~PathFilter() { }
  34. virtual bool operator () (const Path & path) { return true; }
  35. };
  36. extern PathFilter defaultPathFilter;
  37. void dumpPath(const Path & path, Sink & sink,
  38. PathFilter & filter = defaultPathFilter);
  39. struct ParseSink
  40. {
  41. virtual void createDirectory(const Path & path) { };
  42. virtual void createRegularFile(const Path & path) { };
  43. virtual void isExecutable() { };
  44. virtual void preallocateContents(unsigned long long size) { };
  45. virtual void receiveContents(unsigned char * data, unsigned int len) { };
  46. virtual void createSymlink(const Path & path, const string & target) { };
  47. };
  48. void parseDump(ParseSink & sink, Source & source);
  49. void restorePath(const Path & path, Source & source);
  50. }