* Change the abstract syntax of slices. It used to be that ids were used as

keys to reference slice elements, e.g.,

    Slice(["1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["8c99..."]), ...])

  This was wrong, since ids represent contents, not locations.  Therefore we
  now have:

    Slice(["/nix/store/1ef7..."], [("/nix/store/1ef7...-foo", "1ef7", ["/nix/store/8c99-..."]), ...])

* Fix a bug in the computation of slice closures that could cause slice
  elements to be duplicated.
This commit is contained in:
Eelco Dolstra 2003-08-20 12:39:56 +00:00
parent 710175e6a0
commit 624c48260f
4 changed files with 59 additions and 61 deletions

View file

@ -268,7 +268,7 @@ static Expr evalExpr2(EvalState & state, Expr e)
elem.id = id; elem.id = id;
FState fs; FState fs;
fs.type = FState::fsSlice; fs.type = FState::fsSlice;
fs.slice.roots.push_back(id); fs.slice.roots.push_back(dstPath);
fs.slice.elems.push_back(elem); fs.slice.elems.push_back(elem);
Hash pkgHash = hashPackage(state, fs); Hash pkgHash = hashPackage(state, fs);

View file

@ -52,15 +52,15 @@ FSId writeTerm(ATerm t, const string & suffix, FSId id)
} }
static void parseIds(ATermList ids, FSIds & out) static void parsePaths(ATermList paths, Strings & out)
{ {
while (!ATisEmpty(ids)) { while (!ATisEmpty(paths)) {
char * s; char * s;
ATerm id = ATgetFirst(ids); ATerm t = ATgetFirst(paths);
if (!ATmatch(id, "<str>", &s)) if (!ATmatch(t, "<str>", &s))
throw badTerm("not an id", id); throw badTerm("not a path", t);
out.push_back(parseHash(s)); out.push_back(s);
ids = ATgetNext(ids); paths = ATgetNext(paths);
} }
} }
@ -70,22 +70,24 @@ static void checkSlice(const Slice & slice)
if (slice.elems.size() == 0) if (slice.elems.size() == 0)
throw Error("empty slice"); throw Error("empty slice");
FSIdSet decl; StringSet decl;
for (SliceElems::const_iterator i = slice.elems.begin(); for (SliceElems::const_iterator i = slice.elems.begin();
i != slice.elems.end(); i++) i != slice.elems.end(); i++)
decl.insert(i->id); decl.insert(i->path);
for (FSIds::const_iterator i = slice.roots.begin(); for (Strings::const_iterator i = slice.roots.begin();
i != slice.roots.end(); i++) i != slice.roots.end(); i++)
if (decl.find(*i) == decl.end()) if (decl.find(*i) == decl.end())
throw Error(format("undefined id: %1%") % (string) *i); throw Error(format("undefined root path `%1%'") % *i);
for (SliceElems::const_iterator i = slice.elems.begin(); for (SliceElems::const_iterator i = slice.elems.begin();
i != slice.elems.end(); i++) i != slice.elems.end(); i++)
for (FSIds::const_iterator j = i->refs.begin(); for (Strings::const_iterator j = i->refs.begin();
j != i->refs.end(); j++) j != i->refs.end(); j++)
if (decl.find(*j) == decl.end()) if (decl.find(*j) == decl.end())
throw Error(format("undefined id: %1%") % (string) *j); throw Error(
format("undefined path `%1%' referenced by `%2%'")
% *j % i->path);
} }
@ -97,7 +99,7 @@ static bool parseSlice(ATerm t, Slice & slice)
if (!ATmatch(t, "Slice([<list>], [<list>])", &roots, &elems)) if (!ATmatch(t, "Slice([<list>], [<list>])", &roots, &elems))
return false; return false;
parseIds(roots, slice.roots); parsePaths(roots, slice.roots);
while (!ATisEmpty(elems)) { while (!ATisEmpty(elems)) {
char * s1, * s2; char * s1, * s2;
@ -108,7 +110,7 @@ static bool parseSlice(ATerm t, Slice & slice)
SliceElem elem; SliceElem elem;
elem.path = s1; elem.path = s1;
elem.id = parseHash(s2); elem.id = parseHash(s2);
parseIds(refs, elem.refs); parsePaths(refs, elem.refs);
slice.elems.push_back(elem); slice.elems.push_back(elem);
elems = ATgetNext(elems); elems = ATgetNext(elems);
} }
@ -143,7 +145,14 @@ static bool parseDerive(ATerm t, Derive & derive)
outs = ATgetNext(outs); outs = ATgetNext(outs);
} }
parseIds(ins, derive.inputs); while (!ATisEmpty(ins)) {
char * s;
ATerm t = ATgetFirst(ins);
if (!ATmatch(t, "<str>", &s))
throw badTerm("not an id", t);
derive.inputs.push_back(parseHash(s));
ins = ATgetNext(ins);
}
derive.builder = builder; derive.builder = builder;
derive.platform = platform; derive.platform = platform;
@ -182,20 +191,19 @@ FState parseFState(ATerm t)
} }
static ATermList unparseIds(const FSIds & ids) static ATermList unparsePaths(const Strings & paths)
{ {
ATermList l = ATempty; ATermList l = ATempty;
for (FSIds::const_iterator i = ids.begin(); for (Strings::const_iterator i = paths.begin();
i != ids.end(); i++) i != paths.end(); i++)
l = ATinsert(l, l = ATinsert(l, ATmake("<str>", i->c_str()));
ATmake("<str>", ((string) *i).c_str()));
return ATreverse(l); return ATreverse(l);
} }
static ATerm unparseSlice(const Slice & slice) static ATerm unparseSlice(const Slice & slice)
{ {
ATermList roots = unparseIds(slice.roots); ATermList roots = unparsePaths(slice.roots);
ATermList elems = ATempty; ATermList elems = ATempty;
for (SliceElems::const_iterator i = slice.elems.begin(); for (SliceElems::const_iterator i = slice.elems.begin();
@ -204,7 +212,7 @@ static ATerm unparseSlice(const Slice & slice)
ATmake("(<str>, <str>, <term>)", ATmake("(<str>, <str>, <term>)",
i->path.c_str(), i->path.c_str(),
((string) i->id).c_str(), ((string) i->id).c_str(),
unparseIds(i->refs))); unparsePaths(i->refs)));
return ATmake("Slice(<term>, <term>)", roots, elems); return ATmake("Slice(<term>, <term>)", roots, elems);
} }
@ -219,6 +227,11 @@ static ATerm unparseDerive(const Derive & derive)
ATmake("(<str>, <str>)", ATmake("(<str>, <str>)",
i->first.c_str(), ((string) i->second).c_str())); i->first.c_str(), ((string) i->second).c_str()));
ATermList ins = ATempty;
for (FSIds::const_iterator i = derive.inputs.begin();
i != derive.inputs.end(); i++)
ins = ATinsert(ins, ATmake("<str>", ((string) *i).c_str()));
ATermList args = ATempty; ATermList args = ATempty;
for (Strings::const_iterator i = derive.args.begin(); for (Strings::const_iterator i = derive.args.begin();
i != derive.args.end(); i++) i != derive.args.end(); i++)
@ -233,7 +246,7 @@ static ATerm unparseDerive(const Derive & derive)
return ATmake("Derive(<term>, <term>, <str>, <str>, <term>, <term>)", return ATmake("Derive(<term>, <term>, <str>, <str>, <term>, <term>)",
ATreverse(outs), ATreverse(outs),
unparseIds(derive.inputs), ATreverse(ins),
derive.platform.c_str(), derive.platform.c_str(),
derive.builder.c_str(), derive.builder.c_str(),
ATreverse(args), ATreverse(args),

View file

@ -16,14 +16,14 @@ struct SliceElem
{ {
string path; string path;
FSId id; FSId id;
FSIds refs; Strings refs;
}; };
typedef list<SliceElem> SliceElems; typedef list<SliceElem> SliceElems;
struct Slice struct Slice
{ {
FSIds roots; Strings roots;
SliceElems elems; SliceElems elems;
}; };

View file

@ -101,7 +101,7 @@ FSId normaliseFState(FSId id, FSIdSet pending)
if (id2 != id) { if (id2 != id) {
FState fs = parseFState(termFromId(id2)); FState fs = parseFState(termFromId(id2));
debug(format("skipping build of %1%, someone beat us to it") debug(format("skipping build of %1%, someone beat us to it")
% (string) id); % (string) id);
if (fs.type != FState::fsSlice) abort(); if (fs.type != FState::fsSlice) abort();
return id2; return id2;
} }
@ -110,7 +110,7 @@ FSId normaliseFState(FSId id, FSIdSet pending)
/* Right platform? */ /* Right platform? */
if (fs.derive.platform != thisSystem) if (fs.derive.platform != thisSystem)
throw Error(format("a `%1%' is required, but I am a `%2%'") throw Error(format("a `%1%' is required, but I am a `%2%'")
% fs.derive.platform % thisSystem); % fs.derive.platform % thisSystem);
/* Realise inputs (and remember all input paths). */ /* Realise inputs (and remember all input paths). */
for (FSIds::iterator i = fs.derive.inputs.begin(); for (FSIds::iterator i = fs.derive.inputs.begin();
@ -149,7 +149,7 @@ FSId normaliseFState(FSId id, FSIdSet pending)
expandId(i->second, i->first, "/", pending); expandId(i->second, i->first, "/", pending);
} catch (Error & e) { } catch (Error & e) {
debug(format("fast build failed for `%1%': %2%") debug(format("fast build failed for `%1%': %2%")
% i->first % e.what()); % i->first % e.what());
fastBuild = false; fastBuild = false;
break; break;
} }
@ -189,7 +189,7 @@ FSId normaliseFState(FSId id, FSIdSet pending)
string path = i->first; string path = i->first;
if (!pathExists(path)) if (!pathExists(path))
throw Error(format("path `%1%' does not exist") % path); throw Error(format("path `%1%' does not exist") % path);
fs.slice.roots.push_back(i->second); fs.slice.roots.push_back(path);
/* For this output path, find the references to other paths contained /* For this output path, find the references to other paths contained
in it. */ in it. */
@ -201,7 +201,7 @@ FSId normaliseFState(FSId id, FSIdSet pending)
elem.id = i->second; elem.id = i->second;
/* For each path referenced by this output path, add its id to the /* For each path referenced by this output path, add its id to the
slice element and add the id to the `used' set (so that the slice element and add the id to the `usedPaths' set (so that the
elements referenced by *its* slice are added below). */ elements referenced by *its* slice are added below). */
for (Strings::iterator j = refPaths.begin(); for (Strings::iterator j = refPaths.begin();
j != refPaths.end(); j++) j != refPaths.end(); j++)
@ -210,18 +210,12 @@ FSId normaliseFState(FSId id, FSIdSet pending)
ElemMap::iterator k; ElemMap::iterator k;
OutPaths::iterator l; OutPaths::iterator l;
/* Is it an input path? */ elem.refs.push_back(path);
if ((k = inMap.find(path)) != inMap.end()) {
elem.refs.push_back(k->second.id); if ((k = inMap.find(path)) != inMap.end())
usedPaths.insert(k->second.path); usedPaths.insert(k->second.path);
} else if ((l = outPaths.find(path)) == outPaths.end())
abort();
/* Or an output path? */
else if ((l = outPaths.find(path)) != outPaths.end())
elem.refs.push_back(l->second);
/* Can't happen. */
else abort();
} }
fs.slice.elems.push_back(elem); fs.slice.elems.push_back(elem);
@ -229,40 +223,31 @@ FSId normaliseFState(FSId id, FSIdSet pending)
/* Close the slice. That is, for any referenced path, add the paths /* Close the slice. That is, for any referenced path, add the paths
referenced by it. */ referenced by it. */
FSIdSet donePaths; StringSet donePaths;
while (!usedPaths.empty()) { while (!usedPaths.empty()) {
StringSet::iterator i = usedPaths.begin(); StringSet::iterator i = usedPaths.begin();
string path = *i; string path = *i;
usedPaths.erase(i); usedPaths.erase(i);
if (donePaths.find(path) != donePaths.end()) continue;
donePaths.insert(path);
ElemMap::iterator j = inMap.find(path); ElemMap::iterator j = inMap.find(path);
if (j == inMap.end()) abort(); if (j == inMap.end()) abort();
donePaths.insert(j->second.id);
fs.slice.elems.push_back(j->second); fs.slice.elems.push_back(j->second);
for (FSIds::iterator k = j->second.refs.begin(); for (Strings::iterator k = j->second.refs.begin();
k != j->second.refs.end(); k++) k != j->second.refs.end(); k++)
if (donePaths.find(*k) == donePaths.end()) { usedPaths.insert(*k);
/* !!! performance */
bool found = false;
for (ElemMap::iterator l = inMap.begin();
l != inMap.end(); l++)
if (l->second.id == *k) {
usedPaths.insert(l->first);
found = true;
}
if (!found) abort();
}
} }
/* For debugging, print out the referenced and unreferenced paths. */ /* For debugging, print out the referenced and unreferenced paths. */
for (ElemMap::iterator i = inMap.begin(); for (ElemMap::iterator i = inMap.begin();
i != inMap.end(); i++) i != inMap.end(); i++)
{ {
FSIdSet::iterator j = donePaths.find(i->second.id); StringSet::iterator j = donePaths.find(i->second.path);
if (j == donePaths.end()) if (j == donePaths.end())
debug(format("NOT referenced: `%1%'") % i->second.path); debug(format("NOT referenced: `%1%'") % i->second.path);
else else
@ -319,11 +304,11 @@ Strings fstatePaths(const FSId & id)
if (fs.type == FState::fsSlice) { if (fs.type == FState::fsSlice) {
/* !!! fix complexity */ /* !!! fix complexity */
for (FSIds::const_iterator i = fs.slice.roots.begin(); for (Strings::const_iterator i = fs.slice.roots.begin();
i != fs.slice.roots.end(); i++) i != fs.slice.roots.end(); i++)
for (SliceElems::const_iterator j = fs.slice.elems.begin(); for (SliceElems::const_iterator j = fs.slice.elems.begin();
j != fs.slice.elems.end(); j++) j != fs.slice.elems.end(); j++)
if (*i == j->id) paths.push_back(j->path); if (*i == j->path) paths.push_back(j->path);
} }
else if (fs.type == FState::fsDerive) { else if (fs.type == FState::fsDerive) {