about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/archive.cc37
-rw-r--r--src/libutil/serialise.cc71
-rw-r--r--src/libutil/serialise.hh43
3 files changed, 54 insertions, 97 deletions
diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc
index 9e16e04ae4b5..0187f062b21d 100644
--- a/src/libutil/archive.cc
+++ b/src/libutil/archive.cc
@@ -39,8 +39,7 @@ PathFilter defaultPathFilter;
 static void dumpContents(const Path & path, size_t size,
     Sink & sink)
 {
-    writeString("contents", sink);
-    writeLongLong(size, sink);
+    sink << "contents" << size;
 
     AutoCloseFD fd = open(path.c_str(), O_RDONLY);
     if (fd == -1) throw SysError(format("opening file ‘%1%’") % path);
@@ -65,21 +64,17 @@ static void dump(const Path & path, Sink & sink, PathFilter & filter)
     if (lstat(path.c_str(), &st))
         throw SysError(format("getting attributes of path ‘%1%’") % path);
 
-    writeString("(", sink);
+    sink << "(";
 
     if (S_ISREG(st.st_mode)) {
-        writeString("type", sink);
-        writeString("regular", sink);
-        if (st.st_mode & S_IXUSR) {
-            writeString("executable", sink);
-            writeString("", sink);
-        }
+        sink << "type" << "regular";
+        if (st.st_mode & S_IXUSR)
+            sink << "executable" << "";
         dumpContents(path, (size_t) st.st_size, sink);
     }
 
     else if (S_ISDIR(st.st_mode)) {
-        writeString("type", sink);
-        writeString("directory", sink);
+        sink << "type" << "directory";
 
         /* If we're on a case-insensitive system like Mac OS X, undo
            the case hack applied by restorePath(). */
@@ -101,32 +96,24 @@ static void dump(const Path & path, Sink & sink, PathFilter & filter)
 
         for (auto & i : unhacked)
             if (filter(path + "/" + i.first)) {
-                writeString("entry", sink);
-                writeString("(", sink);
-                writeString("name", sink);
-                writeString(i.first, sink);
-                writeString("node", sink);
+                sink << "entry" << "(" << "name" << i.first << "node";
                 dump(path + "/" + i.second, sink, filter);
-                writeString(")", sink);
+                sink << ")";
             }
     }
 
-    else if (S_ISLNK(st.st_mode)) {
-        writeString("type", sink);
-        writeString("symlink", sink);
-        writeString("target", sink);
-        writeString(readLink(path), sink);
-    }
+    else if (S_ISLNK(st.st_mode))
+        sink << "type" << "symlink" << "target" << readLink(path);
 
     else throw Error(format("file ‘%1%’ has an unsupported type") % path);
 
-    writeString(")", sink);
+    sink << ")";
 }
 
 
 void dumpPath(const Path & path, Sink & sink, PathFilter & filter)
 {
-    writeString(archiveVersion1, sink);
+    sink << archiveVersion1;
     dump(path, sink, filter);
 }
 
diff --git a/src/libutil/serialise.cc b/src/libutil/serialise.cc
index 789227102864..f8e9d00c12b8 100644
--- a/src/libutil/serialise.cc
+++ b/src/libutil/serialise.cc
@@ -16,11 +16,11 @@ BufferedSink::~BufferedSink()
     delete[] buffer;
 }
 
-    
+
 void BufferedSink::operator () (const unsigned char * data, size_t len)
 {
     if (!buffer) buffer = new unsigned char[bufSize];
-    
+
     while (len) {
         /* Optimisation: bypass the buffer if the data exceeds the
            buffer size. */
@@ -96,7 +96,7 @@ size_t BufferedSource::read(unsigned char * data, size_t len)
     if (!buffer) buffer = new unsigned char[bufSize];
 
     if (!bufPosIn) bufPosIn = readUnbuffered(buffer, bufSize);
-            
+
     /* Copy out the data in the buffer. */
     size_t n = len > bufPosIn - bufPosOut ? bufPosIn - bufPosOut : len;
     memcpy(data, buffer + bufPosOut, n);
@@ -144,79 +144,38 @@ void writePadding(size_t len, Sink & sink)
 }
 
 
-void writeInt(unsigned int n, Sink & 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));
-}
-
-Sink & operator << (Sink & out, unsigned int n)
-{
-    writeInt(n, out);
-    return out;
-}
-
-
-void writeLongLong(unsigned long long n, Sink & sink)
-{
-    unsigned char buf[8];
-    buf[0] = n & 0xff;
-    buf[1] = (n >> 8) & 0xff;
-    buf[2] = (n >> 16) & 0xff;
-    buf[3] = (n >> 24) & 0xff;
-    buf[4] = (n >> 32) & 0xff;
-    buf[5] = (n >> 40) & 0xff;
-    buf[6] = (n >> 48) & 0xff;
-    buf[7] = (n >> 56) & 0xff;
-    sink(buf, sizeof(buf));
-}
-
-
 void writeString(const unsigned char * buf, size_t len, Sink & sink)
 {
-    writeInt(len, sink);
+    sink << len;
     sink(buf, len);
     writePadding(len, sink);
 }
 
 
-void writeString(const string & s, Sink & sink)
+Sink & operator << (Sink & sink, const string & s)
 {
     writeString((const unsigned char *) s.data(), s.size(), sink);
-}
-
-Sink & operator << (Sink & out, const string & s)
-{
-    writeString(s, out);
-    return out;
+    return sink;
 }
 
 
 template<class T> void writeStrings(const T & ss, Sink & sink)
 {
-    writeInt(ss.size(), sink);
+    sink << ss.size();
     for (auto & i : ss)
-        writeString(i, sink);
+        sink << i;
 }
 
-template void writeStrings(const Paths & ss, Sink & sink);
-template void writeStrings(const PathSet & ss, Sink & sink);
-
-Sink & operator << (Sink & out, const Strings & s)
+Sink & operator << (Sink & sink, const Strings & s)
 {
-    writeStrings(s, out);
-    return out;
+    writeStrings(s, sink);
+    return sink;
 }
 
-Sink & operator << (Sink & out, const StringSet & s)
+Sink & operator << (Sink & sink, const StringSet & s)
 {
-    writeStrings(s, out);
-    return out;
+    writeStrings(s, sink);
+    return sink;
 }
 
 
@@ -271,7 +230,7 @@ size_t readString(unsigned char * buf, size_t max, Source & source)
     return len;
 }
 
- 
+
 string readString(Source & source)
 {
     size_t len = readInt(source);
diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh
index 3a20ad032254..97ac3e912f85 100644
--- a/src/libutil/serialise.hh
+++ b/src/libutil/serialise.hh
@@ -1,13 +1,14 @@
 #pragma once
 
 #include "types.hh"
+#include "util.hh"
 
 
 namespace nix {
 
 
 /* Abstract destination of binary data. */
-struct Sink 
+struct Sink
 {
     virtual ~Sink() { }
     virtual void operator () (const unsigned char * data, size_t len) = 0;
@@ -25,9 +26,9 @@ struct BufferedSink : Sink
     ~BufferedSink();
 
     void operator () (const unsigned char * data, size_t len);
-    
+
     void flush();
-    
+
     virtual void write(const unsigned char * data, size_t len) = 0;
 };
 
@@ -36,7 +37,7 @@ struct BufferedSink : Sink
 struct Source
 {
     virtual ~Source() { }
-    
+
     /* Store exactly ‘len’ bytes in the buffer pointed to by ‘data’.
        It blocks until all the requested data is available, or throws
        an error if it is not going to be available.   */
@@ -58,9 +59,9 @@ struct BufferedSource : Source
     BufferedSource(size_t bufSize = 32 * 1024)
         : bufSize(bufSize), bufPosIn(0), bufPosOut(0), buffer(0) { }
     ~BufferedSource();
-    
+
     size_t read(unsigned char * data, size_t len);
-    
+
     /* Underlying read call, to be overridden. */
     virtual size_t readUnbuffered(unsigned char * data, size_t len) = 0;
 
@@ -78,7 +79,7 @@ struct FdSink : BufferedSink
     FdSink() : fd(-1), warn(false), written(0) { }
     FdSink(int fd) : fd(fd), warn(false), written(0) { }
     ~FdSink();
-    
+
     void write(const unsigned char * data, size_t len);
 };
 
@@ -107,21 +108,31 @@ struct StringSource : Source
     const string & s;
     size_t pos;
     StringSource(const string & _s) : s(_s), pos(0) { }
-    size_t read(unsigned char * data, size_t len);    
+    size_t read(unsigned char * data, size_t len);
 };
 
 
 void writePadding(size_t len, Sink & sink);
-void writeInt(unsigned int n, Sink & sink);
-void writeLongLong(unsigned long long n, Sink & sink);
 void writeString(const unsigned char * buf, size_t len, Sink & sink);
-void writeString(const string & s, Sink & sink);
-template<class T> void writeStrings(const T & ss, Sink & sink);
 
-Sink & operator << (Sink & out, unsigned int n);
-Sink & operator << (Sink & out, const string & s);
-Sink & operator << (Sink & out, const Strings & s);
-Sink & operator << (Sink & out, const StringSet & s);
+inline Sink & operator << (Sink & sink, uint64_t n)
+{
+    unsigned char buf[8];
+    buf[0] = n & 0xff;
+    buf[1] = (n >> 8) & 0xff;
+    buf[2] = (n >> 16) & 0xff;
+    buf[3] = (n >> 24) & 0xff;
+    buf[4] = (n >> 32) & 0xff;
+    buf[5] = (n >> 40) & 0xff;
+    buf[6] = (n >> 48) & 0xff;
+    buf[7] = (n >> 56) & 0xff;
+    sink(buf, sizeof(buf));
+    return sink;
+}
+
+Sink & operator << (Sink & sink, const string & s);
+Sink & operator << (Sink & sink, const Strings & s);
+Sink & operator << (Sink & sink, const StringSet & s);
 
 
 void readPadding(size_t len, Source & source);