use 'best' hash for source authentication
[ntk/apt.git] / ftparchive / cachedb.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: cachedb.h,v 1.4 2004/05/08 19:41:01 mdz Exp $
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
15 #include <apt-pkg/debfile.h>
16
17 #include <db.h>
18 #include <errno.h>
19 #include <string>
20 #include <string.h>
21 #include <stdint.h>
22 #include <stdio.h>
23
24 #include "contents.h"
25 #include "sources.h"
26
27 class FileFd;
28
29
30 class CacheDB
31 {
32 protected:
33
34 // Database state/access
35 DBT Key;
36 DBT Data;
37 char TmpKey[600];
38 DB *Dbp;
39 bool DBLoaded;
40 bool ReadOnly;
41 std::string DBFile;
42
43 // Generate a key for the DB of a given type
44 void _InitQuery(const char *Type)
45 {
46 memset(&Key,0,sizeof(Key));
47 memset(&Data,0,sizeof(Data));
48 Key.data = TmpKey;
49 Key.size = snprintf(TmpKey,sizeof(TmpKey),"%s:%s",FileName.c_str(), Type);
50 }
51
52 void InitQueryStats() {
53 _InitQuery("st");
54 }
55 void InitQuerySource() {
56 _InitQuery("cs");
57 }
58 void InitQueryControl() {
59 _InitQuery("cl");
60 }
61 void InitQueryContent() {
62 _InitQuery("cn");
63 }
64
65 inline bool Get()
66 {
67 return Dbp->get(Dbp,0,&Key,&Data,0) == 0;
68 };
69 inline bool Put(const void *In,unsigned long const &Length)
70 {
71 if (ReadOnly == true)
72 return true;
73 Data.size = Length;
74 Data.data = (void *)In;
75 if (DBLoaded == true && (errno = Dbp->put(Dbp,0,&Key,&Data,0)) != 0)
76 {
77 DBLoaded = false;
78 return false;
79 }
80 return true;
81 }
82 bool OpenFile();
83 void CloseFile();
84
85 bool OpenDebFile();
86 void CloseDebFile();
87
88 bool GetFileStat(bool const &doStat = false);
89 bool GetCurStat();
90 bool LoadControl();
91 bool LoadContents(bool const &GenOnly);
92 bool LoadSource();
93 bool GetMD5(bool const &GenOnly);
94 bool GetSHA1(bool const &GenOnly);
95 bool GetSHA256(bool const &GenOnly);
96 bool GetSHA512(bool const &GenOnly);
97
98 // Stat info stored in the DB, Fixed types since it is written to disk.
99 enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2),
100 FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5),
101 FlSHA512=(1<<6), FlSource=(1<<7),
102 };
103
104 struct StatStore
105 {
106 uint32_t Flags;
107 uint32_t mtime;
108 uint64_t FileSize;
109 uint8_t MD5[16];
110 uint8_t SHA1[20];
111 uint8_t SHA256[32];
112 uint8_t SHA512[64];
113 } CurStat;
114 struct StatStore OldStat;
115
116 // 'set' state
117 std::string FileName;
118 FileFd *Fd;
119 debDebFile *DebFile;
120
121 public:
122
123 // Data collection helpers
124 debDebFile::MemControlExtract Control;
125 ContentsExtract Contents;
126 DscExtract Dsc;
127
128 std::string MD5Res;
129 std::string SHA1Res;
130 std::string SHA256Res;
131 std::string SHA512Res;
132
133 // Runtime statistics
134 struct Stats
135 {
136 double Bytes;
137 double MD5Bytes;
138 double SHA1Bytes;
139 double SHA256Bytes;
140 double SHA512Bytes;
141 unsigned long Packages;
142 unsigned long Misses;
143 unsigned long long DeLinkBytes;
144
145 inline void Add(const Stats &S) {
146 Bytes += S.Bytes;
147 MD5Bytes += S.MD5Bytes;
148 SHA1Bytes += S.SHA1Bytes;
149 SHA256Bytes += S.SHA256Bytes;
150 SHA512Bytes += S.SHA512Bytes;
151 Packages += S.Packages;
152 Misses += S.Misses;
153 DeLinkBytes += S.DeLinkBytes;
154 };
155 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0),
156 SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {};
157 } Stats;
158
159 bool ReadyDB(std::string const &DB);
160 inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;};
161 inline bool Loaded() {return DBLoaded == true;};
162
163 inline unsigned long long GetFileSize(void) {return CurStat.FileSize;}
164
165 bool SetFile(std::string const &FileName,struct stat St,FileFd *Fd);
166
167 // terrible old overloaded interface
168 bool GetFileInfo(std::string const &FileName,
169 bool const &DoControl,
170 bool const &DoContents,
171 bool const &GenContentsOnly,
172 bool const &DoSource,
173 bool const &DoMD5,
174 bool const &DoSHA1,
175 bool const &DoSHA256,
176 bool const &DoSHA512,
177 bool const &checkMtime = false);
178
179 bool Finish();
180
181 bool Clean();
182
183 CacheDB(std::string const &DB) : Dbp(0), Fd(NULL), DebFile(0) {TmpKey[0]='\0'; ReadyDB(DB);};
184 ~CacheDB() {ReadyDB(std::string()); delete DebFile;};
185 };
186
187 #endif