#include "references.hh" #include "hash.hh" #include "util.hh" #include "archive.hh" #include #include namespace nix { static unsigned int refLength = 32; /* characters */ static void search(const unsigned char * s, unsigned int len, StringSet & hashes, StringSet & seen) { static bool initialised = false; static bool isBase32[256]; if (!initialised) { for (unsigned int i = 0; i < 256; ++i) isBase32[i] = false; for (unsigned int i = 0; i < base32Chars.size(); ++i) isBase32[(unsigned char) base32Chars[i]] = true; initialised = true; } for (unsigned int i = 0; i + refLength <= len; ) { int j; bool match = true; for (j = refLength - 1; j >= 0; --j) if (!isBase32[(unsigned char) s[i + j]]) { i += j + 1; match = false; break; } if (!match) continue; string ref((const char *) s + i, refLength); if (hashes.find(ref) != hashes.end()) { debug(format("found reference to `%1%' at offset `%2%'") % ref % i); seen.insert(ref); hashes.erase(ref); } ++i; } } struct RefScanSink : Sink { HashSink hashSink; StringSet hashes; StringSet seen; string tail; RefScanSink() : hashSink(htSHA256) { } void operator () (const unsigned char * data, size_t len); }; void RefScanSink::operator () (const unsigned char * data, size_t len) { hashSink(data, len); /* It's possible that a reference spans the previous and current fragment, so search in the concatenation of the tail of the previous fragment and the start of the current fragment. */ string s = tail + string((const char *) data, len > refLength ? refLength : len); search((const unsigned char *) s.data(), s.size(), hashes, seen); search(data, len, hashes, seen); unsigned int tailLen = len <= refLength ? len : refLength; tail = string(tail, tail.size() < refLength - tailLen ? 0 : tail.size() - (refLength - tailLen)) + string((const char *) data + len - tailLen, tailLen); } PathSet scanForReferences(const string & path, const PathSet & refs, HashResult & hash) { RefScanSink sink; std::map backMap; /* For efficiency (and a higher hit rate), just search for the hash part of the file name. (This assumes that all references have the form `HASH-bla'). */ foreach (PathSet::const_iterator, i, refs) { string baseName = baseNameOf(*i); string::size_type pos = baseName.find('-'); if (pos == string::npos) throw Error(format("bad reference `%1%'") % *i); string s = string(baseName, 0, pos); assert(s.size() == refLength); assert(backMap.find(s) == backMap.end()); // parseHash(htSHA256, s); sink.hashes.insert(s); backMap[s] = *i; } /* Look for the hashes in the NAR dump of the path. */ dumpPath(path, sink); /* Map the hashes found back to their store paths. */ PathSet found; foreach (StringSet::iterator, i, sink.seen) { std::map::iterator j; if ((j = backMap.find(*i)) == backMap.end()) abort(); found.insert(j->second); } hash = sink.hashSink.finish(); return found; } } /builders.scm, tests/challenge.scm, tests/cpan.scm, tests/cpio.scm, tests/cran.scm, tests/cve.scm, tests/derivations.scm, tests/elpa.scm, tests/file-systems.scm, tests/gem.scm, tests/gexp.scm, tests/gnu-maintenance.scm, tests/grafts.scm, tests/graph.scm, tests/gremlin.scm, tests/hackage.scm, tests/hash.scm, tests/import-utils.scm, tests/lint.scm, tests/monads.scm, tests/nar.scm, tests/packages.scm, tests/pk-crypto.scm, tests/pki.scm, tests/profiles.scm, tests/publish.scm, tests/pypi.scm, tests/records.scm, tests/scripts-build.scm, tests/scripts.scm, tests/services.scm, tests/sets.scm, tests/size.scm, tests/snix.scm, tests/store.scm, tests/substitute.scm, tests/syscalls.scm, tests/system.scm, tests/ui.scm, tests/union.scm, tests/upstream.scm, tests/utils.scm: Don't exit at the end of test groups. * tests/containers.scm: Likewise. Use 'test-skip' instead of exiting with error code 77. Mathieu Lirzin 2016-03-06tests: Disable grafting by default for most tests....This allows tests to run as expected even in the presence of replacements among the bootstrap packages, such as Perl (commit d8173f21f7b4e3cb83541b8fa70621d2b6d4ce1c). * tests/cpan.scm: Add (%graft? #f). * tests/derivations.scm: Likewise. * tests/graph.scm: Likewise. * tests/monads.scm: Likewise. * tests/profiles.scm: Likewise. * tests/gexp.scm: Likewise. ("gexp->derivation vs. grafts"): Explicitly reenable grafting before, and disable it after, using 'set-grafting'. Ludovic Courtès 2015-09-04monads: Fix 'liftN' fallback case....Reported by Andy Wingo <wingo@igalia.com>. * guix/monads.scm (define-lift) <fallback case>: Add missing #'. Remove extra formal parameter. * tests/monads.scm ("lift"): Add test with 'lift1' as a procedure. Ludovic Courtès 2015-06-08monads: Allow n-ary '>>=' expressions....Suggested by Federico Beffa <beffa@fbengineering.ch>. * guix/monads.scm (bind-syntax): New macro. (with-monad): Use it instead of 'identifier-syntax'. * tests/monads.scm (">>= with more than two arguments"): New test. * doc/guix.texi (The Store Monad): Explain that there can be several MPROC. Add an example. Ludovic Courtès 2015-05-27monads: 'foldm', 'mapm', and 'anym' now take a list of regular values....* guix/monads.scm (foldm, mapm, anym): Change to take a list of regular values as is customary. * tests/monads.scm ("mapm", "anym"): Adjust accordingly. Ludovic Courtès