use printf instead of echo in testing framework
[ntk/apt.git] / ftparchive / cachedb.h
CommitLineData
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
27class FileFd;
28
ce928105 29
b2e465d6
AL
30class 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
243b2a38
MV
88 // GetCurStat needs some compat code, see lp #1274466)
89 bool GetCurStatCompatOldFormat();
90 bool GetCurStatCompatNewFormat();
cde41ae8 91 bool GetCurStat();
243b2a38
MV
92
93 bool GetFileStat(bool const &doStat = false);
cde41ae8 94 bool LoadControl();
9209ec47 95 bool LoadContents(bool const &GenOnly);
ce928105 96 bool LoadSource();
9209ec47
DK
97 bool GetMD5(bool const &GenOnly);
98 bool GetSHA1(bool const &GenOnly);
99 bool GetSHA256(bool const &GenOnly);
9a961efc 100 bool GetSHA512(bool const &GenOnly);
b2e465d6
AL
101
102 // Stat info stored in the DB, Fixed types since it is written to disk.
cde41ae8 103 enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2),
9a961efc 104 FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5),
ce928105
MV
105 FlSHA512=(1<<6), FlSource=(1<<7),
106 };
9a961efc 107
243b2a38
MV
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
b2e465d6
AL
122 struct StatStore
123 {
b2e465d6 124 uint32_t Flags;
cde41ae8 125 uint32_t mtime;
650faab0 126 uint64_t FileSize;
cde41ae8
MV
127 uint8_t MD5[16];
128 uint8_t SHA1[20];
129 uint8_t SHA256[32];
9a961efc 130 uint8_t SHA512[64];
b2e465d6
AL
131 } CurStat;
132 struct StatStore OldStat;
133
134 // 'set' state
8f3ba4e8 135 std::string FileName;
b2e465d6
AL
136 FileFd *Fd;
137 debDebFile *DebFile;
138
139 public:
140
141 // Data collection helpers
142 debDebFile::MemControlExtract Control;
143 ContentsExtract Contents;
ce928105
MV
144 DscExtract Dsc;
145
8f3ba4e8
DK
146 std::string MD5Res;
147 std::string SHA1Res;
148 std::string SHA256Res;
149 std::string SHA512Res;
b2e465d6
AL
150
151 // Runtime statistics
152 struct Stats
153 {
154 double Bytes;
155 double MD5Bytes;
cde41ae8
MV
156 double SHA1Bytes;
157 double SHA256Bytes;
9a961efc 158 double SHA512Bytes;
b2e465d6
AL
159 unsigned long Packages;
160 unsigned long Misses;
650faab0 161 unsigned long long DeLinkBytes;
b2e465d6 162
cde41ae8 163 inline void Add(const Stats &S) {
9a961efc
MV
164 Bytes += S.Bytes;
165 MD5Bytes += S.MD5Bytes;
166 SHA1Bytes += S.SHA1Bytes;
cde41ae8 167 SHA256Bytes += S.SHA256Bytes;
9a961efc
MV
168 SHA512Bytes += S.SHA512Bytes;
169 Packages += S.Packages;
170 Misses += S.Misses;
171 DeLinkBytes += S.DeLinkBytes;
172 };
dcaa1185
DK
173 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0),
174 SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {};
b2e465d6
AL
175 } Stats;
176
21ea1dbb 177 bool ReadyDB(std::string const &DB = "");
b2e465d6
AL
178 inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;};
179 inline bool Loaded() {return DBLoaded == true;};
180
650faab0 181 inline unsigned long long GetFileSize(void) {return CurStat.FileSize;}
cde41ae8 182
8f3ba4e8 183 bool SetFile(std::string const &FileName,struct stat St,FileFd *Fd);
ce928105
MV
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
b2e465d6
AL
197 bool Finish();
198
199 bool Clean();
200
21ea1dbb
MV
201 CacheDB(std::string const &DB);
202 ~CacheDB();
b2e465d6
AL
203};
204
205#endif