| 1 | // -*- mode: cpp; mode: fold -*- |
| 2 | // Description /*{{{*/ |
| 3 | // $Id: cachedb.h,v 1.4 2004/05/08 19:41:01 mdz Exp $ |
| 4 | /* ###################################################################### |
| 5 | |
| 6 | CacheDB |
| 7 | |
| 8 | Simple uniform interface to a cache database. |
| 9 | |
| 10 | ##################################################################### */ |
| 11 | /*}}}*/ |
| 12 | #ifndef CACHEDB_H |
| 13 | #define CACHEDB_H |
| 14 | |
| 15 | #include <apt-pkg/debfile.h> |
| 16 | |
| 17 | #include <db.h> |
| 18 | #include <errno.h> |
| 19 | #include <string> |
| 20 | #include <string.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #include "contents.h" |
| 25 | #include "sources.h" |
| 26 | |
| 27 | class FileFd; |
| 28 | |
| 29 | |
| 30 | class CacheDB |
| 31 | { |
| 32 | protected: |
| 33 | |
| 34 | // Database state/access |
| 35 | DBT Key; |
| 36 | DBT Data; |
| 37 | char TmpKey[600]; |
| 38 | DB *Dbp; |
| 39 | bool DBLoaded; |
| 40 | bool ReadOnly; |
| 41 | std::string DBFile; |
| 42 | |
| 43 | // Generate a key for the DB of a given type |
| 44 | void _InitQuery(const char *Type) |
| 45 | { |
| 46 | memset(&Key,0,sizeof(Key)); |
| 47 | memset(&Data,0,sizeof(Data)); |
| 48 | Key.data = TmpKey; |
| 49 | Key.size = snprintf(TmpKey,sizeof(TmpKey),"%s:%s",FileName.c_str(), Type); |
| 50 | } |
| 51 | |
| 52 | void InitQueryStats() { |
| 53 | _InitQuery("st"); |
| 54 | } |
| 55 | void InitQuerySource() { |
| 56 | _InitQuery("cs"); |
| 57 | } |
| 58 | void InitQueryControl() { |
| 59 | _InitQuery("cl"); |
| 60 | } |
| 61 | void InitQueryContent() { |
| 62 | _InitQuery("cn"); |
| 63 | } |
| 64 | |
| 65 | inline bool Get() |
| 66 | { |
| 67 | return Dbp->get(Dbp,0,&Key,&Data,0) == 0; |
| 68 | }; |
| 69 | inline bool Put(const void *In,unsigned long const &Length) |
| 70 | { |
| 71 | if (ReadOnly == true) |
| 72 | return true; |
| 73 | Data.size = Length; |
| 74 | Data.data = (void *)In; |
| 75 | if (DBLoaded == true && (errno = Dbp->put(Dbp,0,&Key,&Data,0)) != 0) |
| 76 | { |
| 77 | DBLoaded = false; |
| 78 | return false; |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | bool OpenFile(); |
| 83 | void CloseFile(); |
| 84 | |
| 85 | bool OpenDebFile(); |
| 86 | void CloseDebFile(); |
| 87 | |
| 88 | // GetCurStat needs some compat code, see lp #1274466) |
| 89 | bool GetCurStatCompatOldFormat(); |
| 90 | bool GetCurStatCompatNewFormat(); |
| 91 | bool GetCurStat(); |
| 92 | |
| 93 | bool GetFileStat(bool const &doStat = false); |
| 94 | bool LoadControl(); |
| 95 | bool LoadContents(bool const &GenOnly); |
| 96 | bool LoadSource(); |
| 97 | bool GetMD5(bool const &GenOnly); |
| 98 | bool GetSHA1(bool const &GenOnly); |
| 99 | bool GetSHA256(bool const &GenOnly); |
| 100 | bool GetSHA512(bool const &GenOnly); |
| 101 | |
| 102 | // Stat info stored in the DB, Fixed types since it is written to disk. |
| 103 | enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2), |
| 104 | FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5), |
| 105 | FlSHA512=(1<<6), FlSource=(1<<7), |
| 106 | }; |
| 107 | |
| 108 | // the on-disk format changed (FileSize increased to 64bit) in |
| 109 | // commit 650faab0 which will lead to corruption with old caches |
| 110 | struct StatStoreOldFormat |
| 111 | { |
| 112 | uint32_t Flags; |
| 113 | uint32_t mtime; |
| 114 | uint32_t FileSize; |
| 115 | uint8_t MD5[16]; |
| 116 | uint8_t SHA1[20]; |
| 117 | uint8_t SHA256[32]; |
| 118 | } CurStatOldFormat; |
| 119 | |
| 120 | // WARNING: this struct is read/written to the DB so do not change the |
| 121 | // layout of the fields (see lp #1274466), only append to it |
| 122 | struct StatStore |
| 123 | { |
| 124 | uint32_t Flags; |
| 125 | uint32_t mtime; |
| 126 | uint64_t FileSize; |
| 127 | uint8_t MD5[16]; |
| 128 | uint8_t SHA1[20]; |
| 129 | uint8_t SHA256[32]; |
| 130 | uint8_t SHA512[64]; |
| 131 | } CurStat; |
| 132 | struct StatStore OldStat; |
| 133 | |
| 134 | // 'set' state |
| 135 | std::string FileName; |
| 136 | FileFd *Fd; |
| 137 | debDebFile *DebFile; |
| 138 | |
| 139 | public: |
| 140 | |
| 141 | // Data collection helpers |
| 142 | debDebFile::MemControlExtract Control; |
| 143 | ContentsExtract Contents; |
| 144 | DscExtract Dsc; |
| 145 | |
| 146 | std::string MD5Res; |
| 147 | std::string SHA1Res; |
| 148 | std::string SHA256Res; |
| 149 | std::string SHA512Res; |
| 150 | |
| 151 | // Runtime statistics |
| 152 | struct Stats |
| 153 | { |
| 154 | double Bytes; |
| 155 | double MD5Bytes; |
| 156 | double SHA1Bytes; |
| 157 | double SHA256Bytes; |
| 158 | double SHA512Bytes; |
| 159 | unsigned long Packages; |
| 160 | unsigned long Misses; |
| 161 | unsigned long long DeLinkBytes; |
| 162 | |
| 163 | inline void Add(const Stats &S) { |
| 164 | Bytes += S.Bytes; |
| 165 | MD5Bytes += S.MD5Bytes; |
| 166 | SHA1Bytes += S.SHA1Bytes; |
| 167 | SHA256Bytes += S.SHA256Bytes; |
| 168 | SHA512Bytes += S.SHA512Bytes; |
| 169 | Packages += S.Packages; |
| 170 | Misses += S.Misses; |
| 171 | DeLinkBytes += S.DeLinkBytes; |
| 172 | }; |
| 173 | Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0), |
| 174 | SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {}; |
| 175 | } Stats; |
| 176 | |
| 177 | bool ReadyDB(std::string const &DB = ""); |
| 178 | inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;}; |
| 179 | inline bool Loaded() {return DBLoaded == true;}; |
| 180 | |
| 181 | inline unsigned long long GetFileSize(void) {return CurStat.FileSize;} |
| 182 | |
| 183 | bool SetFile(std::string const &FileName,struct stat St,FileFd *Fd); |
| 184 | |
| 185 | // terrible old overloaded interface |
| 186 | bool GetFileInfo(std::string const &FileName, |
| 187 | bool const &DoControl, |
| 188 | bool const &DoContents, |
| 189 | bool const &GenContentsOnly, |
| 190 | bool const &DoSource, |
| 191 | bool const &DoMD5, |
| 192 | bool const &DoSHA1, |
| 193 | bool const &DoSHA256, |
| 194 | bool const &DoSHA512, |
| 195 | bool const &checkMtime = false); |
| 196 | |
| 197 | bool Finish(); |
| 198 | |
| 199 | bool Clean(); |
| 200 | |
| 201 | CacheDB(std::string const &DB); |
| 202 | ~CacheDB(); |
| 203 | }; |
| 204 | |
| 205 | #endif |