diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 657c3bf71..7d179da8e 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -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(dstPath)); } diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index fc62a9993..bb53caacc 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -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(), ""); } diff --git a/src/libstore/local-store.hh b/src/libstore/local-store.hh index 3d47446f6..77e46fc3c 100644 --- a/src/libstore/local-store.hh +++ b/src/libstore/local-store.hh @@ -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, diff --git a/src/libstore/remote-store.cc b/src/libstore/remote-store.cc index f79b22310..d83d501ee 100644 --- a/src/libstore/remote-store.cc +++ b/src/libstore/remote-store.cc @@ -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); diff --git a/src/libstore/remote-store.hh b/src/libstore/remote-store.hh index cb9124a4c..717da7377 100644 --- a/src/libstore/remote-store.hh +++ b/src/libstore/remote-store.hh @@ -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, diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index fe4ecfad5..bd330a6da 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -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 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); diff --git a/src/libstore/store-api.hh b/src/libstore/store-api.hh index adfd40a91..1c6f73728 100644 --- a/src/libstore/store-api.hh +++ b/src/libstore/store-api.hh @@ -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 computeStorePathForPath(const Path & srcPath, - bool recursive = true, string hashAlgo = "sha256", + bool recursive = true, HashType hashAlgo = htSHA256, PathFilter & filter = defaultPathFilter); /* Preparatory part of addTextToStore(). diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index 70604a50a..eef01fe4d 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -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"); +} + + } diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh index c3932b11b..062d97254 100644 --- a/src/libutil/hash.hh +++ b/src/libutil/hash.hh @@ -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; diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index e03e82221..065d7d4c0 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -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); } diff --git a/src/nix-worker/nix-worker.cc b/src/nix-worker/nix-worker.cc index e4fe94926..c956f839e 100644 --- a/src/nix-worker/nix-worker.cc +++ b/src/nix-worker/nix-worker.cc @@ -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);