* Realisation of Derive(...) expressions.

This commit is contained in:
Eelco Dolstra 2003-06-27 14:56:12 +00:00
parent 3da9687854
commit 40b5936691
7 changed files with 193 additions and 165 deletions

View file

@ -18,7 +18,7 @@ typedef map<string, string> Environment;
/* Return true iff the given path exists. */
bool pathExists(string path)
bool pathExists(const string & path)
{
int res;
struct stat st;
@ -30,33 +30,15 @@ bool pathExists(string path)
}
#if 0
/* Compute a derived value by running a program. */
static Hash computeDerived(Hash sourceHash, string targetName,
string platform, Hash prog, Environment env)
/* Run a program. */
static void runProgram(const string & program, Environment env)
{
string targetPath = nixValues + "/" +
(string) sourceHash + "-nf";
/* Check whether the target already exists. */
if (pathExists(targetPath))
throw Error("derived value in " + targetPath + " already exists");
/* Find the program corresponding to the hash `prog'. */
string progPath = queryValuePath(prog);
/* Finalize the environment. */
env["out"] = targetPath;
/* Create a log file. */
string logFileName =
nixLogDir + "/" + baseNameOf(targetPath) + ".log";
string logFileName = nixLogDir + "/run.log";
/* !!! auto-pclose on exit */
FILE * logFile = popen(("tee " + logFileName + " >&2").c_str(), "w"); /* !!! escaping */
if (!logFile)
throw SysError("unable to create log file " + logFileName);
try {
throw SysError(format("unable to create log file %1%") % logFileName);
/* Fork a child to build the package. */
pid_t pid;
@ -107,21 +89,20 @@ static Hash computeDerived(Hash sourceHash, string targetName,
/* Dup the log handle into stderr. */
if (dup2(fileno(logFile), STDERR_FILENO) == -1)
throw Error("cannot pipe standard error into log file: " + string(strerror(errno)));
throw SysError("cannot pipe standard error into log file");
/* Dup stderr to stdin. */
if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1)
throw Error("cannot dup stderr into stdout");
throw SysError("cannot dup stderr into stdout");
/* Make the program executable. !!! hack. */
if (chmod(progPath.c_str(), 0755))
throw Error("cannot make program executable");
if (chmod(program.c_str(), 0755))
throw SysError("cannot make program executable");
/* Execute the program. This should not return. */
execle(progPath.c_str(), baseNameOf(progPath).c_str(), 0, env2);
execle(program.c_str(), baseNameOf(program).c_str(), 0, env2);
throw Error("unable to execute builder: " +
string(strerror(errno)));
throw SysError(format("unable to execute %1%") % program);
} catch (exception & e) {
cerr << "build error: " << e.what() << endl;
@ -144,44 +125,12 @@ static Hash computeDerived(Hash sourceHash, string targetName,
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
throw Error("unable to build package");
/* Check whether the result was created. */
if (!pathExists(targetPath))
throw Error("program " + progPath +
" failed to create a result in " + targetPath);
#if 0
/* Remove write permission from the value. */
int res = system(("chmod -R -w " + targetPath).c_str()); // !!! escaping
if (WEXITSTATUS(res) != 0)
throw Error("cannot remove write permission from " + targetPath);
#endif
} catch (exception &) {
// system(("rm -rf " + targetPath).c_str());
throw;
}
/* Hash the result. */
Hash targetHash = hashPath(targetPath);
/* Register targetHash -> targetPath. !!! this should be in
values.cc. */
setDB(nixDB, dbRefs, targetHash, targetName);
/* Register that targetHash was produced by evaluating
sourceHash; i.e., that targetHash is a normal form of
sourceHash. !!! this shouldn't be here */
setDB(nixDB, dbNFs, sourceHash, targetHash);
return targetHash;
}
#endif
/* Throw an exception if the given platform string is not supported by
the platform we are executing on. */
static void checkPlatform(string platform)
static void checkPlatform(const string & platform)
{
if (platform != thisSystem)
throw Error(format("a `%1%' is required, but I am a `%2%'")
@ -250,33 +199,39 @@ struct RStatus
};
static void realise(RStatus & status, FState fs)
static FState realise(RStatus & status, FState fs)
{
char * s;
char * s1, * s2, * s3;
Content content;
ATermList refs;
ATermList refs, ins, outs, bnds;
if (ATmatch(fs, "File(<str>, <term>, [<list>])", &s, &content, &refs)) {
string path(s);
if (ATmatch(fs, "File(<str>, <term>, [<list>])", &s1, &content, &refs)) {
string path(s1);
if (path[0] != '/') throw Error("absolute path expected: " + path);
/* Realise referenced paths. */
ATermList refs2 = ATempty;
while (!ATisEmpty(refs)) {
realise(status, ATgetFirst(refs));
refs2 = ATappend(refs2, realise(status, ATgetFirst(refs)));
refs = ATgetNext(refs);
}
refs2 = ATreverse(refs2);
if (!ATmatch(content, "Hash(<str>)", &s))
if (!ATmatch(content, "Hash(<str>)", &s1))
throw badTerm("hash expected", content);
Hash hash = parseHash(s);
Hash hash = parseHash(s1);
/* Normal form. */
ATerm nf = ATmake("File(<str>, <term>, <list>)",
path.c_str(), content, refs2);
/* Perhaps the path already exists and has the right hash? */
if (pathExists(path)) {
if (hash == hashPath(path)) {
debug(format("path %1% already has hash %2%")
% path % (string) hash);
return;
return nf;
}
throw Error(format("path %1% exists, but does not have hash %2%")
@ -286,19 +241,79 @@ static void realise(RStatus & status, FState fs)
/* Do we know a path with that hash? If so, copy it. */
string path2 = queryFromStore(hash);
copyFile(path2, path);
return nf;
}
else if (ATmatch(fs, "Derive()")) {
else if (ATmatch(fs, "Derive(<str>, <str>, [<list>], <str>, [<list>])",
&s1, &s2, &ins, &s3, &bnds))
{
string platform(s1), builder(s2), outPath(s3);
checkPlatform(platform);
/* Realise inputs. */
ATermList ins2 = ATempty;
while (!ATisEmpty(ins)) {
ins2 = ATappend(ins2, realise(status, ATgetFirst(ins)));
ins = ATgetNext(ins);
}
ins2 = ATreverse(ins2);
/* Build the environment. */
Environment env;
while (!ATisEmpty(bnds)) {
ATerm bnd = ATgetFirst(bnds);
if (!ATmatch(bnd, "(<str>, <str>)", &s1, &s2))
throw badTerm("string expected", bnd);
env[s1] = s2;
bnds = ATgetNext(bnds);
}
else throw badTerm("bad file system state expression", fs);
/* Check whether the target already exists. */
if (pathExists(outPath))
deleteFromStore(outPath);
// throw Error(format("path %1% already exists") % outPath);
/* Run the builder. */
runProgram(builder, env);
/* Check whether the result was created. */
if (!pathExists(outPath))
throw Error(format("program %1% failed to create a result in %2%")
% builder % outPath);
#if 0
/* Remove write permission from the value. */
int res = system(("chmod -R -w " + targetPath).c_str()); // !!! escaping
if (WEXITSTATUS(res) != 0)
throw Error("cannot remove write permission from " + targetPath);
#endif
/* Hash the result. */
Hash outHash = hashPath(outPath);
/* Register targetHash -> targetPath. !!! this should be in
values.cc. */
setDB(nixDB, dbRefs, outHash, outPath);
#if 0
/* Register that targetHash was produced by evaluating
sourceHash; i.e., that targetHash is a normal form of
sourceHash. !!! this shouldn't be here */
setDB(nixDB, dbNFs, sourceHash, targetHash);
#endif
return ATmake("File(<str>, Hash(<str>), <list>)",
outPath.c_str(), ((string) outHash).c_str(), ins2);
}
throw badTerm("bad file system state expression", fs);
}
void realiseFState(FState fs)
FState realiseFState(FState fs)
{
RStatus status;
realise(status, fs);
return realise(status, fs);
}

View file

@ -23,7 +23,7 @@ using namespace std;
self-referential. This prevents us from having to deal with
cycles.
Derive : String * Path * [FState] * [Path] * [(String, String)] -> [FState]
Derive : String * Path * [FState] * Path * [(String, String)] -> FState
Derive(platform, builder, ins, outs, env) specifies the creation of
new file objects (in paths declared by `outs') by the execution of
@ -61,7 +61,7 @@ typedef ATerm Content;
/* Realise a $f$-normalised expression in the file system. */
void realiseFState(FState fs);
FState realiseFState(FState fs);
/* Return a canonical textual representation of an expression. */
string printTerm(ATerm t);

View file

@ -13,7 +13,9 @@
void realise(FState fs)
{
realiseFState(fs);
cout << format("%1% => %2%\n")
% printTerm(fs)
% printTerm(realiseFState(fs));
}
@ -145,15 +147,25 @@ void runTests()
"File(<str>, Hash(<str>), [])",
builder1fn.c_str(),
((string) builder1h).c_str());
realiseFState(fs1);
realiseFState(fs1);
realise(fs1);
realise(fs1);
FState fs2 = ATmake(
"File(<str>, Hash(<str>), [])",
(builder1fn + "_bla").c_str(),
((string) builder1h).c_str());
realiseFState(fs2);
realiseFState(fs2);
realise(fs2);
realise(fs2);
string out1fn = nixStore + "/hello.txt";
FState fs3 = ATmake(
"Derive(<str>, <str>, [<term>], <str>, [(\"out\", <str>)])",
thisSystem.c_str(),
builder1fn.c_str(),
fs1,
out1fn.c_str(),
out1fn.c_str());
realise(fs3);
#if 0
Expr e1 = ATmake("Exec(Str(<str>), Hash(<str>), [])",

View file

@ -11,10 +11,15 @@
string thisSystem = SYSTEM;
SysError::SysError(string msg)
Error::Error(const format & f)
{
err = f.str();
}
SysError::SysError(const format & f)
: Error(format("%1%: %2%") % f.str() % strerror(errno))
{
char * sysMsg = strerror(errno);
err = msg + ": " + sysMsg;
}

View file

@ -18,22 +18,21 @@ class Error : public exception
protected:
string err;
public:
Error() { }
Error(format f) { err = f.str(); }
~Error() throw () { }
Error(const format & f);
~Error() throw () { };
const char * what() const throw () { return err.c_str(); }
};
class SysError : public Error
{
public:
SysError(string msg);
SysError(const format & f);
};
class UsageError : public Error
{
public:
UsageError(string _err) : Error(_err) { };
UsageError(const format & f) : Error(f) { };
};

View file

@ -127,16 +127,13 @@ string fetchURL(string url)
#endif
void deleteFromStore(Hash hash)
void deleteFromStore(const string & path)
{
string fn;
if (queryDB(nixDB, dbRefs, hash, fn)) {
string prefix = nixStore + "/";
if (string(fn, prefix.size()) != prefix)
throw Error("path " + fn + " is not in the store");
deletePath(fn);
delDB(nixDB, dbRefs, hash);
}
if (string(path, 0, prefix.size()) != prefix)
throw Error(format("path %1% is not in the store") % path);
deletePath(path);
// delDB(nixDB, dbRefs, hash);
}

View file

@ -15,7 +15,7 @@ void copyFile(string src, string dst);
void addToStore(string srcPath, string & dstPath, Hash & hash);
/* Delete a value from the nixStore directory. */
void deleteFromStore(Hash hash);
void deleteFromStore(const string & path);
/* !!! */
string queryFromStore(Hash hash);