]>
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 | ||
13500573 | 15 | |
b2e465d6 | 16 | |
c9569a1e | 17 | #include <db.h> |
b2e465d6 AL |
18 | #include <string> |
19 | #include <apt-pkg/debfile.h> | |
20 | #include <inttypes.h> | |
21 | #include <sys/stat.h> | |
22 | #include <errno.h> | |
23 | ||
24 | #include "contents.h" | |
25 | ||
26 | class CacheDB | |
27 | { | |
28 | protected: | |
29 | ||
30 | // Database state/access | |
31 | DBT Key; | |
32 | DBT Data; | |
33 | char TmpKey[600]; | |
34 | DB *Dbp; | |
35 | bool DBLoaded; | |
36 | bool ReadOnly; | |
37 | string DBFile; | |
38 | ||
39 | // Generate a key for the DB of a given type | |
40 | inline void InitQuery(const char *Type) | |
41 | { | |
42 | memset(&Key,0,sizeof(Key)); | |
43 | memset(&Data,0,sizeof(Data)); | |
44 | Key.data = TmpKey; | |
cde41ae8 | 45 | Key.size = snprintf(TmpKey,sizeof(TmpKey),"%s:%s",FileName.c_str(), Type); |
b2e465d6 AL |
46 | } |
47 | ||
48 | inline bool Get() | |
49 | { | |
50 | return Dbp->get(Dbp,0,&Key,&Data,0) == 0; | |
51 | }; | |
9209ec47 | 52 | inline bool Put(const void *In,unsigned long const &Length) |
b2e465d6 AL |
53 | { |
54 | if (ReadOnly == true) | |
55 | return true; | |
56 | Data.size = Length; | |
57 | Data.data = (void *)In; | |
58 | if (DBLoaded == true && (errno = Dbp->put(Dbp,0,&Key,&Data,0)) != 0) | |
59 | { | |
60 | DBLoaded = false; | |
61 | return false; | |
62 | } | |
63 | return true; | |
64 | } | |
cde41ae8 | 65 | bool OpenFile(); |
ff574e76 | 66 | bool GetFileStat(bool const &doStat = false); |
cde41ae8 MV |
67 | bool GetCurStat(); |
68 | bool LoadControl(); | |
9209ec47 DK |
69 | bool LoadContents(bool const &GenOnly); |
70 | bool GetMD5(bool const &GenOnly); | |
71 | bool GetSHA1(bool const &GenOnly); | |
72 | bool GetSHA256(bool const &GenOnly); | |
9a961efc | 73 | bool GetSHA512(bool const &GenOnly); |
b2e465d6 AL |
74 | |
75 | // Stat info stored in the DB, Fixed types since it is written to disk. | |
cde41ae8 | 76 | enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2), |
9a961efc MV |
77 | FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5), |
78 | FlSHA512=(1<<6)}; | |
79 | ||
b2e465d6 AL |
80 | struct StatStore |
81 | { | |
b2e465d6 | 82 | uint32_t Flags; |
cde41ae8 MV |
83 | uint32_t mtime; |
84 | uint32_t FileSize; | |
85 | uint8_t MD5[16]; | |
86 | uint8_t SHA1[20]; | |
87 | uint8_t SHA256[32]; | |
9a961efc | 88 | uint8_t SHA512[64]; |
b2e465d6 AL |
89 | } CurStat; |
90 | struct StatStore OldStat; | |
91 | ||
92 | // 'set' state | |
93 | string FileName; | |
b2e465d6 AL |
94 | FileFd *Fd; |
95 | debDebFile *DebFile; | |
96 | ||
97 | public: | |
98 | ||
99 | // Data collection helpers | |
100 | debDebFile::MemControlExtract Control; | |
101 | ContentsExtract Contents; | |
cde41ae8 MV |
102 | string MD5Res; |
103 | string SHA1Res; | |
104 | string SHA256Res; | |
9a961efc | 105 | string SHA512Res; |
b2e465d6 AL |
106 | |
107 | // Runtime statistics | |
108 | struct Stats | |
109 | { | |
110 | double Bytes; | |
111 | double MD5Bytes; | |
cde41ae8 MV |
112 | double SHA1Bytes; |
113 | double SHA256Bytes; | |
9a961efc | 114 | double SHA512Bytes; |
b2e465d6 AL |
115 | unsigned long Packages; |
116 | unsigned long Misses; | |
117 | unsigned long DeLinkBytes; | |
118 | ||
cde41ae8 | 119 | inline void Add(const Stats &S) { |
9a961efc MV |
120 | Bytes += S.Bytes; |
121 | MD5Bytes += S.MD5Bytes; | |
122 | SHA1Bytes += S.SHA1Bytes; | |
cde41ae8 | 123 | SHA256Bytes += S.SHA256Bytes; |
9a961efc MV |
124 | SHA512Bytes += S.SHA512Bytes; |
125 | Packages += S.Packages; | |
126 | Misses += S.Misses; | |
127 | DeLinkBytes += S.DeLinkBytes; | |
128 | }; | |
cde41ae8 | 129 | Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0), Packages(0), Misses(0), DeLinkBytes(0) {}; |
b2e465d6 AL |
130 | } Stats; |
131 | ||
9209ec47 | 132 | bool ReadyDB(string const &DB); |
b2e465d6 AL |
133 | inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;}; |
134 | inline bool Loaded() {return DBLoaded == true;}; | |
135 | ||
cde41ae8 MV |
136 | inline off_t GetFileSize(void) {return CurStat.FileSize;} |
137 | ||
9209ec47 DK |
138 | bool SetFile(string const &FileName,struct stat St,FileFd *Fd); |
139 | bool GetFileInfo(string const &FileName, bool const &DoControl, bool const &DoContents, bool const &GenContentsOnly, | |
9a961efc | 140 | bool const &DoMD5, bool const &DoSHA1, bool const &DoSHA256, bool const &DoSHA512, bool const &checkMtime = false); |
b2e465d6 AL |
141 | bool Finish(); |
142 | ||
143 | bool Clean(); | |
144 | ||
9209ec47 | 145 | CacheDB(string const &DB) : Dbp(0), Fd(NULL), DebFile(0) {ReadyDB(DB);}; |
b2e465d6 AL |
146 | ~CacheDB() {ReadyDB(string()); delete DebFile;}; |
147 | }; | |
148 | ||
149 | #endif |