2003-03-13 16:28:32 +00:00
|
|
|
#include <iostream>
|
2003-03-14 16:43:14 +00:00
|
|
|
#include <fstream>
|
2003-03-13 16:28:32 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2003-03-14 16:43:14 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <list>
|
2003-03-24 17:49:56 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <set>
|
2003-03-24 11:50:20 +00:00
|
|
|
#include <map>
|
2003-03-14 16:43:14 +00:00
|
|
|
#include <cstdio>
|
2003-03-13 16:28:32 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <db4/db_cxx.h>
|
|
|
|
|
2003-04-01 14:00:47 +00:00
|
|
|
extern "C" {
|
|
|
|
#include <aterm1.h>
|
|
|
|
}
|
|
|
|
|
2003-03-24 17:49:56 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "md5.h"
|
|
|
|
}
|
|
|
|
|
2003-03-13 16:28:32 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
2003-03-20 16:53:00 +00:00
|
|
|
#define PKGINFO_ENVVAR "NIX_DB"
|
2003-03-13 16:28:32 +00:00
|
|
|
#define PKGINFO_PATH "/pkg/sys/var/pkginfo"
|
|
|
|
|
2003-03-20 16:53:00 +00:00
|
|
|
#define PKGHOME_ENVVAR "NIX_PKGHOME"
|
|
|
|
|
2003-03-13 16:28:32 +00:00
|
|
|
|
2003-03-14 16:43:14 +00:00
|
|
|
static string dbRefs = "refs";
|
|
|
|
static string dbInstPkgs = "pkginst";
|
2003-04-02 15:34:05 +00:00
|
|
|
static string dbPrebuilts = "prebuilts";
|
2003-03-14 16:43:14 +00:00
|
|
|
|
|
|
|
|
2003-03-13 16:28:32 +00:00
|
|
|
static string prog;
|
|
|
|
static string dbfile = PKGINFO_PATH;
|
|
|
|
|
|
|
|
|
2003-03-20 16:53:00 +00:00
|
|
|
static string pkgHome = "/pkg";
|
|
|
|
|
|
|
|
|
2003-03-24 12:49:40 +00:00
|
|
|
static string thisSystem = SYSTEM;
|
|
|
|
|
|
|
|
|
2003-03-20 16:53:00 +00:00
|
|
|
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 15:53:35 +00:00
|
|
|
class BadRefError : public Error
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BadRefError(string _err) : Error(_err) { };
|
|
|
|
};
|
2003-03-20 16:53:00 +00:00
|
|
|
|
2003-03-21 15:53:35 +00:00
|
|
|
|
2003-04-04 12:02:40 +00:00
|
|
|
typedef vector<string> Strings;
|
|
|
|
|
|
|
|
|
2003-03-21 15:53:35 +00:00
|
|
|
/* Wrapper classes that ensures that the database is closed upon
|
2003-03-14 16:43:14 +00:00
|
|
|
object destruction. */
|
2003-03-13 16:28:32 +00:00
|
|
|
class Db2 : public Db
|
|
|
|
{
|
|
|
|
public:
|
2003-03-21 15:53:35 +00:00
|
|
|
Db2(DbEnv *env, u_int32_t flags) : Db(env, flags) { }
|
|
|
|
~Db2() { close(0); }
|
|
|
|
};
|
2003-03-13 16:28:32 +00:00
|
|
|
|
2003-03-21 15:53:35 +00:00
|
|
|
|
|
|
|
class DbcClose
|
|
|
|
{
|
|
|
|
Dbc * cursor;
|
|
|
|
public:
|
|
|
|
DbcClose(Dbc * c) : cursor(c) { }
|
|
|
|
~DbcClose() { cursor->close(); }
|
2003-03-13 16:28:32 +00: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 15:53:35 +00: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-24 17:49:56 +00:00
|
|
|
string printHash(unsigned char * buf)
|
|
|
|
{
|
|
|
|
ostringstream str;
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
str.fill('0');
|
|
|
|
str.width(2);
|
|
|
|
str << hex << (int) buf[i];
|
|
|
|
}
|
|
|
|
return str.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-14 16:43:14 +00:00
|
|
|
/* Verify that a reference is valid (that is, is a MD5 hash code). */
|
2003-03-24 17:49:56 +00:00
|
|
|
void checkHash(const string & s)
|
2003-03-13 16:28:32 +00:00
|
|
|
{
|
2003-03-14 16:43:14 +00:00
|
|
|
string err = "invalid reference: " + s;
|
|
|
|
if (s.length() != 32)
|
2003-03-21 15:53:35 +00:00
|
|
|
throw BadRefError(err);
|
2003-03-14 16:43:14 +00:00
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
char c = s[i];
|
|
|
|
if (!((c >= '0' && c <= '9') ||
|
|
|
|
(c >= 'a' && c <= 'f')))
|
2003-03-21 15:53:35 +00:00
|
|
|
throw BadRefError(err);
|
2003-03-14 16:43:14 +00:00
|
|
|
}
|
|
|
|
}
|
2003-03-13 16:28:32 +00:00
|
|
|
|
|
|
|
|
2003-03-14 16:43:14 +00:00
|
|
|
/* Compute the MD5 hash of a file. */
|
2003-03-24 17:49:56 +00:00
|
|
|
string hashFile(string filename)
|
2003-03-14 16:43:14 +00:00
|
|
|
{
|
2003-03-24 17:49:56 +00:00
|
|
|
unsigned char hash[16];
|
|
|
|
FILE * file = fopen(filename.c_str(), "rb");
|
|
|
|
if (!file)
|
|
|
|
throw BadRefError("file `" + filename + "' does not exist");
|
|
|
|
int err = md5_stream(file, hash);
|
|
|
|
fclose(file);
|
|
|
|
if (err) throw BadRefError("cannot hash file");
|
|
|
|
return printHash(hash);
|
|
|
|
}
|
2003-03-14 16:43:14 +00:00
|
|
|
|
|
|
|
|
2003-03-24 17:49:56 +00:00
|
|
|
typedef map<string, string> Params;
|
2003-03-14 16:43:14 +00:00
|
|
|
|
|
|
|
|
2003-03-24 17:49:56 +00:00
|
|
|
void readPkgDescr(const string & hash,
|
|
|
|
Params & pkgImports, Params & fileImports, Params & arguments)
|
|
|
|
{
|
|
|
|
string pkgfile;
|
2003-03-13 16:28:32 +00:00
|
|
|
|
2003-03-24 17:49:56 +00:00
|
|
|
if (!queryDB(dbRefs, hash, pkgfile))
|
|
|
|
throw Error("unknown package " + hash);
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-03-24 17:49:56 +00:00
|
|
|
// cerr << "reading information about " + hash + " from " + pkgfile + "\n";
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-03-24 17:49:56 +00:00
|
|
|
/* Verify that the file hasn't changed. !!! race */
|
|
|
|
if (hashFile(pkgfile) != hash)
|
|
|
|
throw Error("file " + pkgfile + " is stale");
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-04-01 14:00:47 +00:00
|
|
|
ATerm term = ATreadFromNamedFile(pkgfile.c_str());
|
|
|
|
if (!term) throw Error("cannot read aterm " + pkgfile);
|
2003-03-13 16:28:32 +00:00
|
|
|
|
2003-04-01 14:00:47 +00:00
|
|
|
ATerm bindings;
|
|
|
|
if (!ATmatch(term, "Descr(<term>)", &bindings))
|
|
|
|
throw Error("invalid term in " + pkgfile);
|
2003-03-13 16:28:32 +00:00
|
|
|
|
2003-04-01 14:00:47 +00:00
|
|
|
char * cname;
|
|
|
|
ATerm value;
|
|
|
|
while (ATmatch(bindings, "[Bind(<str>, <term>), <list>]",
|
|
|
|
&cname, &value, &bindings))
|
|
|
|
{
|
|
|
|
string name(cname);
|
|
|
|
char * arg;
|
|
|
|
if (ATmatch(value, "Pkg(<str>)", &arg)) {
|
|
|
|
checkHash(arg);
|
|
|
|
pkgImports[name] = arg;
|
|
|
|
} else if (ATmatch(value, "File(<str>)", &arg)) {
|
|
|
|
checkHash(arg);
|
|
|
|
fileImports[name] = arg;
|
|
|
|
} else if (ATmatch(value, "Str(<str>)", &arg))
|
|
|
|
arguments[name] = arg;
|
|
|
|
else throw Error("invalid binding in " + pkgfile);
|
2003-03-14 16:43:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-23 23:24:09 +00:00
|
|
|
string getPkg(string hash);
|
2003-03-14 16:43:14 +00:00
|
|
|
|
|
|
|
|
2003-03-24 11:50:20 +00:00
|
|
|
typedef map<string, string> Environment;
|
2003-03-14 16:43:14 +00:00
|
|
|
|
|
|
|
|
2003-03-24 11:50:20 +00:00
|
|
|
void fetchDeps(string hash, Environment & env)
|
2003-03-14 16:43:14 +00:00
|
|
|
{
|
|
|
|
/* Read the package description file. */
|
2003-03-24 12:49:40 +00:00
|
|
|
Params pkgImports, fileImports, arguments;
|
2003-03-24 17:49:56 +00:00
|
|
|
readPkgDescr(hash, pkgImports, fileImports, arguments);
|
2003-03-14 16:43:14 +00:00
|
|
|
|
|
|
|
/* Recursively fetch all the dependencies, filling in the
|
|
|
|
environment as we go along. */
|
2003-03-24 12:49:40 +00:00
|
|
|
for (Params::iterator it = pkgImports.begin();
|
2003-03-14 16:43:14 +00:00
|
|
|
it != pkgImports.end(); it++)
|
|
|
|
{
|
|
|
|
cerr << "fetching package dependency "
|
2003-03-24 12:49:40 +00:00
|
|
|
<< it->first << " <- " << it->second
|
2003-03-14 16:43:14 +00:00
|
|
|
<< endl;
|
2003-03-24 12:49:40 +00:00
|
|
|
env[it->first] = getPkg(it->second);
|
2003-03-14 16:43:14 +00:00
|
|
|
}
|
|
|
|
|
2003-03-24 12:49:40 +00:00
|
|
|
for (Params::iterator it = fileImports.begin();
|
2003-03-14 16:43:14 +00:00
|
|
|
it != fileImports.end(); it++)
|
|
|
|
{
|
|
|
|
cerr << "fetching file dependency "
|
2003-03-24 12:49:40 +00:00
|
|
|
<< it->first << " = " << it->second
|
2003-03-14 16:43:14 +00:00
|
|
|
<< endl;
|
2003-03-13 16:28:32 +00:00
|
|
|
|
2003-03-14 16:43:14 +00:00
|
|
|
string file;
|
2003-03-13 16:28:32 +00:00
|
|
|
|
2003-03-24 12:49:40 +00:00
|
|
|
if (!queryDB(dbRefs, it->second, file))
|
|
|
|
throw Error("unknown file " + it->second);
|
2003-03-13 16:28:32 +00:00
|
|
|
|
2003-03-24 17:49:56 +00:00
|
|
|
if (hashFile(file) != it->second)
|
2003-03-20 16:53:00 +00:00
|
|
|
throw Error("file " + file + " is stale");
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-03-24 12:49:40 +00:00
|
|
|
env[it->first] = file;
|
2003-03-14 16:43:14 +00:00
|
|
|
}
|
2003-03-24 12:49:40 +00:00
|
|
|
|
|
|
|
string buildSystem;
|
|
|
|
|
|
|
|
for (Params::iterator it = arguments.begin();
|
|
|
|
it != arguments.end(); it++)
|
|
|
|
{
|
|
|
|
env[it->first] = it->second;
|
|
|
|
if (it->first == "system")
|
|
|
|
buildSystem = it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buildSystem != thisSystem)
|
|
|
|
throw Error("descriptor requires a `" + buildSystem +
|
|
|
|
"' but I am a `" + thisSystem + "'");
|
2003-03-24 11:50:20 +00:00
|
|
|
}
|
2003-03-14 16:43:14 +00:00
|
|
|
|
|
|
|
|
2003-03-24 11:50:20 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-02 15:34:05 +00:00
|
|
|
string queryPkgId(const string & hash)
|
|
|
|
{
|
|
|
|
Params pkgImports, fileImports, arguments;
|
|
|
|
readPkgDescr(hash, pkgImports, fileImports, arguments);
|
|
|
|
return getFromEnv(arguments, "id");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-24 11:50:20 +00:00
|
|
|
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-24 12:49:40 +00:00
|
|
|
|
2003-04-02 15:34:05 +00:00
|
|
|
string id = getFromEnv(env, "id");
|
|
|
|
|
2003-03-14 16:43:14 +00:00
|
|
|
/* Construct a path for the installed package. */
|
2003-04-02 15:34:05 +00:00
|
|
|
path = pkgHome + "/" + id + "-" + hash;
|
2003-03-14 16:43:14 +00:00
|
|
|
|
|
|
|
/* Create the path. */
|
|
|
|
if (mkdir(path.c_str(), 0777))
|
2003-03-20 16:53:00 +00:00
|
|
|
throw Error("unable to create directory " + path);
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-03-20 16:53:00 +00:00
|
|
|
try {
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-03-20 16:53:00 +00:00
|
|
|
/* Fork a child to build the package. */
|
|
|
|
pid_t pid;
|
|
|
|
switch (pid = fork()) {
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
throw Error("unable to fork");
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-03-20 16:53:00 +00:00
|
|
|
case 0: { /* child */
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-03-20 16:53:00 +00:00
|
|
|
/* Go to the build directory. */
|
|
|
|
if (chdir(path.c_str())) {
|
2003-04-02 15:34:05 +00:00
|
|
|
cerr << "unable to chdir to package directory\n";
|
2003-03-20 16:53:00 +00:00
|
|
|
_exit(1);
|
|
|
|
}
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-04-02 15:34:05 +00:00
|
|
|
/* Try to use a prebuilt. */
|
|
|
|
string prebuiltHash, prebuiltFile;
|
|
|
|
if (queryDB(dbPrebuilts, hash, prebuiltHash) &&
|
|
|
|
queryDB(dbRefs, prebuiltHash, prebuiltFile))
|
|
|
|
{
|
|
|
|
cerr << "substituting prebuilt " << prebuiltFile << endl;
|
|
|
|
|
|
|
|
if (hashFile(prebuiltFile) != prebuiltHash) {
|
|
|
|
cerr << "prebuilt " + prebuiltFile + " is stale\n";
|
|
|
|
goto build;
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = system(("tar xvfj " + prebuiltFile).c_str()); // !!! escaping
|
|
|
|
if (WEXITSTATUS(res) != 0)
|
|
|
|
/* This is a fatal error, because path may now
|
|
|
|
have clobbered. */
|
|
|
|
throw Error("cannot unpack " + prebuiltFile);
|
|
|
|
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
build:
|
|
|
|
|
2003-03-20 16:53:00 +00: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 16:43:14 +00:00
|
|
|
|
2003-03-20 16:53:00 +00:00
|
|
|
/* Execute the builder. This should not return. */
|
|
|
|
execle(builder.c_str(), builder.c_str(), 0, env2);
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-04-02 15:34:05 +00:00
|
|
|
cerr << strerror(errno) << endl;
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-04-02 15:34:05 +00:00
|
|
|
cerr << "unable to execute builder\n";
|
2003-03-23 23:24:09 +00:00
|
|
|
_exit(1); }
|
2003-03-20 16:53:00 +00:00
|
|
|
|
|
|
|
}
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-03-20 16:53:00 +00:00
|
|
|
/* parent */
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-03-20 16:53:00 +00: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 16:43:14 +00:00
|
|
|
|
2003-03-20 16:53:00 +00: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 16:28:32 +00:00
|
|
|
|
2003-03-23 23:24:09 +00:00
|
|
|
setDB(dbInstPkgs, hash, path);
|
2003-03-14 16:43:14 +00:00
|
|
|
}
|
|
|
|
|
2003-03-13 16:28:32 +00:00
|
|
|
|
2003-03-23 23:24:09 +00:00
|
|
|
string getPkg(string hash)
|
2003-03-14 16:43:14 +00:00
|
|
|
{
|
|
|
|
string path;
|
2003-03-24 17:49:56 +00:00
|
|
|
checkHash(hash);
|
2003-03-23 23:24:09 +00:00
|
|
|
while (!queryDB(dbInstPkgs, hash, path))
|
|
|
|
installPkg(hash);
|
2003-03-14 16:43:14 +00:00
|
|
|
return path;
|
2003-03-13 16:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-04 12:02:40 +00:00
|
|
|
void runPkg(string hash,
|
|
|
|
Strings::iterator firstArg,
|
|
|
|
Strings::iterator lastArg)
|
2003-03-23 23:24:09 +00:00
|
|
|
{
|
|
|
|
string src;
|
|
|
|
string path;
|
|
|
|
string cmd;
|
|
|
|
string runner;
|
|
|
|
Environment env;
|
|
|
|
|
2003-03-24 11:50:20 +00:00
|
|
|
/* Fetch dependencies. */
|
|
|
|
fetchDeps(hash, env);
|
2003-03-23 23:24:09 +00:00
|
|
|
|
2003-03-24 11:50:20 +00:00
|
|
|
runner = getFromEnv(env, "run");
|
|
|
|
|
2003-03-28 10:33:34 +00:00
|
|
|
/* 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());
|
2003-03-23 23:24:09 +00:00
|
|
|
}
|
|
|
|
|
2003-03-28 10:33:34 +00:00
|
|
|
/* Create the list of arguments. */
|
|
|
|
const char * args2[env.size() + 2];
|
|
|
|
int i = 0;
|
|
|
|
args2[i++] = runner.c_str();
|
2003-04-04 12:02:40 +00:00
|
|
|
for (Strings::const_iterator it = firstArg; it != lastArg; it++, i++)
|
2003-03-28 10:33:34 +00:00
|
|
|
args2[i] = it->c_str();
|
|
|
|
args2[i] = 0;
|
2003-03-23 23:24:09 +00:00
|
|
|
|
2003-03-28 10:33:34 +00:00
|
|
|
/* Execute the runner. This should not return. */
|
|
|
|
execv(runner.c_str(), (char * *) args2);
|
|
|
|
|
2003-04-02 15:34:05 +00:00
|
|
|
cerr << strerror(errno) << endl;
|
2003-03-28 10:33:34 +00:00
|
|
|
throw Error("unable to execute runner");
|
2003-03-23 23:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-24 17:49:56 +00:00
|
|
|
void ensurePkg(string hash)
|
|
|
|
{
|
|
|
|
Params pkgImports, fileImports, arguments;
|
|
|
|
readPkgDescr(hash, pkgImports, fileImports, arguments);
|
|
|
|
|
|
|
|
if (fileImports.find("build") != fileImports.end())
|
|
|
|
getPkg(hash);
|
|
|
|
else if (fileImports.find("run") != fileImports.end()) {
|
|
|
|
Environment env;
|
|
|
|
fetchDeps(hash, env);
|
|
|
|
} else throw Error("invalid descriptor");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-02 15:34:05 +00:00
|
|
|
void delPkg(string hash)
|
|
|
|
{
|
|
|
|
string path;
|
|
|
|
checkHash(hash);
|
|
|
|
if (queryDB(dbInstPkgs, hash, path)) {
|
|
|
|
int res = system(("rm -rf " + path).c_str()); // !!! escaping
|
|
|
|
delDB(dbInstPkgs, hash); // not a bug
|
|
|
|
if (WEXITSTATUS(res) != 0)
|
|
|
|
throw Error("cannot delete " + path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-04 12:02:40 +00:00
|
|
|
void exportPkgs(string outDir,
|
|
|
|
Strings::iterator firstHash,
|
|
|
|
Strings::iterator lastHash)
|
2003-04-02 15:34:05 +00:00
|
|
|
{
|
2003-04-04 12:02:40 +00:00
|
|
|
for (Strings::iterator it = firstHash; it != lastHash; it++) {
|
2003-04-02 15:34:05 +00:00
|
|
|
string hash = *it;
|
|
|
|
string pkgDir = getPkg(hash);
|
|
|
|
string tmpFile = outDir + "/export_tmp";
|
|
|
|
|
|
|
|
int res = system(("cd " + pkgDir + " && tar cvfj " + tmpFile + " .").c_str()); // !!! escaping
|
|
|
|
if (WEXITSTATUS(res) != 0)
|
|
|
|
throw Error("cannot tar " + pkgDir);
|
|
|
|
|
|
|
|
string prebuiltHash = hashFile(tmpFile);
|
|
|
|
string pkgId = queryPkgId(hash);
|
|
|
|
string prebuiltFile = outDir + "/" +
|
|
|
|
pkgId + "-" + hash + "-" + prebuiltHash + ".tar.bz2";
|
|
|
|
|
|
|
|
rename(tmpFile.c_str(), prebuiltFile.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void regPrebuilt(string pkgHash, string prebuiltHash)
|
|
|
|
{
|
|
|
|
checkHash(pkgHash);
|
|
|
|
checkHash(prebuiltHash);
|
|
|
|
setDB(dbPrebuilts, pkgHash, prebuiltHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-14 16:43:14 +00:00
|
|
|
string absPath(string filename)
|
2003-03-13 16:28:32 +00:00
|
|
|
{
|
2003-03-14 16:43:14 +00:00
|
|
|
if (filename[0] != '/') {
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
if (!getcwd(buf, sizeof(buf)))
|
2003-03-20 16:53:00 +00:00
|
|
|
throw Error("cannot get cwd");
|
2003-03-14 16:43:14 +00:00
|
|
|
filename = string(buf) + "/" + filename;
|
|
|
|
/* !!! canonicalise */
|
|
|
|
}
|
|
|
|
return filename;
|
|
|
|
}
|
2003-03-13 16:28:32 +00:00
|
|
|
|
|
|
|
|
2003-03-14 16:43:14 +00:00
|
|
|
void registerFile(string filename)
|
|
|
|
{
|
|
|
|
filename = absPath(filename);
|
2003-03-24 17:49:56 +00:00
|
|
|
setDB(dbRefs, hashFile(filename), filename);
|
2003-03-13 16:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This is primarily used for bootstrapping. */
|
2003-03-23 23:24:09 +00:00
|
|
|
void registerInstalledPkg(string hash, string path)
|
2003-03-13 16:28:32 +00:00
|
|
|
{
|
2003-03-24 17:49:56 +00:00
|
|
|
checkHash(hash);
|
2003-03-14 16:43:14 +00:00
|
|
|
if (path == "")
|
2003-03-23 23:24:09 +00:00
|
|
|
delDB(dbInstPkgs, hash);
|
2003-03-13 16:28:32 +00:00
|
|
|
else
|
2003-03-23 23:24:09 +00:00
|
|
|
setDB(dbInstPkgs, hash, path);
|
2003-03-14 16:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void initDB()
|
|
|
|
{
|
|
|
|
openDB(dbRefs, false);
|
|
|
|
openDB(dbInstPkgs, false);
|
2003-04-02 15:34:05 +00:00
|
|
|
openDB(dbPrebuilts, false);
|
2003-03-13 16:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-21 15:53:35 +00: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 {
|
2003-03-24 17:49:56 +00:00
|
|
|
if (hashFile(it->second) != it->first) {
|
|
|
|
cerr << "file " << it->second << " has changed\n";
|
2003-03-21 15:53:35 +00:00
|
|
|
delDB(dbRefs, it->first);
|
2003-03-24 17:49:56 +00:00
|
|
|
}
|
2003-03-21 15:53:35 +00:00
|
|
|
} 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 15:58:40 +00:00
|
|
|
void listInstalledPkgs()
|
|
|
|
{
|
|
|
|
DBPairs instPkgs;
|
|
|
|
|
|
|
|
enumDB(dbInstPkgs, instPkgs);
|
|
|
|
|
|
|
|
for (DBPairs::iterator it = instPkgs.begin();
|
|
|
|
it != instPkgs.end(); it++)
|
2003-03-24 17:49:56 +00:00
|
|
|
cout << it->first << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-04 12:02:40 +00:00
|
|
|
void printInfo(Strings::iterator first, Strings::iterator last)
|
2003-03-24 17:49:56 +00:00
|
|
|
{
|
2003-04-04 12:02:40 +00:00
|
|
|
for (Strings::iterator it = first; it != last; it++)
|
2003-03-21 15:58:40 +00:00
|
|
|
{
|
2003-03-24 17:49:56 +00:00
|
|
|
try {
|
2003-04-02 15:34:05 +00:00
|
|
|
cout << *it << " " << queryPkgId(*it) << endl;
|
|
|
|
} catch (Error & e) { // !!! more specific
|
2003-03-24 17:49:56 +00:00
|
|
|
cout << *it << " (descriptor missing)\n";
|
|
|
|
}
|
2003-03-21 15:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-04 12:02:40 +00:00
|
|
|
void computeClosure(Strings::iterator first, Strings::iterator last,
|
2003-03-24 17:49:56 +00:00
|
|
|
set<string> & result)
|
|
|
|
{
|
2003-04-04 12:02:40 +00:00
|
|
|
list<string> workList(first, last);
|
2003-03-24 17:49:56 +00:00
|
|
|
set<string> doneSet;
|
|
|
|
|
|
|
|
while (!workList.empty()) {
|
|
|
|
string hash = workList.front();
|
|
|
|
workList.pop_front();
|
|
|
|
|
|
|
|
if (doneSet.find(hash) == doneSet.end()) {
|
|
|
|
doneSet.insert(hash);
|
|
|
|
|
|
|
|
Params pkgImports, fileImports, arguments;
|
|
|
|
readPkgDescr(hash, pkgImports, fileImports, arguments);
|
|
|
|
|
|
|
|
for (Params::iterator it = pkgImports.begin();
|
|
|
|
it != pkgImports.end(); it++)
|
|
|
|
workList.push_back(it->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = doneSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-04 12:02:40 +00:00
|
|
|
void printClosure(Strings::iterator first, Strings::iterator last)
|
2003-03-24 17:49:56 +00:00
|
|
|
{
|
|
|
|
set<string> allHashes;
|
2003-04-04 12:02:40 +00:00
|
|
|
computeClosure(first, last, allHashes);
|
2003-03-24 17:49:56 +00:00
|
|
|
for (set<string>::iterator it = allHashes.begin();
|
|
|
|
it != allHashes.end(); it++)
|
|
|
|
cout << *it << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string dotQuote(const string & s)
|
|
|
|
{
|
|
|
|
return "\"" + s + "\"";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-04 12:02:40 +00:00
|
|
|
void printGraph(Strings::iterator first, Strings::iterator last)
|
2003-03-24 17:49:56 +00:00
|
|
|
{
|
|
|
|
set<string> allHashes;
|
2003-04-04 12:02:40 +00:00
|
|
|
computeClosure(first, last, allHashes);
|
2003-03-24 17:49:56 +00:00
|
|
|
|
|
|
|
cout << "digraph G {\n";
|
|
|
|
|
|
|
|
for (set<string>::iterator it = allHashes.begin();
|
|
|
|
it != allHashes.end(); it++)
|
|
|
|
{
|
|
|
|
Params pkgImports, fileImports, arguments;
|
|
|
|
readPkgDescr(*it, pkgImports, fileImports, arguments);
|
|
|
|
|
|
|
|
cout << dotQuote(*it) << "[label = \""
|
|
|
|
<< getFromEnv(arguments, "id")
|
|
|
|
<< "\"];\n";
|
|
|
|
|
|
|
|
for (Params::iterator it2 = pkgImports.begin();
|
|
|
|
it2 != pkgImports.end(); it2++)
|
|
|
|
cout << dotQuote(it2->second) << " -> "
|
|
|
|
<< dotQuote(*it) << ";\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-04 12:02:40 +00:00
|
|
|
void run(Strings args)
|
2003-03-13 16:28:32 +00:00
|
|
|
{
|
|
|
|
}
|
2003-03-20 16:53:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
void printUsage()
|
2003-03-13 16:28:32 +00:00
|
|
|
{
|
2003-03-20 16:53:00 +00:00
|
|
|
cerr <<
|
|
|
|
"Usage: nix SUBCOMMAND OPTIONS...
|
|
|
|
|
|
|
|
Subcommands:
|
|
|
|
|
|
|
|
init
|
|
|
|
Initialize the database.
|
|
|
|
|
2003-03-21 15:53:35 +00:00
|
|
|
verify
|
2003-03-23 23:24:09 +00:00
|
|
|
Remove stale entries from the database.
|
2003-03-21 15:53:35 +00:00
|
|
|
|
2003-03-20 16:53:00 +00:00
|
|
|
regfile FILENAME
|
|
|
|
Register FILENAME keyed by its hash.
|
|
|
|
|
|
|
|
reginst HASH PATH
|
|
|
|
Register an installed package.
|
|
|
|
|
|
|
|
getpkg HASH
|
2003-04-02 15:34:05 +00:00
|
|
|
Ensure that the package referenced by HASH is installed. Print
|
2003-03-20 16:53:00 +00:00
|
|
|
out the path of the package on stdout.
|
2003-03-23 23:24:09 +00:00
|
|
|
|
2003-04-02 15:34:05 +00:00
|
|
|
delpkg HASH
|
|
|
|
Uninstall the package referenced by HASH, disregarding any
|
|
|
|
dependencies that other packages may have on HASH.
|
|
|
|
|
2003-03-24 11:50:20 +00:00
|
|
|
listinst
|
|
|
|
Prints a list of installed packages.
|
|
|
|
|
2003-03-28 09:53:22 +00:00
|
|
|
run HASH ARGS...
|
|
|
|
Run the descriptor referenced by HASH with the given arguments.
|
2003-03-24 17:49:56 +00:00
|
|
|
|
|
|
|
ensure HASH
|
|
|
|
Like getpkg, but if HASH refers to a run descriptor, fetch only
|
|
|
|
the dependencies.
|
|
|
|
|
2003-04-02 15:34:05 +00:00
|
|
|
export DIR HASH...
|
|
|
|
Export installed packages to DIR.
|
|
|
|
|
|
|
|
regprebuilt HASH1 HASH2
|
|
|
|
Inform Nix that an export HASH2 can be used to fast-build HASH1.
|
|
|
|
|
2003-03-24 17:49:56 +00:00
|
|
|
info HASH...
|
|
|
|
Print information about the specified descriptors.
|
|
|
|
|
|
|
|
closure HASH...
|
|
|
|
Determine the closure of the set of descriptors under the import
|
|
|
|
relation, starting at the given roots.
|
|
|
|
|
|
|
|
graph HASH...
|
|
|
|
Like closure, but print a dot graph specification.
|
2003-03-20 16:53:00 +00:00
|
|
|
";
|
|
|
|
}
|
2003-03-13 16:28:32 +00:00
|
|
|
|
2003-03-20 16:53:00 +00:00
|
|
|
|
2003-04-04 12:02:40 +00:00
|
|
|
void run(Strings::iterator argCur, Strings::iterator argEnd)
|
2003-03-20 16:53:00 +00:00
|
|
|
{
|
2003-03-14 16:43:14 +00:00
|
|
|
umask(0022);
|
2003-03-13 16:28:32 +00:00
|
|
|
|
2003-03-20 16:53:00 +00:00
|
|
|
if (getenv(PKGINFO_ENVVAR))
|
|
|
|
dbfile = getenv(PKGINFO_ENVVAR);
|
|
|
|
|
|
|
|
if (getenv(PKGHOME_ENVVAR))
|
|
|
|
pkgHome = getenv(PKGHOME_ENVVAR);
|
2003-03-13 16:28:32 +00:00
|
|
|
|
2003-03-28 09:53:22 +00:00
|
|
|
/* Parse the global flags. */
|
2003-04-04 12:02:40 +00:00
|
|
|
for ( ; argCur != argEnd; argCur++) {
|
|
|
|
string arg(*argCur);
|
2003-03-28 09:53:22 +00:00
|
|
|
if (arg == "-h" || arg == "--help") {
|
2003-03-20 16:53:00 +00:00
|
|
|
printUsage();
|
|
|
|
return;
|
2003-03-28 09:53:22 +00:00
|
|
|
} else if (arg == "-d") {
|
2003-03-20 16:53:00 +00:00
|
|
|
dbfile = optarg;
|
2003-03-28 09:53:22 +00:00
|
|
|
} else if (arg[0] == '-') {
|
|
|
|
throw UsageError("invalid option `" + arg + "'");
|
|
|
|
} else break;
|
2003-03-20 16:53:00 +00:00
|
|
|
}
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-04-04 12:02:40 +00:00
|
|
|
UsageError argcError("wrong number of arguments");
|
|
|
|
|
|
|
|
/* Parse the command. */
|
|
|
|
if (argCur == argEnd) throw UsageError("no command specified");
|
|
|
|
string cmd = *argCur++;
|
|
|
|
int argc = argEnd - argCur;
|
|
|
|
|
|
|
|
if (cmd == "init") {
|
|
|
|
if (argc != 0) throw argcError;
|
|
|
|
initDB();
|
|
|
|
} else if (cmd == "verify") {
|
|
|
|
if (argc != 0) throw argcError;
|
|
|
|
verifyDB();
|
|
|
|
} else if (cmd == "getpkg") {
|
|
|
|
if (argc != 1) throw argcError;
|
|
|
|
string path = getPkg(*argCur);
|
|
|
|
cout << path << endl;
|
|
|
|
} else if (cmd == "delpkg") {
|
|
|
|
if (argc != 1) throw argcError;
|
|
|
|
delPkg(*argCur);
|
|
|
|
} else if (cmd == "run") {
|
|
|
|
if (argc < 1) throw argcError;
|
|
|
|
runPkg(*argCur, argCur + 1, argEnd);
|
|
|
|
} else if (cmd == "ensure") {
|
|
|
|
if (argc != 1) throw argcError;
|
|
|
|
ensurePkg(*argCur);
|
|
|
|
} else if (cmd == "export") {
|
|
|
|
if (argc < 1) throw argcError;
|
|
|
|
exportPkgs(*argCur, argCur + 1, argEnd);
|
|
|
|
} else if (cmd == "regprebuilt") {
|
|
|
|
if (argc != 2) throw argcError;
|
|
|
|
regPrebuilt(*argCur, argCur[1]);
|
|
|
|
} else if (cmd == "regfile") {
|
|
|
|
if (argc != 1) throw argcError;
|
|
|
|
registerFile(*argCur);
|
|
|
|
} else if (cmd == "reginst") {
|
|
|
|
if (argc != 2) throw argcError;
|
|
|
|
registerInstalledPkg(*argCur, argCur[1]);
|
|
|
|
} else if (cmd == "listinst") {
|
|
|
|
if (argc != 0) throw argcError;
|
|
|
|
listInstalledPkgs();
|
|
|
|
} else if (cmd == "info") {
|
|
|
|
printInfo(argCur, argEnd);
|
|
|
|
} else if (cmd == "closure") {
|
|
|
|
printClosure(argCur, argEnd);
|
|
|
|
} else if (cmd == "graph") {
|
|
|
|
printGraph(argCur, argEnd);
|
|
|
|
} else
|
|
|
|
throw UsageError("unknown command: " + string(cmd));
|
2003-03-20 16:53:00 +00:00
|
|
|
}
|
2003-03-14 16:43:14 +00:00
|
|
|
|
2003-03-24 17:49:56 +00:00
|
|
|
|
2003-03-20 16:53:00 +00:00
|
|
|
int main(int argc, char * * argv)
|
|
|
|
{
|
2003-04-01 14:00:47 +00:00
|
|
|
ATerm bottomOfStack;
|
|
|
|
ATinit(argc, argv, &bottomOfStack);
|
|
|
|
|
2003-04-04 12:02:40 +00:00
|
|
|
/* Put the arguments in a vector. */
|
|
|
|
Strings args;
|
|
|
|
while (argc--) args.push_back(*argv++);
|
|
|
|
Strings::iterator argCur = args.begin(), argEnd = args.end();
|
|
|
|
|
|
|
|
prog = *argCur++;
|
2003-03-20 16:53:00 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
2003-04-04 12:02:40 +00:00
|
|
|
run(argCur, argEnd);
|
2003-03-20 16:53:00 +00:00
|
|
|
} 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 16:43:14 +00:00
|
|
|
return 1;
|
2003-03-20 16:53:00 +00:00
|
|
|
} catch (exception & e) {
|
|
|
|
cerr << "error: " << e.what() << endl;
|
2003-03-13 16:28:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2003-03-14 16:43:14 +00:00
|
|
|
|
|
|
|
return 0;
|
2003-03-13 16:28:32 +00:00
|
|
|
}
|