* Refactoring to support SHA-1.

This commit is contained in:
Eelco Dolstra 2005-01-13 15:44:44 +00:00
parent d46b4262dc
commit 73992371a3
6 changed files with 36 additions and 18 deletions

View file

@ -60,7 +60,7 @@ static Path copyAtom(EvalState & state, const Path & srcPath)
Hash drvHash = hashDerivation(state, ne); Hash drvHash = hashDerivation(state, ne);
Path drvPath = writeTerm(unparseStoreExpr(ne), ""); Path drvPath = writeTerm(unparseStoreExpr(ne), "");
state.drvHashes[drvPath] = drvHash; state.drvHashes.insert(make_pair(drvPath, drvHash));
state.drvRoots[drvPath] = ne.closure.roots; state.drvRoots[drvPath] = ne.closure.roots;
@ -118,7 +118,7 @@ static void processBinding(EvalState & state, Expr e, StoreExpr & ne,
PathSet drvRoots; PathSet drvRoots;
drvRoots.insert(evalPath(state, a)); drvRoots.insert(evalPath(state, a));
state.drvHashes[drvPath] = drvHash; state.drvHashes.insert(make_pair(drvPath, drvHash));
state.drvRoots[drvPath] = drvRoots; state.drvRoots[drvPath] = drvRoots;
ss.push_back(addInput(state, drvPath, ne)); ss.push_back(addInput(state, drvPath, ne));
@ -188,7 +188,7 @@ static Expr primDerivation(EvalState & state, const ATermVector & _args)
ne.type = StoreExpr::neDerivation; ne.type = StoreExpr::neDerivation;
string drvName; string drvName;
Hash outHash; Hash outHash(htMD5);
bool outHashGiven = false; bool outHashGiven = false;
for (ATermIterator i(attrs.keys()); i; ++i) { for (ATermIterator i(attrs.keys()); i; ++i) {

View file

@ -416,7 +416,7 @@ Path addToStore(const Path & _srcPath)
Path srcPath(absPath(_srcPath)); Path srcPath(absPath(_srcPath));
debug(format("adding `%1%' to the store") % srcPath); debug(format("adding `%1%' to the store") % srcPath);
Hash h; Hash h(htMD5);
{ {
SwitchToOriginalUser sw; SwitchToOriginalUser sw;
h = hashPath(srcPath); h = hashPath(srcPath);

View file

@ -8,14 +8,18 @@ extern "C" {
#include "archive.hh" #include "archive.hh"
Hash::Hash() Hash::Hash(HashType type)
{ {
memset(hash, 0, sizeof(hash)); this->type = type;
if (type == htMD5) hashSize = md5HashSize;
else if (type == htSHA1) hashSize = sha1HashSize;
memset(hash, 0, hashSize);
} }
bool Hash::operator == (const Hash & h2) const bool Hash::operator == (const Hash & h2) const
{ {
if (hashSize != h2.hashSize) return false;
for (unsigned int i = 0; i < hashSize; i++) for (unsigned int i = 0; i < hashSize; i++)
if (hash[i] != h2.hash[i]) return false; if (hash[i] != h2.hash[i]) return false;
return true; return true;
@ -52,10 +56,10 @@ Hash::operator string() const
Hash parseHash(const string & s) Hash parseHash(const string & s)
{ {
Hash hash; Hash hash(htMD5);
if (s.length() != Hash::hashSize * 2) if (s.length() != hash.hashSize * 2)
throw Error(format("invalid hash `%1%'") % s); throw Error(format("invalid hash `%1%'") % s);
for (unsigned int i = 0; i < Hash::hashSize; i++) { for (unsigned int i = 0; i < hash.hashSize; i++) {
string s2(s, i * 2, 2); string s2(s, i * 2, 2);
if (!isxdigit(s2[0]) || !isxdigit(s2[1])) if (!isxdigit(s2[0]) || !isxdigit(s2[1]))
throw Error(format("invalid hash `%1%'") % s); throw Error(format("invalid hash `%1%'") % s);
@ -83,7 +87,7 @@ bool isHash(const string & s)
Hash hashString(const string & s) Hash hashString(const string & s)
{ {
Hash hash; Hash hash(htMD5);
md5_buffer(s.c_str(), s.length(), hash.hash); md5_buffer(s.c_str(), s.length(), hash.hash);
return hash; return hash;
} }
@ -91,7 +95,7 @@ Hash hashString(const string & s)
Hash hashFile(const Path & path) Hash hashFile(const Path & path)
{ {
Hash hash; Hash hash(htMD5);
FILE * file = fopen(path.c_str(), "rb"); FILE * file = fopen(path.c_str(), "rb");
if (!file) if (!file)
throw SysError(format("file `%1%' does not exist") % path); throw SysError(format("file `%1%' does not exist") % path);
@ -115,7 +119,7 @@ struct HashSink : DumpSink
Hash hashPath(const Path & path) Hash hashPath(const Path & path)
{ {
Hash hash; Hash hash(htMD5);
HashSink sink; HashSink sink;
md5_init_ctx(&sink.ctx); md5_init_ctx(&sink.ctx);
dumpPath(path, sink); dumpPath(path, sink);

View file

@ -8,13 +8,23 @@
using namespace std; using namespace std;
typedef enum { htMD5, htSHA1 } HashType;
const int md5HashSize = 16;
const int sha1HashSize = 20;
struct Hash struct Hash
{ {
static const unsigned int hashSize = 16; static const unsigned int maxHashSize = 20;
unsigned char hash[hashSize]; unsigned int hashSize;
unsigned char hash[maxHashSize];
HashType type;
/* Create a zeroed hash object. */ /* Create a zeroed hash object. */
Hash(); Hash(HashType type);
/* Check whether two hash are equal. */ /* Check whether two hash are equal. */
bool operator == (const Hash & h2) const; bool operator == (const Hash & h2) const;

View file

@ -34,6 +34,10 @@ struct DrvInfo
Path drvPath; Path drvPath;
Path outPath; Path outPath;
Hash drvHash; Hash drvHash;
DrvInfo() : drvHash(htMD5)
{
}
}; };
typedef map<Path, DrvInfo> DrvInfos; typedef map<Path, DrvInfo> DrvInfos;

View file

@ -39,9 +39,9 @@ static string makeNode(const string & id, const string & label,
static string symbolicName(const string & path) static string symbolicName(const string & path)
{ {
string p = baseNameOf(path); string p = baseNameOf(path);
if (isHash(string(p, 0, Hash::hashSize * 2)) && if (isHash(string(p, 0, md5HashSize * 2)) &&
p[Hash::hashSize * 2] == '-') p[md5HashSize * 2] == '-')
p = string(p, Hash::hashSize * 2 + 1); p = string(p, md5HashSize * 2 + 1);
return p; return p;
} }