2007-03-30 15:24:35 +02:00
|
|
|
#include "config.h"
|
2006-11-30 18:43:04 +01:00
|
|
|
#include "local-store.hh"
|
2006-09-04 23:06:23 +02:00
|
|
|
#include "globals.hh"
|
|
|
|
#include "archive.hh"
|
|
|
|
#include "pathlocks.hh"
|
|
|
|
#include "derivations-ast.hh"
|
2007-02-21 16:45:32 +01:00
|
|
|
#include "worker-protocol.hh"
|
2010-02-22 12:15:50 +01:00
|
|
|
#include "derivations.hh"
|
2006-09-04 23:06:23 +02:00
|
|
|
|
2003-06-23 15:27:59 +02:00
|
|
|
#include <iostream>
|
2003-12-22 17:40:46 +01:00
|
|
|
#include <algorithm>
|
2003-06-23 15:27:59 +02:00
|
|
|
|
2005-01-19 17:39:47 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2003-10-15 14:42:39 +02:00
|
|
|
#include <unistd.h>
|
2005-01-19 17:39:47 +01:00
|
|
|
#include <utime.h>
|
2008-06-09 15:52:45 +02:00
|
|
|
#include <fcntl.h>
|
2008-07-18 17:34:46 +02:00
|
|
|
#include <errno.h>
|
2009-09-24 09:39:55 +02:00
|
|
|
#include <stdio.h>
|
2003-06-23 15:27:59 +02:00
|
|
|
|
2010-02-18 15:30:42 +01:00
|
|
|
#include <sqlite3.h>
|
|
|
|
|
2006-11-30 19:35:36 +01:00
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
namespace nix {
|
2003-06-23 15:27:59 +02:00
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
|
2010-02-18 14:16:59 +01:00
|
|
|
class SQLiteError : public Error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SQLiteError(sqlite3 * db, const format & f)
|
|
|
|
: Error(format("%1%: %2%") % f.str() % sqlite3_errmsg(db))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-02-18 15:30:42 +01:00
|
|
|
SQLite::~SQLite()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (db && sqlite3_close(db) != SQLITE_OK)
|
|
|
|
throw SQLiteError(db, "closing database");
|
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SQLiteStmt::create(sqlite3 * db, const string & s)
|
|
|
|
{
|
|
|
|
assert(!stmt);
|
|
|
|
if (sqlite3_prepare_v2(db, s.c_str(), -1, &stmt, 0) != SQLITE_OK)
|
|
|
|
throw SQLiteError(db, "creating statement");
|
|
|
|
this->db = db;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-18 16:11:08 +01:00
|
|
|
void SQLiteStmt::reset()
|
|
|
|
{
|
2010-02-18 17:51:27 +01:00
|
|
|
checkInterrupt();
|
2010-02-18 16:11:08 +01:00
|
|
|
assert(stmt);
|
|
|
|
if (sqlite3_reset(stmt) != SQLITE_OK)
|
|
|
|
throw SQLiteError(db, "resetting statement");
|
2010-02-19 17:43:25 +01:00
|
|
|
curArg = 1;
|
2010-02-18 16:11:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-18 15:30:42 +01:00
|
|
|
SQLiteStmt::~SQLiteStmt()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (stmt && sqlite3_finalize(stmt) != SQLITE_OK)
|
|
|
|
throw SQLiteError(db, "finalizing statement");
|
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-19 17:43:25 +01:00
|
|
|
void SQLiteStmt::bind(const string & value)
|
|
|
|
{
|
|
|
|
if (sqlite3_bind_text(stmt, curArg++, value.c_str(), -1, SQLITE_TRANSIENT) != SQLITE_OK)
|
|
|
|
throw SQLiteError(db, "binding argument");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SQLiteStmt::bind(int value)
|
|
|
|
{
|
|
|
|
if (sqlite3_bind_int(stmt, curArg++, value) != SQLITE_OK)
|
|
|
|
throw SQLiteError(db, "binding argument");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SQLiteStmt::bind()
|
|
|
|
{
|
|
|
|
if (sqlite3_bind_null(stmt, curArg++) != SQLITE_OK)
|
|
|
|
throw SQLiteError(db, "binding argument");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-19 17:04:51 +01:00
|
|
|
/* Helper class to ensure that prepared statements are reset when
|
|
|
|
leaving the scope that uses them. Unfinished prepared statements
|
|
|
|
prevent transactions from being aborted, and can cause locks to be
|
|
|
|
kept when they should be released. */
|
|
|
|
struct SQLiteStmtUse
|
|
|
|
{
|
|
|
|
SQLiteStmt & stmt;
|
|
|
|
SQLiteStmtUse(SQLiteStmt & stmt) : stmt(stmt)
|
|
|
|
{
|
|
|
|
stmt.reset();
|
|
|
|
}
|
|
|
|
~SQLiteStmtUse()
|
|
|
|
{
|
2010-02-22 15:18:55 +01:00
|
|
|
try {
|
|
|
|
stmt.reset();
|
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
2010-02-19 17:04:51 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-02-18 15:40:07 +01:00
|
|
|
struct SQLiteTxn
|
|
|
|
{
|
|
|
|
bool active;
|
|
|
|
sqlite3 * db;
|
|
|
|
|
|
|
|
SQLiteTxn(sqlite3 * db) : active(false) {
|
|
|
|
this->db = db;
|
|
|
|
if (sqlite3_exec(db, "begin;", 0, 0, 0) != SQLITE_OK)
|
|
|
|
throw SQLiteError(db, "starting transaction");
|
|
|
|
active = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void commit()
|
|
|
|
{
|
|
|
|
if (sqlite3_exec(db, "commit;", 0, 0, 0) != SQLITE_OK)
|
|
|
|
throw SQLiteError(db, "committing transaction");
|
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
~SQLiteTxn()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (active && sqlite3_exec(db, "rollback;", 0, 0, 0) != SQLITE_OK)
|
|
|
|
throw SQLiteError(db, "aborting transaction");
|
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-03-10 23:27:26 +01:00
|
|
|
void checkStoreNotSymlink()
|
|
|
|
{
|
|
|
|
if (getEnv("NIX_IGNORE_SYMLINK_STORE") == "1") return;
|
|
|
|
Path path = nixStore;
|
|
|
|
struct stat st;
|
|
|
|
while (path != "/") {
|
|
|
|
if (lstat(path.c_str(), &st))
|
|
|
|
throw SysError(format("getting status of `%1%'") % path);
|
|
|
|
if (S_ISLNK(st.st_mode))
|
|
|
|
throw Error(format(
|
|
|
|
"the path `%1%' is a symlink; "
|
|
|
|
"this is not allowed for the Nix store and its parent directories")
|
|
|
|
% path);
|
|
|
|
path = dirOf(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
LocalStore::LocalStore()
|
2003-10-15 14:42:39 +02:00
|
|
|
{
|
2007-08-12 02:29:28 +02:00
|
|
|
substitutablePathsLoaded = false;
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
schemaPath = nixDBPath + "/schema";
|
|
|
|
|
2004-10-25 16:38:23 +02:00
|
|
|
if (readOnlyMode) return;
|
2005-02-09 10:50:29 +01:00
|
|
|
|
2009-09-23 19:05:51 +02:00
|
|
|
/* Create missing state directories if they don't already exist. */
|
2009-03-27 15:19:04 +01:00
|
|
|
createDirs(nixStore);
|
2009-09-23 19:05:51 +02:00
|
|
|
Path profilesDir = nixStateDir + "/profiles";
|
|
|
|
createDirs(nixStateDir + "/profiles");
|
|
|
|
createDirs(nixStateDir + "/temproots");
|
|
|
|
Path gcRootsDir = nixStateDir + "/gcroots";
|
|
|
|
if (!pathExists(gcRootsDir)) {
|
|
|
|
createDirs(gcRootsDir);
|
|
|
|
if (symlink(profilesDir.c_str(), (gcRootsDir + "/profiles").c_str()) == -1)
|
|
|
|
throw SysError(format("creating symlink to `%1%'") % profilesDir);
|
|
|
|
}
|
|
|
|
|
2006-03-10 23:27:26 +01:00
|
|
|
checkStoreNotSymlink();
|
|
|
|
|
2010-02-18 15:30:42 +01:00
|
|
|
/* Acquire the big fat lock in shared mode to make sure that no
|
|
|
|
schema upgrade is in progress. */
|
2008-07-18 17:34:46 +02:00
|
|
|
try {
|
|
|
|
Path globalLockPath = nixDBPath + "/big-lock";
|
|
|
|
globalLock = openLockFile(globalLockPath.c_str(), true);
|
|
|
|
} catch (SysError & e) {
|
|
|
|
if (e.errNo != EACCES) throw;
|
|
|
|
readOnlyMode = true;
|
|
|
|
return;
|
|
|
|
}
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
|
|
if (!lockFile(globalLock, ltRead, false)) {
|
|
|
|
printMsg(lvlError, "waiting for the big Nix store lock...");
|
|
|
|
lockFile(globalLock, ltRead, true);
|
2006-02-16 14:19:15 +01:00
|
|
|
}
|
2010-02-18 15:30:42 +01:00
|
|
|
|
|
|
|
/* Open the Nix database. */
|
|
|
|
if (sqlite3_open_v2((nixDBPath + "/db.sqlite").c_str(), &db.db,
|
|
|
|
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0) != SQLITE_OK)
|
|
|
|
throw Error("cannot open SQLite database");
|
|
|
|
|
|
|
|
if (sqlite3_busy_timeout(db, 60000) != SQLITE_OK)
|
|
|
|
throw SQLiteError(db, "setting timeout");
|
2010-02-18 17:21:59 +01:00
|
|
|
|
2010-02-19 18:15:22 +01:00
|
|
|
if (sqlite3_exec(db, "pragma foreign_keys = 1;", 0, 0, 0) != SQLITE_OK)
|
|
|
|
throw SQLiteError(db, "enabling foreign keys");
|
|
|
|
|
2010-02-18 17:21:59 +01:00
|
|
|
/* !!! check whether sqlite has been built with foreign key
|
|
|
|
support */
|
2009-09-23 19:05:51 +02:00
|
|
|
|
2010-02-18 15:30:42 +01:00
|
|
|
/* Check the current database schema and if necessary do an
|
2010-02-18 15:40:07 +01:00
|
|
|
upgrade. !!! Race condition: several processes could start
|
|
|
|
the upgrade at the same time. */
|
2008-06-09 15:52:45 +02:00
|
|
|
int curSchema = getSchema();
|
2005-02-09 10:50:29 +01:00
|
|
|
if (curSchema > nixSchemaVersion)
|
|
|
|
throw Error(format("current Nix store schema is version %1%, but I only support %2%")
|
|
|
|
% curSchema % nixSchemaVersion);
|
2008-06-09 15:52:45 +02:00
|
|
|
if (curSchema == 0) { /* new store */
|
2010-02-18 15:30:42 +01:00
|
|
|
curSchema = nixSchemaVersion;
|
|
|
|
initSchema();
|
2008-06-09 15:52:45 +02:00
|
|
|
writeFile(schemaPath, (format("%1%") % nixSchemaVersion).str());
|
2005-02-09 10:50:29 +01:00
|
|
|
}
|
2010-02-18 15:30:42 +01:00
|
|
|
else if (curSchema == 1) throw Error("your Nix store is no longer supported");
|
|
|
|
else if (curSchema < 5)
|
2010-02-18 14:16:59 +01:00
|
|
|
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.");
|
2010-02-18 15:30:42 +01:00
|
|
|
else if (curSchema < 6) upgradeStore6();
|
|
|
|
else prepareStatements();
|
2010-01-29 13:22:58 +01:00
|
|
|
|
|
|
|
doFsync = queryBoolSetting("fsync-metadata", false);
|
2003-10-15 14:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-30 18:43:04 +01:00
|
|
|
LocalStore::~LocalStore()
|
2006-03-01 17:36:35 +01:00
|
|
|
{
|
2007-05-01 17:16:17 +02:00
|
|
|
try {
|
2008-08-02 14:54:35 +02:00
|
|
|
foreach (RunningSubstituters::iterator, i, runningSubstituters) {
|
2009-03-28 20:41:53 +01:00
|
|
|
i->second.to.close();
|
|
|
|
i->second.from.close();
|
2008-08-02 14:54:35 +02:00
|
|
|
i->second.pid.wait(true);
|
|
|
|
}
|
2007-05-01 17:16:17 +02:00
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
2006-03-01 17:36:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
int LocalStore::getSchema()
|
2003-10-15 14:42:39 +02:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
int curSchema = 0;
|
|
|
|
if (pathExists(schemaPath)) {
|
|
|
|
string s = readFile(schemaPath);
|
|
|
|
if (!string2Int(s, curSchema))
|
|
|
|
throw Error(format("`%1%' is corrupt") % schemaPath);
|
|
|
|
}
|
|
|
|
return curSchema;
|
2003-10-15 14:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-18 14:16:59 +01:00
|
|
|
void LocalStore::initSchema()
|
|
|
|
{
|
2010-02-19 17:04:51 +01:00
|
|
|
#include "schema.sql.hh"
|
|
|
|
|
2010-02-18 14:16:59 +01:00
|
|
|
if (sqlite3_exec(db, (const char *) schema, 0, 0, 0) != SQLITE_OK)
|
|
|
|
throw SQLiteError(db, "initialising database schema");
|
2010-02-18 15:30:42 +01:00
|
|
|
|
|
|
|
prepareStatements();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LocalStore::prepareStatements()
|
|
|
|
{
|
|
|
|
stmtRegisterValidPath.create(db,
|
2010-02-19 17:04:51 +01:00
|
|
|
"insert or replace into ValidPaths (path, hash, registrationTime, deriver) values (?, ?, ?, ?);");
|
2010-02-18 15:30:42 +01:00
|
|
|
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 from ValidPaths where path = ?;");
|
|
|
|
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 = ?;");
|
2010-02-19 18:15:22 +01:00
|
|
|
stmtRegisterFailedPath.create(db,
|
|
|
|
"insert into FailedPaths (path, time) values (?, ?);");
|
|
|
|
stmtHasPathFailed.create(db,
|
|
|
|
"select time from FailedPaths where path = ?;");
|
2010-02-22 12:15:50 +01:00
|
|
|
stmtAddDerivationOutput.create(db,
|
|
|
|
"insert or replace into DerivationOutputs (drv, id, path) values (?, ?, ?);");
|
2010-02-22 12:44:17 +01:00
|
|
|
stmtQueryValidDerivers.create(db,
|
|
|
|
"select v.id, v.path from DerivationOutputs d join ValidPaths v on d.drv = v.id where d.path = ?;");
|
2010-02-22 13:44:36 +01:00
|
|
|
stmtQueryDerivationOutputs.create(db,
|
|
|
|
"select id, path from DerivationOutputs where drv = ?;");
|
2010-02-18 14:16:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
void canonicalisePathMetaData(const Path & path, bool recurse)
|
2005-01-19 17:39:47 +01:00
|
|
|
{
|
|
|
|
checkInterrupt();
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(path.c_str(), &st))
|
|
|
|
throw SysError(format("getting attributes of path `%1%'") % path);
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
/* Change ownership to the current uid. If it's a symlink, use
|
2006-12-09 21:02:27 +01:00
|
|
|
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
|
2007-02-06 21:03:53 +01:00
|
|
|
if (lchown(path.c_str(), geteuid(), (gid_t) -1) == -1)
|
2006-12-09 21:02:27 +01:00
|
|
|
#else
|
|
|
|
if (!S_ISLNK(st.st_mode) &&
|
2007-02-06 21:03:53 +01:00
|
|
|
chown(path.c_str(), geteuid(), (gid_t) -1) == -1)
|
2006-12-09 21:02:27 +01:00
|
|
|
#endif
|
|
|
|
throw SysError(format("changing owner of `%1%' to %2%")
|
|
|
|
% path % geteuid());
|
|
|
|
}
|
|
|
|
|
2005-01-19 17:39:47 +01:00
|
|
|
if (!S_ISLNK(st.st_mode)) {
|
|
|
|
|
|
|
|
/* Mask out all type related bits. */
|
|
|
|
mode_t mode = st.st_mode & ~S_IFMT;
|
|
|
|
|
|
|
|
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)
|
|
|
|
throw SysError(format("changing mode of `%1%' to %2$o") % path % mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st.st_mtime != 0) {
|
|
|
|
struct utimbuf utimbuf;
|
|
|
|
utimbuf.actime = st.st_atime;
|
2009-06-13 18:30:58 +02:00
|
|
|
utimbuf.modtime = 1; /* 1 second into the epoch */
|
2005-01-19 17:39:47 +01:00
|
|
|
if (utime(path.c_str(), &utimbuf) == -1)
|
|
|
|
throw SysError(format("changing modification time of `%1%'") % path);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-10-10 00:14:27 +02:00
|
|
|
if (recurse && S_ISDIR(st.st_mode)) {
|
2005-01-19 17:39:47 +01:00
|
|
|
Strings names = readDirectory(path);
|
2009-04-21 13:52:16 +02:00
|
|
|
foreach (Strings::iterator, i, names)
|
2008-06-09 15:52:45 +02:00
|
|
|
canonicalisePathMetaData(path + "/" + *i, true);
|
2006-12-09 21:02:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void canonicalisePathMetaData(const Path & path)
|
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
canonicalisePathMetaData(path, true);
|
2006-12-09 21:02:27 +01:00
|
|
|
|
|
|
|
/* 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))
|
|
|
|
throw SysError(format("getting attributes of path `%1%'") % path);
|
|
|
|
|
|
|
|
if (st.st_uid != geteuid()) {
|
|
|
|
assert(S_ISLNK(st.st_mode));
|
|
|
|
throw Error(format("wrong ownership of top-level store path `%1%'") % path);
|
2005-01-19 17:39:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
void LocalStore::registerValidPath(const Path & path,
|
|
|
|
const Hash & hash, const PathSet & references,
|
|
|
|
const Path & deriver)
|
|
|
|
{
|
|
|
|
ValidPathInfo info;
|
|
|
|
info.path = path;
|
|
|
|
info.hash = hash;
|
|
|
|
info.references = references;
|
|
|
|
info.deriver = deriver;
|
|
|
|
registerValidPath(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-19 17:04:51 +01:00
|
|
|
unsigned long long LocalStore::addValidPath(const ValidPathInfo & info)
|
2003-10-10 16:46:28 +02:00
|
|
|
{
|
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.hash));
|
|
|
|
stmtRegisterValidPath.bind(info.registrationTime);
|
|
|
|
if (info.deriver != "")
|
|
|
|
stmtRegisterValidPath.bind(info.deriver);
|
|
|
|
else
|
|
|
|
stmtRegisterValidPath.bind(); // null
|
2010-02-19 17:04:51 +01:00
|
|
|
if (sqlite3_step(stmtRegisterValidPath) != SQLITE_DONE)
|
|
|
|
throw SQLiteError(db, format("registering valid path `%1%' in database") % info.path);
|
2010-02-22 12:15:50 +01:00
|
|
|
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)) {
|
|
|
|
ATerm t = ATreadFromNamedFile(info.path.c_str());
|
|
|
|
if (!t) throw Error(format("cannot read derivation `%1%'") % info.path);
|
|
|
|
Derivation drv = parseDerivation(t);
|
|
|
|
foreach (DerivationOutputs::iterator, i, drv.outputs) {
|
|
|
|
SQLiteStmtUse use(stmtAddDerivationOutput);
|
|
|
|
stmtAddDerivationOutput.bind(id);
|
|
|
|
stmtAddDerivationOutput.bind(i->first);
|
|
|
|
stmtAddDerivationOutput.bind(i->second.path);
|
|
|
|
if (sqlite3_step(stmtAddDerivationOutput) != SQLITE_DONE)
|
|
|
|
throw SQLiteError(db, format("adding derivation output for `%1%' in database") % info.path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
2010-02-19 17:04:51 +01:00
|
|
|
}
|
|
|
|
|
2008-06-09 15:52:45 +02: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)
|
|
|
|
throw SQLiteError(db, "adding reference to database");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LocalStore::registerValidPath(const ValidPathInfo & info)
|
|
|
|
{
|
2008-12-03 16:51:17 +01:00
|
|
|
assert(info.hash.type == htSHA256);
|
2010-02-19 17:04:51 +01:00
|
|
|
ValidPathInfo info2(info);
|
|
|
|
if (info2.registrationTime == 0) info2.registrationTime = time(0);
|
|
|
|
|
|
|
|
SQLiteTxn txn(db);
|
|
|
|
|
|
|
|
unsigned long long id = addValidPath(info2);
|
2008-12-03 16:51:17 +01:00
|
|
|
|
2010-02-19 17:04:51 +01:00
|
|
|
foreach (PathSet::const_iterator, i, info2.references) {
|
|
|
|
ValidPathInfo ref = queryPathInfo(*i);
|
|
|
|
addReference(id, ref.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
txn.commit();
|
2003-10-15 14:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-25 22:05:42 +01:00
|
|
|
void LocalStore::registerFailedPath(const Path & path)
|
|
|
|
{
|
2010-02-19 18:15:22 +01:00
|
|
|
if (hasPathFailed(path)) return;
|
|
|
|
SQLiteStmtUse use(stmtRegisterFailedPath);
|
|
|
|
stmtRegisterFailedPath.bind(path);
|
|
|
|
stmtRegisterFailedPath.bind(time(0));
|
|
|
|
if (sqlite3_step(stmtRegisterFailedPath) != SQLITE_DONE)
|
|
|
|
throw SQLiteError(db, format("registering failed path `%1%'") % path);
|
2009-03-25 22:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LocalStore::hasPathFailed(const Path & path)
|
|
|
|
{
|
2010-02-19 18:15:22 +01:00
|
|
|
SQLiteStmtUse use(stmtHasPathFailed);
|
|
|
|
stmtHasPathFailed.bind(path);
|
|
|
|
int res = sqlite3_step(stmtHasPathFailed);
|
|
|
|
if (res != SQLITE_DONE && res != SQLITE_ROW)
|
|
|
|
throw SQLiteError(db, "querying whether path failed");
|
|
|
|
return res == SQLITE_ROW;
|
2009-03-25 22:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
Hash parseHashField(const Path & path, const string & s)
|
2003-10-10 17:25:21 +02:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
string::size_type colon = s.find(':');
|
|
|
|
if (colon == string::npos)
|
|
|
|
throw Error(format("corrupt hash `%1%' in valid-path entry for `%2%'")
|
|
|
|
% s % path);
|
|
|
|
HashType ht = parseHashType(string(s, 0, colon));
|
|
|
|
if (ht == htUnknown)
|
|
|
|
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));
|
2003-10-10 17:25:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-18 16:52:57 +01:00
|
|
|
ValidPathInfo LocalStore::queryPathInfo(const Path & path)
|
2006-11-30 18:43:04 +01:00
|
|
|
{
|
2010-02-19 17:04:51 +01:00
|
|
|
ValidPathInfo info;
|
|
|
|
info.path = path;
|
2008-06-09 15:52:45 +02:00
|
|
|
|
|
|
|
assertStorePath(path);
|
|
|
|
|
2010-02-18 16:52:57 +01:00
|
|
|
/* Get the path info. */
|
2010-02-19 17:04:51 +01:00
|
|
|
SQLiteStmtUse use1(stmtQueryPathInfo);
|
2010-02-19 17:43:25 +01:00
|
|
|
|
|
|
|
stmtQueryPathInfo.bind(path);
|
2010-02-18 16:52:57 +01:00
|
|
|
|
|
|
|
int r = sqlite3_step(stmtQueryPathInfo);
|
|
|
|
if (r == SQLITE_DONE) throw Error(format("path `%1%' is not valid") % path);
|
|
|
|
if (r != SQLITE_ROW) throw SQLiteError(db, "querying path in database");
|
2010-01-29 12:53:58 +01:00
|
|
|
|
2010-02-19 17:04:51 +01:00
|
|
|
info.id = sqlite3_column_int(stmtQueryPathInfo, 0);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
2010-02-18 16:52:57 +01:00
|
|
|
const char * s = (const char *) sqlite3_column_text(stmtQueryPathInfo, 1);
|
|
|
|
assert(s);
|
2010-02-19 17:04:51 +01:00
|
|
|
info.hash = parseHashField(path, s);
|
2010-02-18 16:52:57 +01:00
|
|
|
|
2010-02-19 17:04:51 +01:00
|
|
|
info.registrationTime = sqlite3_column_int(stmtQueryPathInfo, 2);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
2010-02-18 16:52:57 +01:00
|
|
|
s = (const char *) sqlite3_column_text(stmtQueryPathInfo, 3);
|
2010-02-19 17:04:51 +01:00
|
|
|
if (s) info.deriver = s;
|
2010-02-18 16:52:57 +01:00
|
|
|
|
|
|
|
/* Get the references. */
|
2010-02-19 17:04:51 +01:00
|
|
|
SQLiteStmtUse use2(stmtQueryReferences);
|
2010-02-18 16:52:57 +01:00
|
|
|
|
2010-02-19 17:43:25 +01:00
|
|
|
stmtQueryReferences.bind(info.id);
|
2010-02-18 16:52:57 +01:00
|
|
|
|
|
|
|
while ((r = sqlite3_step(stmtQueryReferences)) == SQLITE_ROW) {
|
|
|
|
s = (const char *) sqlite3_column_text(stmtQueryReferences, 0);
|
|
|
|
assert(s);
|
2010-02-19 17:04:51 +01:00
|
|
|
info.references.insert(s);
|
2008-06-09 15:52:45 +02:00
|
|
|
}
|
|
|
|
|
2010-02-18 16:52:57 +01:00
|
|
|
if (r != SQLITE_DONE)
|
2010-02-18 17:51:27 +01:00
|
|
|
throw SQLiteError(db, format("error getting references of `%1%'") % path);
|
2010-02-18 16:52:57 +01:00
|
|
|
|
2010-02-19 17:04:51 +01:00
|
|
|
return info;
|
2006-11-30 18:43:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
bool LocalStore::isValidPath(const Path & path)
|
2005-01-19 17:59:56 +01:00
|
|
|
{
|
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)
|
|
|
|
throw SQLiteError(db, "querying path in database");
|
|
|
|
return res == SQLITE_ROW;
|
2005-01-19 17:59:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
PathSet LocalStore::queryValidPaths()
|
2006-11-30 18:43:04 +01:00
|
|
|
{
|
2010-02-18 17:51:27 +01:00
|
|
|
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)
|
|
|
|
throw SQLiteError(db, "error getting valid paths");
|
|
|
|
|
|
|
|
return res;
|
2006-11-30 18:43:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
void LocalStore::queryReferences(const Path & path,
|
|
|
|
PathSet & references)
|
2005-02-07 14:40:40 +01:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
ValidPathInfo info = queryPathInfo(path);
|
|
|
|
references.insert(info.references.begin(), info.references.end());
|
2005-02-07 14:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-18 17:21:59 +01:00
|
|
|
void LocalStore::queryReferrers(const Path & path, PathSet & referrers)
|
2005-02-07 14:40:40 +01:00
|
|
|
{
|
2010-02-18 17:21:59 +01:00
|
|
|
assertStorePath(path);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
2010-02-19 17:04:51 +01:00
|
|
|
SQLiteStmtUse use(stmtQueryReferrers);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
2010-02-19 17:43:25 +01:00
|
|
|
stmtQueryReferrers.bind(path);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
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);
|
|
|
|
}
|
2008-06-09 15:52:45 +02:00
|
|
|
|
2010-02-18 17:21:59 +01:00
|
|
|
if (r != SQLITE_DONE)
|
2010-02-18 17:51:27 +01:00
|
|
|
throw SQLiteError(db, format("error getting references of `%1%'") % path);
|
2005-02-07 14:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-12 18:53:44 +02:00
|
|
|
Path LocalStore::queryDeriver(const Path & path)
|
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
return queryPathInfo(path).deriver;
|
2007-06-12 18:53:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-22 12:44:17 +01:00
|
|
|
PathSet LocalStore::queryValidDerivers(const Path & path)
|
|
|
|
{
|
|
|
|
assertStorePath(path);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r != SQLITE_DONE)
|
|
|
|
throw SQLiteError(db, format("error getting valid derivers of `%1%'") % path);
|
|
|
|
|
|
|
|
return derivers;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-22 13:44:36 +01:00
|
|
|
PathSet LocalStore::queryDerivationOutputs(const Path & path)
|
|
|
|
{
|
|
|
|
SQLiteTxn txn(db);
|
|
|
|
|
|
|
|
SQLiteStmtUse use(stmtQueryDerivationOutputs);
|
|
|
|
stmtQueryDerivationOutputs.bind(queryPathInfo(path).id);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r != SQLITE_DONE)
|
|
|
|
throw SQLiteError(db, format("error getting outputs of `%1%'") % path);
|
|
|
|
|
|
|
|
return outputs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-02 14:54:35 +02:00
|
|
|
void LocalStore::startSubstituter(const Path & substituter, RunningSubstituter & run)
|
2003-07-10 17:11:48 +02:00
|
|
|
{
|
2008-08-02 14:54:35 +02:00
|
|
|
if (run.pid != -1) return;
|
|
|
|
|
|
|
|
debug(format("starting substituter program `%1%'") % substituter);
|
|
|
|
|
|
|
|
Pipe toPipe, fromPipe;
|
|
|
|
|
|
|
|
toPipe.create();
|
|
|
|
fromPipe.create();
|
|
|
|
|
|
|
|
run.pid = fork();
|
|
|
|
|
|
|
|
switch (run.pid) {
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
throw SysError("unable to fork");
|
|
|
|
|
|
|
|
case 0: /* child */
|
|
|
|
try {
|
2009-09-23 20:04:55 +02:00
|
|
|
/* Hack to let "make check" succeed on Darwin. The
|
|
|
|
libtool wrapper script sets DYLD_LIBRARY_PATH to our
|
|
|
|
libutil (among others), but Perl also depends on a
|
|
|
|
library named libutil. As a result, substituters
|
|
|
|
written in Perl (i.e. all of them) fail. */
|
|
|
|
unsetenv("DYLD_LIBRARY_PATH");
|
|
|
|
|
2008-08-02 14:54:35 +02:00
|
|
|
fromPipe.readSide.close();
|
|
|
|
toPipe.writeSide.close();
|
|
|
|
if (dup2(toPipe.readSide, STDIN_FILENO) == -1)
|
|
|
|
throw SysError("dupping stdin");
|
|
|
|
if (dup2(fromPipe.writeSide, STDOUT_FILENO) == -1)
|
|
|
|
throw SysError("dupping stdout");
|
|
|
|
closeMostFDs(set<int>());
|
|
|
|
execl(substituter.c_str(), substituter.c_str(), "--query", NULL);
|
|
|
|
throw SysError(format("executing `%1%'") % substituter);
|
|
|
|
} catch (std::exception & e) {
|
|
|
|
std::cerr << "error: " << e.what() << std::endl;
|
2004-06-20 21:17:54 +02:00
|
|
|
}
|
2008-08-02 14:54:35 +02:00
|
|
|
quickExit(1);
|
2004-06-20 21:17:54 +02:00
|
|
|
}
|
2003-12-05 12:05:19 +01:00
|
|
|
|
2008-08-02 14:54:35 +02:00
|
|
|
/* Parent. */
|
|
|
|
|
2009-03-28 20:41:53 +01:00
|
|
|
run.to = toPipe.writeSide.borrow();
|
|
|
|
run.from = fromPipe.readSide.borrow();
|
2004-06-20 21:17:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-28 20:41:53 +01:00
|
|
|
template<class T> T getIntLine(int fd)
|
2008-08-05 12:57:53 +02:00
|
|
|
{
|
2009-03-28 20:41:53 +01:00
|
|
|
string s = readLine(fd);
|
2008-08-05 12:57:53 +02:00
|
|
|
T res;
|
2009-03-28 20:41:53 +01:00
|
|
|
if (!string2Int(s, res)) throw Error("integer expected from stream");
|
2008-08-05 12:57:53 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-12 02:29:28 +02:00
|
|
|
bool LocalStore::hasSubstitutes(const Path & path)
|
2004-06-20 21:17:54 +02:00
|
|
|
{
|
2008-08-02 14:54:35 +02:00
|
|
|
foreach (Paths::iterator, i, substituters) {
|
|
|
|
RunningSubstituter & run(runningSubstituters[*i]);
|
|
|
|
startSubstituter(*i, run);
|
2009-03-28 20:41:53 +01:00
|
|
|
writeLine(run.to, "have\n" + path);
|
|
|
|
if (getIntLine<int>(run.from)) return true;
|
2008-08-02 14:54:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-04 15:15:35 +02:00
|
|
|
bool LocalStore::querySubstitutablePathInfo(const Path & substituter,
|
|
|
|
const Path & path, SubstitutablePathInfo & info)
|
2008-08-02 14:54:35 +02:00
|
|
|
{
|
2008-08-04 15:15:35 +02:00
|
|
|
RunningSubstituter & run(runningSubstituters[substituter]);
|
|
|
|
startSubstituter(substituter, run);
|
2008-08-02 14:54:35 +02:00
|
|
|
|
2009-03-28 20:41:53 +01:00
|
|
|
writeLine(run.to, "info\n" + path);
|
|
|
|
|
|
|
|
if (!getIntLine<int>(run.from)) return false;
|
2008-08-04 15:15:35 +02:00
|
|
|
|
2009-03-28 20:41:53 +01:00
|
|
|
info.deriver = readLine(run.from);
|
2008-08-05 12:57:53 +02:00
|
|
|
if (info.deriver != "") assertStorePath(info.deriver);
|
2009-03-28 20:41:53 +01:00
|
|
|
int nrRefs = getIntLine<int>(run.from);
|
2008-08-04 15:15:35 +02:00
|
|
|
while (nrRefs--) {
|
2009-03-28 20:41:53 +01:00
|
|
|
Path p = readLine(run.from);
|
2008-08-05 12:57:53 +02:00
|
|
|
assertStorePath(p);
|
2008-08-04 15:15:35 +02:00
|
|
|
info.references.insert(p);
|
2008-08-02 14:54:35 +02:00
|
|
|
}
|
2009-03-28 20:41:53 +01:00
|
|
|
info.downloadSize = getIntLine<long long>(run.from);
|
2008-08-02 14:54:35 +02:00
|
|
|
|
2008-08-04 15:15:35 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LocalStore::querySubstitutablePathInfo(const Path & path,
|
|
|
|
SubstitutablePathInfo & info)
|
|
|
|
{
|
|
|
|
foreach (Paths::iterator, i, substituters)
|
|
|
|
if (querySubstitutablePathInfo(*i, path, info)) return true;
|
2008-08-02 14:54:35 +02:00
|
|
|
return false;
|
2004-12-20 14:43:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
Hash LocalStore::queryPathHash(const Path & path)
|
2005-02-09 10:50:29 +01:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
return queryPathInfo(path).hash;
|
2005-02-09 10:50:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
static void dfsVisit(std::map<Path, ValidPathInfo> & infos,
|
|
|
|
const Path & path, PathSet & visited, Paths & sorted)
|
2005-03-02 16:57:06 +01:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
if (visited.find(path) != visited.end()) return;
|
|
|
|
visited.insert(path);
|
2005-03-02 16:57:06 +01:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
ValidPathInfo & info(infos[path]);
|
|
|
|
|
2009-04-21 13:52:16 +02:00
|
|
|
foreach (PathSet::iterator, i, info.references)
|
2008-06-09 15:52:45 +02:00
|
|
|
if (infos.find(*i) != infos.end())
|
|
|
|
dfsVisit(infos, *i, visited, sorted);
|
2005-03-02 16:57:06 +01:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
sorted.push_back(path);
|
2005-03-23 14:07:28 +01:00
|
|
|
}
|
2005-01-19 17:39:47 +01:00
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
void LocalStore::registerValidPaths(const ValidPathInfos & infos)
|
2005-03-23 14:07:28 +01:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
std::map<Path, ValidPathInfo> infosMap;
|
|
|
|
|
|
|
|
/* Sort the paths topologically under the references relation, so
|
|
|
|
that if path A is referenced by B, then A is registered before
|
|
|
|
B. */
|
2009-04-21 13:52:16 +02:00
|
|
|
foreach (ValidPathInfos::const_iterator, i, infos)
|
2008-06-09 15:52:45 +02:00
|
|
|
infosMap[i->path] = *i;
|
2005-03-23 14:07:28 +01:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
PathSet visited;
|
|
|
|
Paths sorted;
|
2009-04-21 13:52:16 +02:00
|
|
|
foreach (ValidPathInfos::const_iterator, i, infos)
|
2008-06-09 15:52:45 +02:00
|
|
|
dfsVisit(infosMap, i->path, visited, sorted);
|
2005-02-07 14:40:40 +01:00
|
|
|
|
2009-04-21 13:52:16 +02:00
|
|
|
foreach (Paths::iterator, i, sorted)
|
2008-06-09 15:52:45 +02:00
|
|
|
registerValidPath(infosMap[*i]);
|
2003-10-08 17:06:59 +02:00
|
|
|
}
|
2003-07-07 11:25:26 +02:00
|
|
|
|
2003-07-31 18:05:35 +02:00
|
|
|
|
2005-01-31 15:00:43 +01:00
|
|
|
/* Invalidate a path. The caller is responsible for checking that
|
2005-12-13 22:04:48 +01:00
|
|
|
there are no referrers. */
|
2008-06-09 15:52:45 +02:00
|
|
|
void LocalStore::invalidatePath(const Path & path)
|
2003-07-08 11:54:47 +02:00
|
|
|
{
|
2007-08-12 02:29:28 +02:00
|
|
|
debug(format("invalidating path `%1%'") % path);
|
2010-02-22 15:18:55 +01:00
|
|
|
|
2010-02-19 17:43:25 +01:00
|
|
|
SQLiteStmtUse use(stmtInvalidatePath);
|
2003-07-08 11:54:47 +02:00
|
|
|
|
2010-02-19 17:43:25 +01:00
|
|
|
stmtInvalidatePath.bind(path);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
2010-02-19 17:43:25 +01:00
|
|
|
if (sqlite3_step(stmtInvalidatePath) != SQLITE_DONE)
|
|
|
|
throw SQLiteError(db, format("invalidating path `%1%' in database") % path);
|
2008-06-09 15:52:45 +02:00
|
|
|
|
2010-02-19 17:43:25 +01:00
|
|
|
/* Note that the foreign key constraints on the Refs table take
|
2010-02-22 15:18:55 +01:00
|
|
|
care of deleting the references entries for `path'. */
|
2003-07-08 11:54:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-03 19:05:14 +01:00
|
|
|
Path LocalStore::addToStoreFromDump(const string & dump, const string & name,
|
|
|
|
bool recursive, HashType hashAlgo)
|
2003-07-07 11:25:26 +02:00
|
|
|
{
|
2008-12-03 19:05:14 +01:00
|
|
|
Hash h = hashString(hashAlgo, dump);
|
2008-12-03 16:51:17 +01:00
|
|
|
|
2008-12-03 19:05:14 +01:00
|
|
|
Path dstPath = makeFixedOutputPath(recursive, hashAlgo, h, name);
|
2003-07-10 17:11:48 +02:00
|
|
|
|
2006-12-01 21:51:18 +01:00
|
|
|
addTempRoot(dstPath);
|
2005-01-31 11:27:25 +01:00
|
|
|
|
2006-12-01 21:51:18 +01:00
|
|
|
if (!isValidPath(dstPath)) {
|
2003-07-10 17:11:48 +02:00
|
|
|
|
2003-10-08 17:06:59 +02:00
|
|
|
/* The first check above is an optimisation to prevent
|
|
|
|
unnecessary lock acquisition. */
|
2003-07-22 17:15:15 +02:00
|
|
|
|
2006-06-01 20:13:33 +02:00
|
|
|
PathLocks outputLock(singleton<PathSet, Path>(dstPath));
|
2003-07-22 17:15:15 +02:00
|
|
|
|
2003-10-08 17:06:59 +02:00
|
|
|
if (!isValidPath(dstPath)) {
|
2004-06-21 09:46:02 +02:00
|
|
|
|
2006-12-09 01:26:24 +01:00
|
|
|
if (pathExists(dstPath)) deletePathWrapped(dstPath);
|
2004-10-25 16:38:23 +02:00
|
|
|
|
2008-12-03 16:51:17 +01:00
|
|
|
if (recursive) {
|
2008-12-03 19:05:14 +01:00
|
|
|
StringSource source(dump);
|
2008-12-03 16:51:17 +01:00
|
|
|
restorePath(dstPath, source);
|
|
|
|
} else
|
2010-01-29 13:22:58 +01:00
|
|
|
writeFile(dstPath, dump);
|
2005-01-14 14:51:38 +01:00
|
|
|
|
2005-01-19 17:39:47 +01:00
|
|
|
canonicalisePathMetaData(dstPath);
|
2008-12-03 16:51:17 +01:00
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
registerValidPath(dstPath,
|
2008-12-03 17:10:17 +01:00
|
|
|
(recursive && hashAlgo == htSHA256) ? h :
|
2008-12-03 19:05:14 +01:00
|
|
|
(recursive ? hashString(htSHA256, dump) : hashPath(htSHA256, dstPath)),
|
2008-12-03 16:51:17 +01:00
|
|
|
PathSet(), "");
|
2003-08-01 11:01:51 +02:00
|
|
|
}
|
2003-11-22 19:45:56 +01:00
|
|
|
|
|
|
|
outputLock.setDeletion(true);
|
2003-06-16 15:33:38 +02:00
|
|
|
}
|
2003-08-04 09:09:36 +02:00
|
|
|
|
2003-10-08 17:06:59 +02:00
|
|
|
return dstPath;
|
2003-06-16 15:33:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-03 19:05:14 +01:00
|
|
|
Path LocalStore::addToStore(const Path & _srcPath,
|
|
|
|
bool recursive, HashType hashAlgo, PathFilter & filter)
|
|
|
|
{
|
|
|
|
Path srcPath(absPath(_srcPath));
|
|
|
|
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;
|
|
|
|
if (recursive)
|
|
|
|
dumpPath(srcPath, sink, filter);
|
|
|
|
else
|
|
|
|
sink.s = readFile(srcPath);
|
|
|
|
|
|
|
|
return addToStoreFromDump(sink.s, baseNameOf(srcPath), recursive, hashAlgo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-03 16:06:30 +01:00
|
|
|
Path LocalStore::addTextToStore(const string & name, const string & s,
|
2005-01-25 22:28:25 +01:00
|
|
|
const PathSet & references)
|
2003-10-15 14:42:39 +02:00
|
|
|
{
|
2008-12-03 16:06:30 +01:00
|
|
|
Path dstPath = computeStorePathForText(name, s, references);
|
2004-02-14 22:44:18 +01:00
|
|
|
|
2006-12-01 21:51:18 +01:00
|
|
|
addTempRoot(dstPath);
|
2005-01-31 11:27:25 +01:00
|
|
|
|
2006-12-01 21:51:18 +01:00
|
|
|
if (!isValidPath(dstPath)) {
|
2003-10-15 14:42:39 +02:00
|
|
|
|
2006-06-01 20:13:33 +02:00
|
|
|
PathLocks outputLock(singleton<PathSet, Path>(dstPath));
|
2003-10-23 12:51:55 +02:00
|
|
|
|
|
|
|
if (!isValidPath(dstPath)) {
|
2004-06-21 09:46:02 +02:00
|
|
|
|
2006-12-09 01:26:24 +01:00
|
|
|
if (pathExists(dstPath)) deletePathWrapped(dstPath);
|
2004-06-21 09:46:02 +02:00
|
|
|
|
2010-01-29 13:22:58 +01:00
|
|
|
writeFile(dstPath, s);
|
2003-10-15 14:42:39 +02:00
|
|
|
|
2005-01-19 17:39:47 +01:00
|
|
|
canonicalisePathMetaData(dstPath);
|
2004-09-09 23:19:20 +02:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
registerValidPath(dstPath,
|
2005-02-07 14:40:40 +01:00
|
|
|
hashPath(htSHA256, dstPath), references, "");
|
2003-10-23 12:51:55 +02:00
|
|
|
}
|
2003-11-22 19:45:56 +01:00
|
|
|
|
|
|
|
outputLock.setDeletion(true);
|
2003-10-15 14:42:39 +02:00
|
|
|
}
|
2005-01-14 14:51:38 +01:00
|
|
|
|
|
|
|
return dstPath;
|
2003-10-15 14:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-21 15:31:42 +01:00
|
|
|
struct HashAndWriteSink : Sink
|
|
|
|
{
|
|
|
|
Sink & writeSink;
|
|
|
|
HashSink hashSink;
|
|
|
|
bool hashing;
|
|
|
|
HashAndWriteSink(Sink & writeSink) : writeSink(writeSink), hashSink(htSHA256)
|
|
|
|
{
|
|
|
|
hashing = true;
|
|
|
|
}
|
|
|
|
virtual void operator ()
|
|
|
|
(const unsigned char * data, unsigned int len)
|
|
|
|
{
|
|
|
|
writeSink(data, len);
|
|
|
|
if (hashing) hashSink(data, len);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define EXPORT_MAGIC 0x4558494e
|
|
|
|
|
|
|
|
|
2007-02-21 18:51:10 +01:00
|
|
|
static void checkSecrecy(const Path & path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (stat(path.c_str(), &st))
|
|
|
|
throw SysError(format("getting status of `%1%'") % path);
|
|
|
|
if ((st.st_mode & (S_IRWXG | S_IRWXO)) != 0)
|
|
|
|
throw Error(format("file `%1%' should be secret (inaccessible to everybody else)!") % path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-21 00:17:20 +01:00
|
|
|
void LocalStore::exportPath(const Path & path, bool sign,
|
|
|
|
Sink & sink)
|
|
|
|
{
|
|
|
|
assertStorePath(path);
|
2007-02-21 15:31:42 +01:00
|
|
|
|
2007-02-21 17:23:25 +01:00
|
|
|
addTempRoot(path);
|
2008-03-01 22:05:33 +01:00
|
|
|
if (!isValidPath(path))
|
2007-02-21 17:23:25 +01:00
|
|
|
throw Error(format("path `%1%' is not valid") % path);
|
|
|
|
|
2007-02-21 15:31:42 +01:00
|
|
|
HashAndWriteSink hashAndWriteSink(sink);
|
2007-02-21 00:17:20 +01:00
|
|
|
|
2007-02-21 15:31:42 +01:00
|
|
|
dumpPath(path, hashAndWriteSink);
|
2007-02-21 00:17:20 +01:00
|
|
|
|
2007-02-21 15:31:42 +01:00
|
|
|
writeInt(EXPORT_MAGIC, hashAndWriteSink);
|
|
|
|
|
|
|
|
writeString(path, hashAndWriteSink);
|
2007-02-21 00:17:20 +01:00
|
|
|
|
|
|
|
PathSet references;
|
2008-03-01 22:05:33 +01:00
|
|
|
queryReferences(path, references);
|
2007-02-21 15:31:42 +01:00
|
|
|
writeStringSet(references, hashAndWriteSink);
|
2007-02-21 00:17:20 +01:00
|
|
|
|
2008-03-01 22:05:33 +01:00
|
|
|
Path deriver = queryDeriver(path);
|
2007-02-21 15:31:42 +01:00
|
|
|
writeString(deriver, hashAndWriteSink);
|
|
|
|
|
|
|
|
if (sign) {
|
|
|
|
Hash hash = hashAndWriteSink.hashSink.finish();
|
|
|
|
hashAndWriteSink.hashing = false;
|
|
|
|
|
|
|
|
writeInt(1, hashAndWriteSink);
|
|
|
|
|
|
|
|
Path tmpDir = createTempDir();
|
|
|
|
AutoDelete delTmp(tmpDir);
|
|
|
|
Path hashFile = tmpDir + "/hash";
|
2010-01-29 13:22:58 +01:00
|
|
|
writeFile(hashFile, printHash(hash));
|
2007-02-21 15:31:42 +01:00
|
|
|
|
2007-02-21 18:51:10 +01:00
|
|
|
Path secretKey = nixConfDir + "/signing-key.sec";
|
|
|
|
checkSecrecy(secretKey);
|
|
|
|
|
2007-02-21 15:31:42 +01:00
|
|
|
Strings args;
|
|
|
|
args.push_back("rsautl");
|
|
|
|
args.push_back("-sign");
|
|
|
|
args.push_back("-inkey");
|
2007-02-21 18:51:10 +01:00
|
|
|
args.push_back(secretKey);
|
2007-02-21 15:31:42 +01:00
|
|
|
args.push_back("-in");
|
|
|
|
args.push_back(hashFile);
|
2007-03-01 14:30:46 +01:00
|
|
|
string signature = runProgram(OPENSSL_PATH, true, args);
|
2007-02-21 15:31:42 +01:00
|
|
|
|
|
|
|
writeString(signature, hashAndWriteSink);
|
|
|
|
|
|
|
|
} else
|
|
|
|
writeInt(0, hashAndWriteSink);
|
2007-02-21 00:17:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-21 16:45:32 +01:00
|
|
|
struct HashAndReadSource : Source
|
|
|
|
{
|
|
|
|
Source & readSource;
|
|
|
|
HashSink hashSink;
|
|
|
|
bool hashing;
|
|
|
|
HashAndReadSource(Source & readSource) : readSource(readSource), hashSink(htSHA256)
|
|
|
|
{
|
|
|
|
hashing = true;
|
|
|
|
}
|
|
|
|
virtual void operator ()
|
|
|
|
(unsigned char * data, unsigned int len)
|
|
|
|
{
|
|
|
|
readSource(data, len);
|
|
|
|
if (hashing) hashSink(data, len);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Path LocalStore::importPath(bool requireSignature, Source & source)
|
|
|
|
{
|
|
|
|
HashAndReadSource hashAndReadSource(source);
|
|
|
|
|
|
|
|
/* 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 = createTempDir(nixStore);
|
2009-03-22 18:36:43 +01:00
|
|
|
AutoDelete delTmp(tmpDir); /* !!! could be GC'ed! */
|
2007-02-21 16:45:32 +01:00
|
|
|
Path unpacked = tmpDir + "/unpacked";
|
|
|
|
|
|
|
|
restorePath(unpacked, hashAndReadSource);
|
|
|
|
|
|
|
|
unsigned int magic = readInt(hashAndReadSource);
|
|
|
|
if (magic != EXPORT_MAGIC)
|
|
|
|
throw Error("Nix archive cannot be imported; wrong format");
|
|
|
|
|
|
|
|
Path dstPath = readStorePath(hashAndReadSource);
|
|
|
|
|
|
|
|
PathSet references = readStorePaths(hashAndReadSource);
|
|
|
|
|
2007-02-28 00:18:57 +01:00
|
|
|
Path deriver = readString(hashAndReadSource);
|
|
|
|
if (deriver != "") assertStorePath(deriver);
|
2007-02-21 16:45:32 +01:00
|
|
|
|
|
|
|
Hash hash = hashAndReadSource.hashSink.finish();
|
|
|
|
hashAndReadSource.hashing = false;
|
|
|
|
|
|
|
|
bool haveSignature = readInt(hashAndReadSource) == 1;
|
|
|
|
|
|
|
|
if (requireSignature && !haveSignature)
|
|
|
|
throw Error("imported archive lacks a signature");
|
|
|
|
|
|
|
|
if (haveSignature) {
|
|
|
|
string signature = readString(hashAndReadSource);
|
|
|
|
|
2007-03-01 13:30:24 +01:00
|
|
|
if (requireSignature) {
|
|
|
|
Path sigFile = tmpDir + "/sig";
|
2010-01-29 13:22:58 +01:00
|
|
|
writeFile(sigFile, signature);
|
2007-03-01 13:30:24 +01:00
|
|
|
|
|
|
|
Strings args;
|
|
|
|
args.push_back("rsautl");
|
|
|
|
args.push_back("-verify");
|
|
|
|
args.push_back("-inkey");
|
|
|
|
args.push_back(nixConfDir + "/signing-key.pub");
|
|
|
|
args.push_back("-pubin");
|
|
|
|
args.push_back("-in");
|
|
|
|
args.push_back(sigFile);
|
2007-03-01 14:30:46 +01:00
|
|
|
string hash2 = runProgram(OPENSSL_PATH, true, args);
|
2007-03-01 13:30:24 +01:00
|
|
|
|
|
|
|
/* 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");
|
|
|
|
}
|
2007-02-21 16:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the actual import. */
|
|
|
|
|
|
|
|
/* !!! way too much code duplication with addTextToStore() etc. */
|
|
|
|
addTempRoot(dstPath);
|
|
|
|
|
|
|
|
if (!isValidPath(dstPath)) {
|
|
|
|
|
2009-02-02 18:24:10 +01:00
|
|
|
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). */
|
|
|
|
Strings locksHeld = tokenizeString(getEnv("NIX_HELD_LOCKS"));
|
|
|
|
if (find(locksHeld.begin(), locksHeld.end(), dstPath) == locksHeld.end())
|
|
|
|
outputLock.lockPaths(singleton<PathSet, Path>(dstPath));
|
2007-02-21 16:45:32 +01:00
|
|
|
|
|
|
|
if (!isValidPath(dstPath)) {
|
|
|
|
|
|
|
|
if (pathExists(dstPath)) deletePathWrapped(dstPath);
|
|
|
|
|
|
|
|
if (rename(unpacked.c_str(), dstPath.c_str()) == -1)
|
|
|
|
throw SysError(format("cannot move `%1%' to `%2%'")
|
|
|
|
% unpacked % dstPath);
|
|
|
|
|
|
|
|
canonicalisePathMetaData(dstPath);
|
|
|
|
|
|
|
|
/* !!! if we were clever, we could prevent the hashPath()
|
|
|
|
here. */
|
2008-06-09 15:52:45 +02:00
|
|
|
if (deriver != "" && !isValidPath(deriver)) deriver = "";
|
|
|
|
registerValidPath(dstPath,
|
2007-02-21 17:23:25 +01:00
|
|
|
hashPath(htSHA256, dstPath), references, deriver);
|
2007-02-21 16:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
outputLock.setDeletion(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dstPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-18 11:34:17 +02:00
|
|
|
void LocalStore::deleteFromStore(const Path & path, unsigned long long & bytesFreed,
|
|
|
|
unsigned long long & blocksFreed)
|
2003-06-23 16:40:49 +02:00
|
|
|
{
|
2005-12-15 22:11:39 +01:00
|
|
|
bytesFreed = 0;
|
2003-10-08 17:06:59 +02:00
|
|
|
|
2004-02-14 22:44:18 +01:00
|
|
|
assertStorePath(path);
|
2003-07-08 11:54:47 +02:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
if (isValidPath(path)) {
|
|
|
|
PathSet referrers; queryReferrers(path, referrers);
|
|
|
|
referrers.erase(path); /* ignore self-references */
|
|
|
|
if (!referrers.empty())
|
|
|
|
throw PathInUse(format("cannot delete path `%1%' because it is in use by `%2%'")
|
|
|
|
% path % showPaths(referrers));
|
|
|
|
invalidatePath(path);
|
2005-01-31 15:00:43 +01:00
|
|
|
}
|
2003-07-08 11:54:47 +02:00
|
|
|
|
2008-06-18 11:34:17 +02:00
|
|
|
deletePathWrapped(path, bytesFreed, blocksFreed);
|
2003-06-23 16:40:49 +02:00
|
|
|
}
|
2003-07-17 14:27:55 +02:00
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
void LocalStore::verifyStore(bool checkContents)
|
2003-07-17 14:27:55 +02:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
/* Check whether all valid paths actually exist. */
|
2007-01-14 18:28:30 +01:00
|
|
|
printMsg(lvlInfo, "checking path existence");
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
PathSet validPaths2 = queryValidPaths(), validPaths;
|
|
|
|
|
2009-04-21 13:06:27 +02:00
|
|
|
foreach (PathSet::iterator, i, validPaths2) {
|
2007-05-01 15:21:05 +02:00
|
|
|
checkInterrupt();
|
2010-02-18 17:51:27 +01:00
|
|
|
/* !!! invalidatePath() will probably fail due to the foreign
|
|
|
|
key constraints on the Ref table. */
|
2008-06-09 15:52:45 +02:00
|
|
|
if (!isStorePath(*i)) {
|
2005-02-08 14:48:53 +01:00
|
|
|
printMsg(lvlError, format("path `%1%' is not in the Nix store") % *i);
|
2008-06-09 15:52:45 +02:00
|
|
|
invalidatePath(*i);
|
|
|
|
} else if (!pathExists(*i)) {
|
|
|
|
printMsg(lvlError, format("path `%1%' disappeared") % *i);
|
|
|
|
invalidatePath(*i);
|
2010-02-18 17:51:27 +01:00
|
|
|
} else validPaths.insert(*i);
|
2003-07-17 14:27:55 +02:00
|
|
|
}
|
|
|
|
|
2010-02-18 17:51:27 +01:00
|
|
|
/* Optionally, check the content hashes (slow). */
|
|
|
|
if (checkContents) {
|
|
|
|
printMsg(lvlInfo, "checking hashes");
|
2007-01-14 18:28:30 +01:00
|
|
|
|
2010-02-18 17:51:27 +01:00
|
|
|
foreach (PathSet::iterator, i, validPaths) {
|
|
|
|
ValidPathInfo info = queryPathInfo(*i);
|
2007-01-14 18:28:30 +01:00
|
|
|
|
2010-02-18 17:51:27 +01:00
|
|
|
/* Check the content hash (optionally - slow). */
|
|
|
|
printMsg(lvlTalkative, format("checking contents of `%1%'") % *i);
|
2008-06-09 15:52:45 +02:00
|
|
|
Hash current = hashPath(info.hash.type, *i);
|
|
|
|
if (current != info.hash) {
|
|
|
|
printMsg(lvlError, format("path `%1%' was modified! "
|
|
|
|
"expected hash `%2%', got `%3%'")
|
|
|
|
% *i % printHash(info.hash) % printHash(current));
|
2005-02-08 14:23:55 +01:00
|
|
|
}
|
|
|
|
}
|
2007-08-13 13:37:39 +02:00
|
|
|
}
|
2010-02-18 16:11:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Functions for upgrading from the pre-SQLite database. */
|
|
|
|
|
|
|
|
PathSet LocalStore::queryValidPathsOld()
|
|
|
|
{
|
|
|
|
PathSet paths;
|
|
|
|
Strings entries = readDirectory(nixDBPath + "/info");
|
|
|
|
foreach (Strings::iterator, i, entries)
|
|
|
|
if (i->at(0) != '.') paths.insert(nixStore + "/" + *i);
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ValidPathInfo LocalStore::queryPathInfoOld(const Path & path)
|
|
|
|
{
|
|
|
|
ValidPathInfo res;
|
|
|
|
res.path = path;
|
|
|
|
|
|
|
|
/* Read the info file. */
|
2010-02-19 17:04:51 +01:00
|
|
|
string baseName = baseNameOf(path);
|
|
|
|
Path infoFile = (format("%1%/info/%2%") % nixDBPath % baseName).str();
|
2010-02-18 16:11:08 +01:00
|
|
|
if (!pathExists(infoFile))
|
|
|
|
throw Error(format("path `%1%' is not valid") % path);
|
|
|
|
string info = readFile(infoFile);
|
|
|
|
|
|
|
|
/* Parse it. */
|
|
|
|
Strings lines = tokenizeString(info, "\n");
|
|
|
|
|
|
|
|
foreach (Strings::iterator, i, lines) {
|
|
|
|
string::size_type p = i->find(':');
|
|
|
|
if (p == string::npos)
|
|
|
|
throw Error(format("corrupt line in `%1%': %2%") % infoFile % *i);
|
|
|
|
string name(*i, 0, p);
|
|
|
|
string value(*i, p + 2);
|
|
|
|
if (name == "References") {
|
|
|
|
Strings refs = tokenizeString(value, " ");
|
|
|
|
res.references = PathSet(refs.begin(), refs.end());
|
|
|
|
} else if (name == "Deriver") {
|
|
|
|
res.deriver = value;
|
|
|
|
} else if (name == "Hash") {
|
|
|
|
res.hash = parseHashField(path, value);
|
|
|
|
} else if (name == "Registered-At") {
|
|
|
|
int n = 0;
|
|
|
|
string2Int(value, n);
|
|
|
|
res.registrationTime = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2007-08-13 13:37:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-18 14:16:59 +01:00
|
|
|
/* Upgrade from schema 5 (Nix 0.12) to schema 6 (Nix >= 0.15). */
|
|
|
|
void LocalStore::upgradeStore6()
|
2009-11-06 02:15:44 +01:00
|
|
|
{
|
2010-02-18 14:16:59 +01:00
|
|
|
if (!lockFile(globalLock, ltWrite, false)) {
|
|
|
|
printMsg(lvlError, "waiting for exclusive access to the Nix store...");
|
|
|
|
lockFile(globalLock, ltWrite, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
printMsg(lvlError, "upgrading Nix store to new schema (this may take a while)...");
|
|
|
|
|
|
|
|
initSchema();
|
|
|
|
|
2010-02-18 16:11:08 +01:00
|
|
|
PathSet validPaths = queryValidPathsOld();
|
2010-02-18 14:16:59 +01:00
|
|
|
|
2010-02-18 15:40:07 +01:00
|
|
|
SQLiteTxn txn(db);
|
|
|
|
|
2010-02-18 14:40:46 +01:00
|
|
|
std::map<Path, sqlite3_int64> pathToId;
|
2010-02-18 14:16:59 +01:00
|
|
|
|
|
|
|
foreach (PathSet::iterator, i, validPaths) {
|
2010-02-18 16:11:08 +01:00
|
|
|
ValidPathInfo info = queryPathInfoOld(*i);
|
2010-02-19 17:04:51 +01:00
|
|
|
pathToId[*i] = addValidPath(info);
|
2010-02-18 14:40:46 +01:00
|
|
|
std::cerr << ".";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "|";
|
|
|
|
|
|
|
|
foreach (PathSet::iterator, i, validPaths) {
|
2010-02-18 16:11:08 +01:00
|
|
|
ValidPathInfo info = queryPathInfoOld(*i);
|
2010-02-18 14:16:59 +01:00
|
|
|
foreach (PathSet::iterator, j, info.references) {
|
2010-02-18 14:40:46 +01:00
|
|
|
if (pathToId.find(*j) == pathToId.end())
|
|
|
|
throw Error(format("path `%1%' referenced by `%2%' is invalid") % *j % *i);
|
2010-02-19 17:04:51 +01:00
|
|
|
addReference(pathToId[*i], pathToId[*j]);
|
2010-02-18 14:16:59 +01:00
|
|
|
}
|
|
|
|
std::cerr << ".";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "\n";
|
|
|
|
|
2010-02-18 15:40:07 +01:00
|
|
|
txn.commit();
|
|
|
|
|
2010-02-18 14:16:59 +01:00
|
|
|
writeFile(schemaPath, (format("%1%") % nixSchemaVersion).str());
|
|
|
|
|
|
|
|
lockFile(globalLock, ltRead, true);
|
2009-11-06 02:15:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
}
|