]>
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 | ||
472ff00e | 15 | #include <apt-pkg/debfile.h> |
b2e465d6 | 16 | |
c9569a1e | 17 | #include <db.h> |
b2e465d6 | 18 | #include <errno.h> |
472ff00e | 19 | #include <string> |
453b82a3 DK |
20 | #include <string.h> |
21 | #include <stdint.h> | |
22 | #include <stdio.h> | |
472ff00e | 23 | |
b2e465d6 | 24 | #include "contents.h" |
ce928105 | 25 | #include "sources.h" |
472ff00e | 26 | |
453b82a3 DK |
27 | class FileFd; |
28 | ||
ce928105 | 29 | |
b2e465d6 AL |
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; | |
8f3ba4e8 | 41 | std::string DBFile; |
b2e465d6 AL |
42 | |
43 | // Generate a key for the DB of a given type | |
37497bd5 | 44 | void _InitQuery(const char *Type) |
b2e465d6 AL |
45 | { |
46 | memset(&Key,0,sizeof(Key)); | |
47 | memset(&Data,0,sizeof(Data)); | |
48 | Key.data = TmpKey; | |
cde41ae8 | 49 | Key.size = snprintf(TmpKey,sizeof(TmpKey),"%s:%s",FileName.c_str(), Type); |
b2e465d6 AL |
50 | } |
51 | ||
37497bd5 MV |
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 | ||
b2e465d6 AL |
65 | inline bool Get() |
66 | { | |
67 | return Dbp->get(Dbp,0,&Key,&Data,0) == 0; | |
68 | }; | |
9209ec47 | 69 | inline bool Put(const void *In,unsigned long const &Length) |
b2e465d6 AL |
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 | } | |
cde41ae8 | 82 | bool OpenFile(); |
ce928105 MV |
83 | void CloseFile(); |
84 | ||
85 | bool OpenDebFile(); | |
86 | void CloseDebFile(); | |
87 | ||
ff574e76 | 88 | bool GetFileStat(bool const &doStat = false); |
cde41ae8 MV |
89 | bool GetCurStat(); |
90 | bool LoadControl(); | |
9209ec47 | 91 | bool LoadContents(bool const &GenOnly); |
ce928105 | 92 | bool LoadSource(); |
9209ec47 DK |
93 | bool GetMD5(bool const &GenOnly); |
94 | bool GetSHA1(bool const &GenOnly); | |
95 | bool GetSHA256(bool const &GenOnly); | |
9a961efc | 96 | bool GetSHA512(bool const &GenOnly); |
b2e465d6 AL |
97 | |
98 | // Stat info stored in the DB, Fixed types since it is written to disk. | |
cde41ae8 | 99 | enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2), |
9a961efc | 100 | FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5), |
ce928105 MV |
101 | FlSHA512=(1<<6), FlSource=(1<<7), |
102 | }; | |
9a961efc | 103 | |
b2e465d6 AL |
104 | struct StatStore |
105 | { | |
b2e465d6 | 106 | uint32_t Flags; |
cde41ae8 | 107 | uint32_t mtime; |
650faab0 | 108 | uint64_t FileSize; |
cde41ae8 MV |
109 | uint8_t MD5[16]; |
110 | uint8_t SHA1[20]; | |
111 | uint8_t SHA256[32]; | |
9a961efc | 112 | uint8_t SHA512[64]; |
b2e465d6 AL |
113 | } CurStat; |
114 | struct StatStore OldStat; | |
115 | ||
116 | // 'set' state | |
8f3ba4e8 | 117 | std::string FileName; |
b2e465d6 AL |
118 | FileFd *Fd; |
119 | debDebFile *DebFile; | |
120 | ||
121 | public: | |
122 | ||
123 | // Data collection helpers | |
124 | debDebFile::MemControlExtract Control; | |
125 | ContentsExtract Contents; | |
ce928105 MV |
126 | DscExtract Dsc; |
127 | ||
8f3ba4e8 DK |
128 | std::string MD5Res; |
129 | std::string SHA1Res; | |
130 | std::string SHA256Res; | |
131 | std::string SHA512Res; | |
b2e465d6 AL |
132 | |
133 | // Runtime statistics | |
134 | struct Stats | |
135 | { | |
136 | double Bytes; | |
137 | double MD5Bytes; | |
cde41ae8 MV |
138 | double SHA1Bytes; |
139 | double SHA256Bytes; | |
9a961efc | 140 | double SHA512Bytes; |
b2e465d6 AL |
141 | unsigned long Packages; |
142 | unsigned long Misses; | |
650faab0 | 143 | unsigned long long DeLinkBytes; |
b2e465d6 | 144 | |
cde41ae8 | 145 | inline void Add(const Stats &S) { |
9a961efc MV |
146 | Bytes += S.Bytes; |
147 | MD5Bytes += S.MD5Bytes; | |
148 | SHA1Bytes += S.SHA1Bytes; | |
cde41ae8 | 149 | SHA256Bytes += S.SHA256Bytes; |
9a961efc MV |
150 | SHA512Bytes += S.SHA512Bytes; |
151 | Packages += S.Packages; | |
152 | Misses += S.Misses; | |
153 | DeLinkBytes += S.DeLinkBytes; | |
154 | }; | |
dcaa1185 DK |
155 | Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0), |
156 | SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {}; | |
b2e465d6 AL |
157 | } Stats; |
158 | ||
8f3ba4e8 | 159 | bool ReadyDB(std::string const &DB); |
b2e465d6 AL |
160 | inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;}; |
161 | inline bool Loaded() {return DBLoaded == true;}; | |
162 | ||
650faab0 | 163 | inline unsigned long long GetFileSize(void) {return CurStat.FileSize;} |
cde41ae8 | 164 | |
8f3ba4e8 | 165 | bool SetFile(std::string const &FileName,struct stat St,FileFd *Fd); |
ce928105 MV |
166 | |
167 | // terrible old overloaded interface | |
168 | bool GetFileInfo(std::string const &FileName, | |
169 | bool const &DoControl, | |
170 | bool const &DoContents, | |
171 | bool const &GenContentsOnly, | |
172 | bool const &DoSource, | |
173 | bool const &DoMD5, | |
174 | bool const &DoSHA1, | |
175 | bool const &DoSHA256, | |
176 | bool const &DoSHA512, | |
177 | bool const &checkMtime = false); | |
178 | ||
b2e465d6 AL |
179 | bool Finish(); |
180 | ||
181 | bool Clean(); | |
182 | ||
dcaa1185 | 183 | CacheDB(std::string const &DB) : Dbp(0), Fd(NULL), DebFile(0) {TmpKey[0]='\0'; ReadyDB(DB);}; |
8f3ba4e8 | 184 | ~CacheDB() {ReadyDB(std::string()); delete DebFile;}; |
b2e465d6 AL |
185 | }; |
186 | ||
187 | #endif |