2003-09-11 08:31:29 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <algorithm>
|
2003-06-20 14:11:31 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2003-06-20 10:40:25 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "archive.hh"
|
|
|
|
#include "util.hh"
|
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
2003-06-23 13:27:59 +00:00
|
|
|
static string archiveVersion1 = "nix-archive-1";
|
|
|
|
|
|
|
|
|
|
|
|
static void writePadding(unsigned int len, DumpSink & sink)
|
2003-06-20 10:40:25 +00:00
|
|
|
{
|
|
|
|
if (len % 8) {
|
|
|
|
unsigned char zero[8];
|
|
|
|
memset(zero, 0, sizeof(zero));
|
|
|
|
sink(zero, 8 - (len % 8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void writeInt(unsigned int n, DumpSink & 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void writeString(const string & s, DumpSink & sink)
|
|
|
|
{
|
|
|
|
unsigned int len = s.length();
|
|
|
|
writeInt(len, sink);
|
|
|
|
sink((const unsigned char *) s.c_str(), len);
|
2003-06-23 13:27:59 +00:00
|
|
|
writePadding(len, sink);
|
2003-06-20 10:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-23 13:27:59 +00:00
|
|
|
static void dump(const string & path, DumpSink & sink);
|
|
|
|
|
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
static void dumpEntries(const Path & path, DumpSink & sink)
|
2003-06-20 10:40:25 +00:00
|
|
|
{
|
2003-11-19 17:27:16 +00:00
|
|
|
Strings names = readDirectory(path);
|
|
|
|
vector<string> names2(names.begin(), names.end());
|
|
|
|
sort(names2.begin(), names2.end());
|
2003-06-20 10:40:25 +00:00
|
|
|
|
2003-11-19 17:27:16 +00:00
|
|
|
for (vector<string>::iterator it = names2.begin();
|
|
|
|
it != names2.end(); it++)
|
2003-06-20 10:40:25 +00:00
|
|
|
{
|
|
|
|
writeString("entry", sink);
|
|
|
|
writeString("(", sink);
|
|
|
|
writeString("name", sink);
|
|
|
|
writeString(*it, sink);
|
2003-06-23 13:27:59 +00:00
|
|
|
writeString("node", sink);
|
|
|
|
dump(path + "/" + *it, sink);
|
2003-06-20 10:40:25 +00:00
|
|
|
writeString(")", sink);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
static void dumpContents(const Path & path, unsigned int size,
|
2003-06-20 10:40:25 +00:00
|
|
|
DumpSink & sink)
|
|
|
|
{
|
|
|
|
writeString("contents", sink);
|
|
|
|
writeInt(size, sink);
|
|
|
|
|
2003-10-22 10:48:22 +00:00
|
|
|
AutoCloseFD fd = open(path.c_str(), O_RDONLY);
|
2003-07-14 10:23:11 +00:00
|
|
|
if (fd == -1) throw SysError(format("opening file `%1%'") % path);
|
2003-06-20 10:40:25 +00:00
|
|
|
|
|
|
|
unsigned char buf[65536];
|
2006-08-31 11:40:39 +00:00
|
|
|
unsigned int left = size;
|
2003-06-20 10:40:25 +00:00
|
|
|
|
2006-08-31 15:38:43 +00:00
|
|
|
while (left > 0) {
|
2006-08-31 11:40:39 +00:00
|
|
|
size_t n = left > sizeof(buf) ? sizeof(buf) : left;
|
|
|
|
readFull(fd, buf, n);
|
|
|
|
left -= n;
|
2003-06-20 10:40:25 +00:00
|
|
|
sink(buf, n);
|
|
|
|
}
|
|
|
|
|
2003-06-23 13:27:59 +00:00
|
|
|
writePadding(size, sink);
|
2003-06-20 10:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
static void dump(const Path & path, DumpSink & sink)
|
2003-06-20 10:40:25 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(path.c_str(), &st))
|
2003-07-14 10:23:11 +00:00
|
|
|
throw SysError(format("getting attributes of path `%1%'") % path);
|
2003-06-20 10:40:25 +00:00
|
|
|
|
|
|
|
writeString("(", sink);
|
|
|
|
|
|
|
|
if (S_ISREG(st.st_mode)) {
|
|
|
|
writeString("type", sink);
|
|
|
|
writeString("regular", sink);
|
2003-07-17 11:25:14 +00:00
|
|
|
if (st.st_mode & S_IXUSR) {
|
|
|
|
writeString("executable", sink);
|
|
|
|
writeString("", sink);
|
|
|
|
}
|
2003-06-20 10:40:25 +00:00
|
|
|
dumpContents(path, st.st_size, sink);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (S_ISDIR(st.st_mode)) {
|
|
|
|
writeString("type", sink);
|
|
|
|
writeString("directory", sink);
|
|
|
|
dumpEntries(path, sink);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (S_ISLNK(st.st_mode)) {
|
|
|
|
writeString("type", sink);
|
|
|
|
writeString("symlink", sink);
|
|
|
|
writeString("target", sink);
|
2004-01-05 16:26:43 +00:00
|
|
|
writeString(readLink(path), sink);
|
2003-06-20 10:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else throw Error("unknown file type: " + path);
|
|
|
|
|
|
|
|
writeString(")", sink);
|
|
|
|
}
|
2003-06-20 14:11:31 +00:00
|
|
|
|
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
void dumpPath(const Path & path, DumpSink & sink)
|
2003-06-23 13:27:59 +00:00
|
|
|
{
|
|
|
|
writeString(archiveVersion1, sink);
|
|
|
|
dump(path, sink);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Error badArchive(string s)
|
|
|
|
{
|
|
|
|
return Error("bad archive: " + s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void readPadding(unsigned int len, RestoreSource & 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 badArchive("non-zero padding");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int readInt(RestoreSource & 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static string readString(RestoreSource & source)
|
|
|
|
{
|
|
|
|
unsigned int len = readInt(source);
|
|
|
|
char buf[len];
|
2003-07-20 21:11:43 +00:00
|
|
|
source((unsigned char *) buf, len);
|
2003-06-23 13:27:59 +00:00
|
|
|
readPadding(len, source);
|
|
|
|
return string(buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void skipGeneric(RestoreSource & source)
|
2003-06-20 14:11:31 +00:00
|
|
|
{
|
2003-06-23 13:27:59 +00:00
|
|
|
if (readString(source) == "(") {
|
|
|
|
while (readString(source) != ")")
|
|
|
|
skipGeneric(source);
|
|
|
|
}
|
2003-06-20 14:11:31 +00:00
|
|
|
}
|
2003-06-23 13:27:59 +00:00
|
|
|
|
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
static void restore(const Path & path, RestoreSource & source);
|
2003-06-23 13:27:59 +00:00
|
|
|
|
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
static void restoreEntry(const Path & path, RestoreSource & source)
|
2003-06-23 13:27:59 +00:00
|
|
|
{
|
|
|
|
string s, name;
|
|
|
|
|
|
|
|
s = readString(source);
|
|
|
|
if (s != "(") throw badArchive("expected open tag");
|
|
|
|
|
|
|
|
while (1) {
|
2004-01-15 20:23:55 +00:00
|
|
|
checkInterrupt();
|
|
|
|
|
2003-06-23 13:27:59 +00:00
|
|
|
s = readString(source);
|
|
|
|
|
|
|
|
if (s == ")") {
|
|
|
|
break;
|
|
|
|
} else if (s == "name") {
|
|
|
|
name = readString(source);
|
|
|
|
} else if (s == "node") {
|
|
|
|
if (s == "") throw badArchive("entry name missing");
|
|
|
|
restore(path + "/" + name, source);
|
|
|
|
} else {
|
|
|
|
throw badArchive("unknown field " + s);
|
|
|
|
skipGeneric(source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
static void restoreContents(int fd, const Path & path, RestoreSource & source)
|
2003-06-23 13:27:59 +00:00
|
|
|
{
|
|
|
|
unsigned int size = readInt(source);
|
|
|
|
unsigned int left = size;
|
|
|
|
unsigned char buf[65536];
|
|
|
|
|
|
|
|
while (left) {
|
2004-01-15 20:23:55 +00:00
|
|
|
checkInterrupt();
|
2003-06-23 13:27:59 +00:00
|
|
|
unsigned int n = sizeof(buf);
|
|
|
|
if (n > left) n = left;
|
|
|
|
source(buf, n);
|
2006-08-31 11:40:39 +00:00
|
|
|
writeFull(fd, buf, n);
|
2003-06-23 13:27:59 +00:00
|
|
|
left -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
readPadding(size, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
static void restore(const Path & path, RestoreSource & source)
|
2003-06-23 13:27:59 +00:00
|
|
|
{
|
|
|
|
string s;
|
|
|
|
|
|
|
|
s = readString(source);
|
|
|
|
if (s != "(") throw badArchive("expected open tag");
|
|
|
|
|
|
|
|
enum { tpUnknown, tpRegular, tpDirectory, tpSymlink } type = tpUnknown;
|
2003-10-22 10:48:22 +00:00
|
|
|
AutoCloseFD fd;
|
2003-06-23 13:27:59 +00:00
|
|
|
|
|
|
|
while (1) {
|
2004-01-15 20:23:55 +00:00
|
|
|
checkInterrupt();
|
|
|
|
|
2003-06-23 13:27:59 +00:00
|
|
|
s = readString(source);
|
|
|
|
|
|
|
|
if (s == ")") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (s == "type") {
|
|
|
|
if (type != tpUnknown)
|
|
|
|
throw badArchive("multiple type fields");
|
|
|
|
string t = readString(source);
|
|
|
|
|
|
|
|
if (t == "regular") {
|
|
|
|
type = tpRegular;
|
|
|
|
fd = open(path.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666);
|
|
|
|
if (fd == -1)
|
|
|
|
throw SysError("creating file " + path);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (t == "directory") {
|
|
|
|
type = tpDirectory;
|
|
|
|
if (mkdir(path.c_str(), 0777) == -1)
|
|
|
|
throw SysError("creating directory " + path);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (t == "symlink") {
|
|
|
|
type = tpSymlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
else throw badArchive("unknown file type " + t);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (s == "contents" && type == tpRegular) {
|
|
|
|
restoreContents(fd, path, source);
|
|
|
|
}
|
|
|
|
|
2003-07-17 11:25:14 +00:00
|
|
|
else if (s == "executable" && type == tpRegular) {
|
|
|
|
readString(source);
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(fd, &st) == -1)
|
|
|
|
throw SysError("fstat");
|
|
|
|
if (fchmod(fd, st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1)
|
|
|
|
throw SysError("fchmod");
|
|
|
|
}
|
|
|
|
|
2003-06-23 13:27:59 +00:00
|
|
|
else if (s == "entry" && type == tpDirectory) {
|
|
|
|
restoreEntry(path, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (s == "target" && type == tpSymlink) {
|
|
|
|
string target = readString(source);
|
|
|
|
if (symlink(target.c_str(), path.c_str()) == -1)
|
|
|
|
throw SysError("creating symlink " + path);
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
throw badArchive("unknown field " + s);
|
|
|
|
skipGeneric(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-08 15:06:59 +00:00
|
|
|
void restorePath(const Path & path, RestoreSource & source)
|
2003-06-23 13:27:59 +00:00
|
|
|
{
|
|
|
|
if (readString(source) != archiveVersion1)
|
|
|
|
throw badArchive("expected Nix archive");
|
|
|
|
restore(path, source);
|
|
|
|
}
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
|
|
|
|
}
|