diff --git a/ftparchive/cachedb.cc b/ftparchive/cachedb.cc index 12eac20..37fad82 100644 --- a/ftparchive/cachedb.cc +++ b/ftparchive/cachedb.cc @@ -99,7 +99,7 @@ bool CacheDB::ReadyDB(std::string const &DB) return _error->Error(_("Unable to open DB file %s: %s"),DB.c_str(), db_strerror(err)); } } - + DBFile = DB; DBLoaded = true; return true; @@ -188,6 +188,29 @@ bool CacheDB::GetFileStat(bool const &doStat) return true; } /*}}}*/ +// CacheDB::GetCurStatCompatOldFormat /*{{{*/ +// --------------------------------------------------------------------- +/* Read the old (32bit FileSize) StateStore format from disk */ +bool CacheDB::GetCurStatCompatOldFormat() +{ + InitQueryStats(); + Data.data = &CurStatOldFormat; + Data.flags = DB_DBT_USERMEM; + Data.ulen = sizeof(CurStatOldFormat); + if (Get() == false) + { + CurStat.Flags = 0; + } else { + CurStat.Flags = CurStatOldFormat.Flags; + CurStat.mtime = CurStatOldFormat.mtime; + CurStat.FileSize = CurStatOldFormat.FileSize; + memcpy(CurStat.MD5, CurStatOldFormat.MD5, sizeof(CurStat.MD5)); + memcpy(CurStat.SHA1, CurStatOldFormat.SHA1, sizeof(CurStat.SHA1)); + memcpy(CurStat.SHA256, CurStatOldFormat.SHA256, sizeof(CurStat.SHA256)); + } + return true; +} + /*}}}*/ // CacheDB::GetCurStat - Set the CurStat variable. /*{{{*/ // --------------------------------------------------------------------- /* Sets the CurStat variable. Either to 0 if no database is used @@ -198,19 +221,28 @@ bool CacheDB::GetCurStat() if (DBLoaded) { - /* First see if there is anything about it - in the database */ - - /* Get the flags (and mtime) */ + // do a first query to just get the size of the data on disk InitQueryStats(); - // Ensure alignment of the returned structure Data.data = &CurStat; - Data.ulen = sizeof(CurStat); Data.flags = DB_DBT_USERMEM; - if (Get() == false) + Data.ulen = 0; + Get(); + + // check if the record is written in the old format (32bit filesize) + if(Data.size == sizeof(CurStatOldFormat)) { - CurStat.Flags = 0; - } + GetCurStatCompatOldFormat(); + } + else if(Data.size == sizeof(CurStat)) + { + Data.ulen = sizeof(CurStat); + if (Get() == false) + { + CurStat.Flags = 0; + } + } else { + return _error->Error("Cache record size mismatch (%ul)", Data.size); + } CurStat.Flags = ntohl(CurStat.Flags); CurStat.FileSize = ntohl(CurStat.FileSize); } diff --git a/ftparchive/cachedb.h b/ftparchive/cachedb.h index edb8594..65cb47d 100644 --- a/ftparchive/cachedb.h +++ b/ftparchive/cachedb.h @@ -86,6 +86,7 @@ class CacheDB void CloseDebFile(); bool GetFileStat(bool const &doStat = false); + bool GetCurStatCompatOldFormat(); bool GetCurStat(); bool LoadControl(); bool LoadContents(bool const &GenOnly); @@ -101,6 +102,20 @@ class CacheDB FlSHA512=(1<<6), FlSource=(1<<7), }; + // the on-disk format changed (FileSize increased to 64bit) in + // commit 650faab0 which will lead to corruption with old caches + struct StatStoreOldFormat + { + uint32_t Flags; + uint32_t mtime; + uint32_t FileSize; + uint8_t MD5[16]; + uint8_t SHA1[20]; + uint8_t SHA256[32]; + } CurStatOldFormat; + + // WARNING: this struct is read/written to the DB so do not change the + // layout of the fields (see lp #1274466), only append to it struct StatStore { uint32_t Flags;