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