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"
|
2023-10-19 16:07:56 +03:00
|
|
|
#include "source-accessor.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;
|
|
|
|
|
2020-10-06 14:36:55 +03:00
|
|
|
static GlobalConfig::Register rArchiveSettings(&archiveSettings);
|
2014-07-16 17:02:05 +03:00
|
|
|
|
2017-10-30 20:57:40 +02:00
|
|
|
PathFilter defaultPathFilter = [](const Path &) { return true; };
|
2003-06-23 16:27:59 +03:00
|
|
|
|
|
|
|
|
2023-10-19 16:07:56 +03:00
|
|
|
void SourceAccessor::dumpPath(
|
|
|
|
const CanonPath & path,
|
|
|
|
Sink & sink,
|
|
|
|
PathFilter & filter)
|
2003-06-20 13:40:25 +03:00
|
|
|
{
|
2023-10-19 16:07:56 +03:00
|
|
|
auto dumpContents = [&](const CanonPath & path)
|
|
|
|
{
|
2023-10-20 17:36:41 +03:00
|
|
|
sink << "contents";
|
|
|
|
std::optional<uint64_t> size;
|
|
|
|
readFile(path, sink, [&](uint64_t _size)
|
|
|
|
{
|
|
|
|
size = _size;
|
|
|
|
sink << _size;
|
|
|
|
});
|
|
|
|
assert(size);
|
|
|
|
writePadding(*size, sink);
|
2023-10-19 16:07:56 +03:00
|
|
|
};
|
2003-06-20 13:40:25 +03:00
|
|
|
|
2023-10-19 16:07:56 +03:00
|
|
|
std::function<void(const CanonPath & path)> dump;
|
2014-07-16 17:02:05 +03:00
|
|
|
|
2023-10-19 16:07:56 +03:00
|
|
|
dump = [&](const CanonPath & path) {
|
|
|
|
checkInterrupt();
|
2003-06-20 13:40:25 +03:00
|
|
|
|
2023-10-19 16:07:56 +03:00
|
|
|
auto st = lstat(path);
|
|
|
|
|
|
|
|
sink << "(";
|
|
|
|
|
|
|
|
if (st.type == tRegular) {
|
|
|
|
sink << "type" << "regular";
|
|
|
|
if (st.isExecutable)
|
|
|
|
sink << "executable" << "";
|
|
|
|
dumpContents(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (st.type == tDirectory) {
|
|
|
|
sink << "type" << "directory";
|
|
|
|
|
|
|
|
/* If we're on a case-insensitive system like macOS, undo
|
|
|
|
the case hack applied by restorePath(). */
|
|
|
|
std::map<std::string, std::string> unhacked;
|
|
|
|
for (auto & i : readDirectory(path))
|
|
|
|
if (archiveSettings.useCaseHack) {
|
|
|
|
std::string name(i.first);
|
|
|
|
size_t pos = i.first.find(caseHackSuffix);
|
|
|
|
if (pos != std::string::npos) {
|
|
|
|
debug("removing case hack suffix from '%s'", path + i.first);
|
|
|
|
name.erase(pos);
|
|
|
|
}
|
|
|
|
if (!unhacked.emplace(name, i.first).second)
|
|
|
|
throw Error("file name collision in between '%s' and '%s'",
|
|
|
|
(path + unhacked[name]),
|
|
|
|
(path + i.first));
|
|
|
|
} else
|
|
|
|
unhacked.emplace(i.first, i.first);
|
|
|
|
|
|
|
|
for (auto & i : unhacked)
|
|
|
|
if (filter((path + i.first).abs())) {
|
|
|
|
sink << "entry" << "(" << "name" << i.first << "node";
|
|
|
|
dump(path + i.second);
|
|
|
|
sink << ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (st.type == tSymlink)
|
|
|
|
sink << "type" << "symlink" << "target" << readLink(path);
|
|
|
|
|
|
|
|
else throw Error("file '%s' has an unsupported type", path);
|
2003-06-20 13:40:25 +03:00
|
|
|
|
2023-10-19 16:07:56 +03:00
|
|
|
sink << ")";
|
|
|
|
};
|
|
|
|
|
|
|
|
sink << narVersionMagic1;
|
|
|
|
dump(path);
|
2003-06-20 13:40:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-18 17:56:35 +02:00
|
|
|
time_t dumpPathAndGetMtime(const Path & path, Sink & sink, PathFilter & filter)
|
2003-06-23 16:27:59 +03:00
|
|
|
{
|
2023-10-19 16:20:10 +03:00
|
|
|
PosixSourceAccessor accessor;
|
2023-10-19 16:07:56 +03:00
|
|
|
accessor.dumpPath(CanonPath::fromCwd(path), sink, filter);
|
|
|
|
return accessor.mtime;
|
2022-02-18 17:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void dumpPath(const Path & path, Sink & sink, PathFilter & filter)
|
|
|
|
{
|
|
|
|
dumpPathAndGetMtime(path, sink, filter);
|
2003-06-23 16:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
void dumpString(std::string_view s, Sink & sink)
|
2016-02-24 17:52:28 +02:00
|
|
|
{
|
|
|
|
sink << narVersionMagic1 << "(" << "type" << "regular" << "contents" << s << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
static SerialisationError badArchive(const std::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
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2020-12-02 15:10:56 +02:00
|
|
|
std::vector<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);
|
2020-12-02 15:10:56 +02:00
|
|
|
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
|
|
|
|
{
|
2022-02-25 17:00:00 +02:00
|
|
|
bool operator() (const std::string & a, const std::string & b) const
|
2014-07-16 17:02:05 +03:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string s;
|
2003-06-23 16:27:59 +03:00
|
|
|
|
|
|
|
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");
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string t = readString(source);
|
2003-06-23 16:27:59 +03:00
|
|
|
|
|
|
|
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);
|
2022-09-22 22:47:43 +03:00
|
|
|
sink.closeRegularFile();
|
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) {
|
2022-02-25 17:00:00 +02:00
|
|
|
std::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);
|
2022-02-25 17:00:00 +02:00
|
|
|
if (name.empty() || name == "." || name == ".." || name.find('/') != std::string::npos || name.find((char) 0) != std::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()) {
|
2023-03-02 16:44:19 +02:00
|
|
|
debug("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) {
|
2022-02-25 17:00:00 +02:00
|
|
|
std::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
|
|
|
{
|
2022-02-25 17:00:00 +02:00
|
|
|
std::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, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|