#include <vector>
#include <string>
#include <sys/stat.h>
+#include <zlib.h>
#include <apti18n.h>
/*}}}*/
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"));
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;
}
/*}}}*/
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 /*{{{*/
// ---------------------------------------------------------------------
inline map_id_t Hash(const std::string &S) const {return sHash(S);}
inline map_id_t Hash(const char *S) const {return sHash(S);}
+ APT_HIDDEN uint32_t CacheHash();
+
// Useful transformation things
static const char *Priority(unsigned char Priority);
map_pointer_t * PkgHashTableP() const { return (map_pointer_t*) (this + 1); }
map_pointer_t * GrpHashTableP() const { return PkgHashTableP() + GetHashTableSize(); }
- /** \brief Size of the complete cache file */
+ /** \brief Hash of the file (TODO: Rename) */
map_filesize_small_t CacheFileSize;
bool CheckSizes(Header &Against) const APT_PURE;
else
Cache.HeaderP->SetArchitectures(idxArchitecture);
+ // Calculate the hash for the empty map, so ReMap does not fail
+ Cache.HeaderP->CacheFileSize = Cache.CacheHash();
Cache.ReMap();
}
else
return;
Cache.HeaderP->Dirty = false;
- Cache.HeaderP->CacheFileSize = Map.Size();
+ Cache.HeaderP->CacheFileSize = Cache.CacheHash();
+
+ if (_config->FindB("Debug::pkgCacheGen", false))
+ std::clog << "Produced cache with hash " << Cache.HeaderP->CacheFileSize << std::endl;
Map.Sync(0,sizeof(pkgCache::Header));
}
/*}}}*/
// Write out the proper header
Gen->GetCache().HeaderP->Dirty = false;
+ Gen->GetCache().HeaderP->CacheFileSize = Gen->GetCache().CacheHash();
if (SCacheF.Seek(0) == false ||
SCacheF.Write(Map->Data(),sizeof(*Gen->GetCache().HeaderP)) == false)
return _error->Error(_("IO Error saving source cache"));