2003-09-11 08:31:29 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <algorithm>
|
2003-06-20 14:11:31 +00:00
|
|
|
#include <vector>
|
2014-07-16 14:02:05 +00:00
|
|
|
#include <map>
|
2014-07-17 21:57:17 +00:00
|
|
|
|
2014-07-18 10:54:30 +00:00
|
|
|
#include <strings.h> // for strcasecmp
|
2003-06-20 14:11:31 +00:00
|
|
|
|
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"
|
2018-03-27 16:41:31 +00:00
|
|
|
#include "config.hh"
|
2003-06-20 10:40:25 +00:00
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
namespace nix {
|
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
struct ArchiveSettings : Config
|
|
|
|
{
|
|
|
|
Setting<bool> useCaseHack{this,
|
|
|
|
#if __APPLE__
|
|
|
|
true,
|
|
|
|
#else
|
|
|
|
false,
|
|
|
|
#endif
|
|
|
|
"use-case-hack",
|
|
|
|
"Whether to enable a Darwin-specific hack for dealing with file name collisions."};
|
|
|
|
};
|
2006-09-04 21:06:23 +00:00
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
static ArchiveSettings archiveSettings;
|
|
|
|
|
|
|
|
static GlobalConfig::Register r1(&archiveSettings);
|
2014-07-16 14:02:05 +00:00
|
|
|
|
2016-02-24 15:52:28 +00:00
|
|
|
const std::string narVersionMagic1 = "nix-archive-1";
|
2003-06-23 13:27:59 +00:00
|
|
|
|
2014-07-16 14:02:05 +00:00
|
|
|
static string caseHackSuffix = "~nix~case~hack~";
|
2003-06-23 13:27:59 +00:00
|
|
|
|
2017-10-30 18:57:40 +00:00
|
|
|
PathFilter defaultPathFilter = [](const Path &) { return true; };
|
2003-06-23 13:27:59 +00:00
|
|
|
|
|
|
|
|
2014-07-16 14:02:05 +00:00
|
|
|
static void dumpContents(const Path & path, size_t size,
|
2006-11-30 19:19:59 +00:00
|
|
|
Sink & sink)
|
2003-06-20 10:40:25 +00:00
|
|
|
{
|
2015-07-19 23:16:16 +00:00
|
|
|
sink << "contents" << size;
|
2003-06-20 10:40:25 +00:00
|
|
|
|
2016-06-09 14:15:58 +00:00
|
|
|
AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
2017-07-30 11:27:57 +00:00
|
|
|
if (!fd) throw SysError(format("opening file '%1%'") % path);
|
2014-07-16 14:02:05 +00:00
|
|
|
|
2018-03-01 21:00:58 +00:00
|
|
|
std::vector<unsigned char> buf(65536);
|
2009-04-16 12:03:17 +00:00
|
|
|
size_t left = size;
|
2003-06-20 10:40:25 +00:00
|
|
|
|
2006-08-31 15:38:43 +00:00
|
|
|
while (left > 0) {
|
2018-03-22 12:20:55 +00:00
|
|
|
auto n = std::min(left, buf.size());
|
2018-03-01 21:00:58 +00:00
|
|
|
readFull(fd.get(), buf.data(), n);
|
2006-08-31 11:40:39 +00:00
|
|
|
left -= n;
|
2018-03-01 21:00:58 +00:00
|
|
|
sink(buf.data(), n);
|
2003-06-20 10:40:25 +00:00
|
|
|
}
|
|
|
|
|
2003-06-23 13:27:59 +00:00
|
|
|
writePadding(size, sink);
|
2003-06-20 10:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-12 23:05:01 +00:00
|
|
|
static void dump(const Path & path, Sink & sink, PathFilter & filter)
|
2003-06-20 10:40:25 +00:00
|
|
|
{
|
2017-08-14 13:28:16 +00:00
|
|
|
checkInterrupt();
|
|
|
|
|
2003-06-20 10:40:25 +00:00
|
|
|
struct stat st;
|
|
|
|
if (lstat(path.c_str(), &st))
|
2017-07-30 11:27:57 +00:00
|
|
|
throw SysError(format("getting attributes of path '%1%'") % path);
|
2003-06-20 10:40:25 +00:00
|
|
|
|
2015-07-19 23:16:16 +00:00
|
|
|
sink << "(";
|
2003-06-20 10:40:25 +00:00
|
|
|
|
|
|
|
if (S_ISREG(st.st_mode)) {
|
2015-07-19 23:16:16 +00:00
|
|
|
sink << "type" << "regular";
|
|
|
|
if (st.st_mode & S_IXUSR)
|
|
|
|
sink << "executable" << "";
|
2009-04-16 12:03:17 +00:00
|
|
|
dumpContents(path, (size_t) st.st_size, sink);
|
2014-07-16 14:02:05 +00:00
|
|
|
}
|
2003-06-20 10:40:25 +00:00
|
|
|
|
|
|
|
else if (S_ISDIR(st.st_mode)) {
|
2015-07-19 23:16:16 +00:00
|
|
|
sink << "type" << "directory";
|
2014-07-16 14:02:05 +00:00
|
|
|
|
2017-07-30 10:26:17 +00:00
|
|
|
/* If we're on a case-insensitive system like macOS, undo
|
2014-07-16 14:02:05 +00:00
|
|
|
the case hack applied by restorePath(). */
|
|
|
|
std::map<string, string> unhacked;
|
2014-08-01 14:37:47 +00:00
|
|
|
for (auto & i : readDirectory(path))
|
2018-03-27 16:41:31 +00:00
|
|
|
if (archiveSettings.useCaseHack) {
|
2014-08-01 14:37:47 +00:00
|
|
|
string name(i.name);
|
|
|
|
size_t pos = i.name.find(caseHackSuffix);
|
2014-07-16 14:02:05 +00:00
|
|
|
if (pos != string::npos) {
|
2017-07-30 11:27:57 +00:00
|
|
|
debug(format("removing case hack suffix from '%1%'") % (path + "/" + i.name));
|
2014-07-16 14:02:05 +00:00
|
|
|
name.erase(pos);
|
|
|
|
}
|
|
|
|
if (unhacked.find(name) != unhacked.end())
|
2017-07-30 11:27:57 +00:00
|
|
|
throw Error(format("file name collision in between '%1%' and '%2%'")
|
2014-08-01 14:37:47 +00:00
|
|
|
% (path + "/" + unhacked[name]) % (path + "/" + i.name));
|
|
|
|
unhacked[name] = i.name;
|
2014-07-16 14:02:05 +00:00
|
|
|
} else
|
2014-08-01 14:37:47 +00:00
|
|
|
unhacked[i.name] = i.name;
|
2014-07-16 14:02:05 +00:00
|
|
|
|
|
|
|
for (auto & i : unhacked)
|
|
|
|
if (filter(path + "/" + i.first)) {
|
2015-07-19 23:16:16 +00:00
|
|
|
sink << "entry" << "(" << "name" << i.first << "node";
|
2014-07-16 14:02:05 +00:00
|
|
|
dump(path + "/" + i.second, sink, filter);
|
2015-07-19 23:16:16 +00:00
|
|
|
sink << ")";
|
2014-07-16 14:02:05 +00:00
|
|
|
}
|
2003-06-20 10:40:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 23:16:16 +00:00
|
|
|
else if (S_ISLNK(st.st_mode))
|
|
|
|
sink << "type" << "symlink" << "target" << readLink(path);
|
2003-06-20 10:40:25 +00:00
|
|
|
|
2017-07-30 11:27:57 +00:00
|
|
|
else throw Error(format("file '%1%' has an unsupported type") % path);
|
2003-06-20 10:40:25 +00:00
|
|
|
|
2015-07-19 23:16:16 +00:00
|
|
|
sink << ")";
|
2003-06-20 10:40:25 +00:00
|
|
|
}
|
2003-06-20 14:11:31 +00:00
|
|
|
|
|
|
|
|
2006-12-12 23:05:01 +00:00
|
|
|
void dumpPath(const Path & path, Sink & sink, PathFilter & filter)
|
2003-06-23 13:27:59 +00:00
|
|
|
{
|
2016-02-24 15:52:28 +00:00
|
|
|
sink << narVersionMagic1;
|
2006-12-12 21:51:02 +00:00
|
|
|
dump(path, sink, filter);
|
2003-06-23 13:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-24 15:52:28 +00:00
|
|
|
void dumpString(const std::string & s, Sink & sink)
|
|
|
|
{
|
|
|
|
sink << narVersionMagic1 << "(" << "type" << "regular" << "contents" << s << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-22 17:36:43 +00:00
|
|
|
static SerialisationError badArchive(string s)
|
2003-06-23 13:27:59 +00:00
|
|
|
{
|
2009-03-22 17:36:43 +00:00
|
|
|
return SerialisationError("bad archive: " + s);
|
2003-06-23 13:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-16 14:02:05 +00:00
|
|
|
#if 0
|
2006-11-30 19:19:59 +00:00
|
|
|
static void skipGeneric(Source & 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
|
|
|
}
|
2014-07-16 14:02:05 +00:00
|
|
|
#endif
|
2003-06-23 13:27:59 +00:00
|
|
|
|
|
|
|
|
2008-12-03 17:30:32 +00:00
|
|
|
static void parseContents(ParseSink & sink, Source & source, const Path & path)
|
2003-06-23 13:27:59 +00:00
|
|
|
{
|
2009-03-22 17:36:43 +00:00
|
|
|
unsigned long long size = readLongLong(source);
|
2014-07-16 14:02:05 +00:00
|
|
|
|
2009-03-22 17:36:43 +00:00
|
|
|
sink.preallocateContents(size);
|
|
|
|
|
|
|
|
unsigned long long left = size;
|
2018-03-01 21:00:58 +00:00
|
|
|
std::vector<unsigned char> buf(65536);
|
2003-06-23 13:27:59 +00:00
|
|
|
|
|
|
|
while (left) {
|
2004-01-15 20:23:55 +00:00
|
|
|
checkInterrupt();
|
2018-03-01 21:00:58 +00:00
|
|
|
auto n = buf.size();
|
|
|
|
if ((unsigned long long)n > left) n = left;
|
|
|
|
source(buf.data(), n);
|
|
|
|
sink.receiveContents(buf.data(), n);
|
2003-06-23 13:27:59 +00:00
|
|
|
left -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
readPadding(size, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-16 14:02:05 +00:00
|
|
|
struct CaseInsensitiveCompare
|
|
|
|
{
|
|
|
|
bool operator() (const string & a, const string & b) const
|
|
|
|
{
|
|
|
|
return strcasecmp(a.c_str(), b.c_str()) < 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-12-03 17:30:32 +00:00
|
|
|
static void parse(ParseSink & sink, Source & source, const Path & path)
|
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;
|
|
|
|
|
2014-07-16 14:02:05 +00:00
|
|
|
std::map<Path, int, CaseInsensitiveCompare> names;
|
|
|
|
|
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;
|
2008-12-03 17:30:32 +00:00
|
|
|
sink.createRegularFile(path);
|
2003-06-23 13:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (t == "directory") {
|
2008-12-03 17:30:32 +00:00
|
|
|
sink.createDirectory(path);
|
2003-06-23 13:27:59 +00:00
|
|
|
type = tpDirectory;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (t == "symlink") {
|
|
|
|
type = tpSymlink;
|
|
|
|
}
|
2014-07-16 14:02:05 +00:00
|
|
|
|
2003-06-23 13:27:59 +00:00
|
|
|
else throw badArchive("unknown file type " + t);
|
2014-07-16 14:02:05 +00:00
|
|
|
|
2003-06-23 13:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (s == "contents" && type == tpRegular) {
|
2008-12-03 17:30:32 +00:00
|
|
|
parseContents(sink, source, path);
|
2003-06-23 13:27:59 +00:00
|
|
|
}
|
|
|
|
|
2003-07-17 11:25:14 +00:00
|
|
|
else if (s == "executable" && type == tpRegular) {
|
2016-02-24 15:52:28 +00:00
|
|
|
auto s = readString(source);
|
|
|
|
if (s != "") throw badArchive("executable marker has non-empty value");
|
2008-12-03 17:30:32 +00:00
|
|
|
sink.isExecutable();
|
2003-07-17 11:25:14 +00:00
|
|
|
}
|
|
|
|
|
2003-06-23 13:27:59 +00:00
|
|
|
else if (s == "entry" && type == tpDirectory) {
|
2014-07-16 14:30:50 +00:00
|
|
|
string name, prevName;
|
2014-07-16 14:02:05 +00: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 14:30:50 +00:00
|
|
|
if (name.empty() || name == "." || name == ".." || name.find('/') != string::npos || name.find((char) 0) != string::npos)
|
2017-07-30 11:27:57 +00:00
|
|
|
throw Error(format("NAR contains invalid file name '%1%'") % name);
|
2014-07-16 14:30:50 +00:00
|
|
|
if (name <= prevName)
|
|
|
|
throw Error("NAR directory is not sorted");
|
|
|
|
prevName = name;
|
2018-03-27 16:41:31 +00:00
|
|
|
if (archiveSettings.useCaseHack) {
|
2014-07-16 14:02:05 +00:00
|
|
|
auto i = names.find(name);
|
|
|
|
if (i != names.end()) {
|
2017-07-30 11:27:57 +00:00
|
|
|
debug(format("case collision between '%1%' and '%2%'") % i->first % name);
|
2014-07-16 14:02:05 +00:00
|
|
|
name += caseHackSuffix;
|
2015-10-29 12:26:55 +00:00
|
|
|
name += std::to_string(++i->second);
|
2014-07-16 14:02:05 +00: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 13:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (s == "target" && type == tpSymlink) {
|
|
|
|
string target = readString(source);
|
2008-12-03 17:30:32 +00:00
|
|
|
sink.createSymlink(path, target);
|
2003-06-23 13:27:59 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 14:02:05 +00:00
|
|
|
else
|
2003-06-23 13:27:59 +00:00
|
|
|
throw badArchive("unknown field " + s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-03 17:30:32 +00:00
|
|
|
void parseDump(ParseSink & sink, Source & source)
|
2003-06-23 13:27:59 +00:00
|
|
|
{
|
2014-07-16 14:02:05 +00:00
|
|
|
string version;
|
2009-03-22 17:36:43 +00: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 15:52:28 +00:00
|
|
|
if (version != narVersionMagic1)
|
2009-03-22 17:36:43 +00:00
|
|
|
throw badArchive("input doesn't look like a Nix archive");
|
2008-12-03 17:30:32 +00: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)
|
2017-07-30 11:27:57 +00:00
|
|
|
throw SysError(format("creating directory '%1%'") % p);
|
2008-12-03 17:30:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void createRegularFile(const Path & path)
|
|
|
|
{
|
|
|
|
Path p = dstPath + path;
|
2016-06-09 14:15:58 +00:00
|
|
|
fd = open(p.c_str(), O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, 0666);
|
2017-07-30 11:27:57 +00:00
|
|
|
if (!fd) throw SysError(format("creating file '%1%'") % p);
|
2008-12-03 17:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void isExecutable()
|
|
|
|
{
|
|
|
|
struct stat st;
|
2016-07-11 19:44:44 +00:00
|
|
|
if (fstat(fd.get(), &st) == -1)
|
2008-12-03 17:30:32 +00:00
|
|
|
throw SysError("fstat");
|
2016-07-11 19:44:44 +00:00
|
|
|
if (fchmod(fd.get(), st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1)
|
2008-12-03 17:30:32 +00:00
|
|
|
throw SysError("fchmod");
|
|
|
|
}
|
|
|
|
|
2009-03-22 17:36:43 +00:00
|
|
|
void preallocateContents(unsigned long long len)
|
|
|
|
{
|
|
|
|
#if HAVE_POSIX_FALLOCATE
|
|
|
|
if (len) {
|
2016-07-11 19:44:44 +00:00
|
|
|
errno = posix_fallocate(fd.get(), 0, len);
|
2009-09-30 09:54:29 +00: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 17:36:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-12-03 17:30:32 +00:00
|
|
|
void receiveContents(unsigned char * data, unsigned int len)
|
|
|
|
{
|
2016-07-11 19:44:44 +00:00
|
|
|
writeFull(fd.get(), data, len);
|
2008-12-03 17:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void createSymlink(const Path & path, const string & target)
|
|
|
|
{
|
|
|
|
Path p = dstPath + path;
|
2014-02-27 22:17:53 +00:00
|
|
|
nix::createSymlink(target, p);
|
2008-12-03 17:30:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-16 14:02:05 +00:00
|
|
|
|
2008-12-03 17:30:32 +00:00
|
|
|
void restorePath(const Path & path, Source & source)
|
|
|
|
{
|
|
|
|
RestoreSink sink;
|
|
|
|
sink.dstPath = path;
|
|
|
|
parseDump(sink, source);
|
2003-06-23 13:27:59 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 14:02:05 +00:00
|
|
|
|
2018-03-21 21:56:02 +00:00
|
|
|
void copyNAR(Source & source, Sink & sink)
|
|
|
|
{
|
|
|
|
// FIXME: if 'source' is the output of dumpPath() followed by EOF,
|
|
|
|
// we should just forward all data directly without parsing.
|
|
|
|
|
|
|
|
ParseSink parseSink; /* null sink; just parse the NAR */
|
|
|
|
|
|
|
|
LambdaSource wrapper([&](unsigned char * data, size_t len) {
|
|
|
|
auto n = source.read(data, len);
|
|
|
|
sink(data, n);
|
|
|
|
return n;
|
|
|
|
});
|
|
|
|
|
|
|
|
parseDump(parseSink, wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|