lix/src/fstate.cc

231 lines
5.8 KiB
C++
Raw Normal View History

2003-07-07 07:43:58 +00:00
#include "fstate.hh"
#include "globals.hh"
2003-07-07 07:43:58 +00:00
#include "store.hh"
string printTerm(ATerm t)
2003-06-17 13:37:44 +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)
{
return Error(format("%1%, in `%2%'") % f.str() % printTerm(t));
}
Hash hashTerm(ATerm t)
{
return hashString(printTerm(t));
}
2003-07-20 19:29:38 +00:00
ATerm termFromId(const FSId & id)
2003-07-04 12:18:06 +00:00
{
string path = expandId(id);
2003-07-04 12:18:06 +00:00
ATerm t = ATreadFromNamedFile(path.c_str());
if (!t) throw Error(format("cannot read aterm from `%1%'") % path);
2003-07-04 12:18:06 +00:00
return t;
}
FSId writeTerm(ATerm t, const string & suffix, FSId id)
2003-07-04 12:18:06 +00:00
{
/* By default, the id of a term is its hash. */
if (id == 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-20 19:29:38 +00:00
// debug(format("written term %1% = %2%") % (string) id %
// printTerm(t));
Transaction txn(nixDB);
registerPath(txn, path, id);
txn.commit();
return id;
2003-07-04 12:18:06 +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-20 19:29:38 +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);
}
/* Parse a slice. */
2003-07-20 19:29:38 +00:00
static bool parseSlice(ATerm t, Slice & slice)
{
ATermList roots, elems;
2003-07-20 19:29:38 +00:00
if (!ATmatch(t, "Slice([<list>], [<list>])", &roots, &elems))
return false;
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);
}
checkSlice(slice);
2003-07-20 19:29:38 +00:00
return true;
}
2003-07-20 19:29:38 +00:00
static bool parseDerive(ATerm t, Derive & derive)
{
ATermList outs, ins, bnds;
char * builder;
char * platform;
2003-07-20 19:29:38 +00:00
if (!ATmatch(t, "Derive([<list>], [<list>], <str>, <str>, [<list>])",
&outs, &ins, &builder, &platform, &bnds))
return false;
2003-07-20 19:29:38 +00:00
while (!ATisEmpty(outs)) {
char * s1, * s2;
ATerm t = ATgetFirst(outs);
if (!ATmatch(t, "(<str>, <str>)", &s1, &s2))
throw badTerm("not a derive output", t);
derive.outputs.push_back(DeriveOutput(s1, parseHash(s2)));
outs = ATgetNext(outs);
}
2003-07-20 19:29:38 +00:00
parseIds(ins, derive.inputs);
2003-07-20 19:29:38 +00:00
derive.builder = builder;
derive.platform = platform;
while (!ATisEmpty(bnds)) {
char * s1, * s2;
ATerm bnd = ATgetFirst(bnds);
if (!ATmatch(bnd, "(<str>, <str>)", &s1, &s2))
throw badTerm("tuple of strings expected", bnd);
2003-07-20 19:29:38 +00:00
derive.env.push_back(StringPair(s1, s2));
bnds = ATgetNext(bnds);
}
2003-07-20 19:29:38 +00:00
return true;
}
2003-07-20 19:29:38 +00:00
FState parseFState(ATerm t)
2003-07-16 11:05:59 +00:00
{
2003-07-20 19:29:38 +00:00
FState fs;
if (parseSlice(t, fs.slice))
fs.type = FState::fsSlice;
else if (parseDerive(t, fs.derive))
fs.type = FState::fsDerive;
else throw badTerm("not an fstate-expression", t);
return fs;
2003-07-16 11:05:59 +00:00
}
2003-07-20 19:29:38 +00:00
static ATermList unparseIds(const FSIds & ids)
{
2003-07-20 19:29:38 +00:00
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);
}
2003-07-20 19:29:38 +00:00
static ATerm unparseSlice(const Slice & slice)
{
2003-07-20 19:29:38 +00:00
ATermList roots = unparseIds(slice.roots);
ATermList elems = ATempty;
for (SliceElems::const_iterator i = slice.elems.begin();
i != slice.elems.end(); i++)
2003-07-20 19:29:38 +00:00
elems = ATinsert(elems,
ATmake("(<str>, <str>, <term>)",
i->path.c_str(),
((string) i->id).c_str(),
unparseIds(i->refs)));
2003-07-20 19:29:38 +00:00
return ATmake("Slice(<term>, <term>)", roots, elems);
}
2003-07-15 22:28:27 +00:00
2003-07-20 19:29:38 +00:00
static ATerm unparseDerive(const Derive & derive)
2003-07-15 22:28:27 +00:00
{
2003-07-20 19:29:38 +00:00
ATermList outs = ATempty;
for (DeriveOutputs::const_iterator i = derive.outputs.begin();
i != derive.outputs.end(); i++)
outs = ATinsert(outs,
ATmake("(<str>, <str>)",
i->first.c_str(), ((string) i->second).c_str()));
2003-07-15 22:28:27 +00:00
2003-07-20 19:29:38 +00:00
ATermList env = ATempty;
for (StringPairs::const_iterator i = derive.env.begin();
i != derive.env.end(); i++)
env = ATinsert(env,
ATmake("(<str>, <str>)",
i->first.c_str(), i->second.c_str()));
2003-07-15 22:28:27 +00:00
2003-07-20 19:29:38 +00:00
return ATmake("Derive(<term>, <term>, <str>, <str>, <term>)",
ATreverse(outs),
unparseIds(derive.inputs),
derive.builder.c_str(),
derive.platform.c_str(),
ATreverse(env));
2003-07-15 22:28:27 +00:00
}
2003-07-16 11:05:59 +00:00
2003-07-20 19:29:38 +00:00
ATerm unparseFState(const FState & fs)
2003-07-16 11:05:59 +00:00
{
2003-07-20 19:29:38 +00:00
if (fs.type == FState::fsSlice)
return unparseSlice(fs.slice);
else if (fs.type == FState::fsDerive)
return unparseDerive(fs.derive);
else abort();
2003-07-16 11:05:59 +00:00
}