merge stuff from donkult
[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
13500573 15
472ff00e 16#include <apt-pkg/debfile.h>
b2e465d6 17
c9569a1e 18#include <db.h>
b2e465d6
AL
19#include <inttypes.h>
20#include <sys/stat.h>
21#include <errno.h>
472ff00e
DK
22#include <string>
23
b2e465d6 24#include "contents.h"
472ff00e 25
b2e465d6
AL
26class CacheDB
27{
28 protected:
29
30 // Database state/access
31 DBT Key;
32 DBT Data;
33 char TmpKey[600];
34 DB *Dbp;
35 bool DBLoaded;
36 bool ReadOnly;
8f3ba4e8 37 std::string DBFile;
b2e465d6
AL
38
39 // Generate a key for the DB of a given type
40 inline void InitQuery(const char *Type)
41 {
42 memset(&Key,0,sizeof(Key));
43 memset(&Data,0,sizeof(Data));
44 Key.data = TmpKey;
cde41ae8 45 Key.size = snprintf(TmpKey,sizeof(TmpKey),"%s:%s",FileName.c_str(), Type);
b2e465d6
AL
46 }
47
48 inline bool Get()
49 {
50 return Dbp->get(Dbp,0,&Key,&Data,0) == 0;
51 };
9209ec47 52 inline bool Put(const void *In,unsigned long const &Length)
b2e465d6
AL
53 {
54 if (ReadOnly == true)
55 return true;
56 Data.size = Length;
57 Data.data = (void *)In;
58 if (DBLoaded == true && (errno = Dbp->put(Dbp,0,&Key,&Data,0)) != 0)
59 {
60 DBLoaded = false;
61 return false;
62 }
63 return true;
64 }
cde41ae8 65 bool OpenFile();
ff574e76 66 bool GetFileStat(bool const &doStat = false);
cde41ae8
MV
67 bool GetCurStat();
68 bool LoadControl();
9209ec47
DK
69 bool LoadContents(bool const &GenOnly);
70 bool GetMD5(bool const &GenOnly);
71 bool GetSHA1(bool const &GenOnly);
72 bool GetSHA256(bool const &GenOnly);
9a961efc 73 bool GetSHA512(bool const &GenOnly);
b2e465d6
AL
74
75 // Stat info stored in the DB, Fixed types since it is written to disk.
cde41ae8 76 enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2),
9a961efc
MV
77 FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5),
78 FlSHA512=(1<<6)};
79
b2e465d6
AL
80 struct StatStore
81 {
b2e465d6 82 uint32_t Flags;
cde41ae8 83 uint32_t mtime;
650faab0 84 uint64_t FileSize;
cde41ae8
MV
85 uint8_t MD5[16];
86 uint8_t SHA1[20];
87 uint8_t SHA256[32];
9a961efc 88 uint8_t SHA512[64];
b2e465d6
AL
89 } CurStat;
90 struct StatStore OldStat;
91
92 // 'set' state
8f3ba4e8 93 std::string FileName;
b2e465d6
AL
94 FileFd *Fd;
95 debDebFile *DebFile;
96
97 public:
98
99 // Data collection helpers
100 debDebFile::MemControlExtract Control;
101 ContentsExtract Contents;
8f3ba4e8
DK
102 std::string MD5Res;
103 std::string SHA1Res;
104 std::string SHA256Res;
105 std::string SHA512Res;
b2e465d6
AL
106
107 // Runtime statistics
108 struct Stats
109 {
110 double Bytes;
111 double MD5Bytes;
cde41ae8
MV
112 double SHA1Bytes;
113 double SHA256Bytes;
9a961efc 114 double SHA512Bytes;
b2e465d6
AL
115 unsigned long Packages;
116 unsigned long Misses;
650faab0 117 unsigned long long DeLinkBytes;
b2e465d6 118
cde41ae8 119 inline void Add(const Stats &S) {
9a961efc
MV
120 Bytes += S.Bytes;
121 MD5Bytes += S.MD5Bytes;
122 SHA1Bytes += S.SHA1Bytes;
cde41ae8 123 SHA256Bytes += S.SHA256Bytes;
9a961efc
MV
124 SHA512Bytes += S.SHA512Bytes;
125 Packages += S.Packages;
126 Misses += S.Misses;
127 DeLinkBytes += S.DeLinkBytes;
128 };
dcaa1185
DK
129 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0),
130 SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {};
b2e465d6
AL
131 } Stats;
132
8f3ba4e8 133 bool ReadyDB(std::string const &DB);
b2e465d6
AL
134 inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;};
135 inline bool Loaded() {return DBLoaded == true;};
136
650faab0 137 inline unsigned long long GetFileSize(void) {return CurStat.FileSize;}
cde41ae8 138
8f3ba4e8
DK
139 bool SetFile(std::string const &FileName,struct stat St,FileFd *Fd);
140 bool GetFileInfo(std::string const &FileName, bool const &DoControl, bool const &DoContents, bool const &GenContentsOnly,
9a961efc 141 bool const &DoMD5, bool const &DoSHA1, bool const &DoSHA256, bool const &DoSHA512, bool const &checkMtime = false);
b2e465d6
AL
142 bool Finish();
143
144 bool Clean();
145
dcaa1185 146 CacheDB(std::string const &DB) : Dbp(0), Fd(NULL), DebFile(0) {TmpKey[0]='\0'; ReadyDB(DB);};
8f3ba4e8 147 ~CacheDB() {ReadyDB(std::string()); delete DebFile;};
b2e465d6
AL
148};
149
150#endif