From 8798fae30450a88c339c8f23d7e0c75f5df2ef1c Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 18 Nov 2003 10:47:59 +0000 Subject: * Source tree refactoring. --- configure.ac | 2 +- src/Makefile.am | 2 +- src/fix-ng/Makefile.am | 7 +- src/fix/Makefile.am | 8 - src/fix/fix.cc | 489 ---------------------------------------------- src/libmain/Makefile.am | 2 +- src/libnix/Makefile.am | 7 +- src/libnix/archive.cc | 334 ------------------------------- src/libnix/archive.hh | 60 ------ src/libnix/aterm.cc | 93 --------- src/libnix/aterm.hh | 77 -------- src/libnix/hash.cc | 124 ------------ src/libnix/hash.hh | 51 ----- src/libnix/md5.c | 448 ------------------------------------------ src/libnix/md5.h | 151 -------------- src/libnix/test-aterm.cc | 66 ------- src/libnix/util.cc | 318 ------------------------------ src/libnix/util.hh | 168 ---------------- src/libutil/Makefile.am | 14 ++ src/libutil/archive.cc | 334 +++++++++++++++++++++++++++++++ src/libutil/archive.hh | 60 ++++++ src/libutil/aterm.cc | 93 +++++++++ src/libutil/aterm.hh | 77 ++++++++ src/libutil/hash.cc | 124 ++++++++++++ src/libutil/hash.hh | 51 +++++ src/libutil/md5.c | 448 ++++++++++++++++++++++++++++++++++++++++++ src/libutil/md5.h | 151 ++++++++++++++ src/libutil/test-aterm.cc | 66 +++++++ src/libutil/util.cc | 318 ++++++++++++++++++++++++++++++ src/libutil/util.hh | 168 ++++++++++++++++ src/nix-hash/Makefile.am | 6 +- src/nix/Makefile.am | 6 +- 32 files changed, 1921 insertions(+), 2402 deletions(-) delete mode 100644 src/fix/Makefile.am delete mode 100644 src/fix/fix.cc delete mode 100644 src/libnix/archive.cc delete mode 100644 src/libnix/archive.hh delete mode 100644 src/libnix/aterm.cc delete mode 100644 src/libnix/aterm.hh delete mode 100644 src/libnix/hash.cc delete mode 100644 src/libnix/hash.hh delete mode 100644 src/libnix/md5.c delete mode 100644 src/libnix/md5.h delete mode 100644 src/libnix/test-aterm.cc delete mode 100644 src/libnix/util.cc delete mode 100644 src/libnix/util.hh create mode 100644 src/libutil/Makefile.am create mode 100644 src/libutil/archive.cc create mode 100644 src/libutil/archive.hh create mode 100644 src/libutil/aterm.cc create mode 100644 src/libutil/aterm.hh create mode 100644 src/libutil/hash.cc create mode 100644 src/libutil/hash.hh create mode 100644 src/libutil/md5.c create mode 100644 src/libutil/md5.h create mode 100644 src/libutil/test-aterm.cc create mode 100644 src/libutil/util.cc create mode 100644 src/libutil/util.hh diff --git a/configure.ac b/configure.ac index 54db203171..cc7db29c81 100644 --- a/configure.ac +++ b/configure.ac @@ -28,11 +28,11 @@ AC_CONFIG_FILES([Makefile src/Makefile src/boost/Makefile src/boost/format/Makefile + src/libutil/Makefile src/libnix/Makefile src/libmain/Makefile src/nix/Makefile src/nix-hash/Makefile - src/fix/Makefile src/fix-ng/Makefile scripts/Makefile corepkgs/Makefile diff --git a/src/Makefile.am b/src/Makefile.am index 57af4dbc8f..e62c29d7ce 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1 +1 @@ -SUBDIRS = boost libnix libmain nix nix-hash fix fix-ng +SUBDIRS = boost libutil libnix libmain nix nix-hash fix-ng diff --git a/src/fix-ng/Makefile.am b/src/fix-ng/Makefile.am index 359e39c46c..cf85493297 100644 --- a/src/fix-ng/Makefile.am +++ b/src/fix-ng/Makefile.am @@ -1,11 +1,12 @@ bin_PROGRAMS = fix-ng fix_ng_SOURCES = fix-expr.cc parser.cc eval.cc primops.cc fix.cc -fix_ng_LDADD = ../libmain/libmain.a ../libnix/libnix.a ../boost/format/libformat.a \ - -L../../externals/inst/lib -ldb_cxx -lsglr -lATB -lconversion -lasfix2 -lmept -lATerm +fix_ng_LDADD = ../libmain/libmain.a ../libnix/libnix.a ../libutil/libutil.a \ + ../boost/format/libformat.a -L../../externals/inst/lib -ldb_cxx \ + -lsglr -lATB -lconversion -lasfix2 -lmept -lATerm AM_CXXFLAGS = \ - -I.. -I../../externals/inst/include -I../libnix -I../libmain + -I.. -I../../externals/inst/include -I../libutil -I../libnix -I../libmain # Parse table generation. diff --git a/src/fix/Makefile.am b/src/fix/Makefile.am deleted file mode 100644 index 4786db54c3..0000000000 --- a/src/fix/Makefile.am +++ /dev/null @@ -1,8 +0,0 @@ -bin_PROGRAMS = fix - -fix_SOURCES = fix.cc -fix_LDADD = ../libmain/libmain.a ../libnix/libnix.a ../boost/format/libformat.a \ - -L../../externals/inst/lib -ldb_cxx -lATerm - -AM_CXXFLAGS = \ - -I.. -I../../externals/inst/include -I../libnix -I../libmain diff --git a/src/fix/fix.cc b/src/fix/fix.cc deleted file mode 100644 index 8b8441050d..0000000000 --- a/src/fix/fix.cc +++ /dev/null @@ -1,489 +0,0 @@ -#include -#include - -#include "globals.hh" -#include "normalise.hh" -#include "shared.hh" - - -typedef ATerm Expr; - -typedef map NormalForms; -typedef map PkgPaths; -typedef map PkgHashes; - -struct EvalState -{ - Paths searchDirs; - NormalForms normalForms; - PkgPaths pkgPaths; - PkgHashes pkgHashes; /* normalised package hashes */ - Expr blackHole; - - EvalState() - { - blackHole = ATmake("BlackHole()"); - if (!blackHole) throw Error("cannot build black hole"); - } -}; - - -static Expr evalFile(EvalState & state, const Path & path); -static Expr evalExpr(EvalState & state, Expr e); - - -static Path searchPath(const Paths & searchDirs, const Path & relPath) -{ - if (string(relPath, 0, 1) == "/") return relPath; - - for (Paths::const_iterator i = searchDirs.begin(); - i != searchDirs.end(); i++) - { - Path path = *i + "/" + relPath; - if (pathExists(path)) return path; - } - - throw Error( - format("path `%1%' not found in any of the search directories") - % relPath); -} - - -static Expr substExpr(string x, Expr rep, Expr e) -{ - char * s; - Expr e2; - - if (ATmatch(e, "Var()", &s)) - if (x == s) - return rep; - else - return e; - - ATermList formals; - if (ATmatch(e, "Function([], )", &formals, &e2)) { - while (!ATisEmpty(formals)) { - if (!ATmatch(ATgetFirst(formals), "", &s)) - throw badTerm("not a list of formals", (ATerm) formals); - if (x == (string) s) - return e; - formals = ATgetNext(formals); - } - } - - /* Generically substitute in subterms. */ - - if (ATgetType(e) == AT_APPL) { - AFun fun = ATgetAFun(e); - int arity = ATgetArity(fun); - ATermList args = ATempty; - - for (int i = arity - 1; i >= 0; i--) - args = ATinsert(args, substExpr(x, rep, ATgetArgument(e, i))); - - return (ATerm) ATmakeApplList(fun, args); - } - - if (ATgetType(e) == AT_LIST) { - ATermList in = (ATermList) e; - ATermList out = ATempty; - - while (!ATisEmpty(in)) { - out = ATinsert(out, substExpr(x, rep, ATgetFirst(in))); - in = ATgetNext(in); - } - - return (ATerm) ATreverse(out); - } - - throw badTerm("do not know how to substitute", e); -} - - -static Expr substExprMany(ATermList formals, ATermList args, Expr body) -{ - char * s; - Expr e; - - /* !!! check args against formals */ - - while (!ATisEmpty(args)) { - ATerm tup = ATgetFirst(args); - if (!ATmatch(tup, "(, )", &s, &e)) - throw badTerm("expected an argument tuple", tup); - - body = substExpr(s, e, body); - - args = ATgetNext(args); - } - - return body; -} - - -static PathSet nixExprRootsCached(EvalState & state, const Path & nePath) -{ - PkgPaths::iterator i = state.pkgPaths.find(nePath); - if (i != state.pkgPaths.end()) - return i->second; - else { - PathSet paths = nixExprRoots(nePath); - state.pkgPaths[nePath] = paths; - return paths; - } -} - - -static Hash hashPackage(EvalState & state, NixExpr ne) -{ - if (ne.type == NixExpr::neDerivation) { - PathSet inputs2; - for (PathSet::iterator i = ne.derivation.inputs.begin(); - i != ne.derivation.inputs.end(); i++) - { - PkgHashes::iterator j = state.pkgHashes.find(*i); - if (j == state.pkgHashes.end()) - throw Error(format("don't know expression `%1%'") % (string) *i); - inputs2.insert(j->second); - } - ne.derivation.inputs = inputs2; - } - return hashTerm(unparseNixExpr(ne)); -} - - -static string processBinding(EvalState & state, Expr e, NixExpr & ne) -{ - char * s1; - - if (ATmatch(e, "NixExpr()", &s1)) { - Path nePath(s1); - PathSet paths = nixExprRootsCached(state, nePath); - if (paths.size() != 1) abort(); - Path path = *(paths.begin()); - ne.derivation.inputs.insert(nePath); - return path; - } - - if (ATmatch(e, "", &s1)) - return s1; - - if (ATmatch(e, "True")) return "1"; - - if (ATmatch(e, "False")) return ""; - - ATermList l; - if (ATmatch(e, "[]", &l)) { - string s; - bool first = true; - while (!ATisEmpty(l)) { - if (!first) s = s + " "; else first = false; - s += processBinding(state, evalExpr(state, ATgetFirst(l)), ne); - l = ATgetNext(l); - } - return s; - } - - throw badTerm("invalid package binding", e); -} - - -static Expr evalExpr2(EvalState & state, Expr e) -{ - char * s1; - Expr e1, e2, e3, e4; - ATermList bnds; - - /* Normal forms. */ - if (ATmatch(e, "", &s1) || - ATmatch(e, "[]", &e1) || - ATmatch(e, "True") || - ATmatch(e, "False") || - ATmatch(e, "Function([], )", &e1, &e2) || - ATmatch(e, "NixExpr()", &s1)) - return e; - - try { - Hash pkgHash = hashPackage(state, parseNixExpr(e)); - Path pkgPath = writeTerm(e, ""); - state.pkgHashes[pkgPath] = pkgHash; - return ATmake("NixExpr()", pkgPath.c_str()); - } catch (...) { /* !!! catch parse errors only */ - } - - /* Application. */ - if (ATmatch(e, "Call(, [])", &e1, &e2) || - ATmatch(e, "App(, [])", &e1, &e2)) { - e1 = evalExpr(state, e1); - if (!ATmatch(e1, "Function([], )", &e3, &e4)) - throw badTerm("expecting a function", e1); - return evalExpr(state, - substExprMany((ATermList) e3, (ATermList) e2, e4)); - } - - /* Conditional. */ - if (ATmatch(e, "If(, , )", &e1, &e2, &e3)) { - e1 = evalExpr(state, e1); - Expr x; - if (ATmatch(e1, "True")) x = e2; - else if (ATmatch(e1, "False")) x = e3; - else throw badTerm("expecting a boolean", e1); - return evalExpr(state, x); - } - - /* Ad-hoc function for string matching. */ - if (ATmatch(e, "HasSubstr(, )", &e1, &e2)) { - e1 = evalExpr(state, e1); - e2 = evalExpr(state, e2); - - char * s1, * s2; - if (!ATmatch(e1, "", &s1)) - throw badTerm("expecting a string", e1); - if (!ATmatch(e2, "", &s2)) - throw badTerm("expecting a string", e2); - - return - string(s1).find(string(s2)) != string::npos ? - ATmake("True") : ATmake("False"); - } - - /* Platform constant. */ - if (ATmatch(e, "Platform")) { - return ATmake("", thisSystem.c_str()); - } - - /* Fix inclusion. */ - if (ATmatch(e, "IncludeFix()", &s1)) { - Path fileName(s1); - return evalFile(state, s1); - } - - /* Relative files. */ - if (ATmatch(e, "Relative()", &s1)) { - Path srcPath = searchPath(state.searchDirs, s1); - Path dstPath = addToStore(srcPath); - - ClosureElem elem; - NixExpr ne; - ne.type = NixExpr::neClosure; - ne.closure.roots.insert(dstPath); - ne.closure.elems[dstPath] = elem; - - Hash pkgHash = hashPackage(state, ne); - Path pkgPath = writeTerm(unparseNixExpr(ne), ""); - state.pkgHashes[pkgPath] = pkgHash; - - printMsg(lvlChatty, format("copied `%1%' -> closure `%2%'") - % srcPath % pkgPath); - - return ATmake("NixExpr()", pkgPath.c_str()); - } - - /* Packages are transformed into Nix derivation expressions. */ - if (ATmatch(e, "Package([])", &bnds)) { - - /* Evaluate the bindings and put them in a map. */ - map bndMap; - bndMap["platform"] = ATmake("", thisSystem.c_str()); - while (!ATisEmpty(bnds)) { - ATerm bnd = ATgetFirst(bnds); - if (!ATmatch(bnd, "(, )", &s1, &e1)) - throw badTerm("binding expected", bnd); - bndMap[s1] = evalExpr(state, e1); - bnds = ATgetNext(bnds); - } - - /* Gather information for building the derivation - expression. */ - NixExpr ne; - ne.type = NixExpr::neDerivation; - ne.derivation.platform = thisSystem; - string name; - Path outPath; - Hash outHash; - bool outHashGiven = false; - bnds = ATempty; - - for (map::iterator it = bndMap.begin(); - it != bndMap.end(); it++) - { - string key = it->first; - ATerm value = it->second; - - if (key == "args") { - ATermList args; - if (!ATmatch(value, "[]", &args)) - throw badTerm("list expected", value); - - while (!ATisEmpty(args)) { - Expr arg = evalExpr(state, ATgetFirst(args)); - ne.derivation.args.push_back(processBinding(state, arg, ne)); - args = ATgetNext(args); - } - } - - else { - string s = processBinding(state, value, ne); - ne.derivation.env[key] = s; - - if (key == "build") ne.derivation.builder = s; - if (key == "name") name = s; - if (key == "outPath") outPath = s; - if (key == "id") { - outHash = parseHash(s); - outHashGiven = true; - } - } - - bnds = ATinsert(bnds, - ATmake("(, )", key.c_str(), value)); - } - - if (ne.derivation.builder == "") - throw badTerm("no builder specified", e); - - if (name == "") - throw badTerm("no package name specified", e); - - /* Determine the output path. */ - if (!outHashGiven) outHash = hashPackage(state, ne); - if (outPath == "") - /* Hash the Nix expression with no outputs to produce a - unique but deterministic path name for this package. */ - outPath = - canonPath(nixStore + "/" + ((string) outHash).c_str() + "-" + name); - ne.derivation.env["out"] = outPath; - ne.derivation.outputs.insert(outPath); - - /* Write the resulting term into the Nix store directory. */ - Hash pkgHash = outHashGiven - ? hashString((string) outHash + outPath) - : hashPackage(state, ne); - Path pkgPath = writeTerm(unparseNixExpr(ne), "-d-" + name); - state.pkgHashes[pkgPath] = pkgHash; - - printMsg(lvlChatty, format("instantiated `%1%' -> `%2%'") - % name % pkgPath); - - return ATmake("NixExpr()", pkgPath.c_str()); - } - - /* BaseName primitive function. */ - if (ATmatch(e, "BaseName()", &e1)) { - e1 = evalExpr(state, e1); - if (!ATmatch(e1, "", &s1)) - throw badTerm("string expected", e1); - return ATmake("", baseNameOf(s1).c_str()); - } - - /* Barf. */ - throw badTerm("invalid expression", e); -} - - -static Expr evalExpr(EvalState & state, Expr e) -{ - startNest(nest, lvlVomit, - format("evaluating expression: %1%") % e); - - /* Consult the memo table to quickly get the normal form of - previously evaluated expressions. */ - NormalForms::iterator i = state.normalForms.find(e); - if (i != state.normalForms.end()) { - if (i->second == state.blackHole) - throw badTerm("infinite recursion", e); - return i->second; - } - - /* Otherwise, evaluate and memoize. */ - state.normalForms[e] = state.blackHole; - Expr nf = evalExpr2(state, e); - state.normalForms[e] = nf; - return nf; -} - - -static Expr evalFile(EvalState & state, const Path & relPath) -{ - Path path = searchPath(state.searchDirs, relPath); - startNest(nest, lvlTalkative, format("evaluating file `%1%'") % path); - Expr e = ATreadFromNamedFile(path.c_str()); - if (!e) - throw Error(format("unable to read a term from `%1%'") % path); - return evalExpr(state, e); -} - - -static Expr evalStdin(EvalState & state) -{ - startNest(nest, lvlTalkative, format("evaluating standard input")); - Expr e = ATreadFromFile(stdin); - if (!e) - throw Error(format("unable to read a term from stdin")); - return evalExpr(state, e); -} - - -static void printNixExpr(EvalState & state, Expr e) -{ - ATermList es; - char * s; - if (ATmatch(e, "NixExpr()", &s)) { - cout << format("%1%\n") % s; - } - else if (ATmatch(e, "[]", &es)) { - while (!ATisEmpty(es)) { - printNixExpr(state, evalExpr(state, ATgetFirst(es))); - es = ATgetNext(es); - } - } - else throw badTerm("top level does not evaluate to a (list of) Nix expression(s)", e); -} - - -void run(Strings args) -{ - EvalState state; - Strings files; - bool readStdin = false; - - state.searchDirs.push_back("."); - state.searchDirs.push_back(nixDataDir + "/fix"); - - for (Strings::iterator it = args.begin(); - it != args.end(); ) - { - string arg = *it++; - - if (arg == "--includedir" || arg == "-I") { - if (it == args.end()) - throw UsageError(format("argument required in `%1%'") % arg); - state.searchDirs.push_back(*it++); - } - else if (arg == "--verbose" || arg == "-v") - verbosity = (Verbosity) ((int) verbosity + 1); - else if (arg == "-") - readStdin = true; - else if (arg[0] == '-') - throw UsageError(format("unknown flag `%1%`") % arg); - else - files.push_back(arg); - } - - openDB(); - - if (readStdin) { - Expr e = evalStdin(state); - printNixExpr(state, e); - } - - for (Strings::iterator it = files.begin(); - it != files.end(); it++) - { - Expr e = evalFile(state, *it); - printNixExpr(state, e); - } -} - - -string programId = "fix"; diff --git a/src/libmain/Makefile.am b/src/libmain/Makefile.am index aebb1286e7..edd6685450 100644 --- a/src/libmain/Makefile.am +++ b/src/libmain/Makefile.am @@ -7,6 +7,6 @@ AM_CXXFLAGS = \ -DNIX_DATA_DIR=\"$(datadir)\" \ -DNIX_STATE_DIR=\"$(localstatedir)/nix\" \ -DNIX_LOG_DIR=\"$(localstatedir)/log/nix\" \ - -I.. -I../../externals/inst/include -I../libnix + -I.. -I../../externals/inst/include -I../libutil -I../libnix EXTRA_DIST = *.hh diff --git a/src/libnix/Makefile.am b/src/libnix/Makefile.am index 7671b1613d..055ef1af7b 100644 --- a/src/libnix/Makefile.am +++ b/src/libnix/Makefile.am @@ -1,10 +1,11 @@ noinst_LIBRARIES = libnix.a -libnix_a_SOURCES = util.cc hash.cc archive.cc md5.c \ +libnix_a_SOURCES = \ store.cc expr.cc normalise.cc exec.cc \ - globals.cc db.cc references.cc pathlocks.cc aterm.cc + globals.cc db.cc references.cc pathlocks.cc -AM_CXXFLAGS = -DSYSTEM=\"@host@\" -Wall -I.. -I../../externals/inst/include +AM_CXXFLAGS = -DSYSTEM=\"@host@\" -Wall \ + -I.. -I../../externals/inst/include -I../libutil EXTRA_DIST = *.hh *.h test-builder-*.sh diff --git a/src/libnix/archive.cc b/src/libnix/archive.cc deleted file mode 100644 index ed57df4c9f..0000000000 --- a/src/libnix/archive.cc +++ /dev/null @@ -1,334 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "archive.hh" -#include "util.hh" - - -static string archiveVersion1 = "nix-archive-1"; - - -static void writePadding(unsigned int len, DumpSink & sink) -{ - if (len % 8) { - unsigned char zero[8]; - memset(zero, 0, sizeof(zero)); - sink(zero, 8 - (len % 8)); - } -} - - -static void writeInt(unsigned int n, DumpSink & sink) -{ - unsigned char buf[8]; - memset(buf, 0, sizeof(buf)); - buf[0] = n & 0xff; - buf[1] = (n >> 8) & 0xff; - buf[2] = (n >> 16) & 0xff; - buf[3] = (n >> 24) & 0xff; - sink(buf, sizeof(buf)); -} - - -static void writeString(const string & s, DumpSink & sink) -{ - unsigned int len = s.length(); - writeInt(len, sink); - sink((const unsigned char *) s.c_str(), len); - writePadding(len, sink); -} - - -static void dump(const string & path, DumpSink & sink); - - -static void dumpEntries(const Path & path, DumpSink & sink) -{ - AutoCloseDir dir = opendir(path.c_str()); - if (!dir) throw SysError("opening directory " + path); - - vector names; - - struct dirent * dirent; - while (errno = 0, dirent = readdir(dir)) { - string name = dirent->d_name; - if (name == "." || name == "..") continue; - names.push_back(name); - } - if (errno) throw SysError("reading directory " + path); - - sort(names.begin(), names.end()); - - for (vector::iterator it = names.begin(); - it != names.end(); it++) - { - writeString("entry", sink); - writeString("(", sink); - writeString("name", sink); - writeString(*it, sink); - writeString("node", sink); - dump(path + "/" + *it, sink); - writeString(")", sink); - } -} - - -static void dumpContents(const Path & path, unsigned int size, - DumpSink & sink) -{ - writeString("contents", sink); - writeInt(size, sink); - - AutoCloseFD fd = open(path.c_str(), O_RDONLY); - if (fd == -1) throw SysError(format("opening file `%1%'") % path); - - unsigned char buf[65536]; - - unsigned int total = 0; - ssize_t n; - while ((n = read(fd, buf, sizeof(buf)))) { - if (n == -1) throw SysError("reading file " + path); - total += n; - sink(buf, n); - } - - if (total != size) - throw SysError("file changed while reading it: " + path); - - writePadding(size, sink); -} - - -static void dump(const Path & path, DumpSink & sink) -{ - struct stat st; - if (lstat(path.c_str(), &st)) - throw SysError(format("getting attributes of path `%1%'") % path); - - writeString("(", sink); - - if (S_ISREG(st.st_mode)) { - writeString("type", sink); - writeString("regular", sink); - if (st.st_mode & S_IXUSR) { - writeString("executable", sink); - writeString("", sink); - } - dumpContents(path, st.st_size, sink); - } - - else if (S_ISDIR(st.st_mode)) { - writeString("type", sink); - writeString("directory", sink); - dumpEntries(path, sink); - } - - else if (S_ISLNK(st.st_mode)) { - writeString("type", sink); - writeString("symlink", sink); - char buf[st.st_size]; - if (readlink(path.c_str(), buf, st.st_size) != st.st_size) - throw SysError("reading symbolic link " + path); - writeString("target", sink); - writeString(string(buf, st.st_size), sink); - } - - else throw Error("unknown file type: " + path); - - writeString(")", sink); -} - - -void dumpPath(const Path & 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((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 Path & path, RestoreSource & source); - - -static void restoreEntry(const Path & 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 Path & 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 Path & path, RestoreSource & source) -{ - string s; - - s = readString(source); - if (s != "(") throw badArchive("expected open tag"); - - enum { tpUnknown, tpRegular, tpDirectory, tpSymlink } type = tpUnknown; - AutoCloseFD fd; - - 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 == "executable" && type == tpRegular) { - readString(source); - struct stat st; - if (fstat(fd, &st) == -1) - throw SysError("fstat"); - if (fchmod(fd, st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1) - throw SysError("fchmod"); - } - - 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); - } - - } -} - - -void restorePath(const Path & path, RestoreSource & source) -{ - if (readString(source) != archiveVersion1) - throw badArchive("expected Nix archive"); - restore(path, source); -} - diff --git a/src/libnix/archive.hh b/src/libnix/archive.hh deleted file mode 100644 index 67e236668a..0000000000 --- a/src/libnix/archive.hh +++ /dev/null @@ -1,60 +0,0 @@ -#include - -#include "util.hh" - - -/* dumpPath creates a Nix archive of the specified path. The format - is as follows: - - IF path points to a REGULAR FILE: - dump(path) = attrs( - [ ("type", "regular") - , ("contents", contents(path)) - ]) - - IF path points to a DIRECTORY: - dump(path) = attrs( - [ ("type", "directory") - , ("entries", concat(map(f, sort(entries(path))))) - ]) - where f(fn) = attrs( - [ ("name", fn) - , ("file", dump(path + "/" + fn)) - ]) - - where: - - attrs(as) = concat(map(attr, as)) + encN(0) - attrs((a, b)) = encS(a) + encS(b) - - encS(s) = encN(len(s)) + s + (padding until next 64-bit boundary) - - encN(n) = 64-bit little-endian encoding of n. - - contents(path) = the contents of a regular file. - - sort(strings) = lexicographic sort by 8-bit value (strcmp). - - entries(path) = the entries of a directory, without `.' and - `..'. - - `+' denotes string concatenation. */ - -struct DumpSink -{ - virtual void operator () (const unsigned char * data, unsigned int len) = 0; -}; - -void dumpPath(const Path & path, DumpSink & sink); - - -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 - yet available, or throw an error if it is not going to be - available. */ - virtual void operator () (unsigned char * data, unsigned int len) = 0; -}; - -void restorePath(const Path & path, RestoreSource & source); diff --git a/src/libnix/aterm.cc b/src/libnix/aterm.cc deleted file mode 100644 index de7c359521..0000000000 --- a/src/libnix/aterm.cc +++ /dev/null @@ -1,93 +0,0 @@ -#include "aterm.hh" - - -string atPrint(ATerm t) -{ - if (!t) throw Error("attempt to print null aterm"); - char * s = ATwriteToString(t); - if (!s) throw Error("cannot print term"); - return s; -} - - -ostream & operator << (ostream & stream, ATerm e) -{ - return stream << atPrint(e); -} - - -ATMatcher & atMatch(ATMatcher & pos, ATerm t) -{ - pos.t = t; - pos.pos = ATMatcher::funPos; - return pos; -} - - -static inline bool failed(const ATMatcher & pos) -{ - return pos.pos == ATMatcher::failPos; -} - - -static inline ATMatcher & fail(ATMatcher & pos) -{ - pos.pos = ATMatcher::failPos; - return pos; -} - - -ATMatcher & operator >> (ATMatcher & pos, ATerm & out) -{ - out = 0; - if (failed(pos)) return pos; - if (pos.pos == ATMatcher::funPos || - ATgetType(pos.t) != AT_APPL || - pos.pos >= (int) ATgetArity(ATgetAFun(pos.t))) - return fail(pos); - out = ATgetArgument(pos.t, pos.pos); - pos.pos++; - return pos; -} - - -ATMatcher & operator >> (ATMatcher & pos, string & out) -{ - out = ""; - if (pos.pos == ATMatcher::funPos) { - if (ATgetType(pos.t) != AT_APPL) return fail(pos); - out = ATgetName(ATgetAFun(pos.t)); - pos.pos = 0; - } else { - ATerm t; - pos = pos >> t; - if (failed(pos)) return pos; - if (ATgetType(t) != AT_APPL || - ATgetArity(ATgetAFun(t)) != 0) - return fail(pos); - out = ATgetName(ATgetAFun(t)); - } - return pos; -} - - -ATMatcher & operator >> (ATMatcher & pos, const string & s) -{ - string s2; - pos = pos >> s2; - if (failed(pos)) return pos; - if (s != s2) return fail(pos); - return pos; -} - - -ATMatcher & operator >> (ATMatcher & pos, ATermList & out) -{ - out = 0; - ATerm t; - pos = pos >> t; - if (failed(pos)) return pos; - if (ATgetType(t) != AT_LIST) return fail(pos); - out = (ATermList) t; - return pos; -} diff --git a/src/libnix/aterm.hh b/src/libnix/aterm.hh deleted file mode 100644 index 16d8d6bb6d..0000000000 --- a/src/libnix/aterm.hh +++ /dev/null @@ -1,77 +0,0 @@ -#ifndef __ATERM_H -#define __ATERM_H - -extern "C" { -#include -} - -#include "util.hh" - - -/* Print an ATerm. */ -string atPrint(ATerm t); - -/* Write an ATerm to an output stream. */ -ostream & operator << (ostream & stream, ATerm e); - -class ATermIterator -{ - ATermList t; - -public: - ATermIterator(ATermList _t) : t(_t) { } - ATermIterator & operator ++ () - { - t = ATgetNext(t); - return *this; - } - ATerm operator * () - { - return ATgetFirst(t); - } - operator bool () - { - return t != ATempty; - } -}; - - -/* Type-safe matching. */ - -struct ATMatcher -{ - ATerm t; - int pos; - const static int failPos = -2; - const static int funPos = -1; - - ATMatcher() : t(0), pos(failPos) - { - } - - operator bool() const - { - return pos != failPos; - } -}; - -/* Initiate matching of a term. */ -ATMatcher & atMatch(ATMatcher & pos, ATerm t); - -/* Get the next argument of an application. */ -ATMatcher & operator >> (ATMatcher & pos, ATerm & out); - -/* Get the name of the function symbol of an applicatin, or the next - argument of an application as a string. */ -ATMatcher & operator >> (ATMatcher & pos, string & out); - -/* Like the previous, but check that the string is equal to the given - string. */ -ATMatcher & operator >> (ATMatcher & pos, const string & s); - -/* Get the next argument of an application, and verify that it is a - list. */ -ATMatcher & operator >> (ATMatcher & pos, ATermList & out); - - -#endif /* !__ATERM_H */ diff --git a/src/libnix/hash.cc b/src/libnix/hash.cc deleted file mode 100644 index 752b269125..0000000000 --- a/src/libnix/hash.cc +++ /dev/null @@ -1,124 +0,0 @@ -#include - -extern "C" { -#include "md5.h" -} - -#include "hash.hh" -#include "archive.hh" - - -Hash::Hash() -{ - memset(hash, 0, sizeof(hash)); -} - - -bool Hash::operator == (const Hash & h2) const -{ - for (unsigned int i = 0; i < hashSize; i++) - if (hash[i] != h2.hash[i]) return false; - return true; -} - - -bool Hash::operator != (const Hash & h2) const -{ - return !(*this == h2); -} - - -bool Hash::operator < (const Hash & h) const -{ - for (unsigned int i = 0; i < hashSize; i++) { - if (hash[i] < h.hash[i]) return true; - if (hash[i] > h.hash[i]) return false; - } - return false; -} - - -Hash::operator string() const -{ - ostringstream str; - for (unsigned int i = 0; i < hashSize; i++) { - str.fill('0'); - str.width(2); - str << hex << (int) hash[i]; - } - return str.str(); -} - - -Hash parseHash(const string & s) -{ - Hash hash; - if (s.length() != Hash::hashSize * 2) - throw Error(format("invalid hash `%1%'") % s); - for (unsigned int i = 0; i < Hash::hashSize; i++) { - string s2(s, i * 2, 2); - if (!isxdigit(s2[0]) || !isxdigit(s2[1])) - throw Error(format("invalid hash `%1%'") % s); - istringstream str(s2); - int n; - str >> hex >> n; - hash.hash[i] = n; - } - return hash; -} - - -bool isHash(const string & s) -{ - if (s.length() != 32) return false; - for (int i = 0; i < 32; i++) { - char c = s[i]; - if (!((c >= '0' && c <= '9') || - (c >= 'a' && c <= 'f'))) - return false; - } - return true; -} - - -Hash hashString(const string & s) -{ - Hash hash; - md5_buffer(s.c_str(), s.length(), hash.hash); - return hash; -} - - -Hash hashFile(const Path & path) -{ - Hash hash; - FILE * file = fopen(path.c_str(), "rb"); - if (!file) - throw SysError(format("file `%1%' does not exist") % path); - int err = md5_stream(file, hash.hash); - fclose(file); - if (err) throw SysError(format("cannot hash file `%1%'") % path); - return hash; -} - - -struct HashSink : DumpSink -{ - struct md5_ctx ctx; - virtual void operator () - (const unsigned char * data, unsigned int len) - { - md5_process_bytes(data, len, &ctx); - } -}; - - -Hash hashPath(const Path & path) -{ - Hash hash; - HashSink sink; - md5_init_ctx(&sink.ctx); - dumpPath(path, sink); - md5_finish_ctx(&sink.ctx, hash.hash); - return hash; -} diff --git a/src/libnix/hash.hh b/src/libnix/hash.hh deleted file mode 100644 index 0062f987c0..0000000000 --- a/src/libnix/hash.hh +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef __HASH_H -#define __HASH_H - -#include - -#include "util.hh" - -using namespace std; - - -struct Hash -{ - static const unsigned int hashSize = 16; - unsigned char hash[hashSize]; - - /* Create a zeroed hash object. */ - Hash(); - - /* Check whether two hash are equal. */ - bool operator == (const Hash & h2) const; - - /* Check whether two hash are not equal. */ - bool operator != (const Hash & h2) const; - - /* For sorting. */ - bool operator < (const Hash & h) const; - - /* Convert a hash code into a hexadecimal representation. */ - operator string() const; -}; - - -/* Parse a hexadecimal representation of a hash code. */ -Hash parseHash(const string & s); - -/* Verify that the given string is a valid hash code. */ -bool isHash(const string & s); - -/* Compute the hash of the given string. */ -Hash hashString(const string & s); - -/* Compute the hash of the given file. */ -Hash hashFile(const Path & path); - -/* Compute the hash of the given path. The hash is defined as - md5(dump(path)). -*/ -Hash hashPath(const Path & path); - - -#endif /* !__HASH_H */ diff --git a/src/libnix/md5.c b/src/libnix/md5.c deleted file mode 100644 index fa67ebfcdb..0000000000 --- a/src/libnix/md5.c +++ /dev/null @@ -1,448 +0,0 @@ -/* Functions to compute MD5 message digest of files or memory blocks. - according to the definition of MD5 in RFC 1321 from April 1992. - Copyright (C) 1995,1996,1997,1999,2000,2001 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -/* Written by Ulrich Drepper , 1995. */ - -#ifdef HAVE_CONFIG_H -# include -#endif - -#include - -#include -#include - -#include "md5.h" - - -static md5_uint32 SWAP(md5_uint32 n) -{ - static int checked = 0; - static int bigendian = 0; - static md5_uint32 test; - - if (!checked) { - test = 1; - if (* (char *) &test == 0) - bigendian = 1; - checked = 1; - } - - if (bigendian) - return (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)); - else - return n; -} - - -/* This array contains the bytes used to pad the buffer to the next - 64-byte boundary. (RFC 1321, 3.1: Step 1) */ -static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; - - -/* Initialize structure containing state of computation. - (RFC 1321, 3.3: Step 3) */ -void -md5_init_ctx (ctx) - struct md5_ctx *ctx; -{ - ctx->A = 0x67452301; - ctx->B = 0xefcdab89; - ctx->C = 0x98badcfe; - ctx->D = 0x10325476; - - ctx->total[0] = ctx->total[1] = 0; - ctx->buflen = 0; -} - -/* Put result from CTX in first 16 bytes following RESBUF. The result - must be in little endian byte order. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -void * -md5_read_ctx (ctx, resbuf) - const struct md5_ctx *ctx; - void *resbuf; -{ - ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A); - ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B); - ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C); - ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D); - - return resbuf; -} - -/* Process the remaining bytes in the internal buffer and the usual - prolog according to the standard and write the result to RESBUF. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -void * -md5_finish_ctx (ctx, resbuf) - struct md5_ctx *ctx; - void *resbuf; -{ - /* Take yet unprocessed bytes into account. */ - md5_uint32 bytes = ctx->buflen; - size_t pad; - - /* Now count remaining bytes. */ - ctx->total[0] += bytes; - if (ctx->total[0] < bytes) - ++ctx->total[1]; - - pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; - memcpy (&ctx->buffer[bytes], fillbuf, pad); - - /* Put the 64-bit file length in *bits* at the end of the buffer. */ - *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); - *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) | - (ctx->total[0] >> 29)); - - /* Process last bytes. */ - md5_process_block (ctx->buffer, bytes + pad + 8, ctx); - - return md5_read_ctx (ctx, resbuf); -} - -/* Compute MD5 message digest for bytes read from STREAM. The - resulting message digest number will be written into the 16 bytes - beginning at RESBLOCK. */ -int -md5_stream (stream, resblock) - FILE *stream; - void *resblock; -{ - /* Important: BLOCKSIZE must be a multiple of 64. */ -#define BLOCKSIZE 4096 - struct md5_ctx ctx; - char buffer[BLOCKSIZE + 72]; - size_t sum; - - /* Initialize the computation context. */ - md5_init_ctx (&ctx); - - /* Iterate over full file contents. */ - while (1) - { - /* We read the file in blocks of BLOCKSIZE bytes. One call of the - computation function processes the whole buffer so that with the - next round of the loop another block can be read. */ - size_t n; - sum = 0; - - /* Read block. Take care for partial reads. */ - do - { - n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); - - sum += n; - } - while (sum < BLOCKSIZE && n != 0); - if (n == 0 && ferror (stream)) - return 1; - - /* If end of file is reached, end the loop. */ - if (n == 0) - break; - - /* Process buffer with BLOCKSIZE bytes. Note that - BLOCKSIZE % 64 == 0 - */ - md5_process_block (buffer, BLOCKSIZE, &ctx); - } - - /* Add the last bytes if necessary. */ - if (sum > 0) - md5_process_bytes (buffer, sum, &ctx); - - /* Construct result in desired memory. */ - md5_finish_ctx (&ctx, resblock); - return 0; -} - -/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The - result is always in little endian byte order, so that a byte-wise - output yields to the wanted ASCII representation of the message - digest. */ -void * -md5_buffer (buffer, len, resblock) - const char *buffer; - size_t len; - void *resblock; -{ - struct md5_ctx ctx; - - /* Initialize the computation context. */ - md5_init_ctx (&ctx); - - /* Process whole buffer but last len % 64 bytes. */ - md5_process_bytes (buffer, len, &ctx); - - /* Put result in desired memory area. */ - return md5_finish_ctx (&ctx, resblock); -} - - -void -md5_process_bytes (buffer, len, ctx) - const void *buffer; - size_t len; - struct md5_ctx *ctx; -{ - /* When we already have some bits in our internal buffer concatenate - both inputs first. */ - if (ctx->buflen != 0) - { - size_t left_over = ctx->buflen; - size_t add = 128 - left_over > len ? len : 128 - left_over; - - memcpy (&ctx->buffer[left_over], buffer, add); - ctx->buflen += add; - - if (ctx->buflen > 64) - { - md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx); - - ctx->buflen &= 63; - /* The regions in the following copy operation cannot overlap. */ - memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], - ctx->buflen); - } - - buffer = (const char *) buffer + add; - len -= add; - } - - /* Process available complete blocks. */ - if (len >= 64) - { -#if !_STRING_ARCH_unaligned -/* To check alignment gcc has an appropriate operator. Other - compilers don't. */ -# if __GNUC__ >= 2 -# define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0) -# else -# define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0) -# endif - if (UNALIGNED_P (buffer)) - while (len > 64) - { - md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx); - buffer = (const char *) buffer + 64; - len -= 64; - } - else -#endif - { - md5_process_block (buffer, len & ~63, ctx); - buffer = (const char *) buffer + (len & ~63); - len &= 63; - } - } - - /* Move remaining bytes in internal buffer. */ - if (len > 0) - { - size_t left_over = ctx->buflen; - - memcpy (&ctx->buffer[left_over], buffer, len); - left_over += len; - if (left_over >= 64) - { - md5_process_block (ctx->buffer, 64, ctx); - left_over -= 64; - memcpy (ctx->buffer, &ctx->buffer[64], left_over); - } - ctx->buflen = left_over; - } -} - - -/* These are the four functions used in the four steps of the MD5 algorithm - and defined in the RFC 1321. The first function is a little bit optimized - (as found in Colin Plumbs public domain implementation). */ -/* #define FF(b, c, d) ((b & c) | (~b & d)) */ -#define FF(b, c, d) (d ^ (b & (c ^ d))) -#define FG(b, c, d) FF (d, b, c) -#define FH(b, c, d) (b ^ c ^ d) -#define FI(b, c, d) (c ^ (b | ~d)) - -/* Process LEN bytes of BUFFER, accumulating context into CTX. - It is assumed that LEN % 64 == 0. */ - -void -md5_process_block (buffer, len, ctx) - const void *buffer; - size_t len; - struct md5_ctx *ctx; -{ - md5_uint32 correct_words[16]; - const md5_uint32 *words = buffer; - size_t nwords = len / sizeof (md5_uint32); - const md5_uint32 *endp = words + nwords; - md5_uint32 A = ctx->A; - md5_uint32 B = ctx->B; - md5_uint32 C = ctx->C; - md5_uint32 D = ctx->D; - - /* First increment the byte count. RFC 1321 specifies the possible - length of the file up to 2^64 bits. Here we only compute the - number of bytes. Do a double word increment. */ - ctx->total[0] += len; - if (ctx->total[0] < len) - ++ctx->total[1]; - - /* Process all bytes in the buffer with 64 bytes in each round of - the loop. */ - while (words < endp) - { - md5_uint32 *cwp = correct_words; - md5_uint32 A_save = A; - md5_uint32 B_save = B; - md5_uint32 C_save = C; - md5_uint32 D_save = D; - - /* First round: using the given function, the context and a constant - the next context is computed. Because the algorithms processing - unit is a 32-bit word and it is determined to work on words in - little endian byte order we perhaps have to change the byte order - before the computation. To reduce the work for the next steps - we store the swapped words in the array CORRECT_WORDS. */ - -#define OP(a, b, c, d, s, T) \ - do \ - { \ - a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \ - ++words; \ - CYCLIC (a, s); \ - a += b; \ - } \ - while (0) - - /* It is unfortunate that C does not provide an operator for - cyclic rotation. Hope the C compiler is smart enough. */ -#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s))) - - /* Before we start, one word to the strange constants. - They are defined in RFC 1321 as - - T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 - */ - - /* Round 1. */ - OP (A, B, C, D, 7, 0xd76aa478); - OP (D, A, B, C, 12, 0xe8c7b756); - OP (C, D, A, B, 17, 0x242070db); - OP (B, C, D, A, 22, 0xc1bdceee); - OP (A, B, C, D, 7, 0xf57c0faf); - OP (D, A, B, C, 12, 0x4787c62a); - OP (C, D, A, B, 17, 0xa8304613); - OP (B, C, D, A, 22, 0xfd469501); - OP (A, B, C, D, 7, 0x698098d8); - OP (D, A, B, C, 12, 0x8b44f7af); - OP (C, D, A, B, 17, 0xffff5bb1); - OP (B, C, D, A, 22, 0x895cd7be); - OP (A, B, C, D, 7, 0x6b901122); - OP (D, A, B, C, 12, 0xfd987193); - OP (C, D, A, B, 17, 0xa679438e); - OP (B, C, D, A, 22, 0x49b40821); - - /* For the second to fourth round we have the possibly swapped words - in CORRECT_WORDS. Redefine the macro to take an additional first - argument specifying the function to use. */ -#undef OP -#define OP(f, a, b, c, d, k, s, T) \ - do \ - { \ - a += f (b, c, d) + correct_words[k] + T; \ - CYCLIC (a, s); \ - a += b; \ - } \ - while (0) - - /* Round 2. */ - OP (FG, A, B, C, D, 1, 5, 0xf61e2562); - OP (FG, D, A, B, C, 6, 9, 0xc040b340); - OP (FG, C, D, A, B, 11, 14, 0x265e5a51); - OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa); - OP (FG, A, B, C, D, 5, 5, 0xd62f105d); - OP (FG, D, A, B, C, 10, 9, 0x02441453); - OP (FG, C, D, A, B, 15, 14, 0xd8a1e681); - OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8); - OP (FG, A, B, C, D, 9, 5, 0x21e1cde6); - OP (FG, D, A, B, C, 14, 9, 0xc33707d6); - OP (FG, C, D, A, B, 3, 14, 0xf4d50d87); - OP (FG, B, C, D, A, 8, 20, 0x455a14ed); - OP (FG, A, B, C, D, 13, 5, 0xa9e3e905); - OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8); - OP (FG, C, D, A, B, 7, 14, 0x676f02d9); - OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a); - - /* Round 3. */ - OP (FH, A, B, C, D, 5, 4, 0xfffa3942); - OP (FH, D, A, B, C, 8, 11, 0x8771f681); - OP (FH, C, D, A, B, 11, 16, 0x6d9d6122); - OP (FH, B, C, D, A, 14, 23, 0xfde5380c); - OP (FH, A, B, C, D, 1, 4, 0xa4beea44); - OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9); - OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60); - OP (FH, B, C, D, A, 10, 23, 0xbebfbc70); - OP (FH, A, B, C, D, 13, 4, 0x289b7ec6); - OP (FH, D, A, B, C, 0, 11, 0xeaa127fa); - OP (FH, C, D, A, B, 3, 16, 0xd4ef3085); - OP (FH, B, C, D, A, 6, 23, 0x04881d05); - OP (FH, A, B, C, D, 9, 4, 0xd9d4d039); - OP (FH, D, A, B, C, 12, 11, 0xe6db99e5); - OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8); - OP (FH, B, C, D, A, 2, 23, 0xc4ac5665); - - /* Round 4. */ - OP (FI, A, B, C, D, 0, 6, 0xf4292244); - OP (FI, D, A, B, C, 7, 10, 0x432aff97); - OP (FI, C, D, A, B, 14, 15, 0xab9423a7); - OP (FI, B, C, D, A, 5, 21, 0xfc93a039); - OP (FI, A, B, C, D, 12, 6, 0x655b59c3); - OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92); - OP (FI, C, D, A, B, 10, 15, 0xffeff47d); - OP (FI, B, C, D, A, 1, 21, 0x85845dd1); - OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f); - OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0); - OP (FI, C, D, A, B, 6, 15, 0xa3014314); - OP (FI, B, C, D, A, 13, 21, 0x4e0811a1); - OP (FI, A, B, C, D, 4, 6, 0xf7537e82); - OP (FI, D, A, B, C, 11, 10, 0xbd3af235); - OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb); - OP (FI, B, C, D, A, 9, 21, 0xeb86d391); - - /* Add the starting values of the context. */ - A += A_save; - B += B_save; - C += C_save; - D += D_save; - } - - /* Put checksum in context given as argument. */ - ctx->A = A; - ctx->B = B; - ctx->C = C; - ctx->D = D; -} diff --git a/src/libnix/md5.h b/src/libnix/md5.h deleted file mode 100644 index 6301e4558c..0000000000 --- a/src/libnix/md5.h +++ /dev/null @@ -1,151 +0,0 @@ -/* Declaration of functions and data types used for MD5 sum computing - library functions. - Copyright (C) 1995,1996,1997,1999,2000,2001 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -#ifndef _MD5_H -#define _MD5_H 1 - -#include - -#if defined HAVE_LIMITS_H || _LIBC -# include -#endif - -/* The following contortions are an attempt to use the C preprocessor - to determine an unsigned integral type that is 32 bits wide. An - alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but - doing that would require that the configure script compile and *run* - the resulting executable. Locally running cross-compiled executables - is usually not possible. */ - -#ifdef _LIBC -# include -typedef uint32_t md5_uint32; -typedef uintptr_t md5_uintptr; -#else -# if defined __STDC__ && __STDC__ -# define UINT_MAX_32_BITS 4294967295U -# else -# define UINT_MAX_32_BITS 0xFFFFFFFF -# endif - -/* If UINT_MAX isn't defined, assume it's a 32-bit type. - This should be valid for all systems GNU cares about because - that doesn't include 16-bit systems, and only modern systems - (that certainly have ) have 64+-bit integral types. */ - -# ifndef UINT_MAX -# define UINT_MAX UINT_MAX_32_BITS -# endif - -# if UINT_MAX == UINT_MAX_32_BITS - typedef unsigned int md5_uint32; -# else -# if USHRT_MAX == UINT_MAX_32_BITS - typedef unsigned short md5_uint32; -# else -# if ULONG_MAX == UINT_MAX_32_BITS - typedef unsigned long md5_uint32; -# else - /* The following line is intended to evoke an error. - Using #error is not portable enough. */ - "Cannot determine unsigned 32-bit data type." -# endif -# endif -# endif -/* We have to make a guess about the integer type equivalent in size - to pointers which should always be correct. */ -typedef unsigned long int md5_uintptr; -#endif - -#undef __P -#if defined (__STDC__) && __STDC__ -# define __P(x) x -#else -# define __P(x) () -#endif - -/* Structure to save state of computation between the single steps. */ -struct md5_ctx -{ - md5_uint32 A; - md5_uint32 B; - md5_uint32 C; - md5_uint32 D; - - md5_uint32 total[2]; - md5_uint32 buflen; - char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32)))); -}; - -/* - * The following three functions are build up the low level used in - * the functions `md5_stream' and `md5_buffer'. - */ - -/* Initialize structure containing state of computation. - (RFC 1321, 3.3: Step 3) */ -extern void md5_init_ctx __P ((struct md5_ctx *ctx)); - -/* Starting with the result of former calls of this function (or the - initialization function update the context for the next LEN bytes - starting at BUFFER. - It is necessary that LEN is a multiple of 64!!! */ -extern void md5_process_block __P ((const void *buffer, size_t len, - struct md5_ctx *ctx)); - -/* Starting with the result of former calls of this function (or the - initialization function update the context for the next LEN bytes - starting at BUFFER. - It is NOT required that LEN is a multiple of 64. */ -extern void md5_process_bytes __P ((const void *buffer, size_t len, - struct md5_ctx *ctx)); - -/* Process the remaining bytes in the buffer and put result from CTX - in first 16 bytes following RESBUF. The result is always in little - endian byte order, so that a byte-wise output yields to the wanted - ASCII representation of the message digest. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -extern void *md5_finish_ctx __P ((struct md5_ctx *ctx, void *resbuf)); - - -/* Put result from CTX in first 16 bytes following RESBUF. The result is - always in little endian byte order, so that a byte-wise output yields - to the wanted ASCII representation of the message digest. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -extern void *md5_read_ctx __P ((const struct md5_ctx *ctx, void *resbuf)); - - -/* Compute MD5 message digest for bytes read from STREAM. The - resulting message digest number will be written into the 16 bytes - beginning at RESBLOCK. */ -extern int md5_stream __P ((FILE *stream, void *resblock)); - -/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The - result is always in little endian byte order, so that a byte-wise - output yields to the wanted ASCII representation of the message - digest. */ -extern void *md5_buffer __P ((const char *buffer, size_t len, - void *resblock)); - -#endif /* md5.h */ diff --git a/src/libnix/test-aterm.cc b/src/libnix/test-aterm.cc deleted file mode 100644 index 325639ca4f..0000000000 --- a/src/libnix/test-aterm.cc +++ /dev/null @@ -1,66 +0,0 @@ -#include "aterm.hh" -#include - - -void runTests() -{ - verbosity = lvlDebug; - - ATMatcher pos; - - ATerm t = ATmake("Call(Foo, Bar, \"xyz\")"); - - debug(format("term: %1%") % t); - - string fun, arg3; - ATerm lhs, rhs; - - if (!(atMatch(pos, t) >> "Call" >> lhs >> rhs >> arg3)) - throw Error("should succeed"); - if (arg3 != "xyz") throw Error("bad 1"); - - if (!(atMatch(pos, t) >> fun >> lhs >> rhs >> arg3)) - throw Error("should succeed"); - if (fun != "Call") throw Error("bad 2"); - if (arg3 != "xyz") throw Error("bad 3"); - - if (!(atMatch(pos, t) >> fun >> lhs >> rhs >> "xyz")) - throw Error("should succeed"); - - if (atMatch(pos, t) >> fun >> lhs >> rhs >> "abc") - throw Error("should fail"); - - if (atMatch(pos, t) >> "Call" >> lhs >> rhs >> "abc") - throw Error("should fail"); - - t = ATmake("X([A, B, C], \"abc\")"); - - ATerm t1, t2, t3; - if (atMatch(pos, t) >> "X" >> t1 >> t2 >> t3) - throw Error("should fail"); - if (!(atMatch(pos, t) >> "X" >> t1 >> t2)) - throw Error("should succeed"); - ATermList ts; - if (!(atMatch(pos, t) >> "X" >> ts >> t2)) - throw Error("should succeed"); - if (ATgetLength(ts) != 3) - throw Error("bad"); - if (atMatch(pos, t) >> "X" >> t1 >> ts) - throw Error("should fail"); -} - - -int main(int argc, char * * argv) -{ - ATerm bottomOfStack; - ATinit(argc, argv, &bottomOfStack); - - try { - runTests(); - } catch (Error & e) { - printMsg(lvlError, format("error: %1%") % e.msg()); - return 1; - } - - return 0; -} diff --git a/src/libnix/util.cc b/src/libnix/util.cc deleted file mode 100644 index 299a37f6c8..0000000000 --- a/src/libnix/util.cc +++ /dev/null @@ -1,318 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include - -#include "util.hh" - - -string thisSystem = SYSTEM; - - -Error::Error(const format & f) -{ - err = f.str(); -} - - -SysError::SysError(const format & f) - : Error(format("%1%: %2%") % f.str() % strerror(errno)) -{ -} - - -Path absPath(Path path, Path dir) -{ - if (path[0] != '/') { - if (dir == "") { - char buf[PATH_MAX]; - if (!getcwd(buf, sizeof(buf))) - throw SysError("cannot get cwd"); - dir = buf; - } - path = dir + "/" + path; - } - return canonPath(path); -} - - -Path canonPath(const Path & path) -{ - string s; - - if (path[0] != '/') - throw Error(format("not an absolute path: `%1%'") % path); - - string::const_iterator i = path.begin(), end = path.end(); - - while (1) { - - /* Skip slashes. */ - while (i != end && *i == '/') i++; - if (i == end) break; - - /* Ignore `.'. */ - if (*i == '.' && (i + 1 == end || i[1] == '/')) - i++; - - /* If `..', delete the last component. */ - else if (*i == '.' && i + 1 < end && i[1] == '.' && - (i + 2 == end || i[2] == '/')) - { - if (!s.empty()) s.erase(s.rfind('/')); - i += 2; - } - - /* Normal component; copy it. */ - else { - s += '/'; - while (i != end && *i != '/') s += *i++; - } - } - - return s.empty() ? "/" : s; -} - - -Path dirOf(const Path & path) -{ - unsigned int pos = path.rfind('/'); - if (pos == string::npos) - throw Error(format("invalid file name: %1%") % path); - return Path(path, 0, pos); -} - - -string baseNameOf(const Path & path) -{ - unsigned int pos = path.rfind('/'); - if (pos == string::npos) - throw Error(format("invalid file name %1% ") % path); - return string(path, pos + 1); -} - - -bool pathExists(const Path & path) -{ - int res; - struct stat st; - res = stat(path.c_str(), &st); - if (!res) return true; - if (errno != ENOENT) - throw SysError(format("getting status of %1%") % path); - return false; -} - - -void deletePath(const Path & path) -{ - printMsg(lvlVomit, format("deleting path `%1%'") % path); - - struct stat st; - if (lstat(path.c_str(), &st)) - throw SysError(format("getting attributes of path `%1%'") % path); - - if (S_ISDIR(st.st_mode)) { - Strings names; - - { - AutoCloseDir dir = opendir(path.c_str()); - - struct dirent * dirent; - while (errno = 0, dirent = readdir(dir)) { - string name = dirent->d_name; - if (name == "." || name == "..") continue; - names.push_back(name); - } - } /* scoped to ensure that dir is closed at this point */ - - /* Make the directory writable. */ - if (!(st.st_mode & S_IWUSR)) { - if (chmod(path.c_str(), st.st_mode | S_IWUSR) == -1) - throw SysError(format("making `%1%' writable")); - } - - for (Strings::iterator i = names.begin(); i != names.end(); i++) - deletePath(path + "/" + *i); - } - - if (remove(path.c_str()) == -1) - throw SysError(format("cannot unlink `%1%'") % path); -} - - -void makePathReadOnly(const Path & path) -{ - struct stat st; - if (lstat(path.c_str(), &st)) - throw SysError(format("getting attributes of path `%1%'") % path); - - if (!S_ISLNK(st.st_mode) && (st.st_mode & S_IWUSR)) { - if (chmod(path.c_str(), st.st_mode & ~S_IWUSR) == -1) - throw SysError(format("making `%1%' read-only") % path); - } - - if (S_ISDIR(st.st_mode)) { - AutoCloseDir dir = opendir(path.c_str()); - - struct dirent * dirent; - while (errno = 0, dirent = readdir(dir)) { - string name = dirent->d_name; - if (name == "." || name == "..") continue; - makePathReadOnly(path + "/" + name); - } - } -} - - -static Path tempName() -{ - static int counter = 0; - char * s = getenv("TMPDIR"); - Path tmpRoot = s ? canonPath(Path(s)) : "/tmp"; - return (format("%1%/nix-%2%-%3%") % tmpRoot % getpid() % counter++).str(); -} - - -Path createTempDir() -{ - while (1) { - Path tmpDir = tempName(); - if (mkdir(tmpDir.c_str(), 0777) == 0) return tmpDir; - if (errno != EEXIST) - throw SysError(format("creating directory `%1%'") % tmpDir); - } -} - - -Verbosity verbosity = lvlError; - -static int nestingLevel = 0; - - -Nest::Nest() -{ - nest = false; -} - - -Nest::~Nest() -{ - if (nest) nestingLevel--; -} - - -void Nest::open(Verbosity level, const format & f) -{ - if (level <= verbosity) { - printMsg_(level, f); - nest = true; - nestingLevel++; - } -} - - -void printMsg_(Verbosity level, const format & f) -{ - if (level > verbosity) return; - string spaces; - for (int i = 0; i < nestingLevel; i++) - spaces += "| "; - cerr << format("%1%%2%\n") % spaces % f.str(); -} - - -void readFull(int fd, unsigned char * buf, size_t count) -{ - while (count) { - ssize_t res = read(fd, (char *) buf, count); - if (res == -1) throw SysError("reading from file"); - if (res == 0) throw Error("unexpected end-of-file"); - count -= res; - buf += res; - } -} - - -void writeFull(int fd, const unsigned char * buf, size_t count) -{ - while (count) { - ssize_t res = write(fd, (char *) buf, count); - if (res == -1) throw SysError("writing to file"); - count -= res; - buf += res; - } -} - - -AutoDelete::AutoDelete(const string & p) : path(p) -{ - del = true; -} - -AutoDelete::~AutoDelete() -{ - if (del) deletePath(path); -} - -void AutoDelete::cancel() -{ - del = false; -} - - -AutoCloseFD::AutoCloseFD() -{ - fd = -1; -} - -AutoCloseFD::AutoCloseFD(int fd) -{ - this->fd = fd; -} - -AutoCloseFD::~AutoCloseFD() -{ - if (fd != -1) close(fd); -} - -void AutoCloseFD::operator =(int fd) -{ - this->fd = fd; -} - -AutoCloseFD::operator int() -{ - return fd; -} - - -AutoCloseDir::AutoCloseDir() -{ - dir = 0; -} - -AutoCloseDir::AutoCloseDir(DIR * dir) -{ - this->dir = dir; -} - -AutoCloseDir::~AutoCloseDir() -{ - if (dir) closedir(dir); -} - -void AutoCloseDir::operator =(DIR * dir) -{ - this->dir = dir; -} - -AutoCloseDir::operator DIR *() -{ - return dir; -} - diff --git a/src/libnix/util.hh b/src/libnix/util.hh deleted file mode 100644 index e6b600eff8..0000000000 --- a/src/libnix/util.hh +++ /dev/null @@ -1,168 +0,0 @@ -#ifndef __UTIL_H -#define __UTIL_H - -#include -#include -#include -#include - -#include -#include -#include - -#include - -using namespace std; -using namespace boost; - - -class Error : public exception -{ -protected: - string err; -public: - Error(const format & f); - ~Error() throw () { }; - const char * what() const throw () { return err.c_str(); } - const string & msg() const throw () { return err; } -}; - -class SysError : public Error -{ -public: - SysError(const format & f); -}; - -class UsageError : public Error -{ -public: - UsageError(const format & f) : Error(f) { }; -}; - - -typedef list Strings; -typedef set StringSet; - - -/* Paths are just strings. */ -typedef string Path; -typedef list Paths; -typedef set PathSet; - - -/* The canonical system name, as returned by config.guess. */ -extern string thisSystem; - - -/* Return an absolutized path, resolving paths relative to the - specified directory, or the current directory otherwise. The path - is also canonicalised. */ -Path absPath(Path path, Path dir = ""); - -/* Canonicalise a path (as in realpath(3)). */ -Path canonPath(const Path & path); - -/* Return the directory part of the given path, i.e., everything - before the final `/'. */ -Path dirOf(const Path & path); - -/* Return the base name of the given path, i.e., everything following - the final `/'. */ -string baseNameOf(const Path & path); - -/* Return true iff the given path exists. */ -bool pathExists(const Path & path); - -/* Delete a path; i.e., in the case of a directory, it is deleted - recursively. Don't use this at home, kids. */ -void deletePath(const Path & path); - -/* Make a path read-only recursively. */ -void makePathReadOnly(const Path & path); - -/* Create a temporary directory. */ -Path createTempDir(); - - -/* Messages. */ - -typedef enum { - lvlError, - lvlTalkative, - lvlChatty, - lvlDebug, - lvlVomit -} Verbosity; - -extern Verbosity verbosity; /* supress msgs > this */ - -class Nest -{ -private: - bool nest; -public: - Nest(); - ~Nest(); - void open(Verbosity level, const format & f); -}; - -void printMsg_(Verbosity level, const format & f); - -#define startNest(varName, level, f) \ - Nest varName; \ - if (level <= verbosity) { \ - varName.open(level, (f)); \ - } - -#define printMsg(level, f) \ - do { \ - if (level <= verbosity) { \ - printMsg_(level, (f)); \ - } \ - } while (0) - -#define debug(f) printMsg(lvlDebug, f) - - -/* Wrappers arount read()/write() that read/write exactly the - requested number of bytes. */ -void readFull(int fd, unsigned char * buf, size_t count); -void writeFull(int fd, const unsigned char * buf, size_t count); - - -/* Automatic cleanup of resources. */ - -class AutoDelete -{ - string path; - bool del; -public: - AutoDelete(const string & p); - ~AutoDelete(); - void cancel(); -}; - -class AutoCloseFD -{ - int fd; -public: - AutoCloseFD(); - AutoCloseFD(int fd); - ~AutoCloseFD(); - void operator =(int fd); - operator int(); -}; - -class AutoCloseDir -{ - DIR * dir; -public: - AutoCloseDir(); - AutoCloseDir(DIR * dir); - ~AutoCloseDir(); - void operator =(DIR * dir); - operator DIR *(); -}; - - -#endif /* !__UTIL_H */ diff --git a/src/libutil/Makefile.am b/src/libutil/Makefile.am new file mode 100644 index 0000000000..46f0a048fe --- /dev/null +++ b/src/libutil/Makefile.am @@ -0,0 +1,14 @@ +noinst_LIBRARIES = libutil.a + +libutil_a_SOURCES = util.cc hash.cc archive.cc md5.c aterm.cc + +AM_CXXFLAGS = -DSYSTEM=\"@host@\" -Wall -I.. -I../../externals/inst/include + +EXTRA_DIST = *.hh *.h + +check_PROGRAMS = test-aterm + +test_aterm_SOURCES = test-aterm.cc +test_aterm_LDADD = libnix.a $(LDADD) ../boost/format/libformat.a \ + -L../../externals/inst/lib -ldb_cxx -lATerm + diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc new file mode 100644 index 0000000000..ed57df4c9f --- /dev/null +++ b/src/libutil/archive.cc @@ -0,0 +1,334 @@ +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "archive.hh" +#include "util.hh" + + +static string archiveVersion1 = "nix-archive-1"; + + +static void writePadding(unsigned int len, DumpSink & sink) +{ + if (len % 8) { + unsigned char zero[8]; + memset(zero, 0, sizeof(zero)); + sink(zero, 8 - (len % 8)); + } +} + + +static void writeInt(unsigned int n, DumpSink & sink) +{ + unsigned char buf[8]; + memset(buf, 0, sizeof(buf)); + buf[0] = n & 0xff; + buf[1] = (n >> 8) & 0xff; + buf[2] = (n >> 16) & 0xff; + buf[3] = (n >> 24) & 0xff; + sink(buf, sizeof(buf)); +} + + +static void writeString(const string & s, DumpSink & sink) +{ + unsigned int len = s.length(); + writeInt(len, sink); + sink((const unsigned char *) s.c_str(), len); + writePadding(len, sink); +} + + +static void dump(const string & path, DumpSink & sink); + + +static void dumpEntries(const Path & path, DumpSink & sink) +{ + AutoCloseDir dir = opendir(path.c_str()); + if (!dir) throw SysError("opening directory " + path); + + vector names; + + struct dirent * dirent; + while (errno = 0, dirent = readdir(dir)) { + string name = dirent->d_name; + if (name == "." || name == "..") continue; + names.push_back(name); + } + if (errno) throw SysError("reading directory " + path); + + sort(names.begin(), names.end()); + + for (vector::iterator it = names.begin(); + it != names.end(); it++) + { + writeString("entry", sink); + writeString("(", sink); + writeString("name", sink); + writeString(*it, sink); + writeString("node", sink); + dump(path + "/" + *it, sink); + writeString(")", sink); + } +} + + +static void dumpContents(const Path & path, unsigned int size, + DumpSink & sink) +{ + writeString("contents", sink); + writeInt(size, sink); + + AutoCloseFD fd = open(path.c_str(), O_RDONLY); + if (fd == -1) throw SysError(format("opening file `%1%'") % path); + + unsigned char buf[65536]; + + unsigned int total = 0; + ssize_t n; + while ((n = read(fd, buf, sizeof(buf)))) { + if (n == -1) throw SysError("reading file " + path); + total += n; + sink(buf, n); + } + + if (total != size) + throw SysError("file changed while reading it: " + path); + + writePadding(size, sink); +} + + +static void dump(const Path & path, DumpSink & sink) +{ + struct stat st; + if (lstat(path.c_str(), &st)) + throw SysError(format("getting attributes of path `%1%'") % path); + + writeString("(", sink); + + if (S_ISREG(st.st_mode)) { + writeString("type", sink); + writeString("regular", sink); + if (st.st_mode & S_IXUSR) { + writeString("executable", sink); + writeString("", sink); + } + dumpContents(path, st.st_size, sink); + } + + else if (S_ISDIR(st.st_mode)) { + writeString("type", sink); + writeString("directory", sink); + dumpEntries(path, sink); + } + + else if (S_ISLNK(st.st_mode)) { + writeString("type", sink); + writeString("symlink", sink); + char buf[st.st_size]; + if (readlink(path.c_str(), buf, st.st_size) != st.st_size) + throw SysError("reading symbolic link " + path); + writeString("target", sink); + writeString(string(buf, st.st_size), sink); + } + + else throw Error("unknown file type: " + path); + + writeString(")", sink); +} + + +void dumpPath(const Path & 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((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 Path & path, RestoreSource & source); + + +static void restoreEntry(const Path & 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 Path & 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 Path & path, RestoreSource & source) +{ + string s; + + s = readString(source); + if (s != "(") throw badArchive("expected open tag"); + + enum { tpUnknown, tpRegular, tpDirectory, tpSymlink } type = tpUnknown; + AutoCloseFD fd; + + 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 == "executable" && type == tpRegular) { + readString(source); + struct stat st; + if (fstat(fd, &st) == -1) + throw SysError("fstat"); + if (fchmod(fd, st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1) + throw SysError("fchmod"); + } + + 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); + } + + } +} + + +void restorePath(const Path & path, RestoreSource & source) +{ + if (readString(source) != archiveVersion1) + throw badArchive("expected Nix archive"); + restore(path, source); +} + diff --git a/src/libutil/archive.hh b/src/libutil/archive.hh new file mode 100644 index 0000000000..67e236668a --- /dev/null +++ b/src/libutil/archive.hh @@ -0,0 +1,60 @@ +#include + +#include "util.hh" + + +/* dumpPath creates a Nix archive of the specified path. The format + is as follows: + + IF path points to a REGULAR FILE: + dump(path) = attrs( + [ ("type", "regular") + , ("contents", contents(path)) + ]) + + IF path points to a DIRECTORY: + dump(path) = attrs( + [ ("type", "directory") + , ("entries", concat(map(f, sort(entries(path))))) + ]) + where f(fn) = attrs( + [ ("name", fn) + , ("file", dump(path + "/" + fn)) + ]) + + where: + + attrs(as) = concat(map(attr, as)) + encN(0) + attrs((a, b)) = encS(a) + encS(b) + + encS(s) = encN(len(s)) + s + (padding until next 64-bit boundary) + + encN(n) = 64-bit little-endian encoding of n. + + contents(path) = the contents of a regular file. + + sort(strings) = lexicographic sort by 8-bit value (strcmp). + + entries(path) = the entries of a directory, without `.' and + `..'. + + `+' denotes string concatenation. */ + +struct DumpSink +{ + virtual void operator () (const unsigned char * data, unsigned int len) = 0; +}; + +void dumpPath(const Path & path, DumpSink & sink); + + +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 + yet available, or throw an error if it is not going to be + available. */ + virtual void operator () (unsigned char * data, unsigned int len) = 0; +}; + +void restorePath(const Path & path, RestoreSource & source); diff --git a/src/libutil/aterm.cc b/src/libutil/aterm.cc new file mode 100644 index 0000000000..de7c359521 --- /dev/null +++ b/src/libutil/aterm.cc @@ -0,0 +1,93 @@ +#include "aterm.hh" + + +string atPrint(ATerm t) +{ + if (!t) throw Error("attempt to print null aterm"); + char * s = ATwriteToString(t); + if (!s) throw Error("cannot print term"); + return s; +} + + +ostream & operator << (ostream & stream, ATerm e) +{ + return stream << atPrint(e); +} + + +ATMatcher & atMatch(ATMatcher & pos, ATerm t) +{ + pos.t = t; + pos.pos = ATMatcher::funPos; + return pos; +} + + +static inline bool failed(const ATMatcher & pos) +{ + return pos.pos == ATMatcher::failPos; +} + + +static inline ATMatcher & fail(ATMatcher & pos) +{ + pos.pos = ATMatcher::failPos; + return pos; +} + + +ATMatcher & operator >> (ATMatcher & pos, ATerm & out) +{ + out = 0; + if (failed(pos)) return pos; + if (pos.pos == ATMatcher::funPos || + ATgetType(pos.t) != AT_APPL || + pos.pos >= (int) ATgetArity(ATgetAFun(pos.t))) + return fail(pos); + out = ATgetArgument(pos.t, pos.pos); + pos.pos++; + return pos; +} + + +ATMatcher & operator >> (ATMatcher & pos, string & out) +{ + out = ""; + if (pos.pos == ATMatcher::funPos) { + if (ATgetType(pos.t) != AT_APPL) return fail(pos); + out = ATgetName(ATgetAFun(pos.t)); + pos.pos = 0; + } else { + ATerm t; + pos = pos >> t; + if (failed(pos)) return pos; + if (ATgetType(t) != AT_APPL || + ATgetArity(ATgetAFun(t)) != 0) + return fail(pos); + out = ATgetName(ATgetAFun(t)); + } + return pos; +} + + +ATMatcher & operator >> (ATMatcher & pos, const string & s) +{ + string s2; + pos = pos >> s2; + if (failed(pos)) return pos; + if (s != s2) return fail(pos); + return pos; +} + + +ATMatcher & operator >> (ATMatcher & pos, ATermList & out) +{ + out = 0; + ATerm t; + pos = pos >> t; + if (failed(pos)) return pos; + if (ATgetType(t) != AT_LIST) return fail(pos); + out = (ATermList) t; + return pos; +} diff --git a/src/libutil/aterm.hh b/src/libutil/aterm.hh new file mode 100644 index 0000000000..16d8d6bb6d --- /dev/null +++ b/src/libutil/aterm.hh @@ -0,0 +1,77 @@ +#ifndef __ATERM_H +#define __ATERM_H + +extern "C" { +#include +} + +#include "util.hh" + + +/* Print an ATerm. */ +string atPrint(ATerm t); + +/* Write an ATerm to an output stream. */ +ostream & operator << (ostream & stream, ATerm e); + +class ATermIterator +{ + ATermList t; + +public: + ATermIterator(ATermList _t) : t(_t) { } + ATermIterator & operator ++ () + { + t = ATgetNext(t); + return *this; + } + ATerm operator * () + { + return ATgetFirst(t); + } + operator bool () + { + return t != ATempty; + } +}; + + +/* Type-safe matching. */ + +struct ATMatcher +{ + ATerm t; + int pos; + const static int failPos = -2; + const static int funPos = -1; + + ATMatcher() : t(0), pos(failPos) + { + } + + operator bool() const + { + return pos != failPos; + } +}; + +/* Initiate matching of a term. */ +ATMatcher & atMatch(ATMatcher & pos, ATerm t); + +/* Get the next argument of an application. */ +ATMatcher & operator >> (ATMatcher & pos, ATerm & out); + +/* Get the name of the function symbol of an applicatin, or the next + argument of an application as a string. */ +ATMatcher & operator >> (ATMatcher & pos, string & out); + +/* Like the previous, but check that the string is equal to the given + string. */ +ATMatcher & operator >> (ATMatcher & pos, const string & s); + +/* Get the next argument of an application, and verify that it is a + list. */ +ATMatcher & operator >> (ATMatcher & pos, ATermList & out); + + +#endif /* !__ATERM_H */ diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc new file mode 100644 index 0000000000..752b269125 --- /dev/null +++ b/src/libutil/hash.cc @@ -0,0 +1,124 @@ +#include + +extern "C" { +#include "md5.h" +} + +#include "hash.hh" +#include "archive.hh" + + +Hash::Hash() +{ + memset(hash, 0, sizeof(hash)); +} + + +bool Hash::operator == (const Hash & h2) const +{ + for (unsigned int i = 0; i < hashSize; i++) + if (hash[i] != h2.hash[i]) return false; + return true; +} + + +bool Hash::operator != (const Hash & h2) const +{ + return !(*this == h2); +} + + +bool Hash::operator < (const Hash & h) const +{ + for (unsigned int i = 0; i < hashSize; i++) { + if (hash[i] < h.hash[i]) return true; + if (hash[i] > h.hash[i]) return false; + } + return false; +} + + +Hash::operator string() const +{ + ostringstream str; + for (unsigned int i = 0; i < hashSize; i++) { + str.fill('0'); + str.width(2); + str << hex << (int) hash[i]; + } + return str.str(); +} + + +Hash parseHash(const string & s) +{ + Hash hash; + if (s.length() != Hash::hashSize * 2) + throw Error(format("invalid hash `%1%'") % s); + for (unsigned int i = 0; i < Hash::hashSize; i++) { + string s2(s, i * 2, 2); + if (!isxdigit(s2[0]) || !isxdigit(s2[1])) + throw Error(format("invalid hash `%1%'") % s); + istringstream str(s2); + int n; + str >> hex >> n; + hash.hash[i] = n; + } + return hash; +} + + +bool isHash(const string & s) +{ + if (s.length() != 32) return false; + for (int i = 0; i < 32; i++) { + char c = s[i]; + if (!((c >= '0' && c <= '9') || + (c >= 'a' && c <= 'f'))) + return false; + } + return true; +} + + +Hash hashString(const string & s) +{ + Hash hash; + md5_buffer(s.c_str(), s.length(), hash.hash); + return hash; +} + + +Hash hashFile(const Path & path) +{ + Hash hash; + FILE * file = fopen(path.c_str(), "rb"); + if (!file) + throw SysError(format("file `%1%' does not exist") % path); + int err = md5_stream(file, hash.hash); + fclose(file); + if (err) throw SysError(format("cannot hash file `%1%'") % path); + return hash; +} + + +struct HashSink : DumpSink +{ + struct md5_ctx ctx; + virtual void operator () + (const unsigned char * data, unsigned int len) + { + md5_process_bytes(data, len, &ctx); + } +}; + + +Hash hashPath(const Path & path) +{ + Hash hash; + HashSink sink; + md5_init_ctx(&sink.ctx); + dumpPath(path, sink); + md5_finish_ctx(&sink.ctx, hash.hash); + return hash; +} diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh new file mode 100644 index 0000000000..0062f987c0 --- /dev/null +++ b/src/libutil/hash.hh @@ -0,0 +1,51 @@ +#ifndef __HASH_H +#define __HASH_H + +#include + +#include "util.hh" + +using namespace std; + + +struct Hash +{ + static const unsigned int hashSize = 16; + unsigned char hash[hashSize]; + + /* Create a zeroed hash object. */ + Hash(); + + /* Check whether two hash are equal. */ + bool operator == (const Hash & h2) const; + + /* Check whether two hash are not equal. */ + bool operator != (const Hash & h2) const; + + /* For sorting. */ + bool operator < (const Hash & h) const; + + /* Convert a hash code into a hexadecimal representation. */ + operator string() const; +}; + + +/* Parse a hexadecimal representation of a hash code. */ +Hash parseHash(const string & s); + +/* Verify that the given string is a valid hash code. */ +bool isHash(const string & s); + +/* Compute the hash of the given string. */ +Hash hashString(const string & s); + +/* Compute the hash of the given file. */ +Hash hashFile(const Path & path); + +/* Compute the hash of the given path. The hash is defined as + md5(dump(path)). +*/ +Hash hashPath(const Path & path); + + +#endif /* !__HASH_H */ diff --git a/src/libutil/md5.c b/src/libutil/md5.c new file mode 100644 index 0000000000..fa67ebfcdb --- /dev/null +++ b/src/libutil/md5.c @@ -0,0 +1,448 @@ +/* Functions to compute MD5 message digest of files or memory blocks. + according to the definition of MD5 in RFC 1321 from April 1992. + Copyright (C) 1995,1996,1997,1999,2000,2001 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +/* Written by Ulrich Drepper , 1995. */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +#include +#include + +#include "md5.h" + + +static md5_uint32 SWAP(md5_uint32 n) +{ + static int checked = 0; + static int bigendian = 0; + static md5_uint32 test; + + if (!checked) { + test = 1; + if (* (char *) &test == 0) + bigendian = 1; + checked = 1; + } + + if (bigendian) + return (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)); + else + return n; +} + + +/* This array contains the bytes used to pad the buffer to the next + 64-byte boundary. (RFC 1321, 3.1: Step 1) */ +static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; + + +/* Initialize structure containing state of computation. + (RFC 1321, 3.3: Step 3) */ +void +md5_init_ctx (ctx) + struct md5_ctx *ctx; +{ + ctx->A = 0x67452301; + ctx->B = 0xefcdab89; + ctx->C = 0x98badcfe; + ctx->D = 0x10325476; + + ctx->total[0] = ctx->total[1] = 0; + ctx->buflen = 0; +} + +/* Put result from CTX in first 16 bytes following RESBUF. The result + must be in little endian byte order. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +void * +md5_read_ctx (ctx, resbuf) + const struct md5_ctx *ctx; + void *resbuf; +{ + ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A); + ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B); + ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C); + ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D); + + return resbuf; +} + +/* Process the remaining bytes in the internal buffer and the usual + prolog according to the standard and write the result to RESBUF. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +void * +md5_finish_ctx (ctx, resbuf) + struct md5_ctx *ctx; + void *resbuf; +{ + /* Take yet unprocessed bytes into account. */ + md5_uint32 bytes = ctx->buflen; + size_t pad; + + /* Now count remaining bytes. */ + ctx->total[0] += bytes; + if (ctx->total[0] < bytes) + ++ctx->total[1]; + + pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; + memcpy (&ctx->buffer[bytes], fillbuf, pad); + + /* Put the 64-bit file length in *bits* at the end of the buffer. */ + *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); + *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) | + (ctx->total[0] >> 29)); + + /* Process last bytes. */ + md5_process_block (ctx->buffer, bytes + pad + 8, ctx); + + return md5_read_ctx (ctx, resbuf); +} + +/* Compute MD5 message digest for bytes read from STREAM. The + resulting message digest number will be written into the 16 bytes + beginning at RESBLOCK. */ +int +md5_stream (stream, resblock) + FILE *stream; + void *resblock; +{ + /* Important: BLOCKSIZE must be a multiple of 64. */ +#define BLOCKSIZE 4096 + struct md5_ctx ctx; + char buffer[BLOCKSIZE + 72]; + size_t sum; + + /* Initialize the computation context. */ + md5_init_ctx (&ctx); + + /* Iterate over full file contents. */ + while (1) + { + /* We read the file in blocks of BLOCKSIZE bytes. One call of the + computation function processes the whole buffer so that with the + next round of the loop another block can be read. */ + size_t n; + sum = 0; + + /* Read block. Take care for partial reads. */ + do + { + n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); + + sum += n; + } + while (sum < BLOCKSIZE && n != 0); + if (n == 0 && ferror (stream)) + return 1; + + /* If end of file is reached, end the loop. */ + if (n == 0) + break; + + /* Process buffer with BLOCKSIZE bytes. Note that + BLOCKSIZE % 64 == 0 + */ + md5_process_block (buffer, BLOCKSIZE, &ctx); + } + + /* Add the last bytes if necessary. */ + if (sum > 0) + md5_process_bytes (buffer, sum, &ctx); + + /* Construct result in desired memory. */ + md5_finish_ctx (&ctx, resblock); + return 0; +} + +/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The + result is always in little endian byte order, so that a byte-wise + output yields to the wanted ASCII representation of the message + digest. */ +void * +md5_buffer (buffer, len, resblock) + const char *buffer; + size_t len; + void *resblock; +{ + struct md5_ctx ctx; + + /* Initialize the computation context. */ + md5_init_ctx (&ctx); + + /* Process whole buffer but last len % 64 bytes. */ + md5_process_bytes (buffer, len, &ctx); + + /* Put result in desired memory area. */ + return md5_finish_ctx (&ctx, resblock); +} + + +void +md5_process_bytes (buffer, len, ctx) + const void *buffer; + size_t len; + struct md5_ctx *ctx; +{ + /* When we already have some bits in our internal buffer concatenate + both inputs first. */ + if (ctx->buflen != 0) + { + size_t left_over = ctx->buflen; + size_t add = 128 - left_over > len ? len : 128 - left_over; + + memcpy (&ctx->buffer[left_over], buffer, add); + ctx->buflen += add; + + if (ctx->buflen > 64) + { + md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx); + + ctx->buflen &= 63; + /* The regions in the following copy operation cannot overlap. */ + memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], + ctx->buflen); + } + + buffer = (const char *) buffer + add; + len -= add; + } + + /* Process available complete blocks. */ + if (len >= 64) + { +#if !_STRING_ARCH_unaligned +/* To check alignment gcc has an appropriate operator. Other + compilers don't. */ +# if __GNUC__ >= 2 +# define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0) +# else +# define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0) +# endif + if (UNALIGNED_P (buffer)) + while (len > 64) + { + md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx); + buffer = (const char *) buffer + 64; + len -= 64; + } + else +#endif + { + md5_process_block (buffer, len & ~63, ctx); + buffer = (const char *) buffer + (len & ~63); + len &= 63; + } + } + + /* Move remaining bytes in internal buffer. */ + if (len > 0) + { + size_t left_over = ctx->buflen; + + memcpy (&ctx->buffer[left_over], buffer, len); + left_over += len; + if (left_over >= 64) + { + md5_process_block (ctx->buffer, 64, ctx); + left_over -= 64; + memcpy (ctx->buffer, &ctx->buffer[64], left_over); + } + ctx->buflen = left_over; + } +} + + +/* These are the four functions used in the four steps of the MD5 algorithm + and defined in the RFC 1321. The first function is a little bit optimized + (as found in Colin Plumbs public domain implementation). */ +/* #define FF(b, c, d) ((b & c) | (~b & d)) */ +#define FF(b, c, d) (d ^ (b & (c ^ d))) +#define FG(b, c, d) FF (d, b, c) +#define FH(b, c, d) (b ^ c ^ d) +#define FI(b, c, d) (c ^ (b | ~d)) + +/* Process LEN bytes of BUFFER, accumulating context into CTX. + It is assumed that LEN % 64 == 0. */ + +void +md5_process_block (buffer, len, ctx) + const void *buffer; + size_t len; + struct md5_ctx *ctx; +{ + md5_uint32 correct_words[16]; + const md5_uint32 *words = buffer; + size_t nwords = len / sizeof (md5_uint32); + const md5_uint32 *endp = words + nwords; + md5_uint32 A = ctx->A; + md5_uint32 B = ctx->B; + md5_uint32 C = ctx->C; + md5_uint32 D = ctx->D; + + /* First increment the byte count. RFC 1321 specifies the possible + length of the file up to 2^64 bits. Here we only compute the + number of bytes. Do a double word increment. */ + ctx->total[0] += len; + if (ctx->total[0] < len) + ++ctx->total[1]; + + /* Process all bytes in the buffer with 64 bytes in each round of + the loop. */ + while (words < endp) + { + md5_uint32 *cwp = correct_words; + md5_uint32 A_save = A; + md5_uint32 B_save = B; + md5_uint32 C_save = C; + md5_uint32 D_save = D; + + /* First round: using the given function, the context and a constant + the next context is computed. Because the algorithms processing + unit is a 32-bit word and it is determined to work on words in + little endian byte order we perhaps have to change the byte order + before the computation. To reduce the work for the next steps + we store the swapped words in the array CORRECT_WORDS. */ + +#define OP(a, b, c, d, s, T) \ + do \ + { \ + a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \ + ++words; \ + CYCLIC (a, s); \ + a += b; \ + } \ + while (0) + + /* It is unfortunate that C does not provide an operator for + cyclic rotation. Hope the C compiler is smart enough. */ +#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s))) + + /* Before we start, one word to the strange constants. + They are defined in RFC 1321 as + + T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 + */ + + /* Round 1. */ + OP (A, B, C, D, 7, 0xd76aa478); + OP (D, A, B, C, 12, 0xe8c7b756); + OP (C, D, A, B, 17, 0x242070db); + OP (B, C, D, A, 22, 0xc1bdceee); + OP (A, B, C, D, 7, 0xf57c0faf); + OP (D, A, B, C, 12, 0x4787c62a); + OP (C, D, A, B, 17, 0xa8304613); + OP (B, C, D, A, 22, 0xfd469501); + OP (A, B, C, D, 7, 0x698098d8); + OP (D, A, B, C, 12, 0x8b44f7af); + OP (C, D, A, B, 17, 0xffff5bb1); + OP (B, C, D, A, 22, 0x895cd7be); + OP (A, B, C, D, 7, 0x6b901122); + OP (D, A, B, C, 12, 0xfd987193); + OP (C, D, A, B, 17, 0xa679438e); + OP (B, C, D, A, 22, 0x49b40821); + + /* For the second to fourth round we have the possibly swapped words + in CORRECT_WORDS. Redefine the macro to take an additional first + argument specifying the function to use. */ +#undef OP +#define OP(f, a, b, c, d, k, s, T) \ + do \ + { \ + a += f (b, c, d) + correct_words[k] + T; \ + CYCLIC (a, s); \ + a += b; \ + } \ + while (0) + + /* Round 2. */ + OP (FG, A, B, C, D, 1, 5, 0xf61e2562); + OP (FG, D, A, B, C, 6, 9, 0xc040b340); + OP (FG, C, D, A, B, 11, 14, 0x265e5a51); + OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa); + OP (FG, A, B, C, D, 5, 5, 0xd62f105d); + OP (FG, D, A, B, C, 10, 9, 0x02441453); + OP (FG, C, D, A, B, 15, 14, 0xd8a1e681); + OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8); + OP (FG, A, B, C, D, 9, 5, 0x21e1cde6); + OP (FG, D, A, B, C, 14, 9, 0xc33707d6); + OP (FG, C, D, A, B, 3, 14, 0xf4d50d87); + OP (FG, B, C, D, A, 8, 20, 0x455a14ed); + OP (FG, A, B, C, D, 13, 5, 0xa9e3e905); + OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8); + OP (FG, C, D, A, B, 7, 14, 0x676f02d9); + OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a); + + /* Round 3. */ + OP (FH, A, B, C, D, 5, 4, 0xfffa3942); + OP (FH, D, A, B, C, 8, 11, 0x8771f681); + OP (FH, C, D, A, B, 11, 16, 0x6d9d6122); + OP (FH, B, C, D, A, 14, 23, 0xfde5380c); + OP (FH, A, B, C, D, 1, 4, 0xa4beea44); + OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9); + OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60); + OP (FH, B, C, D, A, 10, 23, 0xbebfbc70); + OP (FH, A, B, C, D, 13, 4, 0x289b7ec6); + OP (FH, D, A, B, C, 0, 11, 0xeaa127fa); + OP (FH, C, D, A, B, 3, 16, 0xd4ef3085); + OP (FH, B, C, D, A, 6, 23, 0x04881d05); + OP (FH, A, B, C, D, 9, 4, 0xd9d4d039); + OP (FH, D, A, B, C, 12, 11, 0xe6db99e5); + OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8); + OP (FH, B, C, D, A, 2, 23, 0xc4ac5665); + + /* Round 4. */ + OP (FI, A, B, C, D, 0, 6, 0xf4292244); + OP (FI, D, A, B, C, 7, 10, 0x432aff97); + OP (FI, C, D, A, B, 14, 15, 0xab9423a7); + OP (FI, B, C, D, A, 5, 21, 0xfc93a039); + OP (FI, A, B, C, D, 12, 6, 0x655b59c3); + OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92); + OP (FI, C, D, A, B, 10, 15, 0xffeff47d); + OP (FI, B, C, D, A, 1, 21, 0x85845dd1); + OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f); + OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0); + OP (FI, C, D, A, B, 6, 15, 0xa3014314); + OP (FI, B, C, D, A, 13, 21, 0x4e0811a1); + OP (FI, A, B, C, D, 4, 6, 0xf7537e82); + OP (FI, D, A, B, C, 11, 10, 0xbd3af235); + OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb); + OP (FI, B, C, D, A, 9, 21, 0xeb86d391); + + /* Add the starting values of the context. */ + A += A_save; + B += B_save; + C += C_save; + D += D_save; + } + + /* Put checksum in context given as argument. */ + ctx->A = A; + ctx->B = B; + ctx->C = C; + ctx->D = D; +} diff --git a/src/libutil/md5.h b/src/libutil/md5.h new file mode 100644 index 0000000000..6301e4558c --- /dev/null +++ b/src/libutil/md5.h @@ -0,0 +1,151 @@ +/* Declaration of functions and data types used for MD5 sum computing + library functions. + Copyright (C) 1995,1996,1997,1999,2000,2001 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#ifndef _MD5_H +#define _MD5_H 1 + +#include + +#if defined HAVE_LIMITS_H || _LIBC +# include +#endif + +/* The following contortions are an attempt to use the C preprocessor + to determine an unsigned integral type that is 32 bits wide. An + alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but + doing that would require that the configure script compile and *run* + the resulting executable. Locally running cross-compiled executables + is usually not possible. */ + +#ifdef _LIBC +# include +typedef uint32_t md5_uint32; +typedef uintptr_t md5_uintptr; +#else +# if defined __STDC__ && __STDC__ +# define UINT_MAX_32_BITS 4294967295U +# else +# define UINT_MAX_32_BITS 0xFFFFFFFF +# endif + +/* If UINT_MAX isn't defined, assume it's a 32-bit type. + This should be valid for all systems GNU cares about because + that doesn't include 16-bit systems, and only modern systems + (that certainly have ) have 64+-bit integral types. */ + +# ifndef UINT_MAX +# define UINT_MAX UINT_MAX_32_BITS +# endif + +# if UINT_MAX == UINT_MAX_32_BITS + typedef unsigned int md5_uint32; +# else +# if USHRT_MAX == UINT_MAX_32_BITS + typedef unsigned short md5_uint32; +# else +# if ULONG_MAX == UINT_MAX_32_BITS + typedef unsigned long md5_uint32; +# else + /* The following line is intended to evoke an error. + Using #error is not portable enough. */ + "Cannot determine unsigned 32-bit data type." +# endif +# endif +# endif +/* We have to make a guess about the integer type equivalent in size + to pointers which should always be correct. */ +typedef unsigned long int md5_uintptr; +#endif + +#undef __P +#if defined (__STDC__) && __STDC__ +# define __P(x) x +#else +# define __P(x) () +#endif + +/* Structure to save state of computation between the single steps. */ +struct md5_ctx +{ + md5_uint32 A; + md5_uint32 B; + md5_uint32 C; + md5_uint32 D; + + md5_uint32 total[2]; + md5_uint32 buflen; + char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32)))); +}; + +/* + * The following three functions are build up the low level used in + * the functions `md5_stream' and `md5_buffer'. + */ + +/* Initialize structure containing state of computation. + (RFC 1321, 3.3: Step 3) */ +extern void md5_init_ctx __P ((struct md5_ctx *ctx)); + +/* Starting with the result of former calls of this function (or the + initialization function update the context for the next LEN bytes + starting at BUFFER. + It is necessary that LEN is a multiple of 64!!! */ +extern void md5_process_block __P ((const void *buffer, size_t len, + struct md5_ctx *ctx)); + +/* Starting with the result of former calls of this function (or the + initialization function update the context for the next LEN bytes + starting at BUFFER. + It is NOT required that LEN is a multiple of 64. */ +extern void md5_process_bytes __P ((const void *buffer, size_t len, + struct md5_ctx *ctx)); + +/* Process the remaining bytes in the buffer and put result from CTX + in first 16 bytes following RESBUF. The result is always in little + endian byte order, so that a byte-wise output yields to the wanted + ASCII representation of the message digest. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +extern void *md5_finish_ctx __P ((struct md5_ctx *ctx, void *resbuf)); + + +/* Put result from CTX in first 16 bytes following RESBUF. The result is + always in little endian byte order, so that a byte-wise output yields + to the wanted ASCII representation of the message digest. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +extern void *md5_read_ctx __P ((const struct md5_ctx *ctx, void *resbuf)); + + +/* Compute MD5 message digest for bytes read from STREAM. The + resulting message digest number will be written into the 16 bytes + beginning at RESBLOCK. */ +extern int md5_stream __P ((FILE *stream, void *resblock)); + +/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The + result is always in little endian byte order, so that a byte-wise + output yields to the wanted ASCII representation of the message + digest. */ +extern void *md5_buffer __P ((const char *buffer, size_t len, + void *resblock)); + +#endif /* md5.h */ diff --git a/src/libutil/test-aterm.cc b/src/libutil/test-aterm.cc new file mode 100644 index 0000000000..325639ca4f --- /dev/null +++ b/src/libutil/test-aterm.cc @@ -0,0 +1,66 @@ +#include "aterm.hh" +#include + + +void runTests() +{ + verbosity = lvlDebug; + + ATMatcher pos; + + ATerm t = ATmake("Call(Foo, Bar, \"xyz\")"); + + debug(format("term: %1%") % t); + + string fun, arg3; + ATerm lhs, rhs; + + if (!(atMatch(pos, t) >> "Call" >> lhs >> rhs >> arg3)) + throw Error("should succeed"); + if (arg3 != "xyz") throw Error("bad 1"); + + if (!(atMatch(pos, t) >> fun >> lhs >> rhs >> arg3)) + throw Error("should succeed"); + if (fun != "Call") throw Error("bad 2"); + if (arg3 != "xyz") throw Error("bad 3"); + + if (!(atMatch(pos, t) >> fun >> lhs >> rhs >> "xyz")) + throw Error("should succeed"); + + if (atMatch(pos, t) >> fun >> lhs >> rhs >> "abc") + throw Error("should fail"); + + if (atMatch(pos, t) >> "Call" >> lhs >> rhs >> "abc") + throw Error("should fail"); + + t = ATmake("X([A, B, C], \"abc\")"); + + ATerm t1, t2, t3; + if (atMatch(pos, t) >> "X" >> t1 >> t2 >> t3) + throw Error("should fail"); + if (!(atMatch(pos, t) >> "X" >> t1 >> t2)) + throw Error("should succeed"); + ATermList ts; + if (!(atMatch(pos, t) >> "X" >> ts >> t2)) + throw Error("should succeed"); + if (ATgetLength(ts) != 3) + throw Error("bad"); + if (atMatch(pos, t) >> "X" >> t1 >> ts) + throw Error("should fail"); +} + + +int main(int argc, char * * argv) +{ + ATerm bottomOfStack; + ATinit(argc, argv, &bottomOfStack); + + try { + runTests(); + } catch (Error & e) { + printMsg(lvlError, format("error: %1%") % e.msg()); + return 1; + } + + return 0; +} diff --git a/src/libutil/util.cc b/src/libutil/util.cc new file mode 100644 index 0000000000..299a37f6c8 --- /dev/null +++ b/src/libutil/util.cc @@ -0,0 +1,318 @@ +#include +#include +#include + +#include +#include +#include +#include + +#include "util.hh" + + +string thisSystem = SYSTEM; + + +Error::Error(const format & f) +{ + err = f.str(); +} + + +SysError::SysError(const format & f) + : Error(format("%1%: %2%") % f.str() % strerror(errno)) +{ +} + + +Path absPath(Path path, Path dir) +{ + if (path[0] != '/') { + if (dir == "") { + char buf[PATH_MAX]; + if (!getcwd(buf, sizeof(buf))) + throw SysError("cannot get cwd"); + dir = buf; + } + path = dir + "/" + path; + } + return canonPath(path); +} + + +Path canonPath(const Path & path) +{ + string s; + + if (path[0] != '/') + throw Error(format("not an absolute path: `%1%'") % path); + + string::const_iterator i = path.begin(), end = path.end(); + + while (1) { + + /* Skip slashes. */ + while (i != end && *i == '/') i++; + if (i == end) break; + + /* Ignore `.'. */ + if (*i == '.' && (i + 1 == end || i[1] == '/')) + i++; + + /* If `..', delete the last component. */ + else if (*i == '.' && i + 1 < end && i[1] == '.' && + (i + 2 == end || i[2] == '/')) + { + if (!s.empty()) s.erase(s.rfind('/')); + i += 2; + } + + /* Normal component; copy it. */ + else { + s += '/'; + while (i != end && *i != '/') s += *i++; + } + } + + return s.empty() ? "/" : s; +} + + +Path dirOf(const Path & path) +{ + unsigned int pos = path.rfind('/'); + if (pos == string::npos) + throw Error(format("invalid file name: %1%") % path); + return Path(path, 0, pos); +} + + +string baseNameOf(const Path & path) +{ + unsigned int pos = path.rfind('/'); + if (pos == string::npos) + throw Error(format("invalid file name %1% ") % path); + return string(path, pos + 1); +} + + +bool pathExists(const Path & path) +{ + int res; + struct stat st; + res = stat(path.c_str(), &st); + if (!res) return true; + if (errno != ENOENT) + throw SysError(format("getting status of %1%") % path); + return false; +} + + +void deletePath(const Path & path) +{ + printMsg(lvlVomit, format("deleting path `%1%'") % path); + + struct stat st; + if (lstat(path.c_str(), &st)) + throw SysError(format("getting attributes of path `%1%'") % path); + + if (S_ISDIR(st.st_mode)) { + Strings names; + + { + AutoCloseDir dir = opendir(path.c_str()); + + struct dirent * dirent; + while (errno = 0, dirent = readdir(dir)) { + string name = dirent->d_name; + if (name == "." || name == "..") continue; + names.push_back(name); + } + } /* scoped to ensure that dir is closed at this point */ + + /* Make the directory writable. */ + if (!(st.st_mode & S_IWUSR)) { + if (chmod(path.c_str(), st.st_mode | S_IWUSR) == -1) + throw SysError(format("making `%1%' writable")); + } + + for (Strings::iterator i = names.begin(); i != names.end(); i++) + deletePath(path + "/" + *i); + } + + if (remove(path.c_str()) == -1) + throw SysError(format("cannot unlink `%1%'") % path); +} + + +void makePathReadOnly(const Path & path) +{ + struct stat st; + if (lstat(path.c_str(), &st)) + throw SysError(format("getting attributes of path `%1%'") % path); + + if (!S_ISLNK(st.st_mode) && (st.st_mode & S_IWUSR)) { + if (chmod(path.c_str(), st.st_mode & ~S_IWUSR) == -1) + throw SysError(format("making `%1%' read-only") % path); + } + + if (S_ISDIR(st.st_mode)) { + AutoCloseDir dir = opendir(path.c_str()); + + struct dirent * dirent; + while (errno = 0, dirent = readdir(dir)) { + string name = dirent->d_name; + if (name == "." || name == "..") continue; + makePathReadOnly(path + "/" + name); + } + } +} + + +static Path tempName() +{ + static int counter = 0; + char * s = getenv("TMPDIR"); + Path tmpRoot = s ? canonPath(Path(s)) : "/tmp"; + return (format("%1%/nix-%2%-%3%") % tmpRoot % getpid() % counter++).str(); +} + + +Path createTempDir() +{ + while (1) { + Path tmpDir = tempName(); + if (mkdir(tmpDir.c_str(), 0777) == 0) return tmpDir; + if (errno != EEXIST) + throw SysError(format("creating directory `%1%'") % tmpDir); + } +} + + +Verbosity verbosity = lvlError; + +static int nestingLevel = 0; + + +Nest::Nest() +{ + nest = false; +} + + +Nest::~Nest() +{ + if (nest) nestingLevel--; +} + + +void Nest::open(Verbosity level, const format & f) +{ + if (level <= verbosity) { + printMsg_(level, f); + nest = true; + nestingLevel++; + } +} + + +void printMsg_(Verbosity level, const format & f) +{ + if (level > verbosity) return; + string spaces; + for (int i = 0; i < nestingLevel; i++) + spaces += "| "; + cerr << format("%1%%2%\n") % spaces % f.str(); +} + + +void readFull(int fd, unsigned char * buf, size_t count) +{ + while (count) { + ssize_t res = read(fd, (char *) buf, count); + if (res == -1) throw SysError("reading from file"); + if (res == 0) throw Error("unexpected end-of-file"); + count -= res; + buf += res; + } +} + + +void writeFull(int fd, const unsigned char * buf, size_t count) +{ + while (count) { + ssize_t res = write(fd, (char *) buf, count); + if (res == -1) throw SysError("writing to file"); + count -= res; + buf += res; + } +} + + +AutoDelete::AutoDelete(const string & p) : path(p) +{ + del = true; +} + +AutoDelete::~AutoDelete() +{ + if (del) deletePath(path); +} + +void AutoDelete::cancel() +{ + del = false; +} + + +AutoCloseFD::AutoCloseFD() +{ + fd = -1; +} + +AutoCloseFD::AutoCloseFD(int fd) +{ + this->fd = fd; +} + +AutoCloseFD::~AutoCloseFD() +{ + if (fd != -1) close(fd); +} + +void AutoCloseFD::operator =(int fd) +{ + this->fd = fd; +} + +AutoCloseFD::operator int() +{ + return fd; +} + + +AutoCloseDir::AutoCloseDir() +{ + dir = 0; +} + +AutoCloseDir::AutoCloseDir(DIR * dir) +{ + this->dir = dir; +} + +AutoCloseDir::~AutoCloseDir() +{ + if (dir) closedir(dir); +} + +void AutoCloseDir::operator =(DIR * dir) +{ + this->dir = dir; +} + +AutoCloseDir::operator DIR *() +{ + return dir; +} + diff --git a/src/libutil/util.hh b/src/libutil/util.hh new file mode 100644 index 0000000000..e6b600eff8 --- /dev/null +++ b/src/libutil/util.hh @@ -0,0 +1,168 @@ +#ifndef __UTIL_H +#define __UTIL_H + +#include +#include +#include +#include + +#include +#include +#include + +#include + +using namespace std; +using namespace boost; + + +class Error : public exception +{ +protected: + string err; +public: + Error(const format & f); + ~Error() throw () { }; + const char * what() const throw () { return err.c_str(); } + const string & msg() const throw () { return err; } +}; + +class SysError : public Error +{ +public: + SysError(const format & f); +}; + +class UsageError : public Error +{ +public: + UsageError(const format & f) : Error(f) { }; +}; + + +typedef list Strings; +typedef set StringSet; + + +/* Paths are just strings. */ +typedef string Path; +typedef list Paths; +typedef set PathSet; + + +/* The canonical system name, as returned by config.guess. */ +extern string thisSystem; + + +/* Return an absolutized path, resolving paths relative to the + specified directory, or the current directory otherwise. The path + is also canonicalised. */ +Path absPath(Path path, Path dir = ""); + +/* Canonicalise a path (as in realpath(3)). */ +Path canonPath(const Path & path); + +/* Return the directory part of the given path, i.e., everything + before the final `/'. */ +Path dirOf(const Path & path); + +/* Return the base name of the given path, i.e., everything following + the final `/'. */ +string baseNameOf(const Path & path); + +/* Return true iff the given path exists. */ +bool pathExists(const Path & path); + +/* Delete a path; i.e., in the case of a directory, it is deleted + recursively. Don't use this at home, kids. */ +void deletePath(const Path & path); + +/* Make a path read-only recursively. */ +void makePathReadOnly(const Path & path); + +/* Create a temporary directory. */ +Path createTempDir(); + + +/* Messages. */ + +typedef enum { + lvlError, + lvlTalkative, + lvlChatty, + lvlDebug, + lvlVomit +} Verbosity; + +extern Verbosity verbosity; /* supress msgs > this */ + +class Nest +{ +private: + bool nest; +public: + Nest(); + ~Nest(); + void open(Verbosity level, const format & f); +}; + +void printMsg_(Verbosity level, const format & f); + +#define startNest(varName, level, f) \ + Nest varName; \ + if (level <= verbosity) { \ + varName.open(level, (f)); \ + } + +#define printMsg(level, f) \ + do { \ + if (level <= verbosity) { \ + printMsg_(level, (f)); \ + } \ + } while (0) + +#define debug(f) printMsg(lvlDebug, f) + + +/* Wrappers arount read()/write() that read/write exactly the + requested number of bytes. */ +void readFull(int fd, unsigned char * buf, size_t count); +void writeFull(int fd, const unsigned char * buf, size_t count); + + +/* Automatic cleanup of resources. */ + +class AutoDelete +{ + string path; + bool del; +public: + AutoDelete(const string & p); + ~AutoDelete(); + void cancel(); +}; + +class AutoCloseFD +{ + int fd; +public: + AutoCloseFD(); + AutoCloseFD(int fd); + ~AutoCloseFD(); + void operator =(int fd); + operator int(); +}; + +class AutoCloseDir +{ + DIR * dir; +public: + AutoCloseDir(); + AutoCloseDir(DIR * dir); + ~AutoCloseDir(); + void operator =(DIR * dir); + operator DIR *(); +}; + + +#endif /* !__UTIL_H */ diff --git a/src/nix-hash/Makefile.am b/src/nix-hash/Makefile.am index 7fb428a29f..7377a62e7c 100644 --- a/src/nix-hash/Makefile.am +++ b/src/nix-hash/Makefile.am @@ -1,8 +1,8 @@ bin_PROGRAMS = nix-hash nix_hash_SOURCES = nix-hash.cc -nix_hash_LDADD = ../libmain/libmain.a ../libnix/libnix.a ../boost/format/libformat.a \ - -L../../externals/inst/lib -ldb_cxx -lATerm +nix_hash_LDADD = ../libmain/libmain.a ../libnix/libnix.a ../libutil/libutil.a \ + ../boost/format/libformat.a -L../../externals/inst/lib -ldb_cxx -lATerm AM_CXXFLAGS = \ - -I.. -I../../externals/inst/include -I../libnix -I../libmain + -I.. -I../../externals/inst/include -I../libutil -I../libnix -I../libmain diff --git a/src/nix/Makefile.am b/src/nix/Makefile.am index 37415976b3..c4368a37c6 100644 --- a/src/nix/Makefile.am +++ b/src/nix/Makefile.am @@ -1,8 +1,8 @@ bin_PROGRAMS = nix nix_SOURCES = nix.cc dotgraph.cc -nix_LDADD = ../libmain/libmain.a ../libnix/libnix.a ../boost/format/libformat.a \ - -L../../externals/inst/lib -ldb_cxx -lATerm +nix_LDADD = ../libmain/libmain.a ../libnix/libnix.a ../libutil/libutil.a \ + ../boost/format/libformat.a -L../../externals/inst/lib -ldb_cxx -lATerm nix.o: nix-help.txt.hh @@ -12,7 +12,7 @@ nix.o: nix-help.txt.hh echo '"' >> $@ AM_CXXFLAGS = \ - -I.. -I../../externals/inst/include -I../libnix -I../libmain + -I.. -I../../externals/inst/include -I../libutil -I../libnix -I../libmain install-data-local: $(INSTALL) -d $(localstatedir)/nix -- cgit 1.4.1