]> git.saurik.com Git - apt.git/blob - ftparchive/cachedb.h
merged from donkult
[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
16 #include <apt-pkg/debfile.h>
17
18 #include <db.h>
19 #include <inttypes.h>
20 #include <sys/stat.h>
21 #include <errno.h>
22 #include <string>
23
24 #include "contents.h"
25
26 class 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;
37 std::string DBFile;
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;
45 Key.size = snprintf(TmpKey,sizeof(TmpKey),"%s:%s",FileName.c_str(), Type);
46 }
47
48 inline bool Get()
49 {
50 return Dbp->get(Dbp,0,&Key,&Data,0) == 0;
51 };
52 inline bool Put(const void *In,unsigned long const &Length)
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 }
65 bool OpenFile();
66 bool GetFileStat(bool const &doStat = false);
67 bool GetCurStat();
68 bool LoadControl();
69 bool LoadContents(bool const &GenOnly);
70 bool GetMD5(bool const &GenOnly);
71 bool GetSHA1(bool const &GenOnly);
72 bool GetSHA256(bool const &GenOnly);
73 bool GetSHA512(bool const &GenOnly);
74
75 // Stat info stored in the DB, Fixed types since it is written to disk.
76 enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2),
77 FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5),
78 FlSHA512=(1<<6)};
79
80 struct StatStore
81 {
82 uint32_t Flags;
83 uint32_t mtime;
84 uint64_t FileSize;
85 uint8_t MD5[16];
86 uint8_t SHA1[20];
87 uint8_t SHA256[32];
88 uint8_t SHA512[64];
89 } CurStat;
90 struct StatStore OldStat;
91
92 // 'set' state
93 std::string FileName;
94 FileFd *Fd;
95 debDebFile *DebFile;
96
97 public:
98
99 // Data collection helpers
100 debDebFile::MemControlExtract Control;
101 ContentsExtract Contents;
102 std::string MD5Res;
103 std::string SHA1Res;
104 std::string SHA256Res;
105 std::string SHA512Res;
106
107 // Runtime statistics
108 struct Stats
109 {
110 double Bytes;
111 double MD5Bytes;
112 double SHA1Bytes;
113 double SHA256Bytes;
114 double SHA512Bytes;
115 unsigned long Packages;
116 unsigned long Misses;
117 unsigned long long DeLinkBytes;
118
119 inline void Add(const Stats &S) {
120 Bytes += S.Bytes;
121 MD5Bytes += S.MD5Bytes;
122 SHA1Bytes += S.SHA1Bytes;
123 SHA256Bytes += S.SHA256Bytes;
124 SHA512Bytes += S.SHA512Bytes;
125 Packages += S.Packages;
126 Misses += S.Misses;
127 DeLinkBytes += S.DeLinkBytes;
128 };
129 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0), Packages(0), Misses(0), DeLinkBytes(0) {};
130 } Stats;
131
132 bool ReadyDB(std::string const &DB);
133 inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;};
134 inline bool Loaded() {return DBLoaded == true;};
135
136 inline unsigned long long GetFileSize(void) {return CurStat.FileSize;}
137
138 bool SetFile(std::string const &FileName,struct stat St,FileFd *Fd);
139 bool GetFileInfo(std::string const &FileName, bool const &DoControl, bool const &DoContents, bool const &GenContentsOnly,
140 bool const &DoMD5, bool const &DoSHA1, bool const &DoSHA256, bool const &DoSHA512, bool const &checkMtime = false);
141 bool Finish();
142
143 bool Clean();
144
145 CacheDB(std::string const &DB) : Dbp(0), Fd(NULL), DebFile(0) {ReadyDB(DB);};
146 ~CacheDB() {ReadyDB(std::string()); delete DebFile;};
147 };
148
149 #endif