]> git.saurik.com Git - apt.git/blob - ftparchive/cachedb.h
4e33c863505f02a0d5a832bd85fdab60d27bdadb
[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 inline 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 inline bool Get()
53 {
54 return Dbp->get(Dbp,0,&Key,&Data,0) == 0;
55 };
56 inline bool Put(const void *In,unsigned long const &Length)
57 {
58 if (ReadOnly == true)
59 return true;
60 Data.size = Length;
61 Data.data = (void *)In;
62 if (DBLoaded == true && (errno = Dbp->put(Dbp,0,&Key,&Data,0)) != 0)
63 {
64 DBLoaded = false;
65 return false;
66 }
67 return true;
68 }
69 bool OpenFile();
70 void CloseFile();
71
72 bool OpenDebFile();
73 void CloseDebFile();
74
75 bool GetFileStat(bool const &doStat = false);
76 bool GetCurStat();
77 bool LoadControl();
78 bool LoadContents(bool const &GenOnly);
79 bool LoadSource();
80 bool GetMD5(bool const &GenOnly);
81 bool GetSHA1(bool const &GenOnly);
82 bool GetSHA256(bool const &GenOnly);
83 bool GetSHA512(bool const &GenOnly);
84
85 // Stat info stored in the DB, Fixed types since it is written to disk.
86 enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2),
87 FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5),
88 FlSHA512=(1<<6), FlSource=(1<<7),
89 };
90
91 struct StatStore
92 {
93 uint32_t Flags;
94 uint32_t mtime;
95 uint64_t FileSize;
96 uint8_t MD5[16];
97 uint8_t SHA1[20];
98 uint8_t SHA256[32];
99 uint8_t SHA512[64];
100 } CurStat;
101 struct StatStore OldStat;
102
103 // 'set' state
104 std::string FileName;
105 FileFd *Fd;
106 debDebFile *DebFile;
107
108 public:
109
110 // Data collection helpers
111 debDebFile::MemControlExtract Control;
112 ContentsExtract Contents;
113 DscExtract Dsc;
114
115 std::string MD5Res;
116 std::string SHA1Res;
117 std::string SHA256Res;
118 std::string SHA512Res;
119
120 // Runtime statistics
121 struct Stats
122 {
123 double Bytes;
124 double MD5Bytes;
125 double SHA1Bytes;
126 double SHA256Bytes;
127 double SHA512Bytes;
128 unsigned long Packages;
129 unsigned long Misses;
130 unsigned long long DeLinkBytes;
131
132 inline void Add(const Stats &S) {
133 Bytes += S.Bytes;
134 MD5Bytes += S.MD5Bytes;
135 SHA1Bytes += S.SHA1Bytes;
136 SHA256Bytes += S.SHA256Bytes;
137 SHA512Bytes += S.SHA512Bytes;
138 Packages += S.Packages;
139 Misses += S.Misses;
140 DeLinkBytes += S.DeLinkBytes;
141 };
142 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0),
143 SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {};
144 } Stats;
145
146 bool ReadyDB(std::string const &DB);
147 inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;};
148 inline bool Loaded() {return DBLoaded == true;};
149
150 inline unsigned long long GetFileSize(void) {return CurStat.FileSize;}
151
152 bool SetFile(std::string const &FileName,struct stat St,FileFd *Fd);
153
154 // terrible old overloaded interface
155 bool GetFileInfo(std::string const &FileName,
156 bool const &DoControl,
157 bool const &DoContents,
158 bool const &GenContentsOnly,
159 bool const &DoSource,
160 bool const &DoMD5,
161 bool const &DoSHA1,
162 bool const &DoSHA256,
163 bool const &DoSHA512,
164 bool const &checkMtime = false);
165
166 bool Finish();
167
168 bool Clean();
169
170 CacheDB(std::string const &DB) : Dbp(0), Fd(NULL), DebFile(0) {TmpKey[0]='\0'; ReadyDB(DB);};
171 ~CacheDB() {ReadyDB(std::string()); delete DebFile;};
172 };
173
174 #endif