* Pass HashType values instead of strings.

This commit is contained in:
Eelco Dolstra 2008-12-03 16:10:17 +00:00
parent 1307b22223
commit ff762fb499
11 changed files with 36 additions and 23 deletions

View file

@ -400,7 +400,7 @@ static Expr prim_derivationStrict(EvalState & state, const ATermVector & args)
% outputHash % outputHashAlgo);
string s = outputHash;
outputHash = printHash(h);
outPath = makeFixedOutputPath(outputHashRecursive, outputHashAlgo, h, drvName);
outPath = makeFixedOutputPath(outputHashRecursive, ht, h, drvName);
if (outputHashRecursive) outputHashAlgo = "r:" + outputHashAlgo;
}
@ -634,8 +634,8 @@ static Expr prim_filterSource(EvalState & state, const ATermVector & args)
FilterFromExpr filter(state, args[0]);
Path dstPath = readOnlyMode
? computeStorePathForPath(path, true, "sha256", filter).first
: store->addToStore(path, true, "sha256", filter);
? computeStorePathForPath(path, true, htSHA256, filter).first
: store->addToStore(path, true, htSHA256, filter);
return makeStr(dstPath, singleton<PathSet>(dstPath));
}

View file

@ -656,7 +656,7 @@ void LocalStore::invalidatePath(const Path & path)
Path LocalStore::addToStore(const Path & _srcPath,
bool recursive, string hashAlgo, PathFilter & filter)
bool recursive, HashType hashAlgo, PathFilter & filter)
{
Path srcPath(absPath(_srcPath));
debug(format("adding `%1%' to the store") % srcPath);
@ -670,7 +670,7 @@ Path LocalStore::addToStore(const Path & _srcPath,
else
sink.s = readFile(srcPath);
Hash h = hashString(parseHashType(hashAlgo), sink.s);
Hash h = hashString(hashAlgo, sink.s);
Path dstPath = makeFixedOutputPath(recursive, hashAlgo, h, baseNameOf(srcPath));
@ -700,7 +700,7 @@ Path LocalStore::addToStore(const Path & _srcPath,
above (if called with recursive == true and hashAlgo ==
sha256); otherwise, compute it here. */
registerValidPath(dstPath,
(recursive && hashAlgo == "sha256") ? h :
(recursive && hashAlgo == htSHA256) ? h :
(recursive ? hashString(htSHA256, sink.s) : hashPath(htSHA256, dstPath)),
PathSet(), "");
}

View file

@ -90,7 +90,7 @@ public:
const Path & path, SubstitutablePathInfo & info);
Path addToStore(const Path & srcPath,
bool recursive = true, string hashAlgo = "sha256",
bool recursive = true, HashType hashAlgo = htSHA256,
PathFilter & filter = defaultPathFilter);
Path addTextToStore(const string & name, const string & s,

View file

@ -279,16 +279,16 @@ Path RemoteStore::queryDeriver(const Path & path)
Path RemoteStore::addToStore(const Path & _srcPath,
bool recursive, string hashAlgo, PathFilter & filter)
bool recursive, HashType hashAlgo, PathFilter & filter)
{
Path srcPath(absPath(_srcPath));
writeInt(wopAddToStore, to);
writeString(baseNameOf(srcPath), to);
/* backwards compatibility hack */
writeInt((hashAlgo == "sha256" && recursive) ? 0 : 1, to);
writeInt((hashAlgo == htSHA256 && recursive) ? 0 : 1, to);
writeInt(recursive ? 1 : 0, to);
writeString(hashAlgo, to);
writeString(printHashType(hashAlgo), to);
dumpPath(srcPath, to, filter);
processStderr();
return readStorePath(from);

View file

@ -43,7 +43,7 @@ public:
SubstitutablePathInfo & info);
Path addToStore(const Path & srcPath,
bool recursive = true, string hashAlgo = "sha256",
bool recursive = true, HashType hashAlgo = htSHA256,
PathFilter & filter = defaultPathFilter);
Path addTextToStore(const string & name, const string & s,

View file

@ -183,20 +183,21 @@ Path makeStorePath(const string & type,
Path makeFixedOutputPath(bool recursive,
string hashAlgo, Hash hash, string name)
HashType hashAlgo, Hash hash, string name)
{
return hashAlgo == "sha256" && recursive
return hashAlgo == htSHA256 && recursive
? makeStorePath("source", hash, name)
: makeStorePath("output:out", hashString(htSHA256,
"fixed:out:" + (recursive ? (string) "r:" : "") + hashAlgo + ":" + printHash(hash) + ":"),
"fixed:out:" + (recursive ? (string) "r:" : "") +
printHashType(hashAlgo) + ":" + printHash(hash) + ":"),
name);
}
std::pair<Path, Hash> computeStorePathForPath(const Path & srcPath,
bool recursive, string hashAlgo, PathFilter & filter)
bool recursive, HashType hashAlgo, PathFilter & filter)
{
HashType ht(parseHashType(hashAlgo));
HashType ht(hashAlgo);
Hash h = recursive ? hashPath(ht, srcPath, filter) : hashFile(ht, srcPath);
string name = baseNameOf(srcPath);
Path dstPath = makeFixedOutputPath(recursive, hashAlgo, h, name);

View file

@ -174,7 +174,7 @@ public:
object `filter' can be used to exclude files (see
libutil/archive.hh). */
virtual Path addToStore(const Path & srcPath,
bool recursive = true, string hashAlgo = "sha256",
bool recursive = true, HashType hashAlgo = htSHA256,
PathFilter & filter = defaultPathFilter) = 0;
/* Like addToStore, but the contents written to the output path is
@ -277,7 +277,7 @@ Path makeStorePath(const string & type,
const Hash & hash, const string & name);
Path makeFixedOutputPath(bool recursive,
string hashAlgo, Hash hash, string name);
HashType hashAlgo, Hash hash, string name);
/* This is the preparatory part of addToStore() and addToStoreFixed();
@ -285,7 +285,7 @@ Path makeFixedOutputPath(bool recursive,
Returns the store path and the cryptographic hash of the
contents of srcPath. */
std::pair<Path, Hash> computeStorePathForPath(const Path & srcPath,
bool recursive = true, string hashAlgo = "sha256",
bool recursive = true, HashType hashAlgo = htSHA256,
PathFilter & filter = defaultPathFilter);
/* Preparatory part of addTextToStore().

View file

@ -335,4 +335,13 @@ HashType parseHashType(const string & s)
}
string printHashType(HashType ht)
{
if (ht == htMD5) return "md5";
else if (ht == htSHA1) return "sha1";
else if (ht == htSHA256) return "sha256";
else throw Error("cannot print unknown hash type");
}
}

View file

@ -82,6 +82,9 @@ Hash compressHash(const Hash & hash, unsigned int newSize);
/* Parse a string representing a hash type. */
HashType parseHashType(const string & s);
/* And the reverse. */
string printHashType(HashType ht);
union Ctx;

View file

@ -125,7 +125,7 @@ static void opAddFixed(Strings opFlags, Strings opArgs)
if (opArgs.empty())
throw UsageError("first argument must be hash algorithm");
string hashAlgo = opArgs.front();
HashType hashAlgo = parseHashType(opArgs.front());
opArgs.pop_front();
for (Strings::iterator i = opArgs.begin(); i != opArgs.end(); ++i)
@ -155,13 +155,13 @@ static void opPrintFixedPath(Strings opFlags, Strings opArgs)
throw UsageError(format("`--print-fixed-path' requires three arguments"));
Strings::iterator i = opArgs.begin();
string hashAlgo = *i++;
HashType hashAlgo = parseHashType(*i++);
string hash = *i++;
string name = *i++;
cout << format("%1%\n") %
makeFixedOutputPath(recursive, hashAlgo,
parseHash16or32(parseHashType(hashAlgo), hash), name);
parseHash16or32(hashAlgo, hash), name);
}

View file

@ -292,7 +292,7 @@ static void performOp(unsigned int clientVersion,
string baseName = readString(from);
readInt(from); /* obsolete; was `fixed' flag */
bool recursive = readInt(from) == 1;
string hashAlgo = readString(from);
HashType hashAlgo = parseHashType(readString(from));
Path tmp = createTempDir();
AutoDelete delTmp(tmp);