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 ##################################################################### */
19 #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
);
74 // Stat info stored in the DB, Fixed types since it is written to disk.
75 enum FlagList
{FlControl
= (1<<0),FlMD5
=(1<<1),FlContents
=(1<<2),
76 FlSize
=(1<<3), FlSHA1
=(1<<4), FlSHA256
=(1<<5)};
86 struct StatStore OldStat
;
95 // Data collection helpers
96 debDebFile::MemControlExtract Control
;
97 ContentsExtract Contents
;
102 // Runtime statistics
109 unsigned long Packages
;
110 unsigned long Misses
;
111 unsigned long DeLinkBytes
;
113 inline void Add(const Stats
&S
) {
114 Bytes
+= S
.Bytes
; MD5Bytes
+= S
.MD5Bytes
; SHA1Bytes
+= S
.SHA1Bytes
;
115 SHA256Bytes
+= S
.SHA256Bytes
;
116 Packages
+= S
.Packages
; Misses
+= S
.Misses
; DeLinkBytes
+= S
.DeLinkBytes
;};
117 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0), Packages(0), Misses(0), DeLinkBytes(0) {};
120 bool ReadyDB(string
const &DB
);
121 inline bool DBFailed() {return Dbp
!= 0 && DBLoaded
== false;};
122 inline bool Loaded() {return DBLoaded
== true;};
124 inline off_t
GetFileSize(void) {return CurStat
.FileSize
;}
126 bool SetFile(string
const &FileName
,struct stat St
,FileFd
*Fd
);
127 bool GetFileInfo(string
const &FileName
, bool const &DoControl
, bool const &DoContents
, bool const &GenContentsOnly
,
128 bool const &DoMD5
, bool const &DoSHA1
, bool const &DoSHA256
, bool const &checkMtime
= false);
133 CacheDB(string
const &DB
) : Dbp(0), Fd(NULL
), DebFile(0) {ReadyDB(DB
);};
134 ~CacheDB() {ReadyDB(string()); delete DebFile
;};