diff options
Diffstat (limited to 'src/archive.cc')
-rw-r--r-- | src/archive.cc | 196 |
1 files changed, 189 insertions, 7 deletions
diff --git a/src/archive.cc b/src/archive.cc index 591939bb6ad2..d0cf6ca34a7b 100644 --- a/src/archive.cc +++ b/src/archive.cc @@ -10,7 +10,10 @@ #include "util.hh" -static void pad(unsigned int len, DumpSink & sink) +static string archiveVersion1 = "nix-archive-1"; + + +static void writePadding(unsigned int len, DumpSink & sink) { if (len % 8) { unsigned char zero[8]; @@ -37,10 +40,13 @@ static void writeString(const string & s, DumpSink & sink) unsigned int len = s.length(); writeInt(len, sink); sink((const unsigned char *) s.c_str(), len); - pad(len, sink); + writePadding(len, sink); } +static void dump(const string & path, DumpSink & sink); + + static void dumpEntries(const string & path, DumpSink & sink) { DIR * dir = opendir(path.c_str()); @@ -65,8 +71,8 @@ static void dumpEntries(const string & path, DumpSink & sink) writeString("(", sink); writeString("name", sink); writeString(*it, sink); - writeString("file", sink); - dumpPath(path + "/" + *it, sink); + writeString("node", sink); + dump(path + "/" + *it, sink); writeString(")", sink); } @@ -96,13 +102,13 @@ static void dumpContents(const string & path, unsigned int size, if (total != size) throw SysError("file changed while reading it: " + path); - pad(size, sink); + writePadding(size, sink); close(fd); /* !!! close on exception */ } -void dumpPath(const string & path, DumpSink & sink) +static void dump(const string & path, DumpSink & sink) { struct stat st; if (lstat(path.c_str(), &st)) @@ -138,6 +144,182 @@ void dumpPath(const string & path, DumpSink & sink) } -void restorePath(const string & path, ReadSource & source) +void dumpPath(const string & path, DumpSink & sink) +{ + writeString(archiveVersion1, sink); + dump(path, sink); +} + + +static Error badArchive(string s) +{ + return Error("bad archive: " + s); +} + + +static void readPadding(unsigned int len, RestoreSource & source) +{ + if (len % 8) { + unsigned char zero[8]; + unsigned int n = 8 - (len % 8); + source(zero, n); + for (unsigned int i = 0; i < n; i++) + if (zero[i]) throw badArchive("non-zero padding"); + } +} + +static unsigned int readInt(RestoreSource & source) +{ + unsigned char buf[8]; + source(buf, sizeof(buf)); + if (buf[4] || buf[5] || buf[6] || buf[7]) + throw Error("implementation cannot deal with > 32-bit integers"); + return + buf[0] | + (buf[1] << 8) | + (buf[2] << 16) | + (buf[3] << 24); +} + + +static string readString(RestoreSource & source) +{ + unsigned int len = readInt(source); + char buf[len]; + source((const unsigned char *) buf, len); + readPadding(len, source); + return string(buf, len); +} + + +static void skipGeneric(RestoreSource & source) { + if (readString(source) == "(") { + while (readString(source) != ")") + skipGeneric(source); + } } + + +static void restore(const string & path, RestoreSource & source); + + +static void restoreEntry(const string & path, RestoreSource & source) +{ + string s, name; + + s = readString(source); + if (s != "(") throw badArchive("expected open tag"); + + while (1) { + s = readString(source); + + if (s == ")") { + break; + } else if (s == "name") { + name = readString(source); + } else if (s == "node") { + if (s == "") throw badArchive("entry name missing"); + restore(path + "/" + name, source); + } else { + throw badArchive("unknown field " + s); + skipGeneric(source); + } + } +} + + +static void restoreContents(int fd, const string & path, RestoreSource & source) +{ + unsigned int size = readInt(source); + unsigned int left = size; + unsigned char buf[65536]; + + while (left) { + unsigned int n = sizeof(buf); + if (n > left) n = left; + source(buf, n); + if (write(fd, buf, n) != (ssize_t) n) + throw SysError("writing file " + path); + left -= n; + } + + readPadding(size, source); +} + + +static void restore(const string & path, RestoreSource & source) +{ + string s; + + s = readString(source); + if (s != "(") throw badArchive("expected open tag"); + + enum { tpUnknown, tpRegular, tpDirectory, tpSymlink } type = tpUnknown; + int fd = -1; /* !!! close on exception */ + + while (1) { + s = readString(source); + + if (s == ")") { + break; + } + + else if (s == "type") { + if (type != tpUnknown) + throw badArchive("multiple type fields"); + string t = readString(source); + + if (t == "regular") { + type = tpRegular; + fd = open(path.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666); + if (fd == -1) + throw SysError("creating file " + path); + } + + else if (t == "directory") { + type = tpDirectory; + if (mkdir(path.c_str(), 0777) == -1) + throw SysError("creating directory " + path); + } + + else if (t == "symlink") { + type = tpSymlink; + } + + else throw badArchive("unknown file type " + t); + + } + + else if (s == "contents" && type == tpRegular) { + restoreContents(fd, path, source); + } + + else if (s == "entry" && type == tpDirectory) { + restoreEntry(path, source); + } + + else if (s == "target" && type == tpSymlink) { + string target = readString(source); + if (symlink(target.c_str(), path.c_str()) == -1) + throw SysError("creating symlink " + path); + } + + else { + throw badArchive("unknown field " + s); + skipGeneric(source); + } + + } + + if (fd != -1) close(fd); +} + + +void restorePath(const string & path, RestoreSource & source) +{ + if (readString(source) != archiveVersion1) + throw badArchive("expected Nix archive"); + restore(path, source); +} + |