2003-03-13 17:28:32 +01:00
|
|
|
#include <iostream>
|
2003-03-14 17:43:14 +01:00
|
|
|
#include <fstream>
|
2003-03-13 17:28:32 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2003-03-14 17:43:14 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <list>
|
2003-03-24 12:50:20 +01:00
|
|
|
#include <map>
|
2003-03-14 17:43:14 +01:00
|
|
|
#include <cstdio>
|
2003-03-13 17:28:32 +01:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <db4/db_cxx.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
#define PKGINFO_ENVVAR "NIX_DB"
|
2003-03-13 17:28:32 +01:00
|
|
|
#define PKGINFO_PATH "/pkg/sys/var/pkginfo"
|
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
#define PKGHOME_ENVVAR "NIX_PKGHOME"
|
|
|
|
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
static string dbRefs = "refs";
|
|
|
|
static string dbInstPkgs = "pkginst";
|
|
|
|
|
|
|
|
|
2003-03-13 17:28:32 +01:00
|
|
|
static string prog;
|
|
|
|
static string dbfile = PKGINFO_PATH;
|
|
|
|
|
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
static string pkgHome = "/pkg";
|
|
|
|
|
|
|
|
|
|
|
|
class Error : public exception
|
|
|
|
{
|
|
|
|
string err;
|
|
|
|
public:
|
|
|
|
Error(string _err) { err = _err; }
|
|
|
|
~Error() throw () { };
|
|
|
|
const char * what() const throw () { return err.c_str(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class UsageError : public Error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UsageError(string _err) : Error(_err) { };
|
|
|
|
};
|
|
|
|
|
2003-03-21 16:53:35 +01:00
|
|
|
class BadRefError : public Error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BadRefError(string _err) : Error(_err) { };
|
|
|
|
};
|
2003-03-20 17:53:00 +01:00
|
|
|
|
2003-03-21 16:53:35 +01:00
|
|
|
|
|
|
|
/* Wrapper classes that ensures that the database is closed upon
|
2003-03-14 17:43:14 +01:00
|
|
|
object destruction. */
|
2003-03-13 17:28:32 +01:00
|
|
|
class Db2 : public Db
|
|
|
|
{
|
|
|
|
public:
|
2003-03-21 16:53:35 +01:00
|
|
|
Db2(DbEnv *env, u_int32_t flags) : Db(env, flags) { }
|
|
|
|
~Db2() { close(0); }
|
|
|
|
};
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-21 16:53:35 +01:00
|
|
|
|
|
|
|
class DbcClose
|
|
|
|
{
|
|
|
|
Dbc * cursor;
|
|
|
|
public:
|
|
|
|
DbcClose(Dbc * c) : cursor(c) { }
|
|
|
|
~DbcClose() { cursor->close(); }
|
2003-03-13 17:28:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
auto_ptr<Db2> openDB(const string & dbname, bool readonly)
|
|
|
|
{
|
|
|
|
auto_ptr<Db2> db;
|
|
|
|
|
|
|
|
db = auto_ptr<Db2>(new Db2(0, 0));
|
|
|
|
|
|
|
|
db->open(dbfile.c_str(), dbname.c_str(),
|
|
|
|
DB_HASH, readonly ? DB_RDONLY : DB_CREATE, 0666);
|
|
|
|
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool queryDB(const string & dbname, const string & key, string & data)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
auto_ptr<Db2> db = openDB(dbname, true);
|
|
|
|
|
|
|
|
Dbt kt((void *) key.c_str(), key.length());
|
|
|
|
Dbt dt;
|
|
|
|
|
|
|
|
err = db->get(0, &kt, &dt, 0);
|
|
|
|
if (err) return false;
|
|
|
|
|
|
|
|
data = string((char *) dt.get_data(), dt.get_size());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setDB(const string & dbname, const string & key, const string & data)
|
|
|
|
{
|
|
|
|
auto_ptr<Db2> db = openDB(dbname, false);
|
|
|
|
Dbt kt((void *) key.c_str(), key.length());
|
|
|
|
Dbt dt((void *) data.c_str(), data.length());
|
|
|
|
db->put(0, &kt, &dt, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void delDB(const string & dbname, const string & key)
|
|
|
|
{
|
|
|
|
auto_ptr<Db2> db = openDB(dbname, false);
|
|
|
|
Dbt kt((void *) key.c_str(), key.length());
|
|
|
|
db->del(0, &kt, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-21 16:53:35 +01:00
|
|
|
typedef pair<string, string> DBPair;
|
|
|
|
typedef list<DBPair> DBPairs;
|
|
|
|
|
|
|
|
|
|
|
|
void enumDB(const string & dbname, DBPairs & contents)
|
|
|
|
{
|
|
|
|
auto_ptr<Db2> db = openDB(dbname, false);
|
|
|
|
|
|
|
|
Dbc * cursor;
|
|
|
|
db->cursor(0, &cursor, 0);
|
|
|
|
DbcClose cursorCloser(cursor);
|
|
|
|
|
|
|
|
Dbt kt, dt;
|
|
|
|
while (cursor->get(&kt, &dt, DB_NEXT) != DB_NOTFOUND) {
|
|
|
|
string key((char *) kt.get_data(), kt.get_size());
|
|
|
|
string data((char *) dt.get_data(), dt.get_size());
|
|
|
|
contents.push_back(DBPair(key, data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
/* Verify that a reference is valid (that is, is a MD5 hash code). */
|
|
|
|
void checkRef(const string & s)
|
2003-03-13 17:28:32 +01:00
|
|
|
{
|
2003-03-14 17:43:14 +01:00
|
|
|
string err = "invalid reference: " + s;
|
|
|
|
if (s.length() != 32)
|
2003-03-21 16:53:35 +01:00
|
|
|
throw BadRefError(err);
|
2003-03-14 17:43:14 +01:00
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
char c = s[i];
|
|
|
|
if (!((c >= '0' && c <= '9') ||
|
|
|
|
(c >= 'a' && c <= 'f')))
|
2003-03-21 16:53:35 +01:00
|
|
|
throw BadRefError(err);
|
2003-03-14 17:43:14 +01:00
|
|
|
}
|
|
|
|
}
|
2003-03-13 17:28:32 +01:00
|
|
|
|
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
/* Compute the MD5 hash of a file. */
|
|
|
|
string makeRef(string filename)
|
|
|
|
{
|
|
|
|
char hash[33];
|
|
|
|
|
2003-03-21 16:53:35 +01:00
|
|
|
FILE * pipe = popen(("md5sum " + filename + " 2> /dev/null").c_str(), "r");
|
|
|
|
if (!pipe) throw BadRefError("cannot execute md5sum");
|
2003-03-14 17:43:14 +01:00
|
|
|
|
|
|
|
if (fread(hash, 32, 1, pipe) != 1)
|
2003-03-24 00:24:09 +01:00
|
|
|
throw BadRefError("cannot read hash from md5sum of " + filename);
|
2003-03-14 17:43:14 +01:00
|
|
|
hash[32] = 0;
|
|
|
|
|
|
|
|
pclose(pipe);
|
|
|
|
|
|
|
|
checkRef(hash);
|
|
|
|
return hash;
|
|
|
|
}
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
|
|
|
|
struct Dep
|
|
|
|
{
|
|
|
|
string name;
|
|
|
|
string ref;
|
|
|
|
Dep(string _name, string _ref)
|
|
|
|
{
|
|
|
|
name = _name;
|
|
|
|
ref = _ref;
|
2003-03-13 17:28:32 +01:00
|
|
|
}
|
2003-03-14 17:43:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef list<Dep> DepList;
|
|
|
|
|
|
|
|
|
|
|
|
void readPkgDescr(const string & pkgfile,
|
|
|
|
DepList & pkgImports, DepList & fileImports)
|
|
|
|
{
|
|
|
|
ifstream file;
|
|
|
|
file.exceptions(ios::badbit);
|
|
|
|
file.open(pkgfile.c_str());
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
while (!file.eof()) {
|
|
|
|
string line;
|
|
|
|
getline(file, line);
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
int n = line.find('#');
|
|
|
|
if (n >= 0) line = line.erase(n);
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
if ((int) line.find_first_not_of(" ") < 0) continue;
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
istringstream str(line);
|
|
|
|
|
|
|
|
string name, op, ref;
|
|
|
|
str >> name >> op >> ref;
|
|
|
|
|
|
|
|
checkRef(ref);
|
|
|
|
|
|
|
|
if (op == "<-")
|
|
|
|
pkgImports.push_back(Dep(name, ref));
|
|
|
|
else if (op == "=")
|
|
|
|
fileImports.push_back(Dep(name, ref));
|
2003-03-20 17:53:00 +01:00
|
|
|
else throw Error("invalid operator " + op);
|
2003-03-14 17:43:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-24 00:24:09 +01:00
|
|
|
string getPkg(string hash);
|
2003-03-14 17:43:14 +01:00
|
|
|
|
|
|
|
|
2003-03-24 12:50:20 +01:00
|
|
|
typedef map<string, string> Environment;
|
2003-03-14 17:43:14 +01:00
|
|
|
|
|
|
|
|
2003-03-24 12:50:20 +01:00
|
|
|
void fetchDeps(string hash, Environment & env)
|
2003-03-14 17:43:14 +01:00
|
|
|
{
|
|
|
|
string pkgfile;
|
|
|
|
|
2003-03-24 00:24:09 +01:00
|
|
|
if (!queryDB(dbRefs, hash, pkgfile))
|
|
|
|
throw Error("unknown package " + hash);
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-24 12:50:20 +01:00
|
|
|
cerr << "reading information about " + hash + " from " + pkgfile + "\n";
|
2003-03-14 17:43:14 +01:00
|
|
|
|
|
|
|
/* Verify that the file hasn't changed. !!! race */
|
2003-03-24 00:24:09 +01:00
|
|
|
if (makeRef(pkgfile) != hash)
|
2003-03-20 17:53:00 +01:00
|
|
|
throw Error("file " + pkgfile + " is stale");
|
2003-03-14 17:43:14 +01:00
|
|
|
|
|
|
|
/* Read the package description file. */
|
|
|
|
DepList pkgImports, fileImports;
|
|
|
|
readPkgDescr(pkgfile, pkgImports, fileImports);
|
|
|
|
|
|
|
|
/* Recursively fetch all the dependencies, filling in the
|
|
|
|
environment as we go along. */
|
|
|
|
for (DepList::iterator it = pkgImports.begin();
|
|
|
|
it != pkgImports.end(); it++)
|
|
|
|
{
|
|
|
|
cerr << "fetching package dependency "
|
|
|
|
<< it->name << " <- " << it->ref
|
|
|
|
<< endl;
|
2003-03-24 12:50:20 +01:00
|
|
|
env[it->name] = getPkg(it->ref);
|
2003-03-14 17:43:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (DepList::iterator it = fileImports.begin();
|
|
|
|
it != fileImports.end(); it++)
|
|
|
|
{
|
|
|
|
cerr << "fetching file dependency "
|
|
|
|
<< it->name << " = " << it->ref
|
|
|
|
<< endl;
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
string file;
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-21 16:58:40 +01:00
|
|
|
if (!queryDB(dbRefs, it->ref, file))
|
2003-03-20 17:53:00 +01:00
|
|
|
throw Error("unknown file " + it->ref);
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
if (makeRef(file) != it->ref)
|
2003-03-20 17:53:00 +01:00
|
|
|
throw Error("file " + file + " is stale");
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-24 12:50:20 +01:00
|
|
|
env[it->name] = file;
|
2003-03-14 17:43:14 +01:00
|
|
|
}
|
2003-03-24 12:50:20 +01:00
|
|
|
}
|
2003-03-14 17:43:14 +01:00
|
|
|
|
|
|
|
|
2003-03-24 12:50:20 +01:00
|
|
|
string getFromEnv(const Environment & env, const string & key)
|
|
|
|
{
|
|
|
|
Environment::const_iterator it = env.find(key);
|
|
|
|
if (it == env.end())
|
|
|
|
throw Error("key " + key + " not found in the environment");
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void installPkg(string hash)
|
|
|
|
{
|
|
|
|
string pkgfile;
|
|
|
|
string src;
|
|
|
|
string path;
|
|
|
|
string cmd;
|
|
|
|
string builder;
|
|
|
|
Environment env;
|
|
|
|
|
|
|
|
/* Fetch dependencies. */
|
|
|
|
fetchDeps(hash, env);
|
|
|
|
|
|
|
|
builder = getFromEnv(env, "build");
|
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
/* Construct a path for the installed package. */
|
2003-03-24 00:24:09 +01:00
|
|
|
path = pkgHome + "/" + hash;
|
2003-03-14 17:43:14 +01:00
|
|
|
|
|
|
|
/* Create the path. */
|
|
|
|
if (mkdir(path.c_str(), 0777))
|
2003-03-20 17:53:00 +01:00
|
|
|
throw Error("unable to create directory " + path);
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
try {
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
/* Fork a child to build the package. */
|
|
|
|
pid_t pid;
|
|
|
|
switch (pid = fork()) {
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
throw Error("unable to fork");
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
case 0: { /* child */
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
/* Go to the build directory. */
|
|
|
|
if (chdir(path.c_str())) {
|
|
|
|
cout << "unable to chdir to package directory\n";
|
|
|
|
_exit(1);
|
|
|
|
}
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
/* Fill in the environment. We don't bother freeing the
|
|
|
|
strings, since we'll exec or die soon anyway. */
|
|
|
|
const char * env2[env.size() + 1];
|
|
|
|
int i = 0;
|
|
|
|
for (Environment::iterator it = env.begin();
|
|
|
|
it != env.end(); it++, i++)
|
|
|
|
env2[i] = (new string(it->first + "=" + it->second))->c_str();
|
|
|
|
env2[i] = 0;
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
/* Execute the builder. This should not return. */
|
|
|
|
execle(builder.c_str(), builder.c_str(), 0, env2);
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
cout << strerror(errno) << endl;
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
cout << "unable to execute builder\n";
|
2003-03-24 00:24:09 +01:00
|
|
|
_exit(1); }
|
2003-03-20 17:53:00 +01:00
|
|
|
|
|
|
|
}
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
/* parent */
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
/* Wait for the child to finish. */
|
|
|
|
int status;
|
|
|
|
if (waitpid(pid, &status, 0) != pid)
|
|
|
|
throw Error("unable to wait for child");
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
|
|
|
throw Error("unable to build package");
|
|
|
|
|
|
|
|
} catch (exception &) {
|
|
|
|
system(("rm -rf " + path).c_str());
|
|
|
|
throw;
|
|
|
|
}
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-24 00:24:09 +01:00
|
|
|
setDB(dbInstPkgs, hash, path);
|
2003-03-14 17:43:14 +01:00
|
|
|
}
|
|
|
|
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-24 00:24:09 +01:00
|
|
|
string getPkg(string hash)
|
2003-03-14 17:43:14 +01:00
|
|
|
{
|
|
|
|
string path;
|
2003-03-24 00:24:09 +01:00
|
|
|
checkRef(hash);
|
|
|
|
while (!queryDB(dbInstPkgs, hash, path))
|
|
|
|
installPkg(hash);
|
2003-03-14 17:43:14 +01:00
|
|
|
return path;
|
2003-03-13 17:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-24 00:24:09 +01:00
|
|
|
void runPkg(string hash)
|
|
|
|
{
|
|
|
|
string src;
|
|
|
|
string path;
|
|
|
|
string cmd;
|
|
|
|
string runner;
|
|
|
|
Environment env;
|
|
|
|
|
2003-03-24 12:50:20 +01:00
|
|
|
/* Fetch dependencies. */
|
|
|
|
fetchDeps(hash, env);
|
2003-03-24 00:24:09 +01:00
|
|
|
|
2003-03-24 12:50:20 +01:00
|
|
|
runner = getFromEnv(env, "run");
|
|
|
|
|
2003-03-24 00:24:09 +01:00
|
|
|
/* Fork a child to build the package. */
|
|
|
|
pid_t pid;
|
|
|
|
switch (pid = fork()) {
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
throw Error("unable to fork");
|
|
|
|
|
|
|
|
case 0: { /* child */
|
|
|
|
|
|
|
|
/* Fill in the environment. We don't bother freeing the
|
|
|
|
strings, since we'll exec or die soon anyway. */
|
|
|
|
for (Environment::iterator it = env.begin();
|
|
|
|
it != env.end(); it++)
|
|
|
|
{
|
|
|
|
string * s = new string(it->first + "=" + it->second);
|
|
|
|
putenv((char *) s->c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Execute the runner. This should not return. */
|
|
|
|
execl(runner.c_str(), runner.c_str(), 0);
|
|
|
|
|
|
|
|
cout << strerror(errno) << endl;
|
|
|
|
|
|
|
|
cout << "unable to execute runner\n";
|
|
|
|
_exit(1); }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parent */
|
|
|
|
|
|
|
|
/* Wait for the child to finish. */
|
|
|
|
int status;
|
|
|
|
if (waitpid(pid, &status, 0) != pid)
|
|
|
|
throw Error("unable to wait for child");
|
|
|
|
|
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
|
|
|
throw Error("unable to run package");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
string absPath(string filename)
|
2003-03-13 17:28:32 +01:00
|
|
|
{
|
2003-03-14 17:43:14 +01:00
|
|
|
if (filename[0] != '/') {
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
if (!getcwd(buf, sizeof(buf)))
|
2003-03-20 17:53:00 +01:00
|
|
|
throw Error("cannot get cwd");
|
2003-03-14 17:43:14 +01:00
|
|
|
filename = string(buf) + "/" + filename;
|
|
|
|
/* !!! canonicalise */
|
|
|
|
}
|
|
|
|
return filename;
|
|
|
|
}
|
2003-03-13 17:28:32 +01:00
|
|
|
|
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
void registerFile(string filename)
|
|
|
|
{
|
|
|
|
filename = absPath(filename);
|
|
|
|
setDB(dbRefs, makeRef(filename), filename);
|
2003-03-13 17:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This is primarily used for bootstrapping. */
|
2003-03-24 00:24:09 +01:00
|
|
|
void registerInstalledPkg(string hash, string path)
|
2003-03-13 17:28:32 +01:00
|
|
|
{
|
2003-03-24 00:24:09 +01:00
|
|
|
checkRef(hash);
|
2003-03-14 17:43:14 +01:00
|
|
|
if (path == "")
|
2003-03-24 00:24:09 +01:00
|
|
|
delDB(dbInstPkgs, hash);
|
2003-03-13 17:28:32 +01:00
|
|
|
else
|
2003-03-24 00:24:09 +01:00
|
|
|
setDB(dbInstPkgs, hash, path);
|
2003-03-14 17:43:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void initDB()
|
|
|
|
{
|
|
|
|
openDB(dbRefs, false);
|
|
|
|
openDB(dbInstPkgs, false);
|
2003-03-13 17:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-21 16:53:35 +01:00
|
|
|
void verifyDB()
|
|
|
|
{
|
|
|
|
/* Check that all file references are still valid. */
|
|
|
|
DBPairs fileRefs;
|
|
|
|
|
|
|
|
enumDB(dbRefs, fileRefs);
|
|
|
|
|
|
|
|
for (DBPairs::iterator it = fileRefs.begin();
|
|
|
|
it != fileRefs.end(); it++)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (makeRef(it->second) != it->first)
|
|
|
|
delDB(dbRefs, it->first);
|
|
|
|
} catch (BadRefError e) { /* !!! better error check */
|
|
|
|
cerr << "file " << it->second << " has disappeared\n";
|
|
|
|
delDB(dbRefs, it->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that all installed packages are still there. */
|
|
|
|
DBPairs instPkgs;
|
|
|
|
|
|
|
|
enumDB(dbInstPkgs, instPkgs);
|
|
|
|
|
|
|
|
for (DBPairs::iterator it = instPkgs.begin();
|
|
|
|
it != instPkgs.end(); it++)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (stat(it->second.c_str(), &st) == -1) {
|
|
|
|
cerr << "package " << it->first << " has disappeared\n";
|
|
|
|
delDB(dbInstPkgs, it->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: check that all directories in pkgHome are installed
|
|
|
|
packages. */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-21 16:58:40 +01:00
|
|
|
void listInstalledPkgs()
|
|
|
|
{
|
|
|
|
DBPairs instPkgs;
|
|
|
|
|
|
|
|
enumDB(dbInstPkgs, instPkgs);
|
|
|
|
|
|
|
|
for (DBPairs::iterator it = instPkgs.begin();
|
|
|
|
it != instPkgs.end(); it++)
|
|
|
|
{
|
|
|
|
string descr;
|
|
|
|
if (!queryDB(dbRefs, it->first, descr))
|
|
|
|
descr = "descriptor missing";
|
|
|
|
cout << it->first << " " << descr << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-13 17:28:32 +01:00
|
|
|
void run(int argc, char * * argv)
|
|
|
|
{
|
2003-03-21 16:53:35 +01:00
|
|
|
UsageError argcError("wrong number of arguments");
|
2003-03-13 17:28:32 +01:00
|
|
|
string cmd;
|
|
|
|
|
|
|
|
if (argc < 1)
|
2003-03-20 17:53:00 +01:00
|
|
|
throw UsageError("no command specified");
|
2003-03-13 17:28:32 +01:00
|
|
|
|
|
|
|
cmd = argv[0];
|
|
|
|
argc--, argv++;
|
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
if (cmd == "init") {
|
2003-03-21 16:53:35 +01:00
|
|
|
if (argc != 0) throw argcError;
|
2003-03-14 17:43:14 +01:00
|
|
|
initDB();
|
2003-03-21 16:53:35 +01:00
|
|
|
} else if (cmd == "verify") {
|
|
|
|
if (argc != 0) throw argcError;
|
|
|
|
verifyDB();
|
2003-03-14 17:43:14 +01:00
|
|
|
} else if (cmd == "getpkg") {
|
2003-03-21 16:53:35 +01:00
|
|
|
if (argc != 1) throw argcError;
|
2003-03-14 17:43:14 +01:00
|
|
|
string path = getPkg(argv[0]);
|
|
|
|
cout << path << endl;
|
2003-03-24 00:24:09 +01:00
|
|
|
} else if (cmd == "run") {
|
|
|
|
if (argc != 1) throw argcError;
|
|
|
|
runPkg(argv[0]);
|
2003-03-20 17:53:00 +01:00
|
|
|
} else if (cmd == "regfile") {
|
2003-03-21 16:53:35 +01:00
|
|
|
if (argc != 1) throw argcError;
|
2003-03-14 17:43:14 +01:00
|
|
|
registerFile(argv[0]);
|
2003-03-20 17:53:00 +01:00
|
|
|
} else if (cmd == "reginst") {
|
2003-03-21 16:53:35 +01:00
|
|
|
if (argc != 2) throw argcError;
|
2003-03-14 17:43:14 +01:00
|
|
|
registerInstalledPkg(argv[0], argv[1]);
|
2003-03-21 16:58:40 +01:00
|
|
|
} else if (cmd == "listinst") {
|
|
|
|
if (argc != 0) throw argcError;
|
|
|
|
listInstalledPkgs();
|
2003-03-14 17:43:14 +01:00
|
|
|
} else
|
2003-03-20 17:53:00 +01:00
|
|
|
throw UsageError("unknown command: " + string(cmd));
|
2003-03-13 17:28:32 +01:00
|
|
|
}
|
2003-03-20 17:53:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
void printUsage()
|
2003-03-13 17:28:32 +01:00
|
|
|
{
|
2003-03-20 17:53:00 +01:00
|
|
|
cerr <<
|
|
|
|
"Usage: nix SUBCOMMAND OPTIONS...
|
|
|
|
|
|
|
|
Subcommands:
|
|
|
|
|
|
|
|
init
|
|
|
|
Initialize the database.
|
|
|
|
|
2003-03-21 16:53:35 +01:00
|
|
|
verify
|
2003-03-24 00:24:09 +01:00
|
|
|
Remove stale entries from the database.
|
2003-03-21 16:53:35 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
regfile FILENAME
|
|
|
|
Register FILENAME keyed by its hash.
|
|
|
|
|
|
|
|
reginst HASH PATH
|
|
|
|
Register an installed package.
|
|
|
|
|
|
|
|
getpkg HASH
|
|
|
|
Ensure that the package referenced by HASH is installed. Prints
|
|
|
|
out the path of the package on stdout.
|
2003-03-24 00:24:09 +01:00
|
|
|
|
2003-03-24 12:50:20 +01:00
|
|
|
listinst
|
|
|
|
Prints a list of installed packages.
|
|
|
|
|
2003-03-24 00:24:09 +01:00
|
|
|
run HASH
|
|
|
|
Run the descriptor referenced by HASH.
|
2003-03-20 17:53:00 +01:00
|
|
|
";
|
|
|
|
}
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
|
|
|
|
void main2(int argc, char * * argv)
|
|
|
|
{
|
|
|
|
int c;
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-14 17:43:14 +01:00
|
|
|
umask(0022);
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
if (getenv(PKGINFO_ENVVAR))
|
|
|
|
dbfile = getenv(PKGINFO_ENVVAR);
|
|
|
|
|
|
|
|
if (getenv(PKGHOME_ENVVAR))
|
|
|
|
pkgHome = getenv(PKGHOME_ENVVAR);
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
opterr = 0;
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "hd:")) != EOF) {
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
switch (c) {
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
case 'h':
|
|
|
|
printUsage();
|
|
|
|
return;
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
case 'd':
|
|
|
|
dbfile = optarg;
|
|
|
|
break;
|
2003-03-13 17:28:32 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
default:
|
|
|
|
throw UsageError("invalid option `" + string(1, optopt) + "'");
|
|
|
|
break;
|
2003-03-14 17:43:14 +01:00
|
|
|
}
|
2003-03-20 17:53:00 +01:00
|
|
|
}
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
argc -= optind, argv += optind;
|
|
|
|
run(argc, argv);
|
|
|
|
}
|
2003-03-14 17:43:14 +01:00
|
|
|
|
2003-03-20 17:53:00 +01:00
|
|
|
|
|
|
|
int main(int argc, char * * argv)
|
|
|
|
{
|
|
|
|
prog = argv[0];
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
main2(argc, argv);
|
|
|
|
|
|
|
|
} catch (DbException e) {
|
|
|
|
throw Error(e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (UsageError & e) {
|
|
|
|
cerr << "error: " << e.what() << endl
|
|
|
|
<< "Try `nix -h' for more information.\n";
|
2003-03-14 17:43:14 +01:00
|
|
|
return 1;
|
2003-03-20 17:53:00 +01:00
|
|
|
} catch (exception & e) {
|
|
|
|
cerr << "error: " << e.what() << endl;
|
2003-03-13 17:28:32 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2003-03-14 17:43:14 +01:00
|
|
|
|
|
|
|
return 0;
|
2003-03-13 17:28:32 +01:00
|
|
|
}
|