2003-06-16 13:33:38 +00:00
|
|
|
#include <map>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
2003-06-27 09:55:31 +00:00
|
|
|
#include <fcntl.h>
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-07-07 07:43:58 +00:00
|
|
|
#include "fstate.hh"
|
2003-06-16 13:33:38 +00:00
|
|
|
#include "globals.hh"
|
2003-07-07 07:43:58 +00:00
|
|
|
#include "store.hh"
|
2003-06-16 13:33:38 +00:00
|
|
|
#include "db.hh"
|
2003-07-14 10:23:11 +00:00
|
|
|
#include "references.hh"
|
2003-06-16 13:33:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* A Unix environment is a mapping from strings to strings. */
|
|
|
|
typedef map<string, string> Environment;
|
|
|
|
|
|
|
|
|
2003-07-08 13:22:08 +00:00
|
|
|
class AutoDelete
|
2003-06-16 13:33:38 +00:00
|
|
|
{
|
2003-07-08 13:22:08 +00:00
|
|
|
string path;
|
2003-07-16 20:00:51 +00:00
|
|
|
bool del;
|
2003-07-08 13:22:08 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
AutoDelete(const string & p) : path(p)
|
|
|
|
{
|
2003-07-16 20:00:51 +00:00
|
|
|
del = true;
|
2003-07-08 13:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoDelete()
|
|
|
|
{
|
2003-07-16 20:00:51 +00:00
|
|
|
if (del) deletePath(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cancel()
|
|
|
|
{
|
|
|
|
del = false;
|
2003-07-08 13:22:08 +00:00
|
|
|
}
|
|
|
|
};
|
2003-06-16 13:33:38 +00:00
|
|
|
|
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Run a program. */
|
|
|
|
static void runProgram(const string & program, Environment env)
|
2003-06-16 13:33:38 +00:00
|
|
|
{
|
|
|
|
/* Create a log file. */
|
2003-06-27 14:56:12 +00:00
|
|
|
string logFileName = nixLogDir + "/run.log";
|
2003-06-16 13:33:38 +00:00
|
|
|
/* !!! auto-pclose on exit */
|
2003-07-08 13:22:08 +00:00
|
|
|
FILE * logFile = popen(("tee -a " + logFileName + " >&2").c_str(), "w"); /* !!! escaping */
|
2003-06-16 13:33:38 +00:00
|
|
|
if (!logFile)
|
2003-07-08 13:22:08 +00:00
|
|
|
throw SysError(format("creating log file `%1%'") % logFileName);
|
|
|
|
|
|
|
|
/* Create a temporary directory where the build will take
|
|
|
|
place. */
|
|
|
|
static int counter = 0;
|
|
|
|
string tmpDir = (format("/tmp/nix-%1%-%2%") % getpid() % counter++).str();
|
|
|
|
|
|
|
|
if (mkdir(tmpDir.c_str(), 0777) == -1)
|
|
|
|
throw SysError(format("creating directory `%1%'") % tmpDir);
|
|
|
|
|
|
|
|
AutoDelete delTmpDir(tmpDir);
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Fork a child to build the package. */
|
|
|
|
pid_t pid;
|
|
|
|
switch (pid = fork()) {
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
case -1:
|
|
|
|
throw SysError("unable to fork");
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
case 0:
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
try { /* child */
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-07-08 13:22:08 +00:00
|
|
|
if (chdir(tmpDir.c_str()) == -1)
|
|
|
|
throw SysError(format("changing into to `%1%'") % tmpDir);
|
2003-06-27 14:56:12 +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;
|
|
|
|
|
|
|
|
/* Dup the log handle into stderr. */
|
|
|
|
if (dup2(fileno(logFile), STDERR_FILENO) == -1)
|
|
|
|
throw SysError("cannot pipe standard error into log file");
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Dup stderr to stdin. */
|
|
|
|
if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1)
|
|
|
|
throw SysError("cannot dup stderr into stdout");
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Make the program executable. !!! hack. */
|
|
|
|
if (chmod(program.c_str(), 0755))
|
|
|
|
throw SysError("cannot make program executable");
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Execute the program. This should not return. */
|
|
|
|
execle(program.c_str(), baseNameOf(program).c_str(), 0, env2);
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
throw SysError(format("unable to execute %1%") % program);
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
} catch (exception & e) {
|
2003-07-04 12:18:06 +00:00
|
|
|
cerr << format("build error: %1%\n") % e.what();
|
2003-07-04 15:42:03 +00:00
|
|
|
}
|
2003-06-27 14:56:12 +00:00
|
|
|
_exit(1);
|
2003-06-16 13:33:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* parent */
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2003-06-27 14:56:12 +00:00
|
|
|
/* Close the logging pipe. Note that this should not cause
|
|
|
|
the logger to exit until builder exits (because the latter
|
|
|
|
has an open file handle to the former). */
|
|
|
|
pclose(logFile);
|
|
|
|
|
|
|
|
/* Wait for the child to finish. */
|
|
|
|
int status;
|
|
|
|
if (waitpid(pid, &status, 0) != pid)
|
|
|
|
throw Error("unable to wait for child");
|
|
|
|
|
2003-07-16 20:00:51 +00:00
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
|
|
|
delTmpDir.cancel();
|
2003-06-27 14:56:12 +00:00
|
|
|
throw Error("unable to build package");
|
2003-07-16 20:00:51 +00:00
|
|
|
}
|
2003-06-16 13:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Throw an exception if the given platform string is not supported by
|
|
|
|
the platform we are executing on. */
|
2003-06-27 14:56:12 +00:00
|
|
|
static void checkPlatform(const string & platform)
|
2003-06-16 13:33:38 +00:00
|
|
|
{
|
|
|
|
if (platform != thisSystem)
|
2003-06-27 13:55:12 +00:00
|
|
|
throw Error(format("a `%1%' is required, but I am a `%2%'")
|
|
|
|
% platform % thisSystem);
|
2003-06-16 13:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
string printTerm(ATerm t)
|
2003-06-17 13:37:44 +00:00
|
|
|
{
|
2003-06-27 13:55:12 +00:00
|
|
|
char * s = ATwriteToString(t);
|
2003-06-17 13:37:44 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-06 14:20:47 +00:00
|
|
|
Error badTerm(const format & f, ATerm t)
|
2003-06-16 13:33:38 +00:00
|
|
|
{
|
2003-06-27 13:55:12 +00:00
|
|
|
return Error(format("%1%, in `%2%'") % f.str() % printTerm(t));
|
2003-06-16 13:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-27 13:55:12 +00:00
|
|
|
Hash hashTerm(ATerm t)
|
2003-06-16 13:33:38 +00:00
|
|
|
{
|
2003-06-27 13:55:12 +00:00
|
|
|
return hashString(printTerm(t));
|
2003-06-16 13:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-10 15:11:48 +00:00
|
|
|
FState hash2fstate(Hash hash)
|
|
|
|
{
|
|
|
|
return ATmake("Include(<str>)", ((string) hash).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-15 16:28:54 +00:00
|
|
|
ATerm termFromId(const FSId & id, string * p)
|
2003-07-04 12:18:06 +00:00
|
|
|
{
|
2003-07-15 16:28:54 +00:00
|
|
|
string path = expandId(id);
|
2003-07-09 15:02:03 +00:00
|
|
|
if (p) *p = path;
|
2003-07-04 12:18:06 +00:00
|
|
|
ATerm t = ATreadFromNamedFile(path.c_str());
|
2003-07-15 16:28:54 +00:00
|
|
|
if (!t) throw Error(format("cannot read aterm from `%1%'") % path);
|
2003-07-04 12:18:06 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-15 16:28:54 +00:00
|
|
|
FSId writeTerm(ATerm t, const string & suffix, string * p)
|
2003-07-04 12:18:06 +00:00
|
|
|
{
|
2003-07-15 16:28:54 +00:00
|
|
|
FSId id = hashTerm(t);
|
|
|
|
|
|
|
|
string path = canonPath(nixStore + "/" +
|
|
|
|
(string) id + suffix + ".nix");
|
2003-07-04 12:18:06 +00:00
|
|
|
if (!ATwriteToNamedTextFile(t, path.c_str()))
|
|
|
|
throw Error(format("cannot write aterm %1%") % path);
|
2003-07-15 16:28:54 +00:00
|
|
|
|
|
|
|
registerPath(path, id);
|
|
|
|
if (p) *p = path;
|
|
|
|
|
|
|
|
return id;
|
2003-07-04 12:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-15 16:28:54 +00:00
|
|
|
void registerSuccessor(const FSId & id1, const FSId & id2)
|
2003-07-10 18:48:11 +00:00
|
|
|
{
|
2003-07-15 16:28:54 +00:00
|
|
|
setDB(nixDB, dbSuccessors, id1, id2);
|
2003-07-10 18:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-16 11:05:59 +00:00
|
|
|
static FSId storeSuccessor(const FSId & id1, FState sc,
|
|
|
|
string * p)
|
2003-07-09 15:02:03 +00:00
|
|
|
{
|
2003-07-16 11:05:59 +00:00
|
|
|
FSId id2 = writeTerm(sc, "-s-" + (string) id1, p);
|
2003-07-15 16:28:54 +00:00
|
|
|
registerSuccessor(id1, id2);
|
|
|
|
return id2;
|
2003-07-09 15:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-15 16:28:54 +00:00
|
|
|
static void parseIds(ATermList ids, FSIds & out)
|
|
|
|
{
|
|
|
|
while (!ATisEmpty(ids)) {
|
|
|
|
char * s;
|
|
|
|
ATerm id = ATgetFirst(ids);
|
|
|
|
if (!ATmatch(id, "<str>", &s))
|
|
|
|
throw badTerm("not an id", id);
|
|
|
|
out.push_back(parseHash(s));
|
|
|
|
ids = ATgetNext(ids);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-16 20:33:29 +00:00
|
|
|
static void checkSlice(const Slice & slice);
|
|
|
|
|
|
|
|
|
2003-07-15 16:28:54 +00:00
|
|
|
/* Parse a slice. */
|
|
|
|
static Slice parseSlice(FState fs)
|
|
|
|
{
|
|
|
|
Slice slice;
|
|
|
|
ATermList roots, elems;
|
|
|
|
|
|
|
|
if (!ATmatch(fs, "Slice([<list>], [<list>])", &roots, &elems))
|
|
|
|
throw badTerm("not a slice", fs);
|
|
|
|
|
|
|
|
parseIds(roots, slice.roots);
|
|
|
|
|
|
|
|
while (!ATisEmpty(elems)) {
|
|
|
|
char * s1, * s2;
|
|
|
|
ATermList refs;
|
|
|
|
ATerm t = ATgetFirst(elems);
|
|
|
|
if (!ATmatch(t, "(<str>, <str>, [<list>])", &s1, &s2, &refs))
|
|
|
|
throw badTerm("not a slice element", t);
|
|
|
|
SliceElem elem;
|
|
|
|
elem.path = s1;
|
|
|
|
elem.id = parseHash(s2);
|
|
|
|
parseIds(refs, elem.refs);
|
|
|
|
slice.elems.push_back(elem);
|
|
|
|
elems = ATgetNext(elems);
|
|
|
|
}
|
|
|
|
|
2003-07-16 20:33:29 +00:00
|
|
|
checkSlice(slice);
|
|
|
|
|
2003-07-15 16:28:54 +00:00
|
|
|
return slice;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-15 21:24:05 +00:00
|
|
|
static ATermList unparseIds(const FSIds & ids)
|
|
|
|
{
|
|
|
|
ATermList l = ATempty;
|
|
|
|
for (FSIds::const_iterator i = ids.begin();
|
|
|
|
i != ids.end(); i++)
|
|
|
|
l = ATinsert(l,
|
|
|
|
ATmake("<str>", ((string) *i).c_str()));
|
|
|
|
return ATreverse(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static FState unparseSlice(const Slice & slice)
|
|
|
|
{
|
|
|
|
ATermList roots = unparseIds(slice.roots);
|
|
|
|
|
|
|
|
ATermList elems = ATempty;
|
|
|
|
for (SliceElems::const_iterator i = slice.elems.begin();
|
|
|
|
i != slice.elems.end(); i++)
|
|
|
|
elems = ATinsert(elems,
|
|
|
|
ATmake("(<str>, <str>, <term>)",
|
|
|
|
i->path.c_str(),
|
|
|
|
((string) i->id).c_str(),
|
|
|
|
unparseIds(i->refs)));
|
|
|
|
|
|
|
|
return ATmake("Slice(<term>, <term>)", roots, elems);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef set<FSId> FSIdSet;
|
|
|
|
|
|
|
|
|
2003-07-16 11:05:59 +00:00
|
|
|
static Slice normaliseFState2(FSId id, StringSet & usedPaths)
|
2003-07-15 16:28:54 +00:00
|
|
|
{
|
|
|
|
debug(format("normalising fstate"));
|
|
|
|
Nest nest(true);
|
|
|
|
|
|
|
|
/* Try to substitute $id$ by any known successors in order to
|
|
|
|
speed up the rewrite process. */
|
|
|
|
string idSucc;
|
|
|
|
while (queryDB(nixDB, dbSuccessors, id, idSucc)) {
|
|
|
|
debug(format("successor %1% -> %2%") % (string) id % idSucc);
|
|
|
|
id = parseHash(idSucc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the fstate expression. */
|
2003-07-16 11:05:59 +00:00
|
|
|
string fsPath;
|
|
|
|
FState fs = termFromId(id, &fsPath);
|
2003-07-15 16:28:54 +00:00
|
|
|
|
|
|
|
/* Already in normal form (i.e., a slice)? */
|
|
|
|
if (ATgetType(fs) == AT_APPL &&
|
|
|
|
(string) ATgetName(ATgetAFun(fs)) == "Slice")
|
2003-07-16 11:05:59 +00:00
|
|
|
{
|
|
|
|
usedPaths.insert(fsPath);
|
2003-07-15 16:28:54 +00:00
|
|
|
return parseSlice(fs);
|
2003-07-16 11:05:59 +00:00
|
|
|
}
|
2003-07-15 16:28:54 +00:00
|
|
|
|
|
|
|
/* Then we it's a Derive node. */
|
|
|
|
ATermList outs, ins, bnds;
|
|
|
|
char * builder;
|
|
|
|
char * platform;
|
|
|
|
if (!ATmatch(fs, "Derive([<list>], [<list>], <str>, <str>, [<list>])",
|
|
|
|
&outs, &ins, &builder, &platform, &bnds))
|
|
|
|
throw badTerm("not a derive", fs);
|
|
|
|
|
|
|
|
/* Right platform? */
|
|
|
|
checkPlatform(platform);
|
|
|
|
|
|
|
|
/* Realise inputs (and remember all input paths). */
|
|
|
|
FSIds inIds;
|
|
|
|
parseIds(ins, inIds);
|
2003-07-15 21:24:05 +00:00
|
|
|
|
|
|
|
typedef map<string, SliceElem> ElemMap;
|
|
|
|
|
|
|
|
ElemMap inMap;
|
|
|
|
|
2003-07-15 16:28:54 +00:00
|
|
|
for (FSIds::iterator i = inIds.begin(); i != inIds.end(); i++) {
|
|
|
|
Slice slice = normaliseFState(*i);
|
|
|
|
realiseSlice(slice);
|
|
|
|
|
|
|
|
for (SliceElems::iterator j = slice.elems.begin();
|
|
|
|
j != slice.elems.end(); j++)
|
2003-07-15 21:24:05 +00:00
|
|
|
inMap[j->path] = *j;
|
2003-07-15 16:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Strings inPaths;
|
2003-07-15 21:24:05 +00:00
|
|
|
for (ElemMap::iterator i = inMap.begin(); i != inMap.end(); i++)
|
|
|
|
inPaths.push_back(i->second.path);
|
2003-07-15 16:28:54 +00:00
|
|
|
|
|
|
|
/* Build the environment. */
|
|
|
|
Environment env;
|
|
|
|
while (!ATisEmpty(bnds)) {
|
|
|
|
char * s1, * s2;
|
|
|
|
ATerm bnd = ATgetFirst(bnds);
|
|
|
|
if (!ATmatch(bnd, "(<str>, <str>)", &s1, &s2))
|
|
|
|
throw badTerm("tuple of strings expected", bnd);
|
|
|
|
env[s1] = s2;
|
|
|
|
bnds = ATgetNext(bnds);
|
|
|
|
}
|
|
|
|
|
2003-07-16 20:00:51 +00:00
|
|
|
/* Parse the outputs. */
|
2003-07-16 08:30:26 +00:00
|
|
|
typedef map<string, FSId> OutPaths;
|
|
|
|
OutPaths outPaths;
|
2003-07-15 16:28:54 +00:00
|
|
|
while (!ATisEmpty(outs)) {
|
|
|
|
ATerm t = ATgetFirst(outs);
|
|
|
|
char * s1, * s2;
|
|
|
|
if (!ATmatch(t, "(<str>, <str>)", &s1, &s2))
|
|
|
|
throw badTerm("string expected", t);
|
2003-07-16 08:30:26 +00:00
|
|
|
outPaths[s1] = parseHash(s2);
|
2003-07-15 21:24:05 +00:00
|
|
|
inPaths.push_back(s1);
|
2003-07-15 16:28:54 +00:00
|
|
|
outs = ATgetNext(outs);
|
|
|
|
}
|
|
|
|
|
2003-07-16 20:00:51 +00:00
|
|
|
/* We can skip running the builder if we can expand all output
|
|
|
|
paths from their ids. */
|
|
|
|
bool fastBuild = false;
|
|
|
|
#if 0
|
2003-07-16 08:30:26 +00:00
|
|
|
for (OutPaths::iterator i = outPaths.begin();
|
2003-07-15 16:28:54 +00:00
|
|
|
i != outPaths.end(); i++)
|
2003-07-16 20:00:51 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
expandId(i->second, i->first);
|
|
|
|
} catch (...) {
|
|
|
|
fastBuild = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!fastBuild) {
|
2003-07-15 16:28:54 +00:00
|
|
|
|
2003-07-16 20:00:51 +00:00
|
|
|
/* Check that none of the outputs exist. */
|
|
|
|
for (OutPaths::iterator i = outPaths.begin();
|
|
|
|
i != outPaths.end(); i++)
|
|
|
|
if (pathExists(i->first))
|
|
|
|
throw Error(format("path `%1%' exists") % i->first);
|
|
|
|
|
|
|
|
/* Run the builder. */
|
|
|
|
runProgram(builder, env);
|
2003-07-15 16:28:54 +00:00
|
|
|
|
2003-07-16 20:00:51 +00:00
|
|
|
} else
|
|
|
|
debug(format("skipping build"));
|
|
|
|
|
2003-07-15 16:28:54 +00:00
|
|
|
Slice slice;
|
|
|
|
|
|
|
|
/* Check whether the output paths were created, and register each
|
|
|
|
one. */
|
2003-07-15 21:24:05 +00:00
|
|
|
FSIdSet used;
|
2003-07-16 08:30:26 +00:00
|
|
|
for (OutPaths::iterator i = outPaths.begin();
|
2003-07-15 16:28:54 +00:00
|
|
|
i != outPaths.end(); i++)
|
|
|
|
{
|
|
|
|
string path = i->first;
|
|
|
|
if (!pathExists(path))
|
|
|
|
throw Error(format("path `%1%' does not exist") % path);
|
|
|
|
registerPath(path, i->second);
|
|
|
|
slice.roots.push_back(i->second);
|
|
|
|
|
2003-07-15 21:24:05 +00:00
|
|
|
Strings refs = filterReferences(path, inPaths);
|
|
|
|
|
|
|
|
SliceElem elem;
|
|
|
|
elem.path = path;
|
|
|
|
elem.id = i->second;
|
|
|
|
|
|
|
|
for (Strings::iterator j = refs.begin(); j != refs.end(); j++) {
|
|
|
|
ElemMap::iterator k;
|
2003-07-16 08:30:26 +00:00
|
|
|
OutPaths::iterator l;
|
2003-07-15 21:24:05 +00:00
|
|
|
if ((k = inMap.find(*j)) != inMap.end()) {
|
|
|
|
elem.refs.push_back(k->second.id);
|
|
|
|
used.insert(k->second.id);
|
2003-07-16 20:33:29 +00:00
|
|
|
for (FSIds::iterator m = k->second.refs.begin();
|
|
|
|
m != k->second.refs.end(); m++)
|
|
|
|
used.insert(*m);
|
2003-07-16 08:30:26 +00:00
|
|
|
} else if ((l = outPaths.find(*j)) != outPaths.end()) {
|
|
|
|
elem.refs.push_back(l->second);
|
|
|
|
used.insert(l->second);
|
|
|
|
} else
|
|
|
|
throw Error(format("unknown referenced path `%1%'") % *j);
|
2003-07-15 21:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
slice.elems.push_back(elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ElemMap::iterator i = inMap.begin();
|
|
|
|
i != inMap.end(); i++)
|
|
|
|
{
|
|
|
|
FSIdSet::iterator j = used.find(i->second.id);
|
|
|
|
if (j == used.end())
|
|
|
|
debug(format("NOT referenced: `%1%'") % i->second.path);
|
|
|
|
else {
|
|
|
|
debug(format("referenced: `%1%'") % i->second.path);
|
|
|
|
slice.elems.push_back(i->second);
|
|
|
|
}
|
2003-07-15 16:28:54 +00:00
|
|
|
}
|
|
|
|
|
2003-07-15 21:24:05 +00:00
|
|
|
FState nf = unparseSlice(slice);
|
|
|
|
debug(printTerm(nf));
|
2003-07-16 11:05:59 +00:00
|
|
|
storeSuccessor(id, nf, &fsPath);
|
|
|
|
usedPaths.insert(fsPath);
|
2003-07-15 21:24:05 +00:00
|
|
|
|
2003-07-16 20:33:29 +00:00
|
|
|
parseSlice(nf); /* check */
|
|
|
|
|
2003-07-15 16:28:54 +00:00
|
|
|
return slice;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-16 11:05:59 +00:00
|
|
|
Slice normaliseFState(FSId id)
|
|
|
|
{
|
|
|
|
StringSet dummy;
|
|
|
|
return normaliseFState2(id, dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-15 21:24:05 +00:00
|
|
|
static void checkSlice(const Slice & slice)
|
|
|
|
{
|
|
|
|
if (slice.elems.size() == 0)
|
|
|
|
throw Error("empty slice");
|
|
|
|
|
|
|
|
FSIdSet decl;
|
|
|
|
for (SliceElems::const_iterator i = slice.elems.begin();
|
|
|
|
i != slice.elems.end(); i++)
|
|
|
|
decl.insert(i->id);
|
|
|
|
|
|
|
|
for (FSIds::const_iterator i = slice.roots.begin();
|
|
|
|
i != slice.roots.end(); i++)
|
|
|
|
if (decl.find(*i) == decl.end())
|
|
|
|
throw Error(format("undefined id: %1%") % (string) *i);
|
|
|
|
|
|
|
|
for (SliceElems::const_iterator i = slice.elems.begin();
|
|
|
|
i != slice.elems.end(); i++)
|
|
|
|
for (FSIds::const_iterator j = i->refs.begin();
|
|
|
|
j != i->refs.end(); j++)
|
|
|
|
if (decl.find(*j) == decl.end())
|
|
|
|
throw Error(format("undefined id: %1%") % (string) *j);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void realiseSlice(const Slice & slice)
|
2003-07-15 16:28:54 +00:00
|
|
|
{
|
|
|
|
debug(format("realising slice"));
|
|
|
|
Nest nest(true);
|
|
|
|
|
|
|
|
/* Perhaps all paths already contain the right id? */
|
|
|
|
|
|
|
|
bool missing = false;
|
2003-07-15 21:24:05 +00:00
|
|
|
for (SliceElems::const_iterator i = slice.elems.begin();
|
2003-07-15 16:28:54 +00:00
|
|
|
i != slice.elems.end(); i++)
|
|
|
|
{
|
|
|
|
SliceElem elem = *i;
|
|
|
|
string id;
|
|
|
|
if (!queryDB(nixDB, dbPath2Id, elem.path, id)) {
|
|
|
|
if (pathExists(elem.path))
|
|
|
|
throw Error(format("path `%1%' obstructed") % elem.path);
|
|
|
|
missing = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (parseHash(id) != elem.id)
|
|
|
|
throw Error(format("path `%1%' obstructed") % elem.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!missing) {
|
|
|
|
debug(format("already installed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For each element, expand its id at its path. */
|
2003-07-15 21:24:05 +00:00
|
|
|
for (SliceElems::const_iterator i = slice.elems.begin();
|
2003-07-15 16:28:54 +00:00
|
|
|
i != slice.elems.end(); i++)
|
|
|
|
{
|
|
|
|
SliceElem elem = *i;
|
|
|
|
expandId(elem.id, elem.path);
|
|
|
|
}
|
|
|
|
}
|
2003-07-15 22:28:27 +00:00
|
|
|
|
|
|
|
|
2003-07-16 11:05:59 +00:00
|
|
|
Strings fstatePaths(const FSId & id, bool normalise)
|
2003-07-15 22:28:27 +00:00
|
|
|
{
|
|
|
|
Strings paths;
|
|
|
|
|
|
|
|
FState fs = termFromId(id);
|
|
|
|
|
|
|
|
ATermList outs, ins, bnds;
|
|
|
|
char * builder;
|
|
|
|
char * platform;
|
|
|
|
|
2003-07-16 08:30:26 +00:00
|
|
|
if (normalise ||
|
|
|
|
(ATgetType(fs) == AT_APPL &&
|
|
|
|
(string) ATgetName(ATgetAFun(fs)) == "Slice"))
|
2003-07-15 22:28:27 +00:00
|
|
|
{
|
2003-07-16 08:30:26 +00:00
|
|
|
Slice slice;
|
|
|
|
if (normalise)
|
|
|
|
slice = normaliseFState(id);
|
|
|
|
else
|
|
|
|
slice = parseSlice(fs);
|
2003-07-15 22:28:27 +00:00
|
|
|
|
|
|
|
/* !!! fix complexity */
|
|
|
|
for (FSIds::const_iterator i = slice.roots.begin();
|
|
|
|
i != slice.roots.end(); i++)
|
|
|
|
for (SliceElems::const_iterator j = slice.elems.begin();
|
|
|
|
j != slice.elems.end(); j++)
|
|
|
|
if (*i == j->id) paths.push_back(j->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (ATmatch(fs, "Derive([<list>], [<list>], <str>, <str>, [<list>])",
|
|
|
|
&outs, &ins, &builder, &platform, &bnds))
|
|
|
|
{
|
|
|
|
while (!ATisEmpty(outs)) {
|
|
|
|
ATerm t = ATgetFirst(outs);
|
|
|
|
char * s1, * s2;
|
|
|
|
if (!ATmatch(t, "(<str>, <str>)", &s1, &s2))
|
|
|
|
throw badTerm("string expected", t);
|
|
|
|
paths.push_back(s1);
|
|
|
|
outs = ATgetNext(outs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else throw badTerm("in fstatePaths", fs);
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|
2003-07-16 11:05:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
StringSet fstateRefs(const FSId & id)
|
|
|
|
{
|
|
|
|
StringSet paths;
|
|
|
|
Slice slice = normaliseFState2(id, paths);
|
|
|
|
for (SliceElems::const_iterator i = slice.elems.begin();
|
|
|
|
i != slice.elems.end(); i++)
|
|
|
|
paths.insert(i->path);
|
|
|
|
return paths;
|
|
|
|
}
|