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>
32 // Database state/access
41 // Generate a key for the DB of a given type
42 inline void InitQuery(const char *Type
)
44 memset(&Key
,0,sizeof(Key
));
45 memset(&Data
,0,sizeof(Data
));
47 Key
.size
= snprintf(TmpKey
,sizeof(TmpKey
),"%s:%s",FileName
.c_str(), Type
);
52 return Dbp
->get(Dbp
,0,&Key
,&Data
,0) == 0;
54 inline bool Put(const void *In
,unsigned long const &Length
)
59 Data
.data
= (void *)In
;
60 if (DBLoaded
== true && (errno
= Dbp
->put(Dbp
,0,&Key
,&Data
,0)) != 0)
68 bool GetFileStat(bool const &doStat
= false);
71 bool LoadContents(bool const &GenOnly
);
72 bool GetMD5(bool const &GenOnly
);
73 bool GetSHA1(bool const &GenOnly
);
74 bool GetSHA256(bool const &GenOnly
);
75 bool GetSHA512(bool const &GenOnly
);
77 // Stat info stored in the DB, Fixed types since it is written to disk.
78 enum FlagList
{FlControl
= (1<<0),FlMD5
=(1<<1),FlContents
=(1<<2),
79 FlSize
=(1<<3), FlSHA1
=(1<<4), FlSHA256
=(1<<5),
92 struct StatStore OldStat
;
101 // Data collection helpers
102 debDebFile::MemControlExtract Control
;
103 ContentsExtract Contents
;
106 std::string SHA256Res
;
107 std::string SHA512Res
;
109 // Runtime statistics
117 unsigned long Packages
;
118 unsigned long Misses
;
119 unsigned long long DeLinkBytes
;
121 inline void Add(const Stats
&S
) {
123 MD5Bytes
+= S
.MD5Bytes
;
124 SHA1Bytes
+= S
.SHA1Bytes
;
125 SHA256Bytes
+= S
.SHA256Bytes
;
126 SHA512Bytes
+= S
.SHA512Bytes
;
127 Packages
+= S
.Packages
;
129 DeLinkBytes
+= S
.DeLinkBytes
;
131 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0),
132 SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {};
135 bool ReadyDB(std::string
const &DB
);
136 inline bool DBFailed() {return Dbp
!= 0 && DBLoaded
== false;};
137 inline bool Loaded() {return DBLoaded
== true;};
139 inline unsigned long long GetFileSize(void) {return CurStat
.FileSize
;}
141 bool SetFile(std::string
const &FileName
,struct stat St
,FileFd
*Fd
);
142 bool GetFileInfo(std::string
const &FileName
, bool const &DoControl
, bool const &DoContents
, bool const &GenContentsOnly
,
143 bool const &DoMD5
, bool const &DoSHA1
, bool const &DoSHA256
, bool const &DoSHA512
, bool const &checkMtime
= false);
148 CacheDB(std::string
const &DB
) : Dbp(0), Fd(NULL
), DebFile(0) {TmpKey
[0]='\0'; ReadyDB(DB
);};
149 ~CacheDB() {ReadyDB(std::string()); delete DebFile
;};