]>
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 | |
44 | inline void InitQuery(const char *Type) | |
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 | ||
52 | inline bool Get() | |
53 | { | |
54 | return Dbp->get(Dbp,0,&Key,&Data,0) == 0; | |
55 | }; | |
9209ec47 | 56 | inline bool Put(const void *In,unsigned long const &Length) |
b2e465d6 AL |
57 | { |
58 | if (ReadOnly == true) | |
59 | return true; | |
60 | Data.size = Length; | |
61 | Data.data = (void *)In; | |
62 | if (DBLoaded == true && (errno = Dbp->put(Dbp,0,&Key,&Data,0)) != 0) | |
63 | { | |
64 | DBLoaded = false; | |
65 | return false; | |
66 | } | |
67 | return true; | |
68 | } | |
cde41ae8 | 69 | bool OpenFile(); |
ce928105 MV |
70 | void CloseFile(); |
71 | ||
72 | bool OpenDebFile(); | |
73 | void CloseDebFile(); | |
74 | ||
ff574e76 | 75 | bool GetFileStat(bool const &doStat = false); |
cde41ae8 MV |
76 | bool GetCurStat(); |
77 | bool LoadControl(); | |
9209ec47 | 78 | bool LoadContents(bool const &GenOnly); |
ce928105 | 79 | bool LoadSource(); |
9209ec47 DK |
80 | bool GetMD5(bool const &GenOnly); |
81 | bool GetSHA1(bool const &GenOnly); | |
82 | bool GetSHA256(bool const &GenOnly); | |
9a961efc | 83 | bool GetSHA512(bool const &GenOnly); |
b2e465d6 AL |
84 | |
85 | // Stat info stored in the DB, Fixed types since it is written to disk. | |
cde41ae8 | 86 | enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2), |
9a961efc | 87 | FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5), |
ce928105 MV |
88 | FlSHA512=(1<<6), FlSource=(1<<7), |
89 | }; | |
9a961efc | 90 | |
b2e465d6 AL |
91 | struct StatStore |
92 | { | |
b2e465d6 | 93 | uint32_t Flags; |
cde41ae8 | 94 | uint32_t mtime; |
650faab0 | 95 | uint64_t FileSize; |
cde41ae8 MV |
96 | uint8_t MD5[16]; |
97 | uint8_t SHA1[20]; | |
98 | uint8_t SHA256[32]; | |
9a961efc | 99 | uint8_t SHA512[64]; |
b2e465d6 AL |
100 | } CurStat; |
101 | struct StatStore OldStat; | |
102 | ||
103 | // 'set' state | |
8f3ba4e8 | 104 | std::string FileName; |
b2e465d6 AL |
105 | FileFd *Fd; |
106 | debDebFile *DebFile; | |
107 | ||
108 | public: | |
109 | ||
110 | // Data collection helpers | |
111 | debDebFile::MemControlExtract Control; | |
112 | ContentsExtract Contents; | |
ce928105 MV |
113 | DscExtract Dsc; |
114 | ||
8f3ba4e8 DK |
115 | std::string MD5Res; |
116 | std::string SHA1Res; | |
117 | std::string SHA256Res; | |
118 | std::string SHA512Res; | |
b2e465d6 AL |
119 | |
120 | // Runtime statistics | |
121 | struct Stats | |
122 | { | |
123 | double Bytes; | |
124 | double MD5Bytes; | |
cde41ae8 MV |
125 | double SHA1Bytes; |
126 | double SHA256Bytes; | |
9a961efc | 127 | double SHA512Bytes; |
b2e465d6 AL |
128 | unsigned long Packages; |
129 | unsigned long Misses; | |
650faab0 | 130 | unsigned long long DeLinkBytes; |
b2e465d6 | 131 | |
cde41ae8 | 132 | inline void Add(const Stats &S) { |
9a961efc MV |
133 | Bytes += S.Bytes; |
134 | MD5Bytes += S.MD5Bytes; | |
135 | SHA1Bytes += S.SHA1Bytes; | |
cde41ae8 | 136 | SHA256Bytes += S.SHA256Bytes; |
9a961efc MV |
137 | SHA512Bytes += S.SHA512Bytes; |
138 | Packages += S.Packages; | |
139 | Misses += S.Misses; | |
140 | DeLinkBytes += S.DeLinkBytes; | |
141 | }; | |
dcaa1185 DK |
142 | Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0), |
143 | SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {}; | |
b2e465d6 AL |
144 | } Stats; |
145 | ||
8f3ba4e8 | 146 | bool ReadyDB(std::string const &DB); |
b2e465d6 AL |
147 | inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;}; |
148 | inline bool Loaded() {return DBLoaded == true;}; | |
149 | ||
650faab0 | 150 | inline unsigned long long GetFileSize(void) {return CurStat.FileSize;} |
cde41ae8 | 151 | |
8f3ba4e8 | 152 | bool SetFile(std::string const &FileName,struct stat St,FileFd *Fd); |
ce928105 MV |
153 | |
154 | // terrible old overloaded interface | |
155 | bool GetFileInfo(std::string const &FileName, | |
156 | bool const &DoControl, | |
157 | bool const &DoContents, | |
158 | bool const &GenContentsOnly, | |
159 | bool const &DoSource, | |
160 | bool const &DoMD5, | |
161 | bool const &DoSHA1, | |
162 | bool const &DoSHA256, | |
163 | bool const &DoSHA512, | |
164 | bool const &checkMtime = false); | |
165 | ||
b2e465d6 AL |
166 | bool Finish(); |
167 | ||
168 | bool Clean(); | |
169 | ||
dcaa1185 | 170 | CacheDB(std::string const &DB) : Dbp(0), Fd(NULL), DebFile(0) {TmpKey[0]='\0'; ReadyDB(DB);}; |
8f3ba4e8 | 171 | ~CacheDB() {ReadyDB(std::string()); delete DebFile;}; |
b2e465d6 AL |
172 | }; |
173 | ||
174 | #endif |