2006-11-30 21:19:59 +02:00
|
|
|
#include "serialise.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
|
2008-05-21 14:17:31 +03:00
|
|
|
#include <cstring>
|
2011-12-15 14:32:08 +02:00
|
|
|
#include <cerrno>
|
2008-05-21 14:17:31 +03:00
|
|
|
|
2006-11-30 21:19:59 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
2011-12-15 18:19:53 +02:00
|
|
|
void BufferedSink::operator () (const unsigned char * data, size_t len)
|
2006-11-30 21:19:59 +02:00
|
|
|
{
|
2016-07-13 13:03:37 +03:00
|
|
|
if (!buffer) buffer = decltype(buffer)(new unsigned char[bufSize]);
|
2015-07-20 02:16:16 +03:00
|
|
|
|
2011-12-15 01:30:06 +02:00
|
|
|
while (len) {
|
|
|
|
/* Optimisation: bypass the buffer if the data exceeds the
|
2011-12-16 21:44:13 +02:00
|
|
|
buffer size. */
|
|
|
|
if (bufPos + len >= bufSize) {
|
|
|
|
flush();
|
2011-12-15 18:19:53 +02:00
|
|
|
write(data, len);
|
2011-12-15 01:30:06 +02:00
|
|
|
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 13:03:37 +03:00
|
|
|
memcpy(buffer.get() + bufPos, data, n);
|
2011-12-15 01:30:06 +02:00
|
|
|
data += n; bufPos += n; len -= n;
|
|
|
|
if (bufPos == bufSize) flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-15 18:19:53 +02:00
|
|
|
void BufferedSink::flush()
|
2011-12-15 01:30:06 +02:00
|
|
|
{
|
2011-12-15 18:19:53 +02:00
|
|
|
if (bufPos == 0) return;
|
2011-12-16 17:45:42 +02:00
|
|
|
size_t n = bufPos;
|
|
|
|
bufPos = 0; // don't trigger the assert() in ~BufferedSink()
|
2016-07-13 13:03:37 +03:00
|
|
|
write(buffer.get(), n);
|
2011-12-16 17:45:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FdSink::~FdSink()
|
|
|
|
{
|
|
|
|
try { flush(); } catch (...) { ignoreException(); }
|
2006-11-30 21:19:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-10 14:30:09 +03:00
|
|
|
size_t threshold = 256 * 1024 * 1024;
|
|
|
|
|
|
|
|
static void warnLargeDump()
|
|
|
|
{
|
2016-09-21 17:11:01 +03:00
|
|
|
printError("warning: dumping very large path (> 256 MiB); this may run out of memory");
|
2014-06-10 14:30:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-15 18:19:53 +02:00
|
|
|
void FdSink::write(const unsigned char * data, size_t len)
|
|
|
|
{
|
2016-02-26 17:16:08 +02:00
|
|
|
written += len;
|
2014-06-10 14:30:09 +03:00
|
|
|
static bool warned = false;
|
|
|
|
if (warn && !warned) {
|
|
|
|
if (written > threshold) {
|
|
|
|
warnLargeDump();
|
|
|
|
warned = true;
|
|
|
|
}
|
|
|
|
}
|
2016-02-24 12:39:56 +02:00
|
|
|
try {
|
|
|
|
writeFull(fd, data, len);
|
|
|
|
} catch (SysError & e) {
|
|
|
|
_good = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FdSink::good()
|
|
|
|
{
|
|
|
|
return _good;
|
2011-12-15 18:19:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-16 21:44:13 +02:00
|
|
|
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)
|
2006-11-30 21:19:59 +02:00
|
|
|
{
|
2016-07-13 13:03:37 +03:00
|
|
|
if (!buffer) buffer = decltype(buffer)(new unsigned char[bufSize]);
|
2011-12-15 14:32:08 +02:00
|
|
|
|
2016-07-13 13:03:37 +03:00
|
|
|
if (!bufPosIn) bufPosIn = readUnbuffered(buffer.get(), bufSize);
|
2015-07-20 02:16:16 +03:00
|
|
|
|
2011-12-16 21:44:13 +02:00
|
|
|
/* Copy out the data in the buffer. */
|
|
|
|
size_t n = len > bufPosIn - bufPosOut ? bufPosIn - bufPosOut : len;
|
2016-07-13 13:03:37 +03:00
|
|
|
memcpy(data, buffer.get() + bufPosOut, n);
|
2011-12-16 21:44:13 +02:00
|
|
|
bufPosOut += n;
|
|
|
|
if (bufPosIn == bufPosOut) bufPosIn = bufPosOut = 0;
|
|
|
|
return n;
|
2006-11-30 21:19:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-07 16:02:14 +03:00
|
|
|
bool BufferedSource::hasData()
|
|
|
|
{
|
|
|
|
return bufPosOut < bufPosIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-16 21:44:13 +02:00
|
|
|
size_t FdSource::readUnbuffered(unsigned char * data, size_t len)
|
2011-12-15 18:19:53 +02:00
|
|
|
{
|
|
|
|
ssize_t n;
|
|
|
|
do {
|
|
|
|
checkInterrupt();
|
|
|
|
n = ::read(fd, (char *) data, bufSize);
|
|
|
|
} while (n == -1 && errno == EINTR);
|
2016-02-24 12:39:56 +02:00
|
|
|
if (n == -1) { _good = false; throw SysError("reading from file"); }
|
|
|
|
if (n == 0) { _good = false; throw EndOfFile("unexpected end-of-file"); }
|
2016-02-26 17:16:08 +02:00
|
|
|
read += n;
|
2011-12-15 18:19:53 +02:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-24 12:39:56 +02:00
|
|
|
bool FdSource::good()
|
|
|
|
{
|
|
|
|
return _good;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-16 21:44:13 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-15 18:19:53 +02:00
|
|
|
void writePadding(size_t len, Sink & sink)
|
2006-11-30 21:19:59 +02:00
|
|
|
{
|
|
|
|
if (len % 8) {
|
|
|
|
unsigned char zero[8];
|
|
|
|
memset(zero, 0, sizeof(zero));
|
|
|
|
sink(zero, 8 - (len % 8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-16 23:29:46 +02:00
|
|
|
void writeString(const unsigned char * buf, size_t len, Sink & sink)
|
2006-11-30 21:19:59 +02:00
|
|
|
{
|
2015-07-20 02:16:16 +03:00
|
|
|
sink << len;
|
2011-12-16 23:29:46 +02:00
|
|
|
sink(buf, len);
|
2006-11-30 21:19:59 +02:00
|
|
|
writePadding(len, sink);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-20 02:16:16 +03:00
|
|
|
Sink & operator << (Sink & sink, const string & s)
|
2011-12-16 23:29:46 +02:00
|
|
|
{
|
2012-02-09 19:27:45 +02:00
|
|
|
writeString((const unsigned char *) s.data(), s.size(), sink);
|
2015-07-20 02:16:16 +03:00
|
|
|
return sink;
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 18:57:40 +03:00
|
|
|
}
|
|
|
|
|
2011-12-16 23:29:46 +02:00
|
|
|
|
2011-12-17 00:31:25 +02:00
|
|
|
template<class T> void writeStrings(const T & ss, Sink & sink)
|
2006-12-01 00:43:55 +02:00
|
|
|
{
|
2015-07-20 02:16:16 +03:00
|
|
|
sink << ss.size();
|
2015-07-17 20:24:28 +03:00
|
|
|
for (auto & i : ss)
|
2015-07-20 02:16:16 +03:00
|
|
|
sink << i;
|
2006-12-01 00:43:55 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 02:16:16 +03:00
|
|
|
Sink & operator << (Sink & sink, const Strings & s)
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 18:57:40 +03:00
|
|
|
{
|
2015-07-20 02:16:16 +03:00
|
|
|
writeStrings(s, sink);
|
|
|
|
return sink;
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 18:57:40 +03:00
|
|
|
}
|
|
|
|
|
2015-07-20 02:16:16 +03:00
|
|
|
Sink & operator << (Sink & sink, const StringSet & s)
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 18:57:40 +03:00
|
|
|
{
|
2015-07-20 02:16:16 +03:00
|
|
|
writeStrings(s, sink);
|
|
|
|
return sink;
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 18:57:40 +03:00
|
|
|
}
|
|
|
|
|
2006-12-01 00:43:55 +02:00
|
|
|
|
2011-12-15 18:19:53 +02:00
|
|
|
void readPadding(size_t len, Source & source)
|
2006-11-30 21:19:59 +02:00
|
|
|
{
|
|
|
|
if (len % 8) {
|
|
|
|
unsigned char zero[8];
|
2011-12-15 18:19:53 +02:00
|
|
|
size_t n = 8 - (len % 8);
|
2006-11-30 21:19:59 +02:00
|
|
|
source(zero, n);
|
|
|
|
for (unsigned int i = 0; i < n; i++)
|
2009-03-22 19:36:43 +02:00
|
|
|
if (zero[i]) throw SerialisationError("non-zero padding");
|
2006-11-30 21:19:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int readInt(Source & source)
|
|
|
|
{
|
|
|
|
unsigned char buf[8];
|
|
|
|
source(buf, sizeof(buf));
|
|
|
|
if (buf[4] || buf[5] || buf[6] || buf[7])
|
2009-03-22 19:36:43 +02:00
|
|
|
throw SerialisationError("implementation cannot deal with > 32-bit integers");
|
2006-11-30 21:19:59 +02:00
|
|
|
return
|
|
|
|
buf[0] |
|
|
|
|
(buf[1] << 8) |
|
|
|
|
(buf[2] << 16) |
|
|
|
|
(buf[3] << 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-18 12:34:17 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-16 23:29:46 +02:00
|
|
|
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-20 02:16:16 +03:00
|
|
|
|
2006-11-30 21:19:59 +02:00
|
|
|
string readString(Source & source)
|
|
|
|
{
|
2011-12-15 18:19:53 +02:00
|
|
|
size_t len = readInt(source);
|
2006-12-04 19:17:13 +02:00
|
|
|
unsigned char * buf = new unsigned char[len];
|
|
|
|
AutoDeleteArray<unsigned char> d(buf);
|
|
|
|
source(buf, len);
|
2006-11-30 21:19:59 +02:00
|
|
|
readPadding(len, source);
|
2006-12-04 19:17:13 +02:00
|
|
|
return string((char *) buf, len);
|
2006-11-30 21:19:59 +02:00
|
|
|
}
|
|
|
|
|
Allow remote builds without sending the derivation closure
Previously, to build a derivation remotely, we had to copy the entire
closure of the .drv file to the remote machine, even though we only
need the top-level derivation. This is very wasteful: the closure can
contain thousands of store paths, and in some Hydra use cases, include
source paths that are very large (e.g. Git/Mercurial checkouts).
So now there is a new operation, StoreAPI::buildDerivation(), that
performs a build from an in-memory representation of a derivation
(BasicDerivation) rather than from a on-disk .drv file. The only files
that need to be in the Nix store are the sources of the derivation
(drv.inputSrcs), and the needed output paths of the dependencies (as
described by drv.inputDrvs). "nix-store --serve" exposes this
interface.
Note that this is a privileged operation, because you can construct a
derivation that builds any store path whatsoever. Fixing this will
require changing the hashing scheme (i.e., the output paths should be
computed from the other fields in BasicDerivation, allowing them to be
verified without access to other derivations). However, this would be
quite nice because it would allow .drv-free building (e.g. "nix-env
-i" wouldn't have to write any .drv files to disk).
Fixes #173.
2015-07-17 18:57:40 +03:00
|
|
|
Source & operator >> (Source & in, string & s)
|
|
|
|
{
|
|
|
|
s = readString(in);
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-03 13:56:59 +03:00
|
|
|
Source & operator >> (Source & in, unsigned int & n)
|
|
|
|
{
|
|
|
|
n = readInt(in);
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-17 00:31:25 +02:00
|
|
|
template<class T> T readStrings(Source & source)
|
2006-12-01 00:43:55 +02:00
|
|
|
{
|
|
|
|
unsigned int count = readInt(source);
|
2011-12-17 00:31:25 +02:00
|
|
|
T ss;
|
2006-12-01 00:43:55 +02:00
|
|
|
while (count--)
|
2011-12-17 00:31:25 +02:00
|
|
|
ss.insert(ss.end(), readString(source));
|
2006-12-01 00:43:55 +02:00
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
2011-12-17 00:31:25 +02:00
|
|
|
template Paths readStrings(Source & source);
|
|
|
|
template PathSet readStrings(Source & source);
|
|
|
|
|
2006-12-01 00:43:55 +02:00
|
|
|
|
2014-06-10 14:30:09 +03:00
|
|
|
void StringSink::operator () (const unsigned char * data, size_t len)
|
|
|
|
{
|
|
|
|
static bool warned = false;
|
2016-03-04 17:49:56 +02:00
|
|
|
if (!warned && s->size() > threshold) {
|
2014-06-10 14:30:09 +03:00
|
|
|
warnLargeDump();
|
|
|
|
warned = true;
|
|
|
|
}
|
2016-03-04 17:49:56 +02:00
|
|
|
s->append((const char *) data, len);
|
2014-06-10 14:30:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-30 21:19:59 +02:00
|
|
|
}
|