mirror of
https://github.com/NixOS/nix
synced 2025-06-27 08:31:16 +02:00
* Skeleton of the privileged worker program.
* Some refactoring: put the NAR archive integer/string serialisation code in a separate file so it can be reused by the worker protocol implementation.
This commit is contained in:
parent
9adc074dc3
commit
40b3f64b55
12 changed files with 255 additions and 126 deletions
87
src/libutil/serialise.cc
Normal file
87
src/libutil/serialise.cc
Normal file
|
@ -0,0 +1,87 @@
|
|||
#include "serialise.hh"
|
||||
#include "util.hh"
|
||||
|
||||
|
||||
namespace nix {
|
||||
|
||||
|
||||
void FdSink::operator () (const unsigned char * data, unsigned int len)
|
||||
{
|
||||
writeFull(fd, data, len);
|
||||
}
|
||||
|
||||
|
||||
void FdSource::operator () (unsigned char * data, unsigned int len)
|
||||
{
|
||||
readFull(fd, data, len);
|
||||
}
|
||||
|
||||
|
||||
void writePadding(unsigned int len, Sink & sink)
|
||||
{
|
||||
if (len % 8) {
|
||||
unsigned char zero[8];
|
||||
memset(zero, 0, sizeof(zero));
|
||||
sink(zero, 8 - (len % 8));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
void writeString(const string & s, Sink & sink)
|
||||
{
|
||||
unsigned int len = s.length();
|
||||
writeInt(len, sink);
|
||||
sink((const unsigned char *) s.c_str(), len);
|
||||
writePadding(len, sink);
|
||||
}
|
||||
|
||||
|
||||
void readPadding(unsigned int len, Source & 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 Error("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 Error("implementation cannot deal with > 32-bit integers");
|
||||
return
|
||||
buf[0] |
|
||||
(buf[1] << 8) |
|
||||
(buf[2] << 16) |
|
||||
(buf[3] << 24);
|
||||
}
|
||||
|
||||
|
||||
string readString(Source & source)
|
||||
{
|
||||
unsigned int len = readInt(source);
|
||||
char buf[len];
|
||||
source((unsigned char *) buf, len);
|
||||
readPadding(len, source);
|
||||
return string(buf, len);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue