tvl-depot/src/libstore/local-store.cc

1785 lines
56 KiB
C++
Raw Normal View History

#include "config.h"
#include "local-store.hh"
#include "globals.hh"
#include "archive.hh"
#include "pathlocks.hh"
#include "worker-protocol.hh"
#include "derivations.hh"
#include "affinity.hh"
2012-07-30 21:42:18 +02:00
#include <iostream>
2003-12-22 17:40:46 +01:00
#include <algorithm>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <utime.h>
#include <fcntl.h>
#include <errno.h>
2009-09-24 09:39:55 +02:00
#include <stdio.h>
2010-12-17 18:23:15 +01:00
#include <time.h>
#include <grp.h>
#if __linux__
#include <sched.h>
#include <sys/statvfs.h>
#include <sys/mount.h>
#include <sys/ioctl.h>
#endif
#include <sqlite3.h>
namespace nix {
void checkStoreNotSymlink()
{
if (getEnv("NIX_IGNORE_SYMLINK_STORE") == "1") return;
Path path = settings.nixStore;
struct stat st;
while (path != "/") {
if (lstat(path.c_str(), &st))
2014-08-20 17:00:17 +02:00
throw SysError(format("getting status of %1%") % path);
if (S_ISLNK(st.st_mode))
throw Error(format(
2014-08-20 17:00:17 +02:00
"the path %1% is a symlink; "
"this is not allowed for the Nix store and its parent directories")
% path);
path = dirOf(path);
}
}
2016-02-24 17:33:53 +01:00
LocalStore::LocalStore()
: reservedPath(settings.nixDBPath + "/reserved")
, didSetSubstituterEnv(false)
{
schemaPath = settings.nixDBPath + "/schema";
2012-07-30 21:42:18 +02:00
if (settings.readOnlyMode) {
openDB(false);
return;
}
/* Create missing state directories if they don't already exist. */
createDirs(settings.nixStore);
makeStoreWritable();
createDirs(linksDir = settings.nixStore + "/.links");
Path profilesDir = settings.nixStateDir + "/profiles";
createDirs(profilesDir);
createDirs(settings.nixStateDir + "/temproots");
createDirs(settings.nixDBPath);
Path gcRootsDir = settings.nixStateDir + "/gcroots";
if (!pathExists(gcRootsDir)) {
createDirs(gcRootsDir);
createSymlink(profilesDir, gcRootsDir + "/profiles");
}
2012-07-30 21:42:18 +02:00
/* Optionally, create directories and set permissions for a
multi-user install. */
if (getuid() == 0 && settings.buildUsersGroup != "") {
Path perUserDir = profilesDir + "/per-user";
createDirs(perUserDir);
2015-01-08 16:49:31 +01:00
if (chmod(perUserDir.c_str(), 01777) == -1)
2015-01-08 16:59:22 +01:00
throw SysError(format("could not set permissions on %1% to 1777") % perUserDir);
mode_t perm = 01775;
2015-01-08 16:49:31 +01:00
struct group * gr = getgrnam(settings.buildUsersGroup.c_str());
if (!gr)
printMsg(lvlError, format("warning: the group %1% specified in build-users-group does not exist")
% settings.buildUsersGroup);
else {
struct stat st;
if (stat(settings.nixStore.c_str(), &st))
throw SysError(format("getting attributes of path %1%") % settings.nixStore);
if (st.st_uid != 0 || st.st_gid != gr->gr_gid || (st.st_mode & ~S_IFMT) != perm) {
if (chown(settings.nixStore.c_str(), 0, gr->gr_gid) == -1)
throw SysError(format("changing ownership of path %1%") % settings.nixStore);
if (chmod(settings.nixStore.c_str(), perm) == -1)
throw SysError(format("changing permissions on path %1%") % settings.nixStore);
}
}
}
checkStoreNotSymlink();
/* We can't open a SQLite database if the disk is full. Since
this prevents the garbage collector from running when it's most
needed, we reserve some dummy space that we can free just
before doing a garbage collection. */
try {
2016-02-24 17:33:53 +01:00
struct stat st;
if (stat(reservedPath.c_str(), &st) == -1 ||
st.st_size != settings.reservedSize)
{
AutoCloseFD fd = open(reservedPath.c_str(), O_WRONLY | O_CREAT, 0600);
int res = -1;
#if HAVE_POSIX_FALLOCATE
2016-02-24 17:33:53 +01:00
res = posix_fallocate(fd, 0, settings.reservedSize);
#endif
2016-02-24 17:33:53 +01:00
if (res == -1) {
writeFull(fd, string(settings.reservedSize, 'X'));
ftruncate(fd, settings.reservedSize);
}
}
} catch (SysError & e) { /* don't care about errors */
}
/* Acquire the big fat lock in shared mode to make sure that no
schema upgrade is in progress. */
try {
Path globalLockPath = settings.nixDBPath + "/big-lock";
globalLock = openLockFile(globalLockPath.c_str(), true);
} catch (SysError & e) {
if (e.errNo != EACCES) throw;
settings.readOnlyMode = true;
openDB(false);
return;
}
2012-07-30 21:42:18 +02:00
if (!lockFile(globalLock, ltRead, false)) {
printMsg(lvlError, "waiting for the big Nix store lock...");
lockFile(globalLock, ltRead, true);
}
/* Check the current database schema and if necessary do an
upgrade. */
int curSchema = getSchema();
if (curSchema > nixSchemaVersion)
throw Error(format("current Nix store schema is version %1%, but I only support %2%")
% curSchema % nixSchemaVersion);
2012-07-30 21:42:18 +02:00
else if (curSchema == 0) { /* new store */
curSchema = nixSchemaVersion;
openDB(true);
writeFile(schemaPath, (format("%1%") % nixSchemaVersion).str());
}
2012-07-30 21:42:18 +02:00
else if (curSchema < nixSchemaVersion) {
if (curSchema < 5)
throw Error(
"Your Nix store has a database in Berkeley DB format,\n"
"which is no longer supported. To convert to the new format,\n"
"please upgrade Nix to version 0.12 first.");
2012-07-30 21:42:18 +02:00
if (curSchema < 6)
throw Error(
"Your Nix store has a database in flat file format,\n"
"which is no longer supported. To convert to the new format,\n"
"please upgrade Nix to version 1.11 first.");
if (!lockFile(globalLock, ltWrite, false)) {
printMsg(lvlError, "waiting for exclusive access to the Nix store...");
lockFile(globalLock, ltWrite, true);
}
/* Get the schema version again, because another process may
have performed the upgrade already. */
curSchema = getSchema();
if (curSchema < 7) { upgradeStore7(); openDB(true); }
writeFile(schemaPath, (format("%1%") % nixSchemaVersion).str());
lockFile(globalLock, ltRead, true);
}
2012-07-30 21:42:18 +02:00
else openDB(false);
}
LocalStore::~LocalStore()
{
try {
2015-07-17 19:24:28 +02:00
for (auto & i : runningSubstituters) {
if (i.second.disabled) continue;
i.second.to.close();
i.second.from.close();
i.second.error.close();
if (i.second.pid != -1)
i.second.pid.wait(true);
}
} catch (...) {
ignoreException();
}
2014-11-19 17:07:29 +01:00
try {
if (fdTempRoots != -1) {
fdTempRoots.close();
unlink(fnTempRoots.c_str());
}
} catch (...) {
ignoreException();
}
}
int LocalStore::getSchema()
{
int curSchema = 0;
if (pathExists(schemaPath)) {
string s = readFile(schemaPath);
if (!string2Int(s, curSchema))
2014-08-20 17:00:17 +02:00
throw Error(format("%1% is corrupt") % schemaPath);
}
return curSchema;
}
bool LocalStore::haveWriteAccess()
{
return access(settings.nixDBPath.c_str(), R_OK | W_OK) == 0;
}
void LocalStore::openDB(bool create)
2010-02-18 14:16:59 +01:00
{
if (!haveWriteAccess())
2014-08-20 17:00:17 +02:00
throw SysError(format("Nix database directory %1% is not writable") % settings.nixDBPath);
/* Open the Nix database. */
string dbPath = settings.nixDBPath + "/db.sqlite";
if (sqlite3_open_v2(dbPath.c_str(), &db.db,
SQLITE_OPEN_READWRITE | (create ? SQLITE_OPEN_CREATE : 0), 0) != SQLITE_OK)
2014-08-20 17:00:17 +02:00
throw Error(format("cannot open Nix database %1%") % dbPath);
2010-02-19 17:04:51 +01:00
2010-03-08 11:35:45 +01:00
if (sqlite3_busy_timeout(db, 60 * 60 * 1000) != SQLITE_OK)
throwSQLiteError(db, "setting timeout");
if (sqlite3_exec(db, "pragma foreign_keys = 1;", 0, 0, 0) != SQLITE_OK)
throwSQLiteError(db, "enabling foreign keys");
/* !!! check whether sqlite has been built with foreign key
support */
2012-07-30 21:42:18 +02:00
/* Whether SQLite should fsync(). "Normal" synchronous mode
should be safe enough. If the user asks for it, don't sync at
all. This can cause database corruption if the system
crashes. */
string syncMode = settings.fsyncMetadata ? "normal" : "off";
if (sqlite3_exec(db, ("pragma synchronous = " + syncMode + ";").c_str(), 0, 0, 0) != SQLITE_OK)
throwSQLiteError(db, "setting synchronous mode");
/* Set the SQLite journal mode. WAL mode is fastest, so it's the
default. */
string mode = settings.useSQLiteWAL ? "wal" : "truncate";
string prevMode;
{
SQLiteStmt stmt;
stmt.create(db, "pragma main.journal_mode;");
if (sqlite3_step(stmt) != SQLITE_ROW)
throwSQLiteError(db, "querying journal mode");
prevMode = string((const char *) sqlite3_column_text(stmt, 0));
}
if (prevMode != mode &&
sqlite3_exec(db, ("pragma main.journal_mode = " + mode + ";").c_str(), 0, 0, 0) != SQLITE_OK)
throwSQLiteError(db, "setting journal mode");
/* Increase the auto-checkpoint interval to 40000 pages. This
seems enough to ensure that instantiating the NixOS system
derivation is done in a single fsync(). */
if (mode == "wal" && sqlite3_exec(db, "pragma wal_autocheckpoint = 40000;", 0, 0, 0) != SQLITE_OK)
throwSQLiteError(db, "setting autocheckpoint interval");
2012-07-30 21:42:18 +02:00
/* Initialise the database schema, if necessary. */
if (create) {
2012-10-03 22:54:40 +02:00
const char * schema =
#include "schema.sql.hh"
2012-10-03 22:54:40 +02:00
;
if (sqlite3_exec(db, (const char *) schema, 0, 0, 0) != SQLITE_OK)
throwSQLiteError(db, "initialising database schema");
}
/* Prepare SQL statements. */
stmtRegisterValidPath.create(db,
"insert into ValidPaths (path, hash, registrationTime, deriver, narSize) values (?, ?, ?, ?, ?);");
stmtUpdatePathInfo.create(db,
"update ValidPaths set narSize = ?, hash = ? where path = ?;");
stmtAddReference.create(db,
2010-02-19 17:04:51 +01:00
"insert or replace into Refs (referrer, reference) values (?, ?);");
2010-02-18 16:52:57 +01:00
stmtQueryPathInfo.create(db,
"select id, hash, registrationTime, deriver, narSize from ValidPaths where path = ?;");
2010-02-18 16:52:57 +01:00
stmtQueryReferences.create(db,
"select path from Refs join ValidPaths on reference = id where referrer = ?;");
2010-02-18 17:21:59 +01:00
stmtQueryReferrers.create(db,
"select path from Refs join ValidPaths on referrer = id where reference = (select id from ValidPaths where path = ?);");
2010-02-19 17:43:25 +01:00
stmtInvalidatePath.create(db,
"delete from ValidPaths where path = ?;");
stmtRegisterFailedPath.create(db,
"insert or ignore into FailedPaths (path, time) values (?, ?);");
stmtHasPathFailed.create(db,
"select time from FailedPaths where path = ?;");
stmtQueryFailedPaths.create(db,
"select path from FailedPaths;");
// If the path is a derivation, then clear its outputs.
stmtClearFailedPath.create(db,
"delete from FailedPaths where ?1 = '*' or path = ?1 "
"or path in (select d.path from DerivationOutputs d join ValidPaths v on d.drv = v.id where v.path = ?1);");
stmtAddDerivationOutput.create(db,
"insert or replace into DerivationOutputs (drv, id, path) values (?, ?, ?);");
stmtQueryValidDerivers.create(db,
"select v.id, v.path from DerivationOutputs d join ValidPaths v on d.drv = v.id where d.path = ?;");
stmtQueryDerivationOutputs.create(db,
"select id, path from DerivationOutputs where drv = ?;");
// Use "path >= ?" with limit 1 rather than "path like '?%'" to
// ensure efficient lookup.
stmtQueryPathFromHashPart.create(db,
"select path from ValidPaths where path >= ? limit 1;");
2010-02-18 14:16:59 +01:00
}
/* To improve purity, users may want to make the Nix store a read-only
bind mount. So make the Nix store writable for this process. */
void LocalStore::makeStoreWritable()
{
#if __linux__
if (getuid() != 0) return;
/* Check if /nix/store is on a read-only mount. */
struct statvfs stat;
if (statvfs(settings.nixStore.c_str(), &stat) != 0)
throw SysError("getting info about the Nix store mount point");
if (stat.f_flag & ST_RDONLY) {
if (unshare(CLONE_NEWNS) == -1)
throw SysError("setting up a private mount namespace");
2014-12-12 15:10:02 +01:00
if (mount(0, settings.nixStore.c_str(), "none", MS_REMOUNT | MS_BIND, 0) == -1)
throw SysError(format("remounting %1% writable") % settings.nixStore);
}
#endif
}
const time_t mtimeStore = 1; /* 1 second into the epoch */
static void canonicaliseTimestampAndPermissions(const Path & path, const struct stat & st)
{
if (!S_ISLNK(st.st_mode)) {
/* Mask out all type related bits. */
mode_t mode = st.st_mode & ~S_IFMT;
2012-07-30 21:42:18 +02:00
if (mode != 0444 && mode != 0555) {
mode = (st.st_mode & S_IFMT)
| 0444
| (st.st_mode & S_IXUSR ? 0111 : 0);
if (chmod(path.c_str(), mode) == -1)
2014-08-20 17:00:17 +02:00
throw SysError(format("changing mode of %1% to %2$o") % path % mode);
}
}
2012-07-30 21:42:18 +02:00
if (st.st_mtime != mtimeStore) {
struct timeval times[2];
times[0].tv_sec = st.st_atime;
times[0].tv_usec = 0;
times[1].tv_sec = mtimeStore;
times[1].tv_usec = 0;
#if HAVE_LUTIMES
if (lutimes(path.c_str(), times) == -1)
if (errno != ENOSYS ||
(!S_ISLNK(st.st_mode) && utimes(path.c_str(), times) == -1))
#else
if (!S_ISLNK(st.st_mode) && utimes(path.c_str(), times) == -1)
2012-07-30 21:42:18 +02:00
#endif
2014-08-20 17:00:17 +02:00
throw SysError(format("changing modification time of %1%") % path);
}
}
void canonicaliseTimestampAndPermissions(const Path & path)
{
struct stat st;
if (lstat(path.c_str(), &st))
2014-08-20 17:00:17 +02:00
throw SysError(format("getting attributes of path %1%") % path);
canonicaliseTimestampAndPermissions(path, st);
}
static void canonicalisePathMetaData_(const Path & path, uid_t fromUid, InodesSeen & inodesSeen)
{
checkInterrupt();
struct stat st;
if (lstat(path.c_str(), &st))
2014-08-20 17:00:17 +02:00
throw SysError(format("getting attributes of path %1%") % path);
/* Really make sure that the path is of a supported type. */
if (!(S_ISREG(st.st_mode) || S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)))
throw Error(format("file %1% has an unsupported type") % path);
/* Fail if the file is not owned by the build user. This prevents
us from messing up the ownership/permissions of files
hard-linked into the output (e.g. "ln /etc/shadow $out/foo").
However, ignore files that we chown'ed ourselves previously to
ensure that we don't fail on hard links within the same build
(i.e. "touch $out/foo; ln $out/foo $out/bar"). */
if (fromUid != (uid_t) -1 && st.st_uid != fromUid) {
assert(!S_ISDIR(st.st_mode));
if (inodesSeen.find(Inode(st.st_dev, st.st_ino)) == inodesSeen.end())
2014-08-20 17:00:17 +02:00
throw BuildError(format("invalid ownership on file %1%") % path);
mode_t mode = st.st_mode & ~S_IFMT;
assert(S_ISLNK(st.st_mode) || (st.st_uid == geteuid() && (mode == 0444 || mode == 0555) && st.st_mtime == mtimeStore));
return;
}
inodesSeen.insert(Inode(st.st_dev, st.st_ino));
canonicaliseTimestampAndPermissions(path, st);
/* Change ownership to the current uid. If it's a symlink, use
lchown if available, otherwise don't bother. Wrong ownership
of a symlink doesn't matter, since the owning user can't change
the symlink and can't delete it because the directory is not
writable. The only exception is top-level paths in the Nix
store (since that directory is group-writable for the Nix build
users group); we check for this case below. */
if (st.st_uid != geteuid()) {
#if HAVE_LCHOWN
if (lchown(path.c_str(), geteuid(), getegid()) == -1)
#else
if (!S_ISLNK(st.st_mode) &&
chown(path.c_str(), geteuid(), getegid()) == -1)
#endif
2014-08-20 17:00:17 +02:00
throw SysError(format("changing owner of %1% to %2%")
% path % geteuid());
}
if (S_ISDIR(st.st_mode)) {
DirEntries entries = readDirectory(path);
for (auto & i : entries)
canonicalisePathMetaData_(path + "/" + i.name, fromUid, inodesSeen);
}
}
void canonicalisePathMetaData(const Path & path, uid_t fromUid, InodesSeen & inodesSeen)
{
canonicalisePathMetaData_(path, fromUid, inodesSeen);
/* On platforms that don't have lchown(), the top-level path can't
be a symlink, since we can't change its ownership. */
struct stat st;
if (lstat(path.c_str(), &st))
2014-08-20 17:00:17 +02:00
throw SysError(format("getting attributes of path %1%") % path);
if (st.st_uid != geteuid()) {
assert(S_ISLNK(st.st_mode));
2014-08-20 17:00:17 +02:00
throw Error(format("wrong ownership of top-level store path %1%") % path);
}
}
void canonicalisePathMetaData(const Path & path, uid_t fromUid)
{
InodesSeen inodesSeen;
canonicalisePathMetaData(path, fromUid, inodesSeen);
}
void LocalStore::checkDerivationOutputs(const Path & drvPath, const Derivation & drv)
{
string drvName = storePathToName(drvPath);
assert(isDerivation(drvName));
drvName = string(drvName, 0, drvName.size() - drvExtension.size());
2012-07-30 21:42:18 +02:00
if (drv.isFixedOutput()) {
DerivationOutputs::const_iterator out = drv.outputs.find("out");
if (out == drv.outputs.end())
2014-08-20 17:00:17 +02:00
throw Error(format("derivation %1% does not have an output named out") % drvPath);
bool recursive; HashType ht; Hash h;
out->second.parseHashInfo(recursive, ht, h);
Path outPath = makeFixedOutputPath(recursive, ht, h, drvName);
StringPairs::const_iterator j = drv.env.find("out");
if (out->second.path != outPath || j == drv.env.end() || j->second != outPath)
2014-08-20 17:00:17 +02:00
throw Error(format("derivation %1% has incorrect output %2%, should be %3%")
% drvPath % out->second.path % outPath);
}
else {
Derivation drvCopy(drv);
2015-07-17 19:24:28 +02:00
for (auto & i : drvCopy.outputs) {
i.second.path = "";
drvCopy.env[i.first] = "";
}
Hash h = hashDerivationModulo(*this, drvCopy);
2012-07-30 21:42:18 +02:00
2015-07-17 19:24:28 +02:00
for (auto & i : drv.outputs) {
Path outPath = makeOutputPath(i.first, h, drvName);
StringPairs::const_iterator j = drv.env.find(i.first);
if (i.second.path != outPath || j == drv.env.end() || j->second != outPath)
2014-08-20 17:00:17 +02:00
throw Error(format("derivation %1% has incorrect output %2%, should be %3%")
2015-07-17 19:24:28 +02:00
% drvPath % i.second.path % outPath);
}
}
}
unsigned long long LocalStore::addValidPath(const ValidPathInfo & info, bool checkOutputs)
{
2010-02-19 17:04:51 +01:00
SQLiteStmtUse use(stmtRegisterValidPath);
2010-02-19 17:43:25 +01:00
stmtRegisterValidPath.bind(info.path);
stmtRegisterValidPath.bind("sha256:" + printHash(info.narHash));
stmtRegisterValidPath.bind(info.registrationTime == 0 ? time(0) : info.registrationTime);
2010-02-19 17:43:25 +01:00
if (info.deriver != "")
stmtRegisterValidPath.bind(info.deriver);
else
stmtRegisterValidPath.bind(); // null
if (info.narSize != 0)
stmtRegisterValidPath.bind64(info.narSize);
else
stmtRegisterValidPath.bind(); // null
2010-02-19 17:04:51 +01:00
if (sqlite3_step(stmtRegisterValidPath) != SQLITE_DONE)
2014-08-20 17:00:17 +02:00
throwSQLiteError(db, format("registering valid path %1% in database") % info.path);
unsigned long long id = sqlite3_last_insert_rowid(db);
/* If this is a derivation, then store the derivation outputs in
the database. This is useful for the garbage collector: it can
efficiently query whether a path is an output of some
derivation. */
if (isDerivation(info.path)) {
Derivation drv = readDerivation(info.path);
2012-07-30 21:42:18 +02:00
/* Verify that the output paths in the derivation are correct
(i.e., follow the scheme for computing output paths from
derivations). Note that if this throws an error, then the
DB transaction is rolled back, so the path validity
registration above is undone. */
if (checkOutputs) checkDerivationOutputs(info.path, drv);
2012-07-30 21:42:18 +02:00
2015-07-17 19:24:28 +02:00
for (auto & i : drv.outputs) {
SQLiteStmtUse use(stmtAddDerivationOutput);
stmtAddDerivationOutput.bind(id);
2015-07-17 19:24:28 +02:00
stmtAddDerivationOutput.bind(i.first);
stmtAddDerivationOutput.bind(i.second.path);
if (sqlite3_step(stmtAddDerivationOutput) != SQLITE_DONE)
2014-08-20 17:00:17 +02:00
throwSQLiteError(db, format("adding derivation output for %1% in database") % info.path);
}
}
return id;
2010-02-19 17:04:51 +01:00
}
2010-02-19 17:04:51 +01:00
void LocalStore::addReference(unsigned long long referrer, unsigned long long reference)
{
SQLiteStmtUse use(stmtAddReference);
2010-02-19 17:43:25 +01:00
stmtAddReference.bind(referrer);
stmtAddReference.bind(reference);
2010-02-19 17:04:51 +01:00
if (sqlite3_step(stmtAddReference) != SQLITE_DONE)
throwSQLiteError(db, "adding reference to database");
2010-02-19 17:04:51 +01:00
}
void LocalStore::registerFailedPath(const Path & path)
{
retrySQLite<void>([&]() {
SQLiteStmtUse use(stmtRegisterFailedPath);
stmtRegisterFailedPath.bind(path);
stmtRegisterFailedPath.bind(time(0));
if (sqlite3_step(stmtRegisterFailedPath) != SQLITE_DONE)
2014-08-20 17:00:17 +02:00
throwSQLiteError(db, format("registering failed path %1%") % path);
});
}
bool LocalStore::hasPathFailed(const Path & path)
{
return retrySQLite<bool>([&]() {
SQLiteStmtUse use(stmtHasPathFailed);
stmtHasPathFailed.bind(path);
int res = sqlite3_step(stmtHasPathFailed);
if (res != SQLITE_DONE && res != SQLITE_ROW)
throwSQLiteError(db, "querying whether path failed");
return res == SQLITE_ROW;
});
}
PathSet LocalStore::queryFailedPaths()
{
return retrySQLite<PathSet>([&]() {
SQLiteStmtUse use(stmtQueryFailedPaths);
PathSet res;
int r;
while ((r = sqlite3_step(stmtQueryFailedPaths)) == SQLITE_ROW) {
const char * s = (const char *) sqlite3_column_text(stmtQueryFailedPaths, 0);
assert(s);
res.insert(s);
}
if (r != SQLITE_DONE)
throwSQLiteError(db, "error querying failed paths");
return res;
});
}
void LocalStore::clearFailedPaths(const PathSet & paths)
{
retrySQLite<void>([&]() {
SQLiteTxn txn(db);
2015-07-17 19:24:28 +02:00
for (auto & i : paths) {
SQLiteStmtUse use(stmtClearFailedPath);
2015-07-17 19:24:28 +02:00
stmtClearFailedPath.bind(i);
if (sqlite3_step(stmtClearFailedPath) != SQLITE_DONE)
2015-07-17 19:24:28 +02:00
throwSQLiteError(db, format("clearing failed path %1% in database") % i);
}
txn.commit();
});
}
Hash parseHashField(const Path & path, const string & s)
{
string::size_type colon = s.find(':');
if (colon == string::npos)
2014-08-20 17:00:17 +02:00
throw Error(format("corrupt hash %1% in valid-path entry for %2%")
% s % path);
HashType ht = parseHashType(string(s, 0, colon));
if (ht == htUnknown)
2014-08-20 17:00:17 +02:00
throw Error(format("unknown hash type %1% in valid-path entry for %2%")
% string(s, 0, colon) % path);
return parseHash(ht, string(s, colon + 1));
}
2010-02-18 16:52:57 +01:00
ValidPathInfo LocalStore::queryPathInfo(const Path & path)
{
2010-02-19 17:04:51 +01:00
ValidPathInfo info;
info.path = path;
assertStorePath(path);
return retrySQLite<ValidPathInfo>([&]() {
2010-02-19 17:43:25 +01:00
/* Get the path info. */
SQLiteStmtUse use1(stmtQueryPathInfo);
2012-07-30 21:42:18 +02:00
stmtQueryPathInfo.bind(path);
int r = sqlite3_step(stmtQueryPathInfo);
2014-08-20 17:00:17 +02:00
if (r == SQLITE_DONE) throw Error(format("path %1% is not valid") % path);
if (r != SQLITE_ROW) throwSQLiteError(db, "querying path in database");
info.id = sqlite3_column_int(stmtQueryPathInfo, 0);
2012-07-30 21:42:18 +02:00
const char * s = (const char *) sqlite3_column_text(stmtQueryPathInfo, 1);
assert(s);
info.narHash = parseHashField(path, s);
info.registrationTime = sqlite3_column_int(stmtQueryPathInfo, 2);
2010-02-18 16:52:57 +01:00
s = (const char *) sqlite3_column_text(stmtQueryPathInfo, 3);
if (s) info.deriver = s;
/* Note that narSize = NULL yields 0. */
info.narSize = sqlite3_column_int64(stmtQueryPathInfo, 4);
2010-02-18 16:52:57 +01:00
/* Get the references. */
SQLiteStmtUse use2(stmtQueryReferences);
2010-02-18 16:52:57 +01:00
stmtQueryReferences.bind(info.id);
while ((r = sqlite3_step(stmtQueryReferences)) == SQLITE_ROW) {
s = (const char *) sqlite3_column_text(stmtQueryReferences, 0);
assert(s);
info.references.insert(s);
}
2010-02-18 16:52:57 +01:00
if (r != SQLITE_DONE)
2014-08-20 17:00:17 +02:00
throwSQLiteError(db, format("error getting references of %1%") % path);
return info;
});
}
/* Update path info in the database. Currently only updates the
narSize field. */
void LocalStore::updatePathInfo(const ValidPathInfo & info)
{
SQLiteStmtUse use(stmtUpdatePathInfo);
if (info.narSize != 0)
stmtUpdatePathInfo.bind64(info.narSize);
else
stmtUpdatePathInfo.bind(); // null
stmtUpdatePathInfo.bind("sha256:" + printHash(info.narHash));
stmtUpdatePathInfo.bind(info.path);
if (sqlite3_step(stmtUpdatePathInfo) != SQLITE_DONE)
2014-08-20 17:00:17 +02:00
throwSQLiteError(db, format("updating info of path %1% in database") % info.path);
}
unsigned long long LocalStore::queryValidPathId(const Path & path)
{
SQLiteStmtUse use(stmtQueryPathInfo);
stmtQueryPathInfo.bind(path);
int res = sqlite3_step(stmtQueryPathInfo);
if (res == SQLITE_ROW) return sqlite3_column_int(stmtQueryPathInfo, 0);
2014-08-20 17:00:17 +02:00
if (res == SQLITE_DONE) throw Error(format("path %1% is not valid") % path);
throwSQLiteError(db, "querying path in database");
}
bool LocalStore::isValidPath_(const Path & path)
{
2010-02-19 17:04:51 +01:00
SQLiteStmtUse use(stmtQueryPathInfo);
2010-02-19 17:43:25 +01:00
stmtQueryPathInfo.bind(path);
2010-02-18 16:52:57 +01:00
int res = sqlite3_step(stmtQueryPathInfo);
2010-02-18 16:11:08 +01:00
if (res != SQLITE_DONE && res != SQLITE_ROW)
throwSQLiteError(db, "querying path in database");
2010-02-18 16:11:08 +01:00
return res == SQLITE_ROW;
}
bool LocalStore::isValidPath(const Path & path)
{
return retrySQLite<bool>([&]() {
return isValidPath_(path);
});
}
PathSet LocalStore::queryValidPaths(const PathSet & paths)
{
return retrySQLite<PathSet>([&]() {
PathSet res;
2015-07-17 19:24:28 +02:00
for (auto & i : paths)
if (isValidPath_(i)) res.insert(i);
return res;
});
}
2012-07-30 21:42:18 +02:00
PathSet LocalStore::queryAllValidPaths()
{
return retrySQLite<PathSet>([&]() {
SQLiteStmt stmt;
stmt.create(db, "select path from ValidPaths");
PathSet res;
int r;
while ((r = sqlite3_step(stmt)) == SQLITE_ROW) {
const char * s = (const char *) sqlite3_column_text(stmt, 0);
assert(s);
res.insert(s);
}
if (r != SQLITE_DONE)
throwSQLiteError(db, "error getting valid paths");
return res;
});
}
void LocalStore::queryReferrers_(const Path & path, PathSet & referrers)
{
2010-02-19 17:04:51 +01:00
SQLiteStmtUse use(stmtQueryReferrers);
2010-02-19 17:43:25 +01:00
stmtQueryReferrers.bind(path);
2010-02-18 17:21:59 +01:00
int r;
while ((r = sqlite3_step(stmtQueryReferrers)) == SQLITE_ROW) {
const char * s = (const char *) sqlite3_column_text(stmtQueryReferrers, 0);
assert(s);
referrers.insert(s);
}
2010-02-18 17:21:59 +01:00
if (r != SQLITE_DONE)
2014-08-20 17:00:17 +02:00
throwSQLiteError(db, format("error getting references of %1%") % path);
}
void LocalStore::queryReferrers(const Path & path, PathSet & referrers)
{
assertStorePath(path);
return retrySQLite<void>([&]() {
queryReferrers_(path, referrers);
});
}
Path LocalStore::queryDeriver(const Path & path)
{
return queryPathInfo(path).deriver;
}
PathSet LocalStore::queryValidDerivers(const Path & path)
{
assertStorePath(path);
return retrySQLite<PathSet>([&]() {
SQLiteStmtUse use(stmtQueryValidDerivers);
stmtQueryValidDerivers.bind(path);
PathSet derivers;
int r;
while ((r = sqlite3_step(stmtQueryValidDerivers)) == SQLITE_ROW) {
const char * s = (const char *) sqlite3_column_text(stmtQueryValidDerivers, 1);
assert(s);
derivers.insert(s);
}
2012-07-30 21:42:18 +02:00
if (r != SQLITE_DONE)
2014-08-20 17:00:17 +02:00
throwSQLiteError(db, format("error getting valid derivers of %1%") % path);
2012-07-30 21:42:18 +02:00
return derivers;
});
}
PathSet LocalStore::queryDerivationOutputs(const Path & path)
{
return retrySQLite<PathSet>([&]() {
SQLiteStmtUse use(stmtQueryDerivationOutputs);
stmtQueryDerivationOutputs.bind(queryValidPathId(path));
PathSet outputs;
int r;
while ((r = sqlite3_step(stmtQueryDerivationOutputs)) == SQLITE_ROW) {
const char * s = (const char *) sqlite3_column_text(stmtQueryDerivationOutputs, 1);
assert(s);
outputs.insert(s);
}
2012-07-30 21:42:18 +02:00
if (r != SQLITE_DONE)
2014-08-20 17:00:17 +02:00
throwSQLiteError(db, format("error getting outputs of %1%") % path);
return outputs;
});
}
StringSet LocalStore::queryDerivationOutputNames(const Path & path)
{
return retrySQLite<StringSet>([&]() {
SQLiteStmtUse use(stmtQueryDerivationOutputs);
stmtQueryDerivationOutputs.bind(queryValidPathId(path));
StringSet outputNames;
int r;
while ((r = sqlite3_step(stmtQueryDerivationOutputs)) == SQLITE_ROW) {
const char * s = (const char *) sqlite3_column_text(stmtQueryDerivationOutputs, 0);
assert(s);
outputNames.insert(s);
}
2012-07-30 21:42:18 +02:00
if (r != SQLITE_DONE)
2014-08-20 17:00:17 +02:00
throwSQLiteError(db, format("error getting output names of %1%") % path);
return outputNames;
});
}
Path LocalStore::queryPathFromHashPart(const string & hashPart)
{
if (hashPart.size() != storePathHashLen) throw Error("invalid hash part");
2012-07-30 21:42:18 +02:00
Path prefix = settings.nixStore + "/" + hashPart;
2012-07-30 21:42:18 +02:00
return retrySQLite<Path>([&]() {
SQLiteStmtUse use(stmtQueryPathFromHashPart);
stmtQueryPathFromHashPart.bind(prefix);
int res = sqlite3_step(stmtQueryPathFromHashPart);
if (res == SQLITE_DONE) return "";
if (res != SQLITE_ROW) throwSQLiteError(db, "finding path in database");
const char * s = (const char *) sqlite3_column_text(stmtQueryPathFromHashPart, 0);
return s && prefix.compare(0, prefix.size(), s, prefix.size()) == 0 ? s : "";
});
}
void LocalStore::setSubstituterEnv()
{
if (didSetSubstituterEnv) return;
/* Pass configuration options (including those overridden with
--option) to substituters. */
setenv("_NIX_OPTIONS", settings.pack().c_str(), 1);
didSetSubstituterEnv = true;
}
void LocalStore::startSubstituter(const Path & substituter, RunningSubstituter & run)
{
if (run.disabled || run.pid != -1) return;
2012-07-30 21:42:18 +02:00
2014-08-20 17:00:17 +02:00
debug(format("starting substituter program %1%") % substituter);
Pipe toPipe, fromPipe, errorPipe;
2012-07-30 21:42:18 +02:00
toPipe.create();
fromPipe.create();
errorPipe.create();
setSubstituterEnv();
run.pid = startProcess([&]() {
if (dup2(toPipe.readSide, STDIN_FILENO) == -1)
throw SysError("dupping stdin");
if (dup2(fromPipe.writeSide, STDOUT_FILENO) == -1)
throw SysError("dupping stdout");
if (dup2(errorPipe.writeSide, STDERR_FILENO) == -1)
throw SysError("dupping stderr");
execl(substituter.c_str(), substituter.c_str(), "--query", NULL);
2014-08-20 17:00:17 +02:00
throw SysError(format("executing %1%") % substituter);
});
2012-07-30 21:42:18 +02:00
run.program = baseNameOf(substituter);
run.to = toPipe.writeSide.borrow();
run.from = run.fromBuf.fd = fromPipe.readSide.borrow();
run.error = errorPipe.readSide.borrow();
toPipe.readSide.close();
fromPipe.writeSide.close();
errorPipe.writeSide.close();
/* The substituter may exit right away if it's disabled in any way
(e.g. copy-from-other-stores.pl will exit if no other stores
are configured). */
try {
getLineFromSubstituter(run);
} catch (EndOfFile & e) {
run.to.close();
run.from.close();
run.error.close();
run.disabled = true;
if (run.pid.wait(true) != 0) throw;
}
}
/* Read a line from the substituter's stdout, while also processing
its stderr. */
string LocalStore::getLineFromSubstituter(RunningSubstituter & run)
{
string res, err;
/* We might have stdout data left over from the last time. */
if (run.fromBuf.hasData()) goto haveData;
while (1) {
checkInterrupt();
fd_set fds;
FD_ZERO(&fds);
FD_SET(run.from, &fds);
FD_SET(run.error, &fds);
/* Wait for data to appear on the substituter's stdout or
stderr. */
if (select(run.from > run.error ? run.from + 1 : run.error + 1, &fds, 0, 0, 0) == -1) {
if (errno == EINTR) continue;
throw SysError("waiting for input from the substituter");
}
/* Completely drain stderr before dealing with stdout. */
if (FD_ISSET(run.error, &fds)) {
char buf[4096];
ssize_t n = read(run.error, (unsigned char *) buf, sizeof(buf));
if (n == -1) {
if (errno == EINTR) continue;
throw SysError("reading from substituter's stderr");
}
2014-08-20 17:00:17 +02:00
if (n == 0) throw EndOfFile(format("substituter %1% died unexpectedly") % run.program);
err.append(buf, n);
string::size_type p;
while ((p = err.find('\n')) != string::npos) {
printMsg(lvlError, run.program + ": " + string(err, 0, p));
err = string(err, p + 1);
}
}
/* Read from stdout until we get a newline or the buffer is empty. */
else if (run.fromBuf.hasData() || FD_ISSET(run.from, &fds)) {
haveData:
do {
unsigned char c;
run.fromBuf(&c, 1);
if (c == '\n') {
if (!err.empty()) printMsg(lvlError, run.program + ": " + err);
return res;
}
res += c;
} while (run.fromBuf.hasData());
}
}
}
template<class T> T LocalStore::getIntLineFromSubstituter(RunningSubstituter & run)
{
string s = getLineFromSubstituter(run);
T res;
if (!string2Int(s, res)) throw Error("integer expected from stream");
return res;
}
PathSet LocalStore::querySubstitutablePaths(const PathSet & paths)
{
PathSet res;
2015-07-17 19:24:28 +02:00
for (auto & i : settings.substituters) {
if (res.size() == paths.size()) break;
2015-07-17 19:24:28 +02:00
RunningSubstituter & run(runningSubstituters[i]);
startSubstituter(i, run);
if (run.disabled) continue;
string s = "have ";
2015-07-17 19:24:28 +02:00
for (auto & j : paths)
if (res.find(j) == res.end()) { s += j; s += " "; }
writeLine(run.to, s);
while (true) {
/* FIXME: we only read stderr when an error occurs, so
substituters should only write (short) messages to
stderr when they fail. I.e. they shouldn't write debug
output. */
2013-06-07 15:10:23 +02:00
Path path = getLineFromSubstituter(run);
if (path == "") break;
res.insert(path);
}
}
return res;
}
download-from-binary-cache: parallelise fetching of NAR info files Getting substitute information using the binary cache substituter has non-trivial latency overhead. A package or NixOS system configuration can have hundreds of dependencies, and in the worst case (when the local info cache is empty) we have to do a separate HTTP request for each of these. If the ping time to the server is t, getting N info files will take tN seconds; e.g., with a ping time of 0.1s to nixos.org, sequentially downloading 1000 info files (a typical NixOS config) will take at least 100 seconds. To fix this problem, the binary cache substituter can now perform requests in parallel. This required changing the substituter interface to support a function querySubstitutablePathInfos() that queries multiple paths at the same time, and rewriting queryMissing() to take advantage of parallelism. (Due to local caching, parallelising queryMissing() is sufficient for most use cases, since it's almost always called before building a derivation and thus fills the local info cache.) For example, parallelism speeds up querying all 1056 paths in a particular NixOS system configuration from 116s to 2.6s. It works so well because the eccentricity of the top-level derivation in the dependency graph is only 9. So we only need 10 round-trips (when using an unlimited number of parallel connections) to get everything. Currently we do a maximum of 150 parallel connections to the server. Thus it's important that the binary cache server (e.g. nixos.org) has a high connection limit. Alternatively we could use HTTP pipelining, but WWW::Curl doesn't support it and libcurl has a hard-coded limit of 5 requests per pipeline.
2012-07-07 01:08:20 +02:00
void LocalStore::querySubstitutablePathInfos(const Path & substituter,
PathSet & paths, SubstitutablePathInfos & infos)
{
RunningSubstituter & run(runningSubstituters[substituter]);
startSubstituter(substituter, run);
if (run.disabled) return;
download-from-binary-cache: parallelise fetching of NAR info files Getting substitute information using the binary cache substituter has non-trivial latency overhead. A package or NixOS system configuration can have hundreds of dependencies, and in the worst case (when the local info cache is empty) we have to do a separate HTTP request for each of these. If the ping time to the server is t, getting N info files will take tN seconds; e.g., with a ping time of 0.1s to nixos.org, sequentially downloading 1000 info files (a typical NixOS config) will take at least 100 seconds. To fix this problem, the binary cache substituter can now perform requests in parallel. This required changing the substituter interface to support a function querySubstitutablePathInfos() that queries multiple paths at the same time, and rewriting queryMissing() to take advantage of parallelism. (Due to local caching, parallelising queryMissing() is sufficient for most use cases, since it's almost always called before building a derivation and thus fills the local info cache.) For example, parallelism speeds up querying all 1056 paths in a particular NixOS system configuration from 116s to 2.6s. It works so well because the eccentricity of the top-level derivation in the dependency graph is only 9. So we only need 10 round-trips (when using an unlimited number of parallel connections) to get everything. Currently we do a maximum of 150 parallel connections to the server. Thus it's important that the binary cache server (e.g. nixos.org) has a high connection limit. Alternatively we could use HTTP pipelining, but WWW::Curl doesn't support it and libcurl has a hard-coded limit of 5 requests per pipeline.
2012-07-07 01:08:20 +02:00
string s = "info ";
2015-07-17 19:24:28 +02:00
for (auto & i : paths)
if (infos.find(i) == infos.end()) { s += i; s += " "; }
download-from-binary-cache: parallelise fetching of NAR info files Getting substitute information using the binary cache substituter has non-trivial latency overhead. A package or NixOS system configuration can have hundreds of dependencies, and in the worst case (when the local info cache is empty) we have to do a separate HTTP request for each of these. If the ping time to the server is t, getting N info files will take tN seconds; e.g., with a ping time of 0.1s to nixos.org, sequentially downloading 1000 info files (a typical NixOS config) will take at least 100 seconds. To fix this problem, the binary cache substituter can now perform requests in parallel. This required changing the substituter interface to support a function querySubstitutablePathInfos() that queries multiple paths at the same time, and rewriting queryMissing() to take advantage of parallelism. (Due to local caching, parallelising queryMissing() is sufficient for most use cases, since it's almost always called before building a derivation and thus fills the local info cache.) For example, parallelism speeds up querying all 1056 paths in a particular NixOS system configuration from 116s to 2.6s. It works so well because the eccentricity of the top-level derivation in the dependency graph is only 9. So we only need 10 round-trips (when using an unlimited number of parallel connections) to get everything. Currently we do a maximum of 150 parallel connections to the server. Thus it's important that the binary cache server (e.g. nixos.org) has a high connection limit. Alternatively we could use HTTP pipelining, but WWW::Curl doesn't support it and libcurl has a hard-coded limit of 5 requests per pipeline.
2012-07-07 01:08:20 +02:00
writeLine(run.to, s);
while (true) {
2013-06-07 15:10:23 +02:00
Path path = getLineFromSubstituter(run);
if (path == "") break;
if (paths.find(path) == paths.end())
2014-08-20 17:00:17 +02:00
throw Error(format("got unexpected path %1% from substituter") % path);
2013-06-07 15:10:23 +02:00
paths.erase(path);
SubstitutablePathInfo & info(infos[path]);
info.deriver = getLineFromSubstituter(run);
if (info.deriver != "") assertStorePath(info.deriver);
int nrRefs = getIntLineFromSubstituter<int>(run);
while (nrRefs--) {
Path p = getLineFromSubstituter(run);
assertStorePath(p);
info.references.insert(p);
download-from-binary-cache: parallelise fetching of NAR info files Getting substitute information using the binary cache substituter has non-trivial latency overhead. A package or NixOS system configuration can have hundreds of dependencies, and in the worst case (when the local info cache is empty) we have to do a separate HTTP request for each of these. If the ping time to the server is t, getting N info files will take tN seconds; e.g., with a ping time of 0.1s to nixos.org, sequentially downloading 1000 info files (a typical NixOS config) will take at least 100 seconds. To fix this problem, the binary cache substituter can now perform requests in parallel. This required changing the substituter interface to support a function querySubstitutablePathInfos() that queries multiple paths at the same time, and rewriting queryMissing() to take advantage of parallelism. (Due to local caching, parallelising queryMissing() is sufficient for most use cases, since it's almost always called before building a derivation and thus fills the local info cache.) For example, parallelism speeds up querying all 1056 paths in a particular NixOS system configuration from 116s to 2.6s. It works so well because the eccentricity of the top-level derivation in the dependency graph is only 9. So we only need 10 round-trips (when using an unlimited number of parallel connections) to get everything. Currently we do a maximum of 150 parallel connections to the server. Thus it's important that the binary cache server (e.g. nixos.org) has a high connection limit. Alternatively we could use HTTP pipelining, but WWW::Curl doesn't support it and libcurl has a hard-coded limit of 5 requests per pipeline.
2012-07-07 01:08:20 +02:00
}
2013-06-07 15:10:23 +02:00
info.downloadSize = getIntLineFromSubstituter<long long>(run);
info.narSize = getIntLineFromSubstituter<long long>(run);
}
}
download-from-binary-cache: parallelise fetching of NAR info files Getting substitute information using the binary cache substituter has non-trivial latency overhead. A package or NixOS system configuration can have hundreds of dependencies, and in the worst case (when the local info cache is empty) we have to do a separate HTTP request for each of these. If the ping time to the server is t, getting N info files will take tN seconds; e.g., with a ping time of 0.1s to nixos.org, sequentially downloading 1000 info files (a typical NixOS config) will take at least 100 seconds. To fix this problem, the binary cache substituter can now perform requests in parallel. This required changing the substituter interface to support a function querySubstitutablePathInfos() that queries multiple paths at the same time, and rewriting queryMissing() to take advantage of parallelism. (Due to local caching, parallelising queryMissing() is sufficient for most use cases, since it's almost always called before building a derivation and thus fills the local info cache.) For example, parallelism speeds up querying all 1056 paths in a particular NixOS system configuration from 116s to 2.6s. It works so well because the eccentricity of the top-level derivation in the dependency graph is only 9. So we only need 10 round-trips (when using an unlimited number of parallel connections) to get everything. Currently we do a maximum of 150 parallel connections to the server. Thus it's important that the binary cache server (e.g. nixos.org) has a high connection limit. Alternatively we could use HTTP pipelining, but WWW::Curl doesn't support it and libcurl has a hard-coded limit of 5 requests per pipeline.
2012-07-07 01:08:20 +02:00
void LocalStore::querySubstitutablePathInfos(const PathSet & paths,
SubstitutablePathInfos & infos)
{
PathSet todo = paths;
2015-07-17 19:24:28 +02:00
for (auto & i : settings.substituters) {
download-from-binary-cache: parallelise fetching of NAR info files Getting substitute information using the binary cache substituter has non-trivial latency overhead. A package or NixOS system configuration can have hundreds of dependencies, and in the worst case (when the local info cache is empty) we have to do a separate HTTP request for each of these. If the ping time to the server is t, getting N info files will take tN seconds; e.g., with a ping time of 0.1s to nixos.org, sequentially downloading 1000 info files (a typical NixOS config) will take at least 100 seconds. To fix this problem, the binary cache substituter can now perform requests in parallel. This required changing the substituter interface to support a function querySubstitutablePathInfos() that queries multiple paths at the same time, and rewriting queryMissing() to take advantage of parallelism. (Due to local caching, parallelising queryMissing() is sufficient for most use cases, since it's almost always called before building a derivation and thus fills the local info cache.) For example, parallelism speeds up querying all 1056 paths in a particular NixOS system configuration from 116s to 2.6s. It works so well because the eccentricity of the top-level derivation in the dependency graph is only 9. So we only need 10 round-trips (when using an unlimited number of parallel connections) to get everything. Currently we do a maximum of 150 parallel connections to the server. Thus it's important that the binary cache server (e.g. nixos.org) has a high connection limit. Alternatively we could use HTTP pipelining, but WWW::Curl doesn't support it and libcurl has a hard-coded limit of 5 requests per pipeline.
2012-07-07 01:08:20 +02:00
if (todo.empty()) break;
2015-07-17 19:24:28 +02:00
querySubstitutablePathInfos(i, todo, infos);
download-from-binary-cache: parallelise fetching of NAR info files Getting substitute information using the binary cache substituter has non-trivial latency overhead. A package or NixOS system configuration can have hundreds of dependencies, and in the worst case (when the local info cache is empty) we have to do a separate HTTP request for each of these. If the ping time to the server is t, getting N info files will take tN seconds; e.g., with a ping time of 0.1s to nixos.org, sequentially downloading 1000 info files (a typical NixOS config) will take at least 100 seconds. To fix this problem, the binary cache substituter can now perform requests in parallel. This required changing the substituter interface to support a function querySubstitutablePathInfos() that queries multiple paths at the same time, and rewriting queryMissing() to take advantage of parallelism. (Due to local caching, parallelising queryMissing() is sufficient for most use cases, since it's almost always called before building a derivation and thus fills the local info cache.) For example, parallelism speeds up querying all 1056 paths in a particular NixOS system configuration from 116s to 2.6s. It works so well because the eccentricity of the top-level derivation in the dependency graph is only 9. So we only need 10 round-trips (when using an unlimited number of parallel connections) to get everything. Currently we do a maximum of 150 parallel connections to the server. Thus it's important that the binary cache server (e.g. nixos.org) has a high connection limit. Alternatively we could use HTTP pipelining, but WWW::Curl doesn't support it and libcurl has a hard-coded limit of 5 requests per pipeline.
2012-07-07 01:08:20 +02:00
}
}
Hash LocalStore::queryPathHash(const Path & path)
{
return queryPathInfo(path).narHash;
}
void LocalStore::registerValidPath(const ValidPathInfo & info)
{
ValidPathInfos infos;
infos.push_back(info);
registerValidPaths(infos);
}
void LocalStore::registerValidPaths(const ValidPathInfos & infos)
{
/* SQLite will fsync by default, but the new valid paths may not be fsync-ed.
* So some may want to fsync them before registering the validity, at the
* expense of some speed of the path registering operation. */
if (settings.syncBeforeRegistering) sync();
return retrySQLite<void>([&]() {
SQLiteTxn txn(db);
PathSet paths;
2015-07-17 19:24:28 +02:00
for (auto & i : infos) {
assert(i.narHash.type == htSHA256);
2015-07-17 19:24:28 +02:00
if (isValidPath_(i.path))
updatePathInfo(i);
else
2015-07-17 19:24:28 +02:00
addValidPath(i, false);
paths.insert(i.path);
}
2015-07-17 19:24:28 +02:00
for (auto & i : infos) {
unsigned long long referrer = queryValidPathId(i.path);
for (auto & j : i.references)
addReference(referrer, queryValidPathId(j));
}
/* Check that the derivation outputs are correct. We can't do
this in addValidPath() above, because the references might
not be valid yet. */
2015-07-17 19:24:28 +02:00
for (auto & i : infos)
if (isDerivation(i.path)) {
// FIXME: inefficient; we already loaded the
// derivation in addValidPath().
2015-07-17 19:24:28 +02:00
Derivation drv = readDerivation(i.path);
checkDerivationOutputs(i.path, drv);
}
/* Do a topological sort of the paths. This will throw an
error if a cycle is detected and roll back the
transaction. Cycles can only occur when a derivation
has multiple outputs. */
topoSortPaths(paths);
txn.commit();
});
}
/* Invalidate a path. The caller is responsible for checking that
there are no referrers. */
void LocalStore::invalidatePath(const Path & path)
{
2014-08-20 17:00:17 +02:00
debug(format("invalidating path %1%") % path);
drvHashes.erase(path);
2010-02-19 17:43:25 +01:00
SQLiteStmtUse use(stmtInvalidatePath);
2010-02-19 17:43:25 +01:00
stmtInvalidatePath.bind(path);
2010-02-19 17:43:25 +01:00
if (sqlite3_step(stmtInvalidatePath) != SQLITE_DONE)
2014-08-20 17:00:17 +02:00
throwSQLiteError(db, format("invalidating path %1% in database") % path);
2010-02-19 17:43:25 +01:00
/* Note that the foreign key constraints on the Refs table take
care of deleting the references entries for `path'. */
}
Path LocalStore::addToStoreFromDump(const string & dump, const string & name,
bool recursive, HashType hashAlgo, bool repair)
{
Hash h = hashString(hashAlgo, dump);
Path dstPath = makeFixedOutputPath(recursive, hashAlgo, h, name);
addTempRoot(dstPath);
if (repair || !isValidPath(dstPath)) {
/* The first check above is an optimisation to prevent
unnecessary lock acquisition. */
2006-06-01 20:13:33 +02:00
PathLocks outputLock(singleton<PathSet, Path>(dstPath));
if (repair || !isValidPath(dstPath)) {
deletePath(dstPath);
if (recursive) {
StringSource source(dump);
restorePath(dstPath, source);
} else
writeFile(dstPath, dump);
canonicalisePathMetaData(dstPath, -1);
/* Register the SHA-256 hash of the NAR serialisation of
the path in the database. We may just have computed it
above (if called with recursive == true and hashAlgo ==
sha256); otherwise, compute it here. */
HashResult hash;
if (recursive) {
hash.first = hashAlgo == htSHA256 ? h : hashString(htSHA256, dump);
hash.second = dump.size();
} else
hash = hashPath(htSHA256, dstPath);
optimisePath(dstPath); // FIXME: combine with hashPath()
2012-07-30 21:42:18 +02:00
ValidPathInfo info;
info.path = dstPath;
info.narHash = hash.first;
info.narSize = hash.second;
registerValidPath(info);
}
outputLock.setDeletion(true);
}
return dstPath;
}
Path LocalStore::addToStore(const string & name, const Path & _srcPath,
bool recursive, HashType hashAlgo, PathFilter & filter, bool repair)
{
Path srcPath(absPath(_srcPath));
2014-08-20 17:00:17 +02:00
debug(format("adding %1% to the store") % srcPath);
/* Read the whole path into memory. This is not a very scalable
method for very large paths, but `copyPath' is mainly used for
small files. */
StringSink sink;
download-from-binary-cache: parallelise fetching of NAR info files Getting substitute information using the binary cache substituter has non-trivial latency overhead. A package or NixOS system configuration can have hundreds of dependencies, and in the worst case (when the local info cache is empty) we have to do a separate HTTP request for each of these. If the ping time to the server is t, getting N info files will take tN seconds; e.g., with a ping time of 0.1s to nixos.org, sequentially downloading 1000 info files (a typical NixOS config) will take at least 100 seconds. To fix this problem, the binary cache substituter can now perform requests in parallel. This required changing the substituter interface to support a function querySubstitutablePathInfos() that queries multiple paths at the same time, and rewriting queryMissing() to take advantage of parallelism. (Due to local caching, parallelising queryMissing() is sufficient for most use cases, since it's almost always called before building a derivation and thus fills the local info cache.) For example, parallelism speeds up querying all 1056 paths in a particular NixOS system configuration from 116s to 2.6s. It works so well because the eccentricity of the top-level derivation in the dependency graph is only 9. So we only need 10 round-trips (when using an unlimited number of parallel connections) to get everything. Currently we do a maximum of 150 parallel connections to the server. Thus it's important that the binary cache server (e.g. nixos.org) has a high connection limit. Alternatively we could use HTTP pipelining, but WWW::Curl doesn't support it and libcurl has a hard-coded limit of 5 requests per pipeline.
2012-07-07 01:08:20 +02:00
if (recursive)
2016-03-22 14:21:45 +01:00
dumpPath(srcPath, sink, filter);
else
2016-03-04 16:49:56 +01:00
sink.s = make_ref<std::string>(readFile(srcPath));
2016-03-04 16:49:56 +01:00
return addToStoreFromDump(*sink.s, name, recursive, hashAlgo, repair);
}
Path LocalStore::addTextToStore(const string & name, const string & s,
const PathSet & references, bool repair)
{
Path dstPath = computeStorePathForText(name, s, references);
2012-07-30 21:42:18 +02:00
addTempRoot(dstPath);
if (repair || !isValidPath(dstPath)) {
2006-06-01 20:13:33 +02:00
PathLocks outputLock(singleton<PathSet, Path>(dstPath));
if (repair || !isValidPath(dstPath)) {
deletePath(dstPath);
writeFile(dstPath, s);
canonicalisePathMetaData(dstPath, -1);
StringSink sink;
dumpString(s, sink);
2016-03-04 16:49:56 +01:00
auto hash = hashString(htSHA256, *sink.s);
optimisePath(dstPath);
2012-07-30 21:42:18 +02:00
ValidPathInfo info;
info.path = dstPath;
info.narHash = hash;
2016-03-04 16:49:56 +01:00
info.narSize = sink.s->size();
info.references = references;
registerValidPath(info);
}
outputLock.setDeletion(true);
}
return dstPath;
}
struct HashAndWriteSink : Sink
{
Sink & writeSink;
HashSink hashSink;
HashAndWriteSink(Sink & writeSink) : writeSink(writeSink), hashSink(htSHA256)
{
}
virtual void operator () (const unsigned char * data, size_t len)
{
writeSink(data, len);
hashSink(data, len);
}
Hash currentHash()
{
return hashSink.currentHash().first;
}
};
static void checkSecrecy(const Path & path)
{
struct stat st;
if (stat(path.c_str(), &st))
2014-08-20 17:00:17 +02:00
throw SysError(format("getting status of %1%") % path);
if ((st.st_mode & (S_IRWXG | S_IRWXO)) != 0)
2014-08-20 17:00:17 +02:00
throw Error(format("file %1% should be secret (inaccessible to everybody else)!") % path);
}
void LocalStore::exportPath(const Path & path, bool sign,
Sink & sink)
{
assertStorePath(path);
printMsg(lvlTalkative, format("exporting path %1%") % path);
if (!isValidPath(path))
2014-08-20 17:00:17 +02:00
throw Error(format("path %1% is not valid") % path);
HashAndWriteSink hashAndWriteSink(sink);
2012-07-30 21:42:18 +02:00
dumpPath(path, hashAndWriteSink);
/* Refuse to export paths that have changed. This prevents
filesystem corruption from spreading to other machines.
Don't complain if the stored hash is zero (unknown). */
Hash hash = hashAndWriteSink.currentHash();
Hash storedHash = queryPathHash(path);
if (hash != storedHash && storedHash != Hash(storedHash.type))
2014-08-20 17:00:17 +02:00
throw Error(format("hash of path %1% has changed from %2% to %3%!") % path
% printHash(storedHash) % printHash(hash));
PathSet references;
queryReferences(path, references);
hashAndWriteSink << exportMagic << path << references << queryDeriver(path);
if (sign) {
Hash hash = hashAndWriteSink.currentHash();
2012-07-30 21:42:18 +02:00
Path tmpDir = createTempDir();
AutoDelete delTmp(tmpDir);
Path hashFile = tmpDir + "/hash";
writeFile(hashFile, printHash(hash));
Path secretKey = settings.nixConfDir + "/signing-key.sec";
checkSecrecy(secretKey);
Strings args;
args.push_back("rsautl");
args.push_back("-sign");
args.push_back("-inkey");
args.push_back(secretKey);
args.push_back("-in");
args.push_back(hashFile);
string signature = runProgram(OPENSSL_PATH, true, args);
2015-07-20 01:16:16 +02:00
hashAndWriteSink << 1 << signature;
2012-07-30 21:42:18 +02:00
} else
2015-07-20 01:16:16 +02:00
hashAndWriteSink << 0;
}
struct HashAndReadSource : Source
{
Source & readSource;
HashSink hashSink;
bool hashing;
HashAndReadSource(Source & readSource) : readSource(readSource), hashSink(htSHA256)
{
hashing = true;
}
size_t read(unsigned char * data, size_t len)
{
size_t n = readSource.read(data, len);
if (hashing) hashSink(data, n);
return n;
}
};
/* Create a temporary directory in the store that won't be
garbage-collected. */
Path LocalStore::createTempDirInStore()
{
Path tmpDir;
do {
/* There is a slight possibility that `tmpDir' gets deleted by
the GC between createTempDir() and addTempRoot(), so repeat
until `tmpDir' exists. */
tmpDir = createTempDir(settings.nixStore);
addTempRoot(tmpDir);
} while (!pathExists(tmpDir));
return tmpDir;
}
Path LocalStore::importPath(bool requireSignature, Source & source)
{
HashAndReadSource hashAndReadSource(source);
2012-07-30 21:42:18 +02:00
/* We don't yet know what store path this archive contains (the
store path follows the archive data proper), and besides, we
don't know yet whether the signature is valid. */
Path tmpDir = createTempDirInStore();
AutoDelete delTmp(tmpDir);
Path unpacked = tmpDir + "/unpacked";
restorePath(unpacked, hashAndReadSource);
uint32_t magic = readInt(hashAndReadSource);
if (magic != exportMagic)
throw Error("Nix archive cannot be imported; wrong format");
Path dstPath = readStorePath(hashAndReadSource);
printMsg(lvlTalkative, format("importing path %1%") % dstPath);
PathSet references = readStorePaths<PathSet>(hashAndReadSource);
2007-02-28 00:18:57 +01:00
Path deriver = readString(hashAndReadSource);
if (deriver != "") assertStorePath(deriver);
Hash hash = hashAndReadSource.hashSink.finish().first;
hashAndReadSource.hashing = false;
bool haveSignature = readInt(hashAndReadSource) == 1;
if (requireSignature && !haveSignature)
2014-08-20 17:00:17 +02:00
throw Error(format("imported archive of %1% lacks a signature") % dstPath);
2012-07-30 21:42:18 +02:00
if (haveSignature) {
string signature = readString(hashAndReadSource);
if (requireSignature) {
Path sigFile = tmpDir + "/sig";
writeFile(sigFile, signature);
Strings args;
args.push_back("rsautl");
args.push_back("-verify");
args.push_back("-inkey");
args.push_back(settings.nixConfDir + "/signing-key.pub");
args.push_back("-pubin");
args.push_back("-in");
args.push_back(sigFile);
string hash2 = runProgram(OPENSSL_PATH, true, args);
/* Note: runProgram() throws an exception if the signature
is invalid. */
if (printHash(hash) != hash2)
throw Error(
"signed hash doesn't match actual contents of imported "
"archive; archive could be corrupt, or someone is trying "
"to import a Trojan horse");
}
}
/* Do the actual import. */
/* !!! way too much code duplication with addTextToStore() etc. */
addTempRoot(dstPath);
if (!isValidPath(dstPath)) {
PathLocks outputLock;
/* Lock the output path. But don't lock if we're being called
from a build hook (whose parent process already acquired a
lock on this path). */
2012-09-19 21:43:23 +02:00
Strings locksHeld = tokenizeString<Strings>(getEnv("NIX_HELD_LOCKS"));
if (find(locksHeld.begin(), locksHeld.end(), dstPath) == locksHeld.end())
outputLock.lockPaths(singleton<PathSet, Path>(dstPath));
if (!isValidPath(dstPath)) {
deletePath(dstPath);
if (rename(unpacked.c_str(), dstPath.c_str()) == -1)
2014-08-20 17:00:17 +02:00
throw SysError(format("cannot move %1% to %2%")
% unpacked % dstPath);
canonicalisePathMetaData(dstPath, -1);
2012-07-30 21:42:18 +02:00
/* !!! if we were clever, we could prevent the hashPath()
here. */
HashResult hash = hashPath(htSHA256, dstPath);
optimisePath(dstPath); // FIXME: combine with hashPath()
2012-07-30 21:42:18 +02:00
ValidPathInfo info;
info.path = dstPath;
info.narHash = hash.first;
info.narSize = hash.second;
info.references = references;
info.deriver = deriver != "" && isValidPath(deriver) ? deriver : "";
registerValidPath(info);
}
2012-07-30 21:42:18 +02:00
outputLock.setDeletion(true);
}
2012-07-30 21:42:18 +02:00
return dstPath;
}
Paths LocalStore::importPaths(bool requireSignature, Source & source,
std::shared_ptr<FSAccessor> accessor)
{
Paths res;
while (true) {
unsigned long long n = readLongLong(source);
if (n == 0) break;
2014-08-20 17:00:17 +02:00
if (n != 1) throw Error("input doesn't look like something created by nix-store --export");
res.push_back(importPath(requireSignature, source));
}
return res;
}
void LocalStore::invalidatePathChecked(const Path & path)
2003-06-23 16:40:49 +02:00
{
assertStorePath(path);
retrySQLite<void>([&]() {
SQLiteTxn txn(db);
if (isValidPath_(path)) {
PathSet referrers; queryReferrers_(path, referrers);
referrers.erase(path); /* ignore self-references */
if (!referrers.empty())
2014-08-20 17:00:17 +02:00
throw PathInUse(format("cannot delete path %1% because it is in use by %2%")
% path % showPaths(referrers));
invalidatePath(path);
}
txn.commit();
});
2003-06-23 16:40:49 +02:00
}
bool LocalStore::verifyStore(bool checkContents, bool repair)
{
printMsg(lvlError, format("reading the Nix store..."));
bool errors = false;
/* Acquire the global GC lock to prevent a garbage collection. */
AutoCloseFD fdGCLock = openGCLock(ltWrite);
2012-07-30 21:42:18 +02:00
PathSet store;
for (auto & i : readDirectory(settings.nixStore)) store.insert(i.name);
/* Check whether all valid paths actually exist. */
printMsg(lvlInfo, "checking path existence...");
PathSet validPaths2 = queryAllValidPaths(), validPaths, done;
2015-07-17 19:24:28 +02:00
for (auto & i : validPaths2)
verifyPath(i, store, done, validPaths, repair, errors);
/* Release the GC lock so that checking content hashes (which can
take ages) doesn't block the GC or builds. */
fdGCLock.close();
/* Optionally, check the content hashes (slow). */
if (checkContents) {
printMsg(lvlInfo, "checking hashes...");
Hash nullHash(htSHA256);
2015-07-17 19:24:28 +02:00
for (auto & i : validPaths) {
try {
2015-07-17 19:24:28 +02:00
ValidPathInfo info = queryPathInfo(i);
/* Check the content hash (optionally - slow). */
2015-07-17 19:24:28 +02:00
printMsg(lvlTalkative, format("checking contents of %1%") % i);
HashResult current = hashPath(info.narHash.type, i);
2012-07-30 21:42:18 +02:00
if (info.narHash != nullHash && info.narHash != current.first) {
2014-08-20 17:00:17 +02:00
printMsg(lvlError, format("path %1% was modified! "
"expected hash %2%, got %3%")
% i % printHash(info.narHash) % printHash(current.first));
2015-07-17 19:24:28 +02:00
if (repair) repairPath(i); else errors = true;
} else {
bool update = false;
/* Fill in missing hashes. */
if (info.narHash == nullHash) {
2015-07-17 19:24:28 +02:00
printMsg(lvlError, format("fixing missing hash on %1%") % i);
info.narHash = current.first;
update = true;
}
2012-07-30 21:42:18 +02:00
/* Fill in missing narSize fields (from old stores). */
if (info.narSize == 0) {
2015-07-17 19:24:28 +02:00
printMsg(lvlError, format("updating size field on %1% to %2%") % i % current.second);
info.narSize = current.second;
2012-07-30 21:42:18 +02:00
update = true;
}
if (update) updatePathInfo(info);
}
2012-07-30 21:42:18 +02:00
} catch (Error & e) {
/* It's possible that the path got GC'ed, so ignore
errors on invalid paths. */
2015-07-17 19:24:28 +02:00
if (isValidPath(i))
2012-10-04 16:20:23 +02:00
printMsg(lvlError, format("error: %1%") % e.msg());
else
printMsg(lvlError, format("warning: %1%") % e.msg());
errors = true;
}
}
}
return errors;
2010-02-18 16:11:08 +01:00
}
void LocalStore::verifyPath(const Path & path, const PathSet & store,
PathSet & done, PathSet & validPaths, bool repair, bool & errors)
{
checkInterrupt();
2012-07-30 21:42:18 +02:00
if (done.find(path) != done.end()) return;
done.insert(path);
if (!isStorePath(path)) {
2014-08-20 17:00:17 +02:00
printMsg(lvlError, format("path %1% is not in the Nix store") % path);
invalidatePath(path);
return;
}
if (store.find(baseNameOf(path)) == store.end()) {
/* Check any referrers first. If we can invalidate them
first, then we can invalidate this path as well. */
bool canInvalidate = true;
PathSet referrers; queryReferrers(path, referrers);
2015-07-17 19:24:28 +02:00
for (auto & i : referrers)
if (i != path) {
verifyPath(i, store, done, validPaths, repair, errors);
if (validPaths.find(i) != validPaths.end())
canInvalidate = false;
}
if (canInvalidate) {
2014-08-20 17:00:17 +02:00
printMsg(lvlError, format("path %1% disappeared, removing from database...") % path);
invalidatePath(path);
} else {
2014-08-20 17:00:17 +02:00
printMsg(lvlError, format("path %1% disappeared, but it still has valid referrers!") % path);
if (repair)
try {
repairPath(path);
} catch (Error & e) {
printMsg(lvlError, format("warning: %1%") % e.msg());
errors = true;
}
else errors = true;
}
2012-07-30 21:42:18 +02:00
return;
}
2012-07-30 21:42:18 +02:00
validPaths.insert(path);
}
bool LocalStore::pathContentsGood(const Path & path)
{
std::map<Path, bool>::iterator i = pathContentsGoodCache.find(path);
if (i != pathContentsGoodCache.end()) return i->second;
2014-08-20 17:00:17 +02:00
printMsg(lvlInfo, format("checking path %1%...") % path);
ValidPathInfo info = queryPathInfo(path);
bool res;
if (!pathExists(path))
res = false;
else {
HashResult current = hashPath(info.narHash.type, path);
Hash nullHash(htSHA256);
res = info.narHash == nullHash || info.narHash == current.first;
}
pathContentsGoodCache[path] = res;
2014-08-20 17:00:17 +02:00
if (!res) printMsg(lvlError, format("path %1% is corrupted or missing!") % path);
return res;
}
void LocalStore::markContentsGood(const Path & path)
{
pathContentsGoodCache[path] = true;
}
#if defined(FS_IOC_SETFLAGS) && defined(FS_IOC_GETFLAGS) && defined(FS_IMMUTABLE_FL)
static void makeMutable(const Path & path)
{
checkInterrupt();
struct stat st = lstat(path);
if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode)) return;
if (S_ISDIR(st.st_mode)) {
for (auto & i : readDirectory(path))
makeMutable(path + "/" + i.name);
}
/* The O_NOFOLLOW is important to prevent us from changing the
mutable bit on the target of a symlink (which would be a
security hole). */
AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_NOFOLLOW);
if (fd == -1) {
if (errno == ELOOP) return; // it's a symlink
2014-08-20 17:00:17 +02:00
throw SysError(format("opening file %1%") % path);
}
unsigned int flags = 0, old;
/* Silently ignore errors getting/setting the immutable flag so
that we work correctly on filesystems that don't support it. */
if (ioctl(fd, FS_IOC_GETFLAGS, &flags)) return;
old = flags;
flags &= ~FS_IMMUTABLE_FL;
if (old == flags) return;
if (ioctl(fd, FS_IOC_SETFLAGS, &flags)) return;
}
/* Upgrade from schema 6 (Nix 0.15) to schema 7 (Nix >= 1.3). */
void LocalStore::upgradeStore7()
{
if (getuid() != 0) return;
printMsg(lvlError, "removing immutable bits from the Nix store (this may take a while)...");
makeMutable(settings.nixStore);
}
#else
void LocalStore::upgradeStore7()
{
}
#endif
void LocalStore::vacuumDB()
{
if (sqlite3_exec(db, "vacuum;", 0, 0, 0) != SQLITE_OK)
throwSQLiteError(db, "vacuuming SQLite database");
}
}