1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-19 10:50:24 -04:00
nix/src/libutil/serialise.cc

285 lines
5.9 KiB
C++
Raw Normal View History

#include "serialise.hh"
#include "util.hh"
#include <cstring>
#include <cerrno>
#include <memory>
namespace nix {
void BufferedSink::operator () (const unsigned char * data, size_t len)
{
2016-07-13 06:03:37 -04:00
if (!buffer) buffer = decltype(buffer)(new unsigned char[bufSize]);
2015-07-19 19:16:16 -04:00
while (len) {
/* Optimisation: bypass the buffer if the data exceeds the
buffer size. */
if (bufPos + len >= bufSize) {
flush();
write(data, len);
break;
}
/* Otherwise, copy the bytes to the buffer. Flush the buffer
when it's full. */
size_t n = bufPos + len > bufSize ? bufSize - bufPos : len;
2016-07-13 06:03:37 -04:00
memcpy(buffer.get() + bufPos, data, n);
data += n; bufPos += n; len -= n;
if (bufPos == bufSize) flush();
}
}
void BufferedSink::flush()
{
if (bufPos == 0) return;
2011-12-16 10:45:42 -05:00
size_t n = bufPos;
bufPos = 0; // don't trigger the assert() in ~BufferedSink()
2016-07-13 06:03:37 -04:00
write(buffer.get(), n);
2011-12-16 10:45:42 -05:00
}
FdSink::~FdSink()
{
try { flush(); } catch (...) { ignoreException(); }
}
size_t threshold = 256 * 1024 * 1024;
static void warnLargeDump()
{
printError("warning: dumping very large path (> 256 MiB); this may run out of memory");
}
void FdSink::write(const unsigned char * data, size_t len)
{
2016-02-26 10:16:08 -05:00
written += len;
static bool warned = false;
if (warn && !warned) {
if (written > threshold) {
warnLargeDump();
warned = true;
}
}
try {
writeFull(fd, data, len);
} catch (SysError & e) {
_good = true;
}
}
bool FdSink::good()
{
return _good;
}
void Source::operator () (unsigned char * data, size_t len)
{
while (len) {
size_t n = read(data, len);
data += n; len -= n;
}
}
size_t BufferedSource::read(unsigned char * data, size_t len)
{
2016-07-13 06:03:37 -04:00
if (!buffer) buffer = decltype(buffer)(new unsigned char[bufSize]);
2016-07-13 06:03:37 -04:00
if (!bufPosIn) bufPosIn = readUnbuffered(buffer.get(), bufSize);
2015-07-19 19:16:16 -04:00
/* Copy out the data in the buffer. */
size_t n = len > bufPosIn - bufPosOut ? bufPosIn - bufPosOut : len;
2016-07-13 06:03:37 -04:00
memcpy(data, buffer.get() + bufPosOut, n);
bufPosOut += n;
if (bufPosIn == bufPosOut) bufPosIn = bufPosOut = 0;
return n;
}
bool BufferedSource::hasData()
{
return bufPosOut < bufPosIn;
}
size_t FdSource::readUnbuffered(unsigned char * data, size_t len)
{
ssize_t n;
do {
checkInterrupt();
n = ::read(fd, (char *) data, bufSize);
} while (n == -1 && errno == EINTR);
if (n == -1) { _good = false; throw SysError("reading from file"); }
if (n == 0) { _good = false; throw EndOfFile("unexpected end-of-file"); }
2016-02-26 10:16:08 -05:00
read += n;
return n;
}
bool FdSource::good()
{
return _good;
}
size_t StringSource::read(unsigned char * data, size_t len)
{
if (pos == s.size()) throw EndOfFile("end of string reached");
size_t n = s.copy((char *) data, len, pos);
pos += n;
return n;
}
void writePadding(size_t len, Sink & sink)
{
if (len % 8) {
unsigned char zero[8];
memset(zero, 0, sizeof(zero));
sink(zero, 8 - (len % 8));
}
}
void writeString(const unsigned char * buf, size_t len, Sink & sink)
{
2015-07-19 19:16:16 -04:00
sink << len;
sink(buf, len);
writePadding(len, sink);
}
2015-07-19 19:16:16 -04:00
Sink & operator << (Sink & sink, const string & s)
{
writeString((const unsigned char *) s.data(), s.size(), sink);
2015-07-19 19:16:16 -04:00
return sink;
}
template<class T> void writeStrings(const T & ss, Sink & sink)
{
2015-07-19 19:16:16 -04:00
sink << ss.size();
2015-07-17 13:24:28 -04:00
for (auto & i : ss)
2015-07-19 19:16:16 -04:00
sink << i;
}
2015-07-19 19:16:16 -04:00
Sink & operator << (Sink & sink, const Strings & s)
{
2015-07-19 19:16:16 -04:00
writeStrings(s, sink);
return sink;
}
2015-07-19 19:16:16 -04:00
Sink & operator << (Sink & sink, const StringSet & s)
{
2015-07-19 19:16:16 -04:00
writeStrings(s, sink);
return sink;
}
void readPadding(size_t len, Source & source)
{
if (len % 8) {
unsigned char zero[8];
size_t n = 8 - (len % 8);
source(zero, n);
for (unsigned int i = 0; i < n; i++)
if (zero[i]) throw SerialisationError("non-zero padding");
}
}
unsigned int readInt(Source & source)
{
unsigned char buf[8];
source(buf, sizeof(buf));
if (buf[4] || buf[5] || buf[6] || buf[7])
throw SerialisationError("implementation cannot deal with > 32-bit integers");
return
buf[0] |
(buf[1] << 8) |
(buf[2] << 16) |
(buf[3] << 24);
}
unsigned long long readLongLong(Source & source)
{
unsigned char buf[8];
source(buf, sizeof(buf));
return
((unsigned long long) buf[0]) |
((unsigned long long) buf[1] << 8) |
((unsigned long long) buf[2] << 16) |
((unsigned long long) buf[3] << 24) |
((unsigned long long) buf[4] << 32) |
((unsigned long long) buf[5] << 40) |
((unsigned long long) buf[6] << 48) |
((unsigned long long) buf[7] << 56);
}
size_t readString(unsigned char * buf, size_t max, Source & source)
{
size_t len = readInt(source);
if (len > max) throw Error("string is too long");
source(buf, len);
readPadding(len, source);
return len;
}
2015-07-19 19:16:16 -04:00
string readString(Source & source)
{
size_t len = readInt(source);
auto buf = std::make_unique<unsigned char[]>(len);
source(buf.get(), len);
readPadding(len, source);
return string((char *) buf.get(), len);
}
Source & operator >> (Source & in, string & s)
{
s = readString(in);
return in;
}
Source & operator >> (Source & in, unsigned int & n)
{
n = readInt(in);
return in;
}
template<class T> T readStrings(Source & source)
{
unsigned int count = readInt(source);
T ss;
while (count--)
ss.insert(ss.end(), readString(source));
return ss;
}
template Paths readStrings(Source & source);
template PathSet readStrings(Source & source);
void StringSink::operator () (const unsigned char * data, size_t len)
{
static bool warned = false;
2016-03-04 10:49:56 -05:00
if (!warned && s->size() > threshold) {
warnLargeDump();
warned = true;
}
2016-03-04 10:49:56 -05:00
s->append((const char *) data, len);
}
}