about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2003-11-18T10·47+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2003-11-18T10·47+0000
commit8798fae30450a88c339c8f23d7e0c75f5df2ef1c (patch)
tree53d271e89ee3d04490a94a7ea82cf2dc3bbc3c30 /src/libutil
parent45610ae675f6f8d6ecbd48c495cb7012b143d531 (diff)
* Source tree refactoring.
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/Makefile.am14
-rw-r--r--src/libutil/archive.cc334
-rw-r--r--src/libutil/archive.hh60
-rw-r--r--src/libutil/aterm.cc93
-rw-r--r--src/libutil/aterm.hh77
-rw-r--r--src/libutil/hash.cc124
-rw-r--r--src/libutil/hash.hh51
-rw-r--r--src/libutil/md5.c448
-rw-r--r--src/libutil/md5.h151
-rw-r--r--src/libutil/test-aterm.cc66
-rw-r--r--src/libutil/util.cc318
-rw-r--r--src/libutil/util.hh168
12 files changed, 1904 insertions, 0 deletions
diff --git a/src/libutil/Makefile.am b/src/libutil/Makefile.am
new file mode 100644
index 000000000000..46f0a048fe66
--- /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 000000000000..ed57df4c9f7b
--- /dev/null
+++ b/src/libutil/archive.cc
@@ -0,0 +1,334 @@
+#include <cerrno>
+#include <algorithm>
+#include <vector>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <fcntl.h>
+
+#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<string> 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<string>::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 000000000000..67e236668a06
--- /dev/null
+++ b/src/libutil/archive.hh
@@ -0,0 +1,60 @@
+#include <string>
+
+#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 000000000000..de7c359521a7
--- /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 000000000000..16d8d6bb6dfd
--- /dev/null
+++ b/src/libutil/aterm.hh
@@ -0,0 +1,77 @@
+#ifndef __ATERM_H
+#define __ATERM_H
+
+extern "C" {
+#include <aterm2.h>
+}
+
+#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 000000000000..752b269125cb
--- /dev/null
+++ b/src/libutil/hash.cc
@@ -0,0 +1,124 @@
+#include <iostream>
+
+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 000000000000..0062f987c021
--- /dev/null
+++ b/src/libutil/hash.hh
@@ -0,0 +1,51 @@
+#ifndef __HASH_H
+#define __HASH_H
+
+#include <string>
+
+#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 000000000000..fa67ebfcdbdf
--- /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 <drepper@gnu.ai.mit.edu>, 1995.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <sys/types.h>
+
+#include <stdlib.h>
+#include <string.h>
+
+#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 000000000000..6301e4558ca1
--- /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 <stdio.h>
+
+#if defined HAVE_LIMITS_H || _LIBC
+# include <limits.h>
+#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 <stdint.h>
+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 <limits.h>) 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 000000000000..325639ca4f30
--- /dev/null
+++ b/src/libutil/test-aterm.cc
@@ -0,0 +1,66 @@
+#include "aterm.hh"
+#include <iostream>
+
+
+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 000000000000..299a37f6c877
--- /dev/null
+++ b/src/libutil/util.cc
@@ -0,0 +1,318 @@
+#include <iostream>
+#include <cerrno>
+#include <cstdio>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <dirent.h>
+
+#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 000000000000..e6b600eff8ce
--- /dev/null
+++ b/src/libutil/util.hh
@@ -0,0 +1,168 @@
+#ifndef __UTIL_H
+#define __UTIL_H
+
+#include <string>
+#include <list>
+#include <set>
+#include <sstream>
+
+#include <sys/types.h>
+#include <dirent.h>
+#include <unistd.h>
+
+#include <boost/format.hpp>
+
+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<string> Strings;
+typedef set<string> StringSet;
+
+
+/* Paths are just strings. */
+typedef string Path;
+typedef list<Path> Paths;
+typedef set<Path> 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 */