about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2006-12-12T23·05+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2006-12-12T23·05+0000
commita3e6415ba8cf1b8d2a1db40c06997d997eac8afc (patch)
tree16e43edf9a785101b562ee3bfa6cbaeb600fc22a /src/libutil
parentb438d37558eab56a2927771013c9080675381ba8 (diff)
* New primop builtins.filterSource, which can be used to filter files
  from a source directory.  All files for which a predicate function
  returns true are copied to the store.  Typical example is to leave
  out the .svn directory:

    stdenv.mkDerivation {
      ...
      src = builtins.filterSource
        (path: baseNameOf (toString path) != ".svn")
        ./source-dir;
      # as opposed to
      #   src = ./source-dir;
    }

  This is important because the .svn directory influences the hash in
  a rather unpredictable and variable way.

Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/archive.cc21
-rw-r--r--src/libutil/archive.hh8
-rw-r--r--src/libutil/hash.cc4
-rw-r--r--src/libutil/hash.hh5
-rw-r--r--src/libutil/serialise.hh27
-rw-r--r--src/libutil/util.hh4
6 files changed, 50 insertions, 19 deletions
diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc
index 4aedd31f7672..a8e018b69aec 100644
--- a/src/libutil/archive.cc
+++ b/src/libutil/archive.cc
@@ -18,28 +18,29 @@ namespace nix {
 static string archiveVersion1 = "nix-archive-1";
 
 
-DumpFilter defaultDumpFilter;
+PathFilter defaultPathFilter;
 
 
-static void dump(const string & path, Sink & sink, DumpFilter & filter);
+static void dump(const string & path, Sink & sink, PathFilter & filter);
 
 
-static void dumpEntries(const Path & path, Sink & sink, DumpFilter & filter)
+static void dumpEntries(const Path & path, Sink & sink, PathFilter & filter)
 {
     Strings names = readDirectory(path);
     vector<string> names2(names.begin(), names.end());
     sort(names2.begin(), names2.end());
 
-    for (vector<string>::iterator it = names2.begin();
-         it != names2.end(); it++)
+    for (vector<string>::iterator i = names2.begin();
+         i != names2.end(); ++i)
     {
-        if (filter(path)) {
+        Path entry = path + "/" + *i;
+        if (filter(entry)) {
             writeString("entry", sink);
             writeString("(", sink);
             writeString("name", sink);
-            writeString(*it, sink);
+            writeString(*i, sink);
             writeString("node", sink);
-            dump(path + "/" + *it, sink, filter);
+            dump(entry, sink, filter);
             writeString(")", sink);
         }
     }
@@ -69,7 +70,7 @@ static void dumpContents(const Path & path, unsigned int size,
 }
 
 
-static void dump(const Path & path, Sink & sink, DumpFilter & filter)
+static void dump(const Path & path, Sink & sink, PathFilter & filter)
 {
     struct stat st;
     if (lstat(path.c_str(), &st))
@@ -106,7 +107,7 @@ static void dump(const Path & path, Sink & sink, DumpFilter & filter)
 }
 
 
-void dumpPath(const Path & path, Sink & sink, DumpFilter & filter)
+void dumpPath(const Path & path, Sink & sink, PathFilter & filter)
 {
     writeString(archiveVersion1, sink);
     dump(path, sink, filter);
diff --git a/src/libutil/archive.hh b/src/libutil/archive.hh
index 70e836055cb1..5f85e1beb0cf 100644
--- a/src/libutil/archive.hh
+++ b/src/libutil/archive.hh
@@ -45,16 +45,16 @@ namespace nix {
 
      `+' denotes string concatenation. */
 
-struct DumpFilter
+struct PathFilter
 {
-    virtual ~DumpFilter() { }
+    virtual ~PathFilter() { }
     virtual bool operator () (const Path & path) { return true; }
 };
 
-extern DumpFilter defaultDumpFilter;
+extern PathFilter defaultPathFilter;
 
 void dumpPath(const Path & path, Sink & sink,
-    DumpFilter & filter = defaultDumpFilter);
+    PathFilter & filter = defaultPathFilter);
 
 void restorePath(const Path & path, Source & source);
 
diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc
index 8dc33f5d0ca4..262dbef20df2 100644
--- a/src/libutil/hash.cc
+++ b/src/libutil/hash.cc
@@ -294,13 +294,13 @@ struct HashSink : Sink
 };
 
 
-Hash hashPath(HashType ht, const Path & path)
+Hash hashPath(HashType ht, const Path & path, PathFilter & filter)
 {
     HashSink sink;
     sink.ht = ht;
     Hash hash(ht);
     start(ht, sink.ctx);
-    dumpPath(path, sink);
+    dumpPath(path, sink, filter);
     finish(ht, sink.ctx, hash.hash);
     return hash;
 }
diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh
index 74ae51db3286..78227fb6a31b 100644
--- a/src/libutil/hash.hh
+++ b/src/libutil/hash.hh
@@ -69,7 +69,10 @@ Hash hashFile(HashType ht, const Path & path);
 
 /* Compute the hash of the given path.  The hash is defined as
    (essentially) hashString(ht, dumpPath(path)). */
-Hash hashPath(HashType ht, const Path & path);
+struct PathFilter;
+extern PathFilter defaultPathFilter;
+Hash hashPath(HashType ht, const Path & path,
+    PathFilter & filter = defaultPathFilter);
 
 /* Compress a hash to the specified number of bytes by cyclically
    XORing bytes together. */
diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh
index fe3492235e97..c18e82463b5a 100644
--- a/src/libutil/serialise.hh
+++ b/src/libutil/serialise.hh
@@ -66,6 +66,33 @@ struct FdSource : Source
 };
 
 
+/* A sink that writes data to a string. */
+struct StringSink : Sink
+{
+    string s;
+    virtual void operator () (const unsigned char * data, unsigned int len)
+    {
+        s.append((const char *) data, len);
+    }
+};
+
+
+/* A source that reads data from a string. */
+struct StringSource : Source
+{
+    string & s;
+    unsigned int pos;
+    StringSource(string & _s) : s(_s), pos(0) { }
+    virtual void operator () (unsigned char * data, unsigned int len)
+    {
+        s.copy((char *) data, len, pos);
+        pos += len;
+        if (pos > s.size())
+            throw Error("end of string reached");
+    }
+};
+
+
 void writePadding(unsigned int len, Sink & sink);
 void writeInt(unsigned int n, Sink & sink);
 void writeString(const string & s, Sink & sink);
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 8f79ec9be2c1..1cc97145c1aa 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -164,10 +164,10 @@ struct AutoDeleteArray
 
 class AutoDelete
 {
-    string path;
+    Path path;
     bool del;
 public:
-    AutoDelete(const string & p);
+    AutoDelete(const Path & p);
     ~AutoDelete();
     void cancel();
 };