]>
Commit | Line | Data |
---|---|---|
b2e465d6 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
c9569a1e | 3 | // $Id: cachedb.h,v 1.4 2004/05/08 19:41:01 mdz Exp $ |
b2e465d6 AL |
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 | ||
a311fb96 | 15 | #include <apt-pkg/hashes.h> |
472ff00e | 16 | #include <apt-pkg/debfile.h> |
b2e465d6 | 17 | |
c9569a1e | 18 | #include <db.h> |
b2e465d6 | 19 | #include <errno.h> |
472ff00e | 20 | #include <string> |
453b82a3 DK |
21 | #include <string.h> |
22 | #include <stdint.h> | |
23 | #include <stdio.h> | |
472ff00e | 24 | |
b2e465d6 | 25 | #include "contents.h" |
ce928105 | 26 | #include "sources.h" |
472ff00e | 27 | |
453b82a3 DK |
28 | class FileFd; |
29 | ||
ce928105 | 30 | |
b2e465d6 AL |
31 | class CacheDB |
32 | { | |
33 | protected: | |
34 | ||
35 | // Database state/access | |
36 | DBT Key; | |
37 | DBT Data; | |
38 | char TmpKey[600]; | |
39 | DB *Dbp; | |
40 | bool DBLoaded; | |
41 | bool ReadOnly; | |
8f3ba4e8 | 42 | std::string DBFile; |
b2e465d6 AL |
43 | |
44 | // Generate a key for the DB of a given type | |
37497bd5 | 45 | void _InitQuery(const char *Type) |
b2e465d6 AL |
46 | { |
47 | memset(&Key,0,sizeof(Key)); | |
48 | memset(&Data,0,sizeof(Data)); | |
49 | Key.data = TmpKey; | |
cde41ae8 | 50 | Key.size = snprintf(TmpKey,sizeof(TmpKey),"%s:%s",FileName.c_str(), Type); |
b2e465d6 AL |
51 | } |
52 | ||
37497bd5 MV |
53 | void InitQueryStats() { |
54 | _InitQuery("st"); | |
55 | } | |
56 | void InitQuerySource() { | |
57 | _InitQuery("cs"); | |
58 | } | |
59 | void InitQueryControl() { | |
60 | _InitQuery("cl"); | |
61 | } | |
62 | void InitQueryContent() { | |
63 | _InitQuery("cn"); | |
64 | } | |
65 | ||
b2e465d6 AL |
66 | inline bool Get() |
67 | { | |
68 | return Dbp->get(Dbp,0,&Key,&Data,0) == 0; | |
69 | }; | |
9209ec47 | 70 | inline bool Put(const void *In,unsigned long const &Length) |
b2e465d6 AL |
71 | { |
72 | if (ReadOnly == true) | |
73 | return true; | |
74 | Data.size = Length; | |
75 | Data.data = (void *)In; | |
76 | if (DBLoaded == true && (errno = Dbp->put(Dbp,0,&Key,&Data,0)) != 0) | |
77 | { | |
78 | DBLoaded = false; | |
79 | return false; | |
80 | } | |
81 | return true; | |
82 | } | |
cde41ae8 | 83 | bool OpenFile(); |
ce928105 MV |
84 | void CloseFile(); |
85 | ||
86 | bool OpenDebFile(); | |
87 | void CloseDebFile(); | |
88 | ||
ff574e76 | 89 | bool GetFileStat(bool const &doStat = false); |
cde41ae8 MV |
90 | bool GetCurStat(); |
91 | bool LoadControl(); | |
9209ec47 | 92 | bool LoadContents(bool const &GenOnly); |
ce928105 | 93 | bool LoadSource(); |
a311fb96 DK |
94 | bool GetHashes(bool const GenOnly, unsigned int const DoHashes); |
95 | ||
b2e465d6 | 96 | // Stat info stored in the DB, Fixed types since it is written to disk. |
cde41ae8 | 97 | enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2), |
9a961efc | 98 | FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5), |
ce928105 MV |
99 | FlSHA512=(1<<6), FlSource=(1<<7), |
100 | }; | |
9a961efc | 101 | |
b2e465d6 AL |
102 | struct StatStore |
103 | { | |
b2e465d6 | 104 | uint32_t Flags; |
cde41ae8 | 105 | uint32_t mtime; |
650faab0 | 106 | uint64_t FileSize; |
cde41ae8 MV |
107 | uint8_t MD5[16]; |
108 | uint8_t SHA1[20]; | |
109 | uint8_t SHA256[32]; | |
9a961efc | 110 | uint8_t SHA512[64]; |
b2e465d6 AL |
111 | } CurStat; |
112 | struct StatStore OldStat; | |
113 | ||
114 | // 'set' state | |
8f3ba4e8 | 115 | std::string FileName; |
b2e465d6 AL |
116 | FileFd *Fd; |
117 | debDebFile *DebFile; | |
118 | ||
119 | public: | |
120 | ||
121 | // Data collection helpers | |
122 | debDebFile::MemControlExtract Control; | |
123 | ContentsExtract Contents; | |
ce928105 | 124 | DscExtract Dsc; |
a311fb96 | 125 | HashStringList HashesList; |
ce928105 | 126 | |
b2e465d6 AL |
127 | // Runtime statistics |
128 | struct Stats | |
129 | { | |
130 | double Bytes; | |
131 | double MD5Bytes; | |
cde41ae8 MV |
132 | double SHA1Bytes; |
133 | double SHA256Bytes; | |
9a961efc | 134 | double SHA512Bytes; |
b2e465d6 AL |
135 | unsigned long Packages; |
136 | unsigned long Misses; | |
650faab0 | 137 | unsigned long long DeLinkBytes; |
b2e465d6 | 138 | |
cde41ae8 | 139 | inline void Add(const Stats &S) { |
9a961efc MV |
140 | Bytes += S.Bytes; |
141 | MD5Bytes += S.MD5Bytes; | |
142 | SHA1Bytes += S.SHA1Bytes; | |
cde41ae8 | 143 | SHA256Bytes += S.SHA256Bytes; |
9a961efc MV |
144 | SHA512Bytes += S.SHA512Bytes; |
145 | Packages += S.Packages; | |
146 | Misses += S.Misses; | |
147 | DeLinkBytes += S.DeLinkBytes; | |
148 | }; | |
dcaa1185 DK |
149 | Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0), |
150 | SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {}; | |
b2e465d6 AL |
151 | } Stats; |
152 | ||
8f3ba4e8 | 153 | bool ReadyDB(std::string const &DB); |
b2e465d6 AL |
154 | inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;}; |
155 | inline bool Loaded() {return DBLoaded == true;}; | |
156 | ||
650faab0 | 157 | inline unsigned long long GetFileSize(void) {return CurStat.FileSize;} |
cde41ae8 | 158 | |
8f3ba4e8 | 159 | bool SetFile(std::string const &FileName,struct stat St,FileFd *Fd); |
ce928105 MV |
160 | |
161 | // terrible old overloaded interface | |
a311fb96 DK |
162 | bool GetFileInfo(std::string const &FileName, |
163 | bool const &DoControl, | |
164 | bool const &DoContents, | |
165 | bool const &GenContentsOnly, | |
166 | bool const DoSource, | |
167 | unsigned int const DoHashes, | |
168 | bool const &checkMtime = false); | |
ce928105 | 169 | |
b2e465d6 AL |
170 | bool Finish(); |
171 | ||
172 | bool Clean(); | |
173 | ||
dcaa1185 | 174 | CacheDB(std::string const &DB) : Dbp(0), Fd(NULL), DebFile(0) {TmpKey[0]='\0'; ReadyDB(DB);}; |
8f3ba4e8 | 175 | ~CacheDB() {ReadyDB(std::string()); delete DebFile;}; |
b2e465d6 AL |
176 | }; |
177 | ||
178 | #endif |