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 ##################################################################### */
16 #include <apt-pkg/debfile.h>
30 // Database state/access
39 // Generate a key for the DB of a given type
40 inline void InitQuery(const char *Type
)
42 memset(&Key
,0,sizeof(Key
));
43 memset(&Data
,0,sizeof(Data
));
45 Key
.size
= snprintf(TmpKey
,sizeof(TmpKey
),"%s:%s",FileName
.c_str(), Type
);
50 return Dbp
->get(Dbp
,0,&Key
,&Data
,0) == 0;
52 inline bool Put(const void *In
,unsigned long const &Length
)
57 Data
.data
= (void *)In
;
58 if (DBLoaded
== true && (errno
= Dbp
->put(Dbp
,0,&Key
,&Data
,0)) != 0)
66 bool GetFileStat(bool const &doStat
= false);
69 bool LoadContents(bool const &GenOnly
);
70 bool GetMD5(bool const &GenOnly
);
71 bool GetSHA1(bool const &GenOnly
);
72 bool GetSHA256(bool const &GenOnly
);
73 bool GetSHA512(bool const &GenOnly
);
75 // Stat info stored in the DB, Fixed types since it is written to disk.
76 enum FlagList
{FlControl
= (1<<0),FlMD5
=(1<<1),FlContents
=(1<<2),
77 FlSize
=(1<<3), FlSHA1
=(1<<4), FlSHA256
=(1<<5),
90 struct StatStore OldStat
;
99 // Data collection helpers
100 debDebFile::MemControlExtract Control
;
101 ContentsExtract Contents
;
104 std::string SHA256Res
;
105 std::string SHA512Res
;
107 // Runtime statistics
115 unsigned long Packages
;
116 unsigned long Misses
;
117 unsigned long long DeLinkBytes
;
119 inline void Add(const Stats
&S
) {
121 MD5Bytes
+= S
.MD5Bytes
;
122 SHA1Bytes
+= S
.SHA1Bytes
;
123 SHA256Bytes
+= S
.SHA256Bytes
;
124 SHA512Bytes
+= S
.SHA512Bytes
;
125 Packages
+= S
.Packages
;
127 DeLinkBytes
+= S
.DeLinkBytes
;
129 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0), Packages(0), Misses(0), DeLinkBytes(0) {};
132 bool ReadyDB(std::string
const &DB
);
133 inline bool DBFailed() {return Dbp
!= 0 && DBLoaded
== false;};
134 inline bool Loaded() {return DBLoaded
== true;};
136 inline unsigned long long GetFileSize(void) {return CurStat
.FileSize
;}
138 bool SetFile(std::string
const &FileName
,struct stat St
,FileFd
*Fd
);
139 bool GetFileInfo(std::string
const &FileName
, bool const &DoControl
, bool const &DoContents
, bool const &GenContentsOnly
,
140 bool const &DoMD5
, bool const &DoSHA1
, bool const &DoSHA256
, bool const &DoSHA512
, bool const &checkMtime
= false);
145 CacheDB(std::string
const &DB
) : Dbp(0), Fd(NULL
), DebFile(0) {ReadyDB(DB
);};
146 ~CacheDB() {ReadyDB(std::string()); delete DebFile
;};