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