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 "aterm.hh"
|
|
|
|
#include "derivations-ast.hh"
|
2007-02-21 16:45:32 +01:00
|
|
|
#include "worker-protocol.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>
|
2003-06-23 15:27:59 +02:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2006-03-10 23:27:26 +01:00
|
|
|
checkStoreNotSymlink();
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
Path globalLockPath = nixDBPath + "/big-lock";
|
|
|
|
globalLock = openLockFile(globalLockPath.c_str(), true);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
createDirs(nixDBPath + "/info");
|
|
|
|
createDirs(nixDBPath + "/referrer");
|
2005-02-09 10:50:29 +01:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
//printMsg(lvlTalkative, "cannot access Nix database; continuing anyway");
|
|
|
|
//readOnlyMode = true;
|
2005-02-09 10:50:29 +01:00
|
|
|
|
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 */
|
|
|
|
curSchema = nixSchemaVersion;
|
|
|
|
writeFile(schemaPath, (format("%1%") % nixSchemaVersion).str());
|
2005-02-09 10:50:29 +01:00
|
|
|
}
|
2008-06-09 15:52:45 +02:00
|
|
|
if (curSchema == 1) throw Error("your Nix store is no longer supported");
|
|
|
|
if (curSchema < nixSchemaVersion) upgradeStore12();
|
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-06-09 15:52:45 +02:00
|
|
|
flushDelayedUpdates();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-13 00:05:01 +01:00
|
|
|
void copyPath(const Path & src, const Path & dst, PathFilter & filter)
|
2003-06-16 15:33:38 +02:00
|
|
|
{
|
2003-07-31 18:05:35 +02:00
|
|
|
debug(format("copying `%1%' to `%2%'") % src % dst);
|
|
|
|
|
2005-03-03 14:58:02 +01:00
|
|
|
/* Dump an archive of the path `src' into a string buffer, then
|
|
|
|
restore the archive to `dst'. This is not a very good method
|
|
|
|
for very large paths, but `copyPath' is mainly used for small
|
|
|
|
files. */
|
2003-06-23 15:27:59 +02:00
|
|
|
|
2006-12-13 00:05:01 +01:00
|
|
|
StringSink sink;
|
|
|
|
dumpPath(src, sink, filter);
|
2003-06-23 15:27:59 +02:00
|
|
|
|
2006-12-13 00:05:01 +01:00
|
|
|
StringSource source(sink.s);
|
2005-03-03 14:58:02 +01:00
|
|
|
restorePath(dst, source);
|
2003-06-16 15:33:38 +02: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;
|
|
|
|
utimbuf.modtime = 0;
|
|
|
|
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);
|
|
|
|
for (Strings::iterator i = names.begin(); i != names.end(); ++i)
|
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
|
|
|
static Path infoFileFor(const Path & path)
|
2003-12-05 12:05:19 +01:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
string baseName = baseNameOf(path);
|
|
|
|
return (format("%1%/info/%2%") % nixDBPath % baseName).str();
|
2003-12-05 12:05:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
static Path referrersFileFor(const Path & path)
|
2003-12-05 12:05:19 +01:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
string baseName = baseNameOf(path);
|
|
|
|
return (format("%1%/referrer/%2%") % nixDBPath % baseName).str();
|
2005-01-25 22:28:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
static Path tmpFileForAtomicUpdate(const Path & path)
|
2008-01-29 19:17:36 +01:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
return (format("%1%/.%2%.%3%") % dirOf(path) % getpid() % baseNameOf(path)).str();
|
2008-01-29 19:17:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
static void appendReferrer(const Path & from, const Path & to, bool lock)
|
2005-12-12 19:24:42 +01:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
Path referrersFile = referrersFileFor(from);
|
|
|
|
|
|
|
|
PathLocks referrersLock;
|
|
|
|
if (lock) {
|
|
|
|
referrersLock.lockPaths(singleton<PathSet, Path>(referrersFile));
|
|
|
|
referrersLock.setDeletion(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoCloseFD fd = open(referrersFile.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0666);
|
|
|
|
if (fd == -1) throw SysError(format("opening file `%1%'") % referrersFile);
|
|
|
|
|
|
|
|
string s = " " + to;
|
|
|
|
writeFull(fd, (const unsigned char *) s.c_str(), s.size());
|
2005-12-12 19:24:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
/* Atomically update the referrers file. If `purge' is true, the set
|
|
|
|
of referrers is set to `referrers'. Otherwise, the current set of
|
|
|
|
referrers is purged of invalid paths. */
|
|
|
|
void LocalStore::rewriteReferrers(const Path & path, bool purge, PathSet referrers)
|
2005-12-12 19:24:42 +01:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
Path referrersFile = referrersFileFor(path);
|
|
|
|
|
|
|
|
PathLocks referrersLock(singleton<PathSet, Path>(referrersFile));
|
|
|
|
referrersLock.setDeletion(true);
|
|
|
|
|
|
|
|
if (purge)
|
|
|
|
/* queryReferrers() purges invalid paths, so that's all we
|
|
|
|
need. */
|
|
|
|
queryReferrers(path, referrers);
|
|
|
|
|
|
|
|
Path tmpFile = tmpFileForAtomicUpdate(referrersFile);
|
|
|
|
|
|
|
|
AutoCloseFD fd = open(tmpFile.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0666);
|
|
|
|
if (fd == -1) throw SysError(format("opening file `%1%'") % referrersFile);
|
|
|
|
|
|
|
|
string s;
|
|
|
|
foreach (PathSet::const_iterator, i, referrers) {
|
|
|
|
s += " "; s += *i;
|
|
|
|
}
|
|
|
|
|
|
|
|
writeFull(fd, (const unsigned char *) s.c_str(), s.size());
|
|
|
|
|
|
|
|
fd.close(); /* for Windows; can't rename open file */
|
|
|
|
|
|
|
|
if (rename(tmpFile.c_str(), referrersFile.c_str()) == -1)
|
|
|
|
throw SysError(format("cannot rename `%1%' to `%2%'") % tmpFile % referrersFile);
|
2005-12-12 19:24:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
void LocalStore::flushDelayedUpdates()
|
2005-01-27 17:18:39 +01:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
foreach (PathSet::iterator, i, delayedUpdates) {
|
|
|
|
rewriteReferrers(*i, true, PathSet());
|
|
|
|
}
|
|
|
|
delayedUpdates.clear();
|
2005-01-27 17:18:39 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LocalStore::registerValidPath(const ValidPathInfo & info, bool ignoreValidity)
|
2003-10-10 16:46:28 +02:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
Path infoFile = infoFileFor(info.path);
|
2005-01-27 17:18:39 +01:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
ValidPathInfo oldInfo;
|
|
|
|
if (pathExists(infoFile)) oldInfo = queryPathInfo(info.path);
|
2005-03-03 14:10:44 +01:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
/* Note that it's possible for infoFile to already exist. */
|
|
|
|
|
|
|
|
/* Acquire a lock on each referrer file. This prevents those
|
|
|
|
paths from being invalidated. (It would be a violation of the
|
|
|
|
store invariants if we registered info.path as valid while some
|
|
|
|
of its references are invalid.) NB: there can be no deadlock
|
|
|
|
here since we're acquiring the locks in sorted order. */
|
|
|
|
PathSet lockNames;
|
|
|
|
foreach (PathSet::const_iterator, i, info.references)
|
|
|
|
if (*i != info.path) lockNames.insert(referrersFileFor(*i));
|
|
|
|
PathLocks referrerLocks(lockNames);
|
|
|
|
referrerLocks.setDeletion(true);
|
|
|
|
|
|
|
|
string refs;
|
|
|
|
foreach (PathSet::const_iterator, i, info.references) {
|
|
|
|
if (!refs.empty()) refs += " ";
|
|
|
|
refs += *i;
|
|
|
|
|
|
|
|
if (!ignoreValidity && *i != info.path && !isValidPath(*i))
|
|
|
|
throw Error(format("cannot register `%1%' as valid, because its reference `%2%' isn't valid")
|
|
|
|
% info.path % *i);
|
|
|
|
|
|
|
|
/* Update the referrer mapping for *i. This must be done
|
|
|
|
before the info file is written to maintain the invariant
|
|
|
|
that if `path' is a valid path, then all its references
|
|
|
|
have referrer mappings back to `path'. A " " is prefixed
|
|
|
|
to separate it from the previous entry. It's not suffixed
|
|
|
|
to deal with interrupted partial writes to this file. */
|
|
|
|
if (oldInfo.references.find(*i) == oldInfo.references.end())
|
|
|
|
appendReferrer(*i, info.path, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
string s = (format(
|
|
|
|
"Hash: sha256:%1%\n"
|
|
|
|
"References: %2%\n"
|
|
|
|
"Deriver: %3%\n"
|
|
|
|
"Registered-At: %4%\n")
|
|
|
|
% printHash(info.hash) % refs % info.deriver %
|
|
|
|
(oldInfo.registrationTime ? oldInfo.registrationTime : time(0))).str();
|
|
|
|
|
|
|
|
/* Atomically rewrite the info file. */
|
|
|
|
Path tmpFile = tmpFileForAtomicUpdate(infoFile);
|
|
|
|
writeFile(tmpFile, s);
|
|
|
|
if (rename(tmpFile.c_str(), infoFile.c_str()) == -1)
|
|
|
|
throw SysError(format("cannot rename `%1%' to `%2%'") % tmpFile % infoFile);
|
|
|
|
|
|
|
|
pathInfoCache[info.path] = info;
|
2003-10-15 14:42:39 +02: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
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
ValidPathInfo LocalStore::queryPathInfo(const Path & path)
|
2006-11-30 18:43:04 +01:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
ValidPathInfo res;
|
|
|
|
res.path = path;
|
|
|
|
|
|
|
|
assertStorePath(path);
|
|
|
|
|
|
|
|
std::map<Path, ValidPathInfo>::iterator lookup = pathInfoCache.find(path);
|
|
|
|
if (lookup != pathInfoCache.end()) return lookup->second;
|
|
|
|
|
|
|
|
//printMsg(lvlError, "queryPathInfo: " + path);
|
|
|
|
|
|
|
|
/* Read the info file. */
|
|
|
|
Path infoFile = infoFileFor(path);
|
|
|
|
if (!pathExists(infoFile))
|
|
|
|
throw Error(format("path `%1%' is not valid") % path);
|
|
|
|
string info = readFile(infoFile);
|
|
|
|
|
|
|
|
/* Parse it. */
|
|
|
|
Strings lines = tokenizeString(info, "\n");
|
|
|
|
|
|
|
|
for (Strings::iterator i = lines.begin(); i != lines.end(); ++i) {
|
|
|
|
unsigned int p = i->find(':');
|
|
|
|
if (p == string::npos) continue; /* bad line */
|
|
|
|
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 pathInfoCache[path] = res;
|
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
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
return pathExists(infoFileFor(path));
|
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
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
PathSet paths;
|
|
|
|
Strings entries = readDirectory(nixDBPath + "/info");
|
|
|
|
for (Strings::iterator i = entries.begin(); i != entries.end(); ++i)
|
|
|
|
paths.insert(nixStore + "/" + *i);
|
|
|
|
return paths;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
bool LocalStore::queryReferrersInternal(const Path & path, PathSet & referrers)
|
2005-02-07 14:40:40 +01:00
|
|
|
{
|
2008-06-09 15:52:45 +02:00
|
|
|
bool allValid = true;
|
|
|
|
|
|
|
|
if (!isValidPath(path))
|
|
|
|
throw Error(format("path `%1%' is not valid") % path);
|
|
|
|
|
|
|
|
/* No locking is necessary here: updates are only done by
|
|
|
|
appending or by atomically replacing the file. When appending,
|
|
|
|
there is a possibility that we see a partial entry, but it will
|
|
|
|
just be filtered out below (the partially written path will not
|
|
|
|
be valid, so it will be ignored). */
|
|
|
|
|
|
|
|
Path referrersFile = referrersFileFor(path);
|
|
|
|
if (!pathExists(referrersFile)) return true;
|
|
|
|
|
|
|
|
AutoCloseFD fd = open(referrersFile.c_str(), O_RDONLY);
|
|
|
|
if (fd == -1) throw SysError(format("opening file `%1%'") % referrersFile);
|
|
|
|
|
|
|
|
Paths refs = tokenizeString(readFile(fd), " ");
|
|
|
|
|
|
|
|
for (Paths::iterator i = refs.begin(); i != refs.end(); ++i)
|
|
|
|
/* Referrers can be invalid (see registerValidPath() for the
|
|
|
|
invariant), so we only return one if it is valid. */
|
|
|
|
if (isStorePath(*i) && isValidPath(*i)) referrers.insert(*i); else allValid = false;
|
|
|
|
|
|
|
|
return allValid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LocalStore::queryReferrers(const Path & path, PathSet & referrers)
|
|
|
|
{
|
|
|
|
queryReferrersInternal(path, referrers);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-12 02:29:28 +02:00
|
|
|
PathSet LocalStore::querySubstitutablePaths()
|
2003-07-10 17:11:48 +02:00
|
|
|
{
|
2007-08-12 02:29:28 +02:00
|
|
|
if (!substitutablePathsLoaded) {
|
|
|
|
for (Paths::iterator i = substituters.begin(); i != substituters.end(); ++i) {
|
|
|
|
debug(format("running `%1%' to find out substitutable paths") % *i);
|
|
|
|
Strings args;
|
|
|
|
args.push_back("--query-paths");
|
|
|
|
Strings ss = tokenizeString(runProgram(*i, false, args), "\n");
|
|
|
|
for (Strings::iterator j = ss.begin(); j != ss.end(); ++j) {
|
|
|
|
if (!isStorePath(*j))
|
|
|
|
throw Error(format("`%1%' returned a bad substitutable path `%2%'")
|
|
|
|
% *i % *j);
|
|
|
|
substitutablePaths.insert(*j);
|
|
|
|
}
|
2004-06-20 21:17:54 +02:00
|
|
|
}
|
2007-08-12 02:29:28 +02:00
|
|
|
substitutablePathsLoaded = true;
|
2004-06-20 21:17:54 +02:00
|
|
|
}
|
2003-12-05 12:05:19 +01:00
|
|
|
|
2007-08-12 02:29:28 +02:00
|
|
|
return substitutablePaths;
|
2004-06-20 21:17:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-12 02:29:28 +02:00
|
|
|
bool LocalStore::hasSubstitutes(const Path & path)
|
2004-06-20 21:17:54 +02:00
|
|
|
{
|
2007-08-12 02:29:28 +02:00
|
|
|
if (!substitutablePathsLoaded)
|
|
|
|
querySubstitutablePaths();
|
|
|
|
return substitutablePaths.find(path) != substitutablePaths.end();
|
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]);
|
|
|
|
|
|
|
|
for (PathSet::iterator i = info.references.begin();
|
|
|
|
i != info.references.end(); ++i)
|
|
|
|
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. */
|
|
|
|
for (ValidPathInfos::const_iterator i = infos.begin(); i != infos.end(); ++i)
|
|
|
|
infosMap[i->path] = *i;
|
2005-03-23 14:07:28 +01:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
PathSet visited;
|
|
|
|
Paths sorted;
|
|
|
|
for (ValidPathInfos::const_iterator i = infos.begin(); i != infos.end(); ++i)
|
|
|
|
dfsVisit(infosMap, i->path, visited, sorted);
|
2005-02-07 14:40:40 +01:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
for (Paths::iterator i = sorted.begin(); i != sorted.end(); ++i)
|
|
|
|
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);
|
2003-07-08 11:54:47 +02:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
ValidPathInfo info;
|
|
|
|
|
|
|
|
if (pathExists(infoFileFor(path))) {
|
|
|
|
info = queryPathInfo(path);
|
|
|
|
|
|
|
|
/* Remove the info file. */
|
|
|
|
Path p = infoFileFor(path);
|
|
|
|
if (unlink(p.c_str()) == -1)
|
|
|
|
throw SysError(format("unlinking `%1%'") % p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the referrers file for `path'. */
|
|
|
|
Path p = referrersFileFor(path);
|
|
|
|
if (pathExists(p) && unlink(p.c_str()) == -1)
|
|
|
|
throw SysError(format("unlinking `%1%'") % p);
|
|
|
|
|
|
|
|
/* Clear `path' from the info cache. */
|
|
|
|
pathInfoCache.erase(path);
|
|
|
|
delayedUpdates.erase(path);
|
|
|
|
|
|
|
|
/* Cause the referrer files for each path referenced by this one
|
|
|
|
to be updated. This has to happen after removing the info file
|
|
|
|
to preserve the invariant (see registerValidPath()).
|
|
|
|
|
|
|
|
The referrer files are updated lazily in flushDelayedUpdates()
|
|
|
|
to prevent quadratic performance in the garbage collector
|
|
|
|
(i.e., when N referrers to some path X are deleted, we have to
|
|
|
|
rewrite the referrers file for X N times, causing O(N^2) I/O).
|
|
|
|
|
|
|
|
What happens if we die before the referrer file can be updated?
|
|
|
|
That's not a problem, because stale (invalid) entries in the
|
|
|
|
referrer file are ignored by queryReferrers(). Thus a referrer
|
|
|
|
file is allowed to have stale entries; removing them is just an
|
|
|
|
optimisation. verifyStore() gets rid of them eventually.
|
|
|
|
*/
|
|
|
|
foreach (PathSet::iterator, i, info.references)
|
|
|
|
if (*i != path) delayedUpdates.insert(*i);
|
2003-07-08 11:54:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-01 21:51:18 +01:00
|
|
|
Path LocalStore::addToStore(const Path & _srcPath, bool fixed,
|
2006-12-13 00:05:01 +01:00
|
|
|
bool recursive, string hashAlgo, PathFilter & filter)
|
2003-07-07 11:25:26 +02:00
|
|
|
{
|
2003-10-08 17:06:59 +02:00
|
|
|
Path srcPath(absPath(_srcPath));
|
|
|
|
debug(format("adding `%1%' to the store") % srcPath);
|
2003-07-07 11:25:26 +02:00
|
|
|
|
2006-12-01 19:00:01 +01:00
|
|
|
std::pair<Path, Hash> pr =
|
2006-12-13 00:05:01 +01:00
|
|
|
computeStorePathForPath(srcPath, fixed, recursive, hashAlgo, filter);
|
2006-12-01 19:00:01 +01:00
|
|
|
Path & dstPath(pr.first);
|
|
|
|
Hash & h(pr.second);
|
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
|
|
|
|
2006-12-13 00:05:01 +01:00
|
|
|
copyPath(srcPath, dstPath, filter);
|
2003-08-01 11:01:51 +02:00
|
|
|
|
2006-12-13 00:05:01 +01:00
|
|
|
Hash h2 = hashPath(htSHA256, dstPath, filter);
|
2005-01-14 14:51:38 +01:00
|
|
|
if (h != h2)
|
|
|
|
throw Error(format("contents of `%1%' changed while copying it to `%2%' (%3% -> %4%)")
|
2005-01-14 17:04:03 +01:00
|
|
|
% srcPath % dstPath % printHash(h) % printHash(h2));
|
2005-01-14 14:51:38 +01: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, h, 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-30 18:43:04 +01:00
|
|
|
Path LocalStore::addTextToStore(const string & suffix, const string & s,
|
2005-01-25 22:28:25 +01:00
|
|
|
const PathSet & references)
|
2003-10-15 14:42:39 +02:00
|
|
|
{
|
2007-01-29 16:51:37 +01:00
|
|
|
Path dstPath = computeStorePathForText(suffix, 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
|
|
|
|
2003-11-22 16:58:34 +01:00
|
|
|
writeStringToFile(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";
|
|
|
|
writeStringToFile(hashFile, printHash(hash));
|
|
|
|
|
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);
|
|
|
|
AutoDelete delTmp(tmpDir);
|
|
|
|
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";
|
|
|
|
writeStringToFile(sigFile, signature);
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
|
|
|
|
PathLocks outputLock(singleton<PathSet, Path>(dstPath));
|
|
|
|
|
|
|
|
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-09 15:52:45 +02:00
|
|
|
void LocalStore::deleteFromStore(const Path & path, unsigned long long & bytesFreed)
|
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)) {
|
|
|
|
/* Acquire a lock on the referrers file to prevent new
|
|
|
|
referrers to this path from appearing while we're deleting
|
|
|
|
it. */
|
|
|
|
PathLocks referrersLock(singleton<PathSet, Path>(referrersFileFor(path)));
|
|
|
|
referrersLock.setDeletion(true);
|
|
|
|
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
|
|
|
|
2006-12-09 01:26:24 +01:00
|
|
|
deletePathWrapped(path, bytesFreed);
|
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;
|
|
|
|
|
|
|
|
for (PathSet::iterator i = validPaths2.begin(); i != validPaths2.end(); ++i) {
|
2007-05-01 15:21:05 +02:00
|
|
|
checkInterrupt();
|
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);
|
|
|
|
} else
|
2005-02-08 14:48:53 +01:00
|
|
|
validPaths.insert(*i);
|
2003-07-17 14:27:55 +02:00
|
|
|
}
|
|
|
|
|
2007-01-14 18:28:30 +01:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
/* Check the store path meta-information. */
|
|
|
|
printMsg(lvlInfo, "checking path meta-information");
|
2007-01-14 18:28:30 +01:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
std::map<Path, PathSet> referrersCache;
|
|
|
|
|
|
|
|
for (PathSet::iterator i = validPaths.begin(); i != validPaths.end(); ++i) {
|
|
|
|
bool update = false;
|
|
|
|
ValidPathInfo info = queryPathInfo(*i);
|
|
|
|
|
|
|
|
/* Check the references: each reference should be valid, and
|
|
|
|
it should have a matching referrer. */
|
|
|
|
for (PathSet::iterator j = info.references.begin();
|
|
|
|
j != info.references.end(); ++j)
|
|
|
|
{
|
|
|
|
if (referrersCache.find(*j) == referrersCache.end())
|
|
|
|
queryReferrers(*j, referrersCache[*j]);
|
|
|
|
if (referrersCache[*j].find(*i) == referrersCache[*j].end()) {
|
|
|
|
printMsg(lvlError, format("adding missing referrer mapping from `%1%' to `%2%'")
|
|
|
|
% *j % *i);
|
|
|
|
appendReferrer(*j, *i, true);
|
2005-02-08 14:23:55 +01:00
|
|
|
}
|
2008-06-09 15:52:45 +02:00
|
|
|
if (validPaths.find(*j) == validPaths.end()) {
|
|
|
|
printMsg(lvlError, format("incomplete closure: `%1%' needs missing `%2%'")
|
|
|
|
% *i % *j);
|
|
|
|
/* nothing we can do about it... */
|
2005-02-08 14:23:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
/* Check the deriver. (Note that the deriver doesn't have to
|
|
|
|
be a valid path.) */
|
|
|
|
if (!info.deriver.empty() && !isStorePath(info.deriver)) {
|
|
|
|
info.deriver = "";
|
|
|
|
update = true;
|
2007-01-14 18:28:30 +01:00
|
|
|
}
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
/* Check the content hash (optionally - slow). */
|
|
|
|
if (checkContents) {
|
|
|
|
debug(format("checking contents of `%1%'") % *i);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2003-10-10 17:14:29 +02:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
if (update) registerValidPath(info);
|
2007-10-10 00:14:27 +02:00
|
|
|
}
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
referrersCache.clear();
|
2007-10-10 00:14:27 +02:00
|
|
|
|
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
/* Check the referrers. */
|
|
|
|
printMsg(lvlInfo, "checking referrers");
|
2007-10-10 00:14:27 +02:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
std::map<Path, PathSet> referencesCache;
|
2005-12-15 17:53:21 +01:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
Strings entries = readDirectory(nixDBPath + "/referrer");
|
|
|
|
for (Strings::iterator i = entries.begin(); i != entries.end(); ++i) {
|
|
|
|
Path from = nixStore + "/" + *i;
|
|
|
|
|
|
|
|
if (validPaths.find(from) == validPaths.end()) {
|
|
|
|
printMsg(lvlError, format("removing referrers file for invalid `%1%'") % from);
|
|
|
|
Path p = referrersFileFor(from);
|
|
|
|
if (unlink(p.c_str()) == -1)
|
|
|
|
throw SysError(format("unlinking `%1%'") % p);
|
|
|
|
continue;
|
2005-12-15 17:53:21 +01:00
|
|
|
}
|
2005-12-12 20:14:38 +01:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
PathSet referrers;
|
|
|
|
bool allValid = queryReferrersInternal(from, referrers);
|
|
|
|
bool update = false;
|
2005-12-12 20:14:38 +01:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
if (!allValid) {
|
|
|
|
printMsg(lvlError, format("removing some stale referrers for `%1%'") % from);
|
|
|
|
update = true;
|
|
|
|
}
|
2007-08-13 13:37:39 +02:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
/* Each referrer should have a matching reference. */
|
|
|
|
PathSet referrersNew;
|
|
|
|
for (PathSet::iterator j = referrers.begin(); j != referrers.end(); ++j) {
|
|
|
|
if (referencesCache.find(*j) == referencesCache.end())
|
|
|
|
queryReferences(*j, referencesCache[*j]);
|
|
|
|
if (referencesCache[*j].find(from) == referencesCache[*j].end()) {
|
|
|
|
printMsg(lvlError, format("removing unexpected referrer mapping from `%1%' to `%2%'")
|
|
|
|
% from % *j);
|
|
|
|
update = true;
|
|
|
|
} else referrersNew.insert(*j);
|
|
|
|
}
|
2007-08-13 13:37:39 +02:00
|
|
|
|
2008-06-09 15:52:45 +02:00
|
|
|
if (update) rewriteReferrers(from, false, referrersNew);
|
2007-08-13 13:37:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
}
|