diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2003-06-23T13·27+0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2003-06-23T13·27+0000 |
commit | 5f5cab0ac7c26783a4544feb31708d4f8e0f4a51 (patch) | |
tree | 1ac6bf79f849f385ed38b75377fab010880b8d70 /src | |
parent | 85effedca3e4cc3c10ccd835c9ea4fb712418cb9 (diff) |
* A function to restore from a Nix archive.
* addValue() can now import any dumpable FS object.
Diffstat (limited to 'src')
-rw-r--r-- | src/archive.cc | 196 | ||||
-rw-r--r-- | src/archive.hh | 4 | ||||
-rw-r--r-- | src/eval.cc | 2 | ||||
-rw-r--r-- | src/nix.cc | 3 | ||||
-rw-r--r-- | src/test.cc | 24 | ||||
-rw-r--r-- | src/values.cc | 78 |
6 files changed, 293 insertions, 14 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); +} + diff --git a/src/archive.hh b/src/archive.hh index d351c6bf63f4..7d9b1e2b5676 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 3b95588d71cb..dc1fe315783f 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 8380abc20e37..de16ed9822d5 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 2eab91d43bb4..c8e3292e3aee 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 7cef5d3def2b..22f84c83e61b 100644 --- a/src/values.cc +++ b/src/values.cc @@ -1,13 +1,85 @@ +#include <iostream> + +#include <sys/types.h> +#include <sys/wait.h> + #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"); } |