]> git.saurik.com Git - apt.git/blob - ftparchive/cachedb.h
log with the failed item description, not with next try
[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/hashes.h>
16 #include <apt-pkg/debfile.h>
17
18 #include <db.h>
19 #include <errno.h>
20 #include <string>
21 #include <string.h>
22 #include <stdint.h>
23 #include <stdio.h>
24
25 #include "contents.h"
26 #include "sources.h"
27
28 class FileFd;
29
30
31 class CacheDB
32 {
33 protected:
34
35 // Database state/access
36 DBT Key;
37 DBT Data;
38 char TmpKey[600];
39 DB *Dbp;
40 bool DBLoaded;
41 bool ReadOnly;
42 std::string DBFile;
43
44 // Generate a key for the DB of a given type
45 void _InitQuery(const char *Type)
46 {
47 memset(&Key,0,sizeof(Key));
48 memset(&Data,0,sizeof(Data));
49 Key.data = TmpKey;
50 Key.size = snprintf(TmpKey,sizeof(TmpKey),"%s:%s",FileName.c_str(), Type);
51 }
52
53 void InitQueryStats() {
54 _InitQuery("st");
55 }
56 void InitQuerySource() {
57 _InitQuery("cs");
58 }
59 void InitQueryControl() {
60 _InitQuery("cl");
61 }
62 void InitQueryContent() {
63 _InitQuery("cn");
64 }
65
66 inline bool Get()
67 {
68 return Dbp->get(Dbp,0,&Key,&Data,0) == 0;
69 };
70 inline bool Put(const void *In,unsigned long const &Length)
71 {
72 if (ReadOnly == true)
73 return true;
74 Data.size = Length;
75 Data.data = (void *)In;
76 if (DBLoaded == true && (errno = Dbp->put(Dbp,0,&Key,&Data,0)) != 0)
77 {
78 DBLoaded = false;
79 return false;
80 }
81 return true;
82 }
83 bool OpenFile();
84 void CloseFile();
85
86 bool OpenDebFile();
87 void CloseDebFile();
88
89 // GetCurStat needs some compat code, see lp #1274466)
90 bool GetCurStatCompatOldFormat();
91 bool GetCurStatCompatNewFormat();
92 bool GetCurStat();
93
94 bool GetFileStat(bool const &doStat = false);
95 bool LoadControl();
96 bool LoadContents(bool const &GenOnly);
97 bool LoadSource();
98 bool GetHashes(bool const GenOnly, unsigned int const DoHashes);
99
100 // Stat info stored in the DB, Fixed types since it is written to disk.
101 enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2),
102 FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5),
103 FlSHA512=(1<<6), FlSource=(1<<7)
104 };
105
106 // the on-disk format changed (FileSize increased to 64bit) in
107 // commit 650faab0 which will lead to corruption with old caches
108 struct StatStoreOldFormat
109 {
110 uint32_t Flags;
111 uint32_t mtime;
112 uint32_t FileSize;
113 uint8_t MD5[16];
114 uint8_t SHA1[20];
115 uint8_t SHA256[32];
116 } CurStatOldFormat;
117
118 // WARNING: this struct is read/written to the DB so do not change the
119 // layout of the fields (see lp #1274466), only append to it
120 struct StatStore
121 {
122 uint32_t Flags;
123 uint32_t mtime;
124 uint64_t FileSize;
125 uint8_t MD5[16];
126 uint8_t SHA1[20];
127 uint8_t SHA256[32];
128 uint8_t SHA512[64];
129 } CurStat;
130 struct StatStore OldStat;
131
132 // 'set' state
133 std::string FileName;
134 FileFd *Fd;
135 debDebFile *DebFile;
136
137 public:
138
139 // Data collection helpers
140 debDebFile::MemControlExtract Control;
141 ContentsExtract Contents;
142 DscExtract Dsc;
143 HashStringList HashesList;
144
145 // Runtime statistics
146 struct Stats
147 {
148 double Bytes;
149 double MD5Bytes;
150 double SHA1Bytes;
151 double SHA256Bytes;
152 double SHA512Bytes;
153 unsigned long Packages;
154 unsigned long Misses;
155 unsigned long long DeLinkBytes;
156
157 inline void Add(const Stats &S) {
158 Bytes += S.Bytes;
159 MD5Bytes += S.MD5Bytes;
160 SHA1Bytes += S.SHA1Bytes;
161 SHA256Bytes += S.SHA256Bytes;
162 SHA512Bytes += S.SHA512Bytes;
163 Packages += S.Packages;
164 Misses += S.Misses;
165 DeLinkBytes += S.DeLinkBytes;
166 };
167 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0),
168 SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {};
169 } Stats;
170
171 bool ReadyDB(std::string const &DB = "");
172 inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;};
173 inline bool Loaded() {return DBLoaded == true;};
174
175 inline unsigned long long GetFileSize(void) {return CurStat.FileSize;}
176
177 bool SetFile(std::string const &FileName,struct stat St,FileFd *Fd);
178
179 // terrible old overloaded interface
180 bool GetFileInfo(std::string const &FileName,
181 bool const &DoControl,
182 bool const &DoContents,
183 bool const &GenContentsOnly,
184 bool const DoSource,
185 unsigned int const DoHashes,
186 bool const &checkMtime = false);
187
188 bool Finish();
189
190 bool Clean();
191
192 explicit CacheDB(std::string const &DB);
193 ~CacheDB();
194 };
195
196 #endif