1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cachedb.h,v 1.4 2004/05/08 19:41:01 mdz Exp $
4 /* ######################################################################
8 Simple uniform interface to a cache database.
10 ##################################################################### */
15 #include <apt-pkg/debfile.h>
34 // Database state/access
43 // Generate a key for the DB of a given type
44 void _InitQuery(const char *Type
)
46 memset(&Key
,0,sizeof(Key
));
47 memset(&Data
,0,sizeof(Data
));
49 Key
.size
= snprintf(TmpKey
,sizeof(TmpKey
),"%s:%s",FileName
.c_str(), Type
);
52 void InitQueryStats() {
55 void InitQuerySource() {
58 void InitQueryControl() {
61 void InitQueryContent() {
67 return Dbp
->get(Dbp
,0,&Key
,&Data
,0) == 0;
69 inline bool Put(const void *In
,unsigned long const &Length
)
74 Data
.data
= (void *)In
;
75 if (DBLoaded
== true && (errno
= Dbp
->put(Dbp
,0,&Key
,&Data
,0)) != 0)
88 // GetCurStat needs some compat code, see lp #1274466)
89 bool GetCurStatCompatOldFormat();
90 bool GetCurStatCompatNewFormat();
93 bool GetFileStat(bool const &doStat
= false);
95 bool LoadContents(bool const &GenOnly
);
97 bool GetMD5(bool const &GenOnly
);
98 bool GetSHA1(bool const &GenOnly
);
99 bool GetSHA256(bool const &GenOnly
);
100 bool GetSHA512(bool const &GenOnly
);
102 // Stat info stored in the DB, Fixed types since it is written to disk.
103 enum FlagList
{FlControl
= (1<<0),FlMD5
=(1<<1),FlContents
=(1<<2),
104 FlSize
=(1<<3), FlSHA1
=(1<<4), FlSHA256
=(1<<5),
105 FlSHA512
=(1<<6), FlSource
=(1<<7),
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
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
132 struct StatStore OldStat
;
135 std::string FileName
;
141 // Data collection helpers
142 debDebFile::MemControlExtract Control
;
143 ContentsExtract Contents
;
148 std::string SHA256Res
;
149 std::string SHA512Res
;
151 // Runtime statistics
159 unsigned long Packages
;
160 unsigned long Misses
;
161 unsigned long long DeLinkBytes
;
163 inline void Add(const Stats
&S
) {
165 MD5Bytes
+= S
.MD5Bytes
;
166 SHA1Bytes
+= S
.SHA1Bytes
;
167 SHA256Bytes
+= S
.SHA256Bytes
;
168 SHA512Bytes
+= S
.SHA512Bytes
;
169 Packages
+= S
.Packages
;
171 DeLinkBytes
+= S
.DeLinkBytes
;
173 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0),
174 SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {};
177 bool ReadyDB(std::string
const &DB
= "");
178 inline bool DBFailed() {return Dbp
!= 0 && DBLoaded
== false;};
179 inline bool Loaded() {return DBLoaded
== true;};
181 inline unsigned long long GetFileSize(void) {return CurStat
.FileSize
;}
183 bool SetFile(std::string
const &FileName
,struct stat St
,FileFd
*Fd
);
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
,
193 bool const &DoSHA256
,
194 bool const &DoSHA512
,
195 bool const &checkMtime
= false);
201 CacheDB(std::string
const &DB
);