]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/pkgcache.cc
fail installing build-deps if parsing them failed
[apt.git] / apt-pkg / pkgcache.cc
index e65e37bca902b1883ceba2177d1489a85dae56ed..347a0954e1b2c71ea03d3ca8ee5cf5bda41151e6 100644 (file)
@@ -39,6 +39,7 @@
 #include <vector>
 #include <string>
 #include <sys/stat.h>
+#include <zlib.h>
 
 #include <apti18n.h>
                                                                        /*}}}*/
@@ -57,7 +58,7 @@ pkgCache::Header::Header()
    /* Whenever the structures change the major version should be bumped,
       whenever the generator changes the minor version should be bumped. */
    APT_HEADER_SET(MajorVersion, 10);
-   APT_HEADER_SET(MinorVersion, 2);
+   APT_HEADER_SET(MinorVersion, 3);
    APT_HEADER_SET(Dirty, false);
 
    APT_HEADER_SET(HeaderSz, sizeof(pkgCache::Header));
@@ -93,7 +94,7 @@ pkgCache::Header::Header()
    VerSysName = 0;
    Architecture = 0;
    SetArchitectures(0);
-   SetHashTableSize(_config->FindI("APT::Cache-HashTableSize", 10 * 1048));
+   SetHashTableSize(_config->FindI("APT::Cache-HashTableSize", 15013));
    memset(Pools,0,sizeof(Pools));
 
    CacheFileSize = 0;
@@ -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;
 }
                                                                        /*}}}*/
@@ -203,19 +208,44 @@ bool pkgCache::ReMap(bool const &Errorchecks)
    table (480 used items) */
 map_id_t pkgCache::sHash(const string &Str) const
 {
-   unsigned long Hash = 0;
+   uint32_t Hash = 5381;
    for (string::const_iterator I = Str.begin(); I != Str.end(); ++I)
-      Hash = 41 * Hash + tolower_ascii(*I);
+      Hash = 33 * Hash + tolower_ascii(*I);
    return Hash % HeaderP->GetHashTableSize();
 }
 
 map_id_t pkgCache::sHash(const char *Str) const
 {
-   unsigned long Hash = tolower_ascii(*Str);
-   for (const char *I = Str + 1; *I != 0; ++I)
-      Hash = 41 * Hash + tolower_ascii(*I);
+   uint32_t Hash = 5381;
+   for (const char *I = Str; *I != 0; ++I)
+      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                           /*{{{*/
 // ---------------------------------------------------------------------