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