2009-12-17 15:12:44 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2003-09-11 10:31:29 +02:00
|
|
|
#include <cerrno>
|
|
|
|
#include <algorithm>
|
2003-06-20 16:11:31 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2009-03-22 18:36:43 +01:00
|
|
|
#define _XOPEN_SOURCE 600
|
2003-06-20 12:40:25 +02: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 23:06:23 +02:00
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
2003-06-23 15:27:59 +02:00
|
|
|
static string archiveVersion1 = "nix-archive-1";
|
|
|
|
|
|
|
|
|
2006-12-13 00:05:01 +01:00
|
|
|
PathFilter defaultPathFilter;
|
2003-06-23 15:27:59 +02:00
|
|
|
|
|
|
|
|
2006-12-13 00:05:01 +01:00
|
|
|
static void dump(const string & path, Sink & sink, PathFilter & filter);
|
2006-12-12 22:51:02 +01:00
|
|
|
|
|
|
|
|
2006-12-13 00:05:01 +01:00
|
|
|
static void dumpEntries(const Path & path, Sink & sink, PathFilter & filter)
|
2003-06-20 12:40:25 +02:00
|
|
|
{
|
2003-11-19 18:27:16 +01:00
|
|
|
Strings names = readDirectory(path);
|
|
|
|
vector<string> names2(names.begin(), names.end());
|
|
|
|
sort(names2.begin(), names2.end());
|
2003-06-20 12:40:25 +02:00
|
|
|
|
2006-12-13 00:05:01 +01:00
|
|
|
for (vector<string>::iterator i = names2.begin();
|
|
|
|
i != names2.end(); ++i)
|
2003-06-20 12:40:25 +02:00
|
|
|
{
|
2006-12-13 00:05:01 +01:00
|
|
|
Path entry = path + "/" + *i;
|
|
|
|
if (filter(entry)) {
|
2006-12-12 22:51:02 +01:00
|
|
|
writeString("entry", sink);
|
|
|
|
writeString("(", sink);
|
|
|
|
writeString("name", sink);
|
2006-12-13 00:05:01 +01:00
|
|
|
writeString(*i, sink);
|
2006-12-12 22:51:02 +01:00
|
|
|
writeString("node", sink);
|
2006-12-13 00:05:01 +01:00
|
|
|
dump(entry, sink, filter);
|
2006-12-12 22:51:02 +01:00
|
|
|
writeString(")", sink);
|
|
|
|
}
|
2003-06-20 12:40:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-16 14:03:17 +02:00
|
|
|
static void dumpContents(const Path & path, size_t size,
|
2006-11-30 20:19:59 +01:00
|
|
|
Sink & sink)
|
2003-06-20 12:40:25 +02:00
|
|
|
{
|
|
|
|
writeString("contents", sink);
|
2009-03-22 18:36:43 +01:00
|
|
|
writeLongLong(size, sink);
|
2003-06-20 12:40:25 +02:00
|
|
|
|
2003-10-22 12:48:22 +02:00
|
|
|
AutoCloseFD fd = open(path.c_str(), O_RDONLY);
|
2003-07-14 12:23:11 +02:00
|
|
|
if (fd == -1) throw SysError(format("opening file `%1%'") % path);
|
2003-06-20 12:40:25 +02:00
|
|
|
|
|
|
|
unsigned char buf[65536];
|
2009-04-16 14:03:17 +02:00
|
|
|
size_t left = size;
|
2003-06-20 12:40:25 +02:00
|
|
|
|
2006-08-31 17:38:43 +02:00
|
|
|
while (left > 0) {
|
2006-08-31 13:40:39 +02:00
|
|
|
size_t n = left > sizeof(buf) ? sizeof(buf) : left;
|
|
|
|
readFull(fd, buf, n);
|
|
|
|
left -= n;
|
2003-06-20 12:40:25 +02:00
|
|
|
sink(buf, n);
|
|
|
|
}
|
|
|
|
|
2003-06-23 15:27:59 +02:00
|
|
|
writePadding(size, sink);
|
2003-06-20 12:40:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-13 00:05:01 +01:00
|
|
|
static void dump(const Path & path, Sink & sink, PathFilter & filter)
|
2003-06-20 12:40:25 +02:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(path.c_str(), &st))
|
2003-07-14 12:23:11 +02:00
|
|
|
throw SysError(format("getting attributes of path `%1%'") % path);
|
2003-06-20 12:40:25 +02:00
|
|
|
|
|
|
|
writeString("(", sink);
|
|
|
|
|
|
|
|
if (S_ISREG(st.st_mode)) {
|
|
|
|
writeString("type", sink);
|
|
|
|
writeString("regular", sink);
|
2003-07-17 13:25:14 +02:00
|
|
|
if (st.st_mode & S_IXUSR) {
|
|
|
|
writeString("executable", sink);
|
|
|
|
writeString("", sink);
|
|
|
|
}
|
2009-04-16 14:03:17 +02:00
|
|
|
dumpContents(path, (size_t) st.st_size, sink);
|
2003-06-20 12:40:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (S_ISDIR(st.st_mode)) {
|
|
|
|
writeString("type", sink);
|
|
|
|
writeString("directory", sink);
|
2006-12-12 22:51:02 +01:00
|
|
|
dumpEntries(path, sink, filter);
|
2003-06-20 12:40:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (S_ISLNK(st.st_mode)) {
|
|
|
|
writeString("type", sink);
|
|
|
|
writeString("symlink", sink);
|
|
|
|
writeString("target", sink);
|
2004-01-05 17:26:43 +01:00
|
|
|
writeString(readLink(path), sink);
|
2003-06-20 12:40:25 +02:00
|
|
|
}
|
|
|
|
|
2009-03-22 18:36:43 +01:00
|
|
|
else throw Error(format("file `%1%' has an unknown type") % path);
|
2003-06-20 12:40:25 +02:00
|
|
|
|
|
|
|
writeString(")", sink);
|
|
|
|
}
|
2003-06-20 16:11:31 +02:00
|
|
|
|
|
|
|
|
2006-12-13 00:05:01 +01:00
|
|
|
void dumpPath(const Path & path, Sink & sink, PathFilter & filter)
|
2003-06-23 15:27:59 +02:00
|
|
|
{
|
|
|
|
writeString(archiveVersion1, sink);
|
2006-12-12 22:51:02 +01:00
|
|
|
dump(path, sink, filter);
|
2003-06-23 15:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-22 18:36:43 +01:00
|
|
|
static SerialisationError badArchive(string s)
|
2003-06-23 15:27:59 +02:00
|
|
|
{
|
2009-03-22 18:36:43 +01:00
|
|
|
return SerialisationError("bad archive: " + s);
|
2003-06-23 15:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-30 20:19:59 +01:00
|
|
|
static void skipGeneric(Source & source)
|
2003-06-20 16:11:31 +02:00
|
|
|
{
|
2003-06-23 15:27:59 +02:00
|
|
|
if (readString(source) == "(") {
|
|
|
|
while (readString(source) != ")")
|
|
|
|
skipGeneric(source);
|
|
|
|
}
|
2003-06-20 16:11:31 +02:00
|
|
|
}
|
2003-06-23 15:27:59 +02:00
|
|
|
|
|
|
|
|
2008-12-03 18:30:32 +01:00
|
|
|
static void parse(ParseSink & sink, Source & source, const Path & path);
|
2003-06-23 15:27:59 +02:00
|
|
|
|
|
|
|
|
2008-12-03 18:30:32 +01:00
|
|
|
|
|
|
|
static void parseEntry(ParseSink & sink, Source & source, const Path & path)
|
2003-06-23 15:27:59 +02:00
|
|
|
{
|
|
|
|
string s, name;
|
|
|
|
|
|
|
|
s = readString(source);
|
|
|
|
if (s != "(") throw badArchive("expected open tag");
|
|
|
|
|
|
|
|
while (1) {
|
2004-01-15 21:23:55 +01:00
|
|
|
checkInterrupt();
|
|
|
|
|
2003-06-23 15:27:59 +02: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");
|
2008-12-03 18:30:32 +01:00
|
|
|
parse(sink, source, path + "/" + name);
|
2003-06-23 15:27:59 +02:00
|
|
|
} else {
|
|
|
|
throw badArchive("unknown field " + s);
|
|
|
|
skipGeneric(source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-03 18:30:32 +01:00
|
|
|
static void parseContents(ParseSink & sink, Source & source, const Path & path)
|
2003-06-23 15:27:59 +02:00
|
|
|
{
|
2009-03-22 18:36:43 +01:00
|
|
|
unsigned long long size = readLongLong(source);
|
|
|
|
|
|
|
|
sink.preallocateContents(size);
|
|
|
|
|
|
|
|
unsigned long long left = size;
|
2003-06-23 15:27:59 +02:00
|
|
|
unsigned char buf[65536];
|
|
|
|
|
|
|
|
while (left) {
|
2004-01-15 21:23:55 +01:00
|
|
|
checkInterrupt();
|
2003-06-23 15:27:59 +02:00
|
|
|
unsigned int n = sizeof(buf);
|
2009-03-22 18:36:43 +01:00
|
|
|
if ((unsigned long long) n > left) n = left;
|
2003-06-23 15:27:59 +02:00
|
|
|
source(buf, n);
|
2008-12-03 18:30:32 +01:00
|
|
|
sink.receiveContents(buf, n);
|
2003-06-23 15:27:59 +02:00
|
|
|
left -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
readPadding(size, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-03 18:30:32 +01:00
|
|
|
static void parse(ParseSink & sink, Source & source, const Path & path)
|
2003-06-23 15:27:59 +02:00
|
|
|
{
|
|
|
|
string s;
|
|
|
|
|
|
|
|
s = readString(source);
|
|
|
|
if (s != "(") throw badArchive("expected open tag");
|
|
|
|
|
|
|
|
enum { tpUnknown, tpRegular, tpDirectory, tpSymlink } type = tpUnknown;
|
|
|
|
|
|
|
|
while (1) {
|
2004-01-15 21:23:55 +01:00
|
|
|
checkInterrupt();
|
|
|
|
|
2003-06-23 15:27:59 +02: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;
|
2008-12-03 18:30:32 +01:00
|
|
|
sink.createRegularFile(path);
|
2003-06-23 15:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (t == "directory") {
|
2008-12-03 18:30:32 +01:00
|
|
|
sink.createDirectory(path);
|
2003-06-23 15:27:59 +02:00
|
|
|
type = tpDirectory;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (t == "symlink") {
|
|
|
|
type = tpSymlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
else throw badArchive("unknown file type " + t);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (s == "contents" && type == tpRegular) {
|
2008-12-03 18:30:32 +01:00
|
|
|
parseContents(sink, source, path);
|
2003-06-23 15:27:59 +02:00
|
|
|
}
|
|
|
|
|
2003-07-17 13:25:14 +02:00
|
|
|
else if (s == "executable" && type == tpRegular) {
|
|
|
|
readString(source);
|
2008-12-03 18:30:32 +01:00
|
|
|
sink.isExecutable();
|
2003-07-17 13:25:14 +02:00
|
|
|
}
|
|
|
|
|
2003-06-23 15:27:59 +02:00
|
|
|
else if (s == "entry" && type == tpDirectory) {
|
2008-12-03 18:30:32 +01:00
|
|
|
parseEntry(sink, source, path);
|
2003-06-23 15:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (s == "target" && type == tpSymlink) {
|
|
|
|
string target = readString(source);
|
2008-12-03 18:30:32 +01:00
|
|
|
sink.createSymlink(path, target);
|
2003-06-23 15:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
throw badArchive("unknown field " + s);
|
|
|
|
skipGeneric(source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-03 18:30:32 +01:00
|
|
|
void parseDump(ParseSink & sink, Source & source)
|
2003-06-23 15:27:59 +02:00
|
|
|
{
|
2009-03-22 18:36:43 +01:00
|
|
|
string version;
|
|
|
|
try {
|
|
|
|
version = readString(source);
|
|
|
|
} catch (SerialisationError & e) {
|
|
|
|
/* This generally means the integer at the start couldn't be
|
|
|
|
decoded. Ignore and throw the exception below. */
|
|
|
|
}
|
|
|
|
if (version != archiveVersion1)
|
|
|
|
throw badArchive("input doesn't look like a Nix archive");
|
2008-12-03 18:30:32 +01:00
|
|
|
parse(sink, source, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct RestoreSink : ParseSink
|
|
|
|
{
|
|
|
|
Path dstPath;
|
|
|
|
AutoCloseFD fd;
|
|
|
|
|
|
|
|
void createDirectory(const Path & path)
|
|
|
|
{
|
|
|
|
Path p = dstPath + path;
|
|
|
|
if (mkdir(p.c_str(), 0777) == -1)
|
|
|
|
throw SysError(format("creating directory `%1%'") % p);
|
|
|
|
};
|
|
|
|
|
|
|
|
void createRegularFile(const Path & path)
|
|
|
|
{
|
|
|
|
Path p = dstPath + path;
|
2013-09-17 14:06:59 +02:00
|
|
|
fd.close();
|
2008-12-03 18:30:32 +01:00
|
|
|
fd = open(p.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666);
|
|
|
|
if (fd == -1) throw SysError(format("creating file `%1%'") % p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void isExecutable()
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2009-03-22 18:36:43 +01:00
|
|
|
void preallocateContents(unsigned long long len)
|
|
|
|
{
|
|
|
|
#if HAVE_POSIX_FALLOCATE
|
|
|
|
if (len) {
|
|
|
|
errno = posix_fallocate(fd, 0, len);
|
2009-09-30 11:54:29 +02:00
|
|
|
/* Note that EINVAL may indicate that the underlying
|
|
|
|
filesystem doesn't support preallocation (e.g. on
|
|
|
|
OpenSolaris). Since preallocation is just an
|
|
|
|
optimisation, ignore it. */
|
|
|
|
if (errno && errno != EINVAL)
|
|
|
|
throw SysError(format("preallocating file of %1% bytes") % len);
|
2009-03-22 18:36:43 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-12-03 18:30:32 +01:00
|
|
|
void receiveContents(unsigned char * data, unsigned int len)
|
|
|
|
{
|
|
|
|
writeFull(fd, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void createSymlink(const Path & path, const string & target)
|
|
|
|
{
|
|
|
|
Path p = dstPath + path;
|
2014-02-27 23:17:53 +01:00
|
|
|
nix::createSymlink(target, p);
|
2008-12-03 18:30:32 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void restorePath(const Path & path, Source & source)
|
|
|
|
{
|
|
|
|
RestoreSink sink;
|
|
|
|
sink.dstPath = path;
|
|
|
|
parseDump(sink, source);
|
2003-06-23 15:27:59 +02:00
|
|
|
}
|
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
|
|
|
|
}
|