summaryrefslogtreecommitdiff
path: root/apt-pkg/pkgcache.cc
diff options
context:
space:
mode:
authorJulian Andres Klode <jak@debian.org>2015-12-29 15:11:11 +0100
committerJulian Andres Klode <jak@debian.org>2015-12-29 16:37:57 +0100
commit25c7a09d4a207bac875817559580f62c9ee07cb5 (patch)
treec6d92367a3be84be0dee48390505eea51792b6c0 /apt-pkg/pkgcache.cc
parentf161603953f92cc3e778439d7149e5b852f65277 (diff)
Add support for calculating hashes over the entire cache
Diffstat (limited to 'apt-pkg/pkgcache.cc')
-rw-r--r--apt-pkg/pkgcache.cc36
1 files changed, 33 insertions, 3 deletions
diff --git a/apt-pkg/pkgcache.cc b/apt-pkg/pkgcache.cc
index dd25fc51d..347a0954e 100644
--- a/apt-pkg/pkgcache.cc
+++ b/apt-pkg/pkgcache.cc
@@ -39,6 +39,7 @@
#include <vector>
#include <string>
#include <sys/stat.h>
+#include <zlib.h>
#include <apti18n.h>
/*}}}*/
@@ -173,9 +174,6 @@ bool pkgCache::ReMap(bool const &Errorchecks)
HeaderP->CheckSizes(DefHeader) == false)
return _error->Error(_("The package cache file is an incompatible version"));
- if (Map.Size() < HeaderP->CacheFileSize)
- return _error->Error(_("The package cache file is corrupted, it is too small"));
-
if (HeaderP->VerSysName == 0 || HeaderP->Architecture == 0 || HeaderP->GetArchitectures() == 0)
return _error->Error(_("The package cache file is corrupted"));
@@ -193,6 +191,13 @@ bool pkgCache::ReMap(bool const &Errorchecks)
list != StrP + HeaderP->GetArchitectures())
return _error->Error(_("The package cache was built for different architectures: %s vs %s"), StrP + HeaderP->GetArchitectures(), list.c_str());
+
+ auto hash = CacheHash();
+ if (_config->FindB("Debug::pkgCacheGen", false))
+ std::clog << "Opened cache with hash " << hash << ", expecting " << HeaderP->CacheFileSize << "\n";
+ if (hash != HeaderP->CacheFileSize)
+ return _error->Error(_("The package cache file is corrupted, it has the wrong hash"));
+
return true;
}
/*}}}*/
@@ -216,6 +221,31 @@ map_id_t pkgCache::sHash(const char *Str) const
Hash = 33 * Hash + tolower_ascii(*I);
return Hash % HeaderP->GetHashTableSize();
}
+
+uint32_t pkgCache::CacheHash()
+{
+ pkgCache::Header header = {};
+ uLong adler = adler32(0L, Z_NULL, 0);
+
+ if (Map.Size() < sizeof(header))
+ return adler;
+ memcpy(&header, GetMap().Data(), sizeof(header));
+
+ header.Dirty = false;
+ header.CacheFileSize = 0;
+
+ adler = adler32(adler,
+ reinterpret_cast<const unsigned char *>(&header),
+ sizeof(header));
+
+ if (Map.Size() > sizeof(header)) {
+ adler = adler32(adler,
+ static_cast<const unsigned char *>(GetMap().Data()) + sizeof(header),
+ GetMap().Size() - sizeof(header));
+ }
+
+ return adler;
+}
/*}}}*/
// Cache::FindPkg - Locate a package by name /*{{{*/
// ---------------------------------------------------------------------