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>
|
2014-07-16 16:02:05 +02:00
|
|
|
#include <map>
|
2014-07-17 23:57:17 +02:00
|
|
|
|
2014-07-18 12:54:30 +02:00
|
|
|
#include <strings.h> // for strcasecmp
|
2003-06-20 16:11:31 +02:00
|
|
|
|
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 {
|
|
|
|
|
|
|
|
|
2014-07-16 16:02:05 +02:00
|
|
|
bool useCaseHack =
|
|
|
|
#if __APPLE__
|
|
|
|
true;
|
|
|
|
#else
|
|
|
|
false;
|
|
|
|
#endif
|
|
|
|
|
2016-02-24 16:52:28 +01:00
|
|
|
const std::string narVersionMagic1 = "nix-archive-1";
|
2003-06-23 15:27:59 +02:00
|
|
|
|
2014-07-16 16:02:05 +02:00
|
|
|
static string caseHackSuffix = "~nix~case~hack~";
|
2003-06-23 15:27:59 +02:00
|
|
|
|
2006-12-13 00:05:01 +01:00
|
|
|
PathFilter defaultPathFilter;
|
2003-06-23 15:27:59 +02:00
|
|
|
|
|
|
|
|
2014-07-16 16:02:05 +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
|
|
|
{
|
2015-07-20 01:16:16 +02:00
|
|
|
sink << "contents" << size;
|
2003-06-20 12:40:25 +02:00
|
|
|
|
2016-06-09 16:15:58 +02:00
|
|
|
AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
2016-11-25 15:48:27 +01:00
|
|
|
if (!fd) throw SysError(format("opening file '%1%'") % path);
|
2014-07-16 16:02:05 +02:00
|
|
|
|
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;
|
2016-07-11 21:44:44 +02:00
|
|
|
readFull(fd.get(), buf, n);
|
2006-08-31 13:40:39 +02:00
|
|
|
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))
|
2016-11-25 15:48:27 +01:00
|
|
|
throw SysError(format("getting attributes of path '%1%'") % path);
|
2003-06-20 12:40:25 +02:00
|
|
|
|
2015-07-20 01:16:16 +02:00
|
|
|
sink << "(";
|
2003-06-20 12:40:25 +02:00
|
|
|
|
|
|
|
if (S_ISREG(st.st_mode)) {
|
2015-07-20 01:16:16 +02:00
|
|
|
sink << "type" << "regular";
|
|
|
|
if (st.st_mode & S_IXUSR)
|
|
|
|
sink << "executable" << "";
|
2009-04-16 14:03:17 +02:00
|
|
|
dumpContents(path, (size_t) st.st_size, sink);
|
2014-07-16 16:02:05 +02:00
|
|
|
}
|
2003-06-20 12:40:25 +02:00
|
|
|
|
|
|
|
else if (S_ISDIR(st.st_mode)) {
|
2015-07-20 01:16:16 +02:00
|
|
|
sink << "type" << "directory";
|
2014-07-16 16:02:05 +02:00
|
|
|
|
|
|
|
/* If we're on a case-insensitive system like Mac OS X, undo
|
|
|
|
the case hack applied by restorePath(). */
|
|
|
|
std::map<string, string> unhacked;
|
2014-08-01 16:37:47 +02:00
|
|
|
for (auto & i : readDirectory(path))
|
2014-07-16 16:02:05 +02:00
|
|
|
if (useCaseHack) {
|
2014-08-01 16:37:47 +02:00
|
|
|
string name(i.name);
|
|
|
|
size_t pos = i.name.find(caseHackSuffix);
|
2014-07-16 16:02:05 +02:00
|
|
|
if (pos != string::npos) {
|
2016-11-25 15:48:27 +01:00
|
|
|
debug(format("removing case hack suffix from '%1%'") % (path + "/" + i.name));
|
2014-07-16 16:02:05 +02:00
|
|
|
name.erase(pos);
|
|
|
|
}
|
|
|
|
if (unhacked.find(name) != unhacked.end())
|
2016-11-25 15:48:27 +01:00
|
|
|
throw Error(format("file name collision in between '%1%' and '%2%'")
|
2014-08-01 16:37:47 +02:00
|
|
|
% (path + "/" + unhacked[name]) % (path + "/" + i.name));
|
|
|
|
unhacked[name] = i.name;
|
2014-07-16 16:02:05 +02:00
|
|
|
} else
|
2014-08-01 16:37:47 +02:00
|
|
|
unhacked[i.name] = i.name;
|
2014-07-16 16:02:05 +02:00
|
|
|
|
|
|
|
for (auto & i : unhacked)
|
|
|
|
if (filter(path + "/" + i.first)) {
|
2015-07-20 01:16:16 +02:00
|
|
|
sink << "entry" << "(" << "name" << i.first << "node";
|
2014-07-16 16:02:05 +02:00
|
|
|
dump(path + "/" + i.second, sink, filter);
|
2015-07-20 01:16:16 +02:00
|
|
|
sink << ")";
|
2014-07-16 16:02:05 +02:00
|
|
|
}
|
2003-06-20 12:40:25 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 01:16:16 +02:00
|
|
|
else if (S_ISLNK(st.st_mode))
|
|
|
|
sink << "type" << "symlink" << "target" << readLink(path);
|
2003-06-20 12:40:25 +02:00
|
|
|
|
2016-11-25 15:48:27 +01:00
|
|
|
else throw Error(format("file '%1%' has an unsupported type") % path);
|
2003-06-20 12:40:25 +02:00
|
|
|
|
2015-07-20 01:16:16 +02:00
|
|
|
sink << ")";
|
2003-06-20 12:40:25 +02:00
|
|
|
}
|
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
|
|
|
{
|
2016-02-24 16:52:28 +01:00
|
|
|
sink << narVersionMagic1;
|
2006-12-12 22:51:02 +01:00
|
|
|
dump(path, sink, filter);
|
2003-06-23 15:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-24 16:52:28 +01:00
|
|
|
void dumpString(const std::string & s, Sink & sink)
|
|
|
|
{
|
|
|
|
sink << narVersionMagic1 << "(" << "type" << "regular" << "contents" << s << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-16 16:02:05 +02:00
|
|
|
#if 0
|
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
|
|
|
}
|
2014-07-16 16:02:05 +02:00
|
|
|
#endif
|
2003-06-23 15:27:59 +02:00
|
|
|
|
|
|
|
|
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);
|
2014-07-16 16:02:05 +02:00
|
|
|
|
2009-03-22 18:36:43 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-16 16:02:05 +02:00
|
|
|
struct CaseInsensitiveCompare
|
|
|
|
{
|
|
|
|
bool operator() (const string & a, const string & b) const
|
|
|
|
{
|
|
|
|
return strcasecmp(a.c_str(), b.c_str()) < 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-07-16 16:02:05 +02:00
|
|
|
std::map<Path, int, CaseInsensitiveCompare> names;
|
|
|
|
|
2003-06-23 15:27:59 +02:00
|
|
|
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;
|
|
|
|
}
|
2014-07-16 16:02:05 +02:00
|
|
|
|
2003-06-23 15:27:59 +02:00
|
|
|
else throw badArchive("unknown file type " + t);
|
2014-07-16 16:02:05 +02:00
|
|
|
|
2003-06-23 15:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2016-02-24 16:52:28 +01:00
|
|
|
auto s = readString(source);
|
|
|
|
if (s != "") throw badArchive("executable marker has non-empty value");
|
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) {
|
2014-07-16 16:30:50 +02:00
|
|
|
string name, prevName;
|
2014-07-16 16:02:05 +02:00
|
|
|
|
|
|
|
s = readString(source);
|
|
|
|
if (s != "(") throw badArchive("expected open tag");
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
checkInterrupt();
|
|
|
|
|
|
|
|
s = readString(source);
|
|
|
|
|
|
|
|
if (s == ")") {
|
|
|
|
break;
|
|
|
|
} else if (s == "name") {
|
|
|
|
name = readString(source);
|
2014-07-16 16:30:50 +02:00
|
|
|
if (name.empty() || name == "." || name == ".." || name.find('/') != string::npos || name.find((char) 0) != string::npos)
|
2016-11-25 15:48:27 +01:00
|
|
|
throw Error(format("NAR contains invalid file name '%1%'") % name);
|
2014-07-16 16:30:50 +02:00
|
|
|
if (name <= prevName)
|
|
|
|
throw Error("NAR directory is not sorted");
|
|
|
|
prevName = name;
|
2014-07-16 16:02:05 +02:00
|
|
|
if (useCaseHack) {
|
|
|
|
auto i = names.find(name);
|
|
|
|
if (i != names.end()) {
|
2016-11-25 15:48:27 +01:00
|
|
|
debug(format("case collision between '%1%' and '%2%'") % i->first % name);
|
2014-07-16 16:02:05 +02:00
|
|
|
name += caseHackSuffix;
|
2015-10-29 13:26:55 +01:00
|
|
|
name += std::to_string(++i->second);
|
2014-07-16 16:02:05 +02:00
|
|
|
} else
|
|
|
|
names[name] = 0;
|
|
|
|
}
|
|
|
|
} else if (s == "node") {
|
|
|
|
if (s.empty()) throw badArchive("entry name missing");
|
|
|
|
parse(sink, source, path + "/" + name);
|
|
|
|
} else
|
|
|
|
throw badArchive("unknown field " + s);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-07-16 16:02:05 +02:00
|
|
|
else
|
2003-06-23 15:27:59 +02:00
|
|
|
throw badArchive("unknown field " + s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-03 18:30:32 +01:00
|
|
|
void parseDump(ParseSink & sink, Source & source)
|
2003-06-23 15:27:59 +02:00
|
|
|
{
|
2014-07-16 16:02:05 +02:00
|
|
|
string version;
|
2009-03-22 18:36:43 +01:00
|
|
|
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. */
|
|
|
|
}
|
2016-02-24 16:52:28 +01:00
|
|
|
if (version != narVersionMagic1)
|
2009-03-22 18:36:43 +01:00
|
|
|
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)
|
2016-11-25 15:48:27 +01:00
|
|
|
throw SysError(format("creating directory '%1%'") % p);
|
2008-12-03 18:30:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void createRegularFile(const Path & path)
|
|
|
|
{
|
|
|
|
Path p = dstPath + path;
|
2016-06-09 16:15:58 +02:00
|
|
|
fd = open(p.c_str(), O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, 0666);
|
2016-11-25 15:48:27 +01:00
|
|
|
if (!fd) throw SysError(format("creating file '%1%'") % p);
|
2008-12-03 18:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void isExecutable()
|
|
|
|
{
|
|
|
|
struct stat st;
|
2016-07-11 21:44:44 +02:00
|
|
|
if (fstat(fd.get(), &st) == -1)
|
2008-12-03 18:30:32 +01:00
|
|
|
throw SysError("fstat");
|
2016-07-11 21:44:44 +02:00
|
|
|
if (fchmod(fd.get(), st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1)
|
2008-12-03 18:30:32 +01:00
|
|
|
throw SysError("fchmod");
|
|
|
|
}
|
|
|
|
|
2009-03-22 18:36:43 +01:00
|
|
|
void preallocateContents(unsigned long long len)
|
|
|
|
{
|
|
|
|
#if HAVE_POSIX_FALLOCATE
|
|
|
|
if (len) {
|
2016-07-11 21:44:44 +02:00
|
|
|
errno = posix_fallocate(fd.get(), 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)
|
|
|
|
{
|
2016-07-11 21:44:44 +02:00
|
|
|
writeFull(fd.get(), data, len);
|
2008-12-03 18:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-16 16:02:05 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-07-16 16:02:05 +02:00
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
}
|