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