From 5f5cab0ac7c26783a4544feb31708d4f8e0f4a51 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 23 Jun 2003 13:27:59 +0000 Subject: [PATCH] * A function to restore from a Nix archive. * addValue() can now import any dumpable FS object. --- src/archive.cc | 196 +++++++++++++++++++++++++++++++++++++++++++++++-- src/archive.hh | 4 +- src/eval.cc | 2 +- src/nix.cc | 3 +- src/test.cc | 24 +++++- src/values.cc | 78 +++++++++++++++++++- 6 files changed, 293 insertions(+), 14 deletions(-) diff --git a/src/archive.cc b/src/archive.cc index 591939bb6..d0cf6ca34 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); +} + diff --git a/src/archive.hh b/src/archive.hh index d351c6bf6..7d9b1e2b5 100644 --- a/src/archive.hh +++ b/src/archive.hh @@ -48,7 +48,7 @@ struct DumpSink void dumpPath(const string & path, DumpSink & sink); -struct ReadSource +struct RestoreSource { /* The callee should store exactly *len bytes in the buffer pointed to by data. It should block if that much data is not @@ -56,3 +56,5 @@ struct ReadSource available. */ virtual void operator () (const unsigned char * data, unsigned int len) = 0; }; + +void restorePath(const string & path, RestoreSource & source); diff --git a/src/eval.cc b/src/eval.cc index 3b95588d7..dc1fe3157 100644 --- a/src/eval.cc +++ b/src/eval.cc @@ -123,8 +123,8 @@ static Hash computeDerived(Hash sourceHash, string targetName, } catch (exception & e) { cerr << "build error: " << e.what() << endl; - _exit(1); } + _exit(1); } diff --git a/src/nix.cc b/src/nix.cc index 8380abc20..de16ed982 100644 --- a/src/nix.cc +++ b/src/nix.cc @@ -133,7 +133,8 @@ struct StdoutSink : DumpSink (const unsigned char * data, unsigned int len) { /* Don't use cout, it's slow as hell! */ - write(STDOUT_FILENO, (char *) data, len); + if (write(STDOUT_FILENO, (char *) data, len) != len) + throw SysError("writing to stdout"); } }; diff --git a/src/test.cc b/src/test.cc index 2eab91d43..c8e3292e3 100644 --- a/src/test.cc +++ b/src/test.cc @@ -23,7 +23,21 @@ struct MySink : DumpSink virtual void operator () (const unsigned char * data, unsigned int len) { /* Don't use cout, it's slow as hell! */ - write(STDOUT_FILENO, (char *) data, len); + if (write(STDOUT_FILENO, (char *) data, len) != (ssize_t) len) + throw SysError("writing to stdout"); + } +}; + + +struct MySource : RestoreSource +{ + virtual void operator () (const unsigned char * data, unsigned int len) + { + ssize_t res = read(STDIN_FILENO, (char *) data, len); + if (res == -1) + throw SysError("reading from stdin"); + if (res != (ssize_t) len) + throw Error("not enough data available on stdin"); } }; @@ -53,6 +67,14 @@ void runTests() cout << (string) hashPath("scratch") << endl; #endif + /* Restoring. */ +#if 1 + MySource source; + restorePath("outdir", source); + cout << (string) hashPath("outdir") << endl; + return; +#endif + /* Set up the test environment. */ mkdir("scratch", 0777); diff --git a/src/values.cc b/src/values.cc index 7cef5d3de..22f84c83e 100644 --- a/src/values.cc +++ b/src/values.cc @@ -1,13 +1,85 @@ +#include + +#include +#include + #include "values.hh" #include "globals.hh" #include "db.hh" +#include "archive.hh" + + +struct CopySink : DumpSink +{ + int fd; + virtual void operator () (const unsigned char * data, unsigned int len) + { + if (write(fd, (char *) data, len) != (ssize_t) len) + throw SysError("writing to child"); + } +}; + + +struct CopySource : RestoreSource +{ + int fd; + virtual void operator () (const unsigned char * data, unsigned int len) + { + ssize_t res = read(fd, (char *) data, len); + if (res == -1) + throw SysError("reading from parent"); + if (res != (ssize_t) len) + throw Error("not enough data available on parent"); + } +}; static void copyFile(string src, string dst) { - int res = system(("cat " + src + " > " + dst).c_str()); /* !!! escape */ - if (WEXITSTATUS(res) != 0) - throw Error("cannot copy " + src + " to " + dst); + /* Unfortunately C++ doesn't support coprocedures, so we have no + nice way to chain CopySink and CopySource together. Instead we + fork off a child to run the sink. (Fork-less platforms should + use a thread). */ + + /* Create a pipe. */ + int fds[2]; + if (pipe(fds) == -1) throw SysError("creating pipe"); + + /* Fork. */ + pid_t pid; + switch (pid = fork()) { + + case -1: + throw SysError("unable to fork"); + + case 0: /* child */ + try { + close(fds[1]); + CopySource source; + source.fd = fds[0]; + restorePath(dst, source); + _exit(0); + } catch (exception & e) { + cerr << "error: " << e.what() << endl; + } + _exit(1); + } + + close(fds[0]); + + /* Parent. */ + + CopySink sink; + sink.fd = fds[1]; + dumpPath(src, sink); + + /* Wait for the child to finish. */ + int status; + if (waitpid(pid, &status, 0) != pid) + throw SysError("waiting for child"); + + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) + throw Error("cannot copy file: child died"); }