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 "archive.hh"
|
2018-03-27 19:41:31 +03:00
|
|
|
#include "config.hh"
|
2023-10-23 12:05:50 +03:00
|
|
|
#include "posix-source-accessor.hh"
|
2023-10-25 07:43:36 +03:00
|
|
|
#include "file-system.hh"
|
|
|
|
#include "signals.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) {
|
2024-02-05 16:13:11 +02:00
|
|
|
debug("removing case hack suffix from '%s'", path / i.first);
|
2023-10-19 16:07:56 +03:00
|
|
|
name.erase(pos);
|
|
|
|
}
|
|
|
|
if (!unhacked.emplace(name, i.first).second)
|
|
|
|
throw Error("file name collision in between '%s' and '%s'",
|
2024-02-05 16:13:11 +02:00
|
|
|
(path / unhacked[name]),
|
|
|
|
(path / i.first));
|
2023-10-19 16:07:56 +03:00
|
|
|
} else
|
|
|
|
unhacked.emplace(i.first, i.first);
|
|
|
|
|
|
|
|
for (auto & i : unhacked)
|
2024-02-05 16:13:11 +02:00
|
|
|
if (filter((path / i.first).abs())) {
|
2023-10-19 16:07:56 +03:00
|
|
|
sink << "entry" << "(" << "name" << i.first << "node";
|
2024-02-05 16:13:11 +02:00
|
|
|
dump(path / i.second);
|
2023-10-19 16:07:56 +03:00
|
|
|
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
|
|
|
{
|
2024-02-06 23:23:58 +02:00
|
|
|
auto [accessor, canonPath] = PosixSourceAccessor::createAtRoot(path);
|
|
|
|
accessor.dumpPath(canonPath, sink, filter);
|
2023-10-19 16:07:56 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
static void parseContents(CreateRegularFileSink & sink, Source & source)
|
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;
|
2023-12-26 18:40:55 +02:00
|
|
|
std::array<char, 65536> buf;
|
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);
|
2023-12-20 21:47:05 +02:00
|
|
|
sink({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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2024-01-23 00:59:34 +02:00
|
|
|
static void parse(FileSystemObjectSink & 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");
|
|
|
|
|
2014-07-16 17:02:05 +03:00
|
|
|
std::map<Path, int, CaseInsensitiveCompare> names;
|
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
auto getString = [&]() {
|
2004-01-15 22:23:55 +02:00
|
|
|
checkInterrupt();
|
2023-12-20 21:47:05 +02:00
|
|
|
return readString(source);
|
|
|
|
};
|
2004-01-15 22:23:55 +02:00
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
// For first iteration
|
|
|
|
s = getString();
|
|
|
|
|
|
|
|
while (1) {
|
2003-06-23 16:27:59 +03:00
|
|
|
|
|
|
|
if (s == ")") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (s == "type") {
|
2023-12-20 21:47:05 +02:00
|
|
|
std::string t = getString();
|
2003-06-23 16:27:59 +03:00
|
|
|
|
|
|
|
if (t == "regular") {
|
2023-12-20 21:47:05 +02:00
|
|
|
sink.createRegularFile(path, [&](auto & crf) {
|
|
|
|
while (1) {
|
|
|
|
s = getString();
|
|
|
|
|
|
|
|
if (s == "contents") {
|
|
|
|
parseContents(crf, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (s == "executable") {
|
|
|
|
auto s2 = getString();
|
|
|
|
if (s2 != "") throw badArchive("executable marker has non-empty value");
|
|
|
|
crf.isExecutable();
|
|
|
|
}
|
|
|
|
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
while (1) {
|
|
|
|
s = getString();
|
|
|
|
|
|
|
|
if (s == "entry") {
|
|
|
|
std::string name, prevName;
|
|
|
|
|
|
|
|
s = getString();
|
|
|
|
if (s != "(") throw badArchive("expected open tag");
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
s = getString();
|
|
|
|
|
|
|
|
if (s == ")") {
|
|
|
|
break;
|
|
|
|
} else if (s == "name") {
|
|
|
|
name = getString();
|
|
|
|
if (name.empty() || name == "." || name == ".." || name.find('/') != std::string::npos || name.find((char) 0) != std::string::npos)
|
|
|
|
throw Error("NAR contains invalid file name '%1%'", name);
|
|
|
|
if (name <= prevName)
|
|
|
|
throw Error("NAR directory is not sorted");
|
|
|
|
prevName = name;
|
|
|
|
if (archiveSettings.useCaseHack) {
|
|
|
|
auto i = names.find(name);
|
|
|
|
if (i != names.end()) {
|
|
|
|
debug("case collision between '%1%' and '%2%'", i->first, name);
|
|
|
|
name += caseHackSuffix;
|
|
|
|
name += std::to_string(++i->second);
|
|
|
|
} else
|
|
|
|
names[name] = 0;
|
|
|
|
}
|
|
|
|
} else if (s == "node") {
|
|
|
|
if (name.empty()) throw badArchive("entry name missing");
|
|
|
|
parse(sink, source, path + "/" + name);
|
|
|
|
} else
|
|
|
|
throw badArchive("unknown field " + s);
|
|
|
|
}
|
|
|
|
}
|
2014-07-16 17:02:05 +03:00
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
else break;
|
|
|
|
}
|
|
|
|
}
|
2014-07-16 17:02:05 +03:00
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
else if (t == "symlink") {
|
|
|
|
s = getString();
|
2003-06-23 16:27:59 +03:00
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
if (s != "target")
|
|
|
|
throw badArchive("expected 'target' got " + s);
|
2003-06-23 16:27:59 +03:00
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
std::string target = getString();
|
|
|
|
sink.createSymlink(path, target);
|
2003-07-17 14:25:14 +03:00
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
// for the next iteration
|
|
|
|
s = getString();
|
2014-07-16 17:02:05 +03:00
|
|
|
}
|
2003-06-23 16:27:59 +03:00
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
else throw badArchive("unknown file type " + t);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-23 00:59:34 +02:00
|
|
|
void parseDump(FileSystemObjectSink & 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.
|
|
|
|
|
2024-01-23 00:59:34 +02:00
|
|
|
NullFileSystemObjectSink parseSink; /* just parse the NAR */
|
2018-03-21 23:56:02 +02:00
|
|
|
|
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
|
|
|
}
|