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