]> git.saurik.com Git - apt-legacy.git/blob - ftparchive/cachedb.h
Fix compilation of http when embedding into Cydia.
[apt-legacy.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
17 #include <db.h>
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
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 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 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();
67 bool GetCurStat();
68 bool LoadControl();
69 bool LoadContents(bool GenOnly);
70 bool GetMD5(bool GenOnly);
71 bool GetSHA1(bool GenOnly);
72 bool GetSHA256(bool GenOnly);
73
74 // Stat info stored in the DB, Fixed types since it is written to disk.
75 enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2),
76 FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5)};
77 struct StatStore
78 {
79 uint32_t Flags;
80 uint32_t mtime;
81 uint32_t FileSize;
82 uint8_t MD5[16];
83 uint8_t SHA1[20];
84 uint8_t SHA256[32];
85 } CurStat;
86 struct StatStore OldStat;
87
88 // 'set' state
89 string FileName;
90 FileFd *Fd;
91 debDebFile *DebFile;
92
93 public:
94
95 // Data collection helpers
96 debDebFile::MemControlExtract Control;
97 ContentsExtract Contents;
98 string MD5Res;
99 string SHA1Res;
100 string SHA256Res;
101
102 // Runtime statistics
103 struct Stats
104 {
105 double Bytes;
106 double MD5Bytes;
107 double SHA1Bytes;
108 double SHA256Bytes;
109 unsigned long Packages;
110 unsigned long Misses;
111 unsigned long DeLinkBytes;
112
113 inline void Add(const Stats &S) {
114 Bytes += S.Bytes; MD5Bytes += S.MD5Bytes; SHA1Bytes += S.SHA1Bytes;
115 SHA256Bytes += S.SHA256Bytes;
116 Packages += S.Packages; Misses += S.Misses; DeLinkBytes += S.DeLinkBytes;};
117 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0), Packages(0), Misses(0), DeLinkBytes(0) {};
118 } Stats;
119
120 bool ReadyDB(string DB);
121 inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;};
122 inline bool Loaded() {return DBLoaded == true;};
123
124 inline off_t GetFileSize(void) {return CurStat.FileSize;}
125
126 bool SetFile(string FileName,struct stat St,FileFd *Fd);
127 bool GetFileInfo(string FileName, bool DoControl, bool DoContents,
128 bool GenContentsOnly, bool DoMD5, bool DoSHA1, bool DoSHA256);
129 bool Finish();
130
131 bool Clean();
132
133 CacheDB(string DB) : Dbp(0), Fd(NULL), DebFile(0) {ReadyDB(DB);};
134 ~CacheDB() {ReadyDB(string()); delete DebFile;};
135 };
136
137 #endif