]> git.saurik.com Git - apt-legacy.git/blob - ftparchive/cachedb.cc
Added some initial debtags data.
[apt-legacy.git] / ftparchive / cachedb.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: cachedb.cc,v 1.7 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 // Include Files /*{{{*/
13 #ifdef __GNUG__
14 #pragma implementation "cachedb.h"
15 #endif
16
17 #include "cachedb.h"
18
19 #include <apti18n.h>
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/md5.h>
22 #include <apt-pkg/sha1.h>
23 #include <apt-pkg/sha256.h>
24 #include <apt-pkg/strutl.h>
25 #include <apt-pkg/configuration.h>
26
27 #include <netinet/in.h> // htonl, etc
28 /*}}}*/
29
30 // CacheDB::ReadyDB - Ready the DB2 /*{{{*/
31 // ---------------------------------------------------------------------
32 /* This opens the DB2 file for caching package information */
33 bool CacheDB::ReadyDB(string DB)
34 {
35 int err;
36
37 ReadOnly = _config->FindB("APT::FTPArchive::ReadOnlyDB",false);
38
39 // Close the old DB
40 if (Dbp != 0)
41 Dbp->close(Dbp,0);
42
43 /* Check if the DB was disabled while running and deal with a
44 corrupted DB */
45 if (DBFailed() == true)
46 {
47 _error->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile.c_str());
48 rename(DBFile.c_str(),(DBFile+".old").c_str());
49 }
50
51 DBLoaded = false;
52 Dbp = 0;
53 DBFile = string();
54
55 if (DB.empty())
56 return true;
57
58 db_create(&Dbp, NULL, 0);
59 if ((err = Dbp->open(Dbp, NULL, DB.c_str(), NULL, DB_BTREE,
60 (ReadOnly?DB_RDONLY:DB_CREATE),
61 0644)) != 0)
62 {
63 if (err == DB_OLD_VERSION)
64 {
65 _error->Warning(_("DB is old, attempting to upgrade %s"),DBFile.c_str());
66 err = Dbp->upgrade(Dbp, DB.c_str(), 0);
67 if (!err)
68 err = Dbp->open(Dbp, NULL, DB.c_str(), NULL, DB_HASH,
69 (ReadOnly?DB_RDONLY:DB_CREATE), 0644);
70
71 }
72 // the database format has changed from DB_HASH to DB_BTREE in
73 // apt 0.6.44
74 if (err == EINVAL)
75 {
76 _error->Error(_("DB format is invalid. If you upgraded from a older version of apt, please remove and re-create the database."));
77 }
78 if (err)
79 {
80 Dbp = 0;
81 return _error->Error(_("Unable to open DB file %s: %s"),DB.c_str(), db_strerror(err));
82 }
83 }
84
85 DBFile = DB;
86 DBLoaded = true;
87 return true;
88 }
89 /*}}}*/
90 // CacheDB::OpenFile - Open the filei /*{{{*/
91 // ---------------------------------------------------------------------
92 /* */
93 bool CacheDB::OpenFile()
94 {
95 Fd = new FileFd(FileName,FileFd::ReadOnly);
96 if (_error->PendingError() == true)
97 {
98 delete Fd;
99 Fd = NULL;
100 return false;
101 }
102 return true;
103 }
104 /*}}}*/
105 // CacheDB::GetFileStat - Get stats from the file /*{{{*/
106 // ---------------------------------------------------------------------
107 /* This gets the size from the database if it's there. If we need
108 * to look at the file, also get the mtime from the file. */
109 bool CacheDB::GetFileStat()
110 {
111 if ((CurStat.Flags & FlSize) == FlSize)
112 {
113 /* Already worked out the file size */
114 }
115 else
116 {
117 /* Get it from the file. */
118 if (Fd == NULL && OpenFile() == false)
119 {
120 return false;
121 }
122 // Stat the file
123 struct stat St;
124 if (fstat(Fd->Fd(),&St) != 0)
125 {
126 return _error->Errno("fstat",
127 _("Failed to stat %s"),FileName.c_str());
128 }
129 CurStat.FileSize = St.st_size;
130 CurStat.mtime = htonl(St.st_mtime);
131 CurStat.Flags |= FlSize;
132 }
133 return true;
134 }
135 /*}}}*/
136 // CacheDB::GetCurStat - Set the CurStat variable. /*{{{*/
137 // ---------------------------------------------------------------------
138 /* Sets the CurStat variable. Either to 0 if no database is used
139 * or to the value in the database if one is used */
140 bool CacheDB::GetCurStat()
141 {
142 memset(&CurStat,0,sizeof(CurStat));
143
144 if (DBLoaded)
145 {
146 /* First see if thre is anything about it
147 in the database */
148
149 /* Get the flags (and mtime) */
150 InitQuery("st");
151 // Ensure alignment of the returned structure
152 Data.data = &CurStat;
153 Data.ulen = sizeof(CurStat);
154 Data.flags = DB_DBT_USERMEM;
155 if (Get() == false)
156 {
157 CurStat.Flags = 0;
158 }
159 CurStat.Flags = ntohl(CurStat.Flags);
160 CurStat.FileSize = ntohl(CurStat.FileSize);
161 }
162 return true;
163 }
164 /*}}}*/
165 // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
166 // ---------------------------------------------------------------------
167 bool CacheDB::GetFileInfo(string FileName, bool DoControl, bool DoContents,
168 bool GenContentsOnly,
169 bool DoMD5, bool DoSHA1, bool DoSHA256)
170 {
171 this->FileName = FileName;
172
173 if (GetCurStat() == false)
174 {
175 return false;
176 }
177 OldStat = CurStat;
178
179 if (GetFileStat() == false)
180 {
181 delete Fd;
182 Fd = NULL;
183 return false;
184 }
185
186 Stats.Bytes += CurStat.FileSize;
187 Stats.Packages++;
188
189 if (DoControl && LoadControl() == false
190 || DoContents && LoadContents(GenContentsOnly) == false
191 || DoMD5 && GetMD5(false) == false
192 || DoSHA1 && GetSHA1(false) == false
193 || DoSHA256 && GetSHA256(false) == false)
194 {
195 delete Fd;
196 Fd = NULL;
197 delete DebFile;
198 DebFile = NULL;
199 return false;
200 }
201
202 delete Fd;
203 Fd = NULL;
204 delete DebFile;
205 DebFile = NULL;
206
207 return true;
208 }
209 /*}}}*/
210 // CacheDB::LoadControl - Load Control information /*{{{*/
211 // ---------------------------------------------------------------------
212 /* */
213 bool CacheDB::LoadControl()
214 {
215 // Try to read the control information out of the DB.
216 if ((CurStat.Flags & FlControl) == FlControl)
217 {
218 // Lookup the control information
219 InitQuery("cl");
220 if (Get() == true && Control.TakeControl(Data.data,Data.size) == true)
221 return true;
222 CurStat.Flags &= ~FlControl;
223 }
224
225 if (Fd == NULL && OpenFile() == false)
226 {
227 return false;
228 }
229 // Create a deb instance to read the archive
230 if (DebFile == 0)
231 {
232 DebFile = new debDebFile(*Fd);
233 if (_error->PendingError() == true)
234 return false;
235 }
236
237 Stats.Misses++;
238 if (Control.Read(*DebFile) == false)
239 return false;
240
241 if (Control.Control == 0)
242 return _error->Error(_("Archive has no control record"));
243
244 // Write back the control information
245 InitQuery("cl");
246 if (Put(Control.Control,Control.Length) == true)
247 CurStat.Flags |= FlControl;
248 return true;
249 }
250 /*}}}*/
251 // CacheDB::LoadContents - Load the File Listing /*{{{*/
252 // ---------------------------------------------------------------------
253 /* */
254 bool CacheDB::LoadContents(bool GenOnly)
255 {
256 // Try to read the control information out of the DB.
257 if ((CurStat.Flags & FlContents) == FlContents)
258 {
259 if (GenOnly == true)
260 return true;
261
262 // Lookup the contents information
263 InitQuery("cn");
264 if (Get() == true)
265 {
266 if (Contents.TakeContents(Data.data,Data.size) == true)
267 return true;
268 }
269
270 CurStat.Flags &= ~FlContents;
271 }
272
273 if (Fd == NULL && OpenFile() == false)
274 {
275 return false;
276 }
277 // Create a deb instance to read the archive
278 if (DebFile == 0)
279 {
280 DebFile = new debDebFile(*Fd);
281 if (_error->PendingError() == true)
282 return false;
283 }
284
285 if (Contents.Read(*DebFile) == false)
286 return false;
287
288 // Write back the control information
289 InitQuery("cn");
290 if (Put(Contents.Data,Contents.CurSize) == true)
291 CurStat.Flags |= FlContents;
292 return true;
293 }
294 /*}}}*/
295
296 static string bytes2hex(uint8_t *bytes, size_t length) {
297 char space[65];
298 if (length * 2 > sizeof(space) - 1) length = (sizeof(space) - 1) / 2;
299 for (size_t i = 0; i < length; i++)
300 snprintf(&space[i*2], 3, "%02x", bytes[i]);
301 return string(space);
302 }
303
304 static inline unsigned char xdig2num(char dig) {
305 if (isdigit(dig)) return dig - '0';
306 if ('a' <= dig && dig <= 'f') return dig - 'a' + 10;
307 if ('A' <= dig && dig <= 'F') return dig - 'A' + 10;
308 return 0;
309 }
310
311 static void hex2bytes(uint8_t *bytes, const char *hex, int length) {
312 while (length-- > 0) {
313 *bytes = 0;
314 if (isxdigit(hex[0]) && isxdigit(hex[1])) {
315 *bytes = xdig2num(hex[0]) * 16 + xdig2num(hex[1]);
316 hex += 2;
317 }
318 bytes++;
319 }
320 }
321
322 void *memrchr(void *data, char value, int size) {
323 char *cdata = (char *) data;
324 for (int i = 0; i != size; ++i)
325 if (cdata[size - i - 1] == value)
326 return cdata + size - i - 1;
327 return NULL;
328 }
329
330 // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
331 // ---------------------------------------------------------------------
332 /* */
333 bool CacheDB::GetMD5(bool GenOnly)
334 {
335 // Try to read the control information out of the DB.
336 if ((CurStat.Flags & FlMD5) == FlMD5)
337 {
338 if (GenOnly == true)
339 return true;
340
341 MD5Res = bytes2hex(CurStat.MD5, sizeof(CurStat.MD5));
342 return true;
343 }
344
345 Stats.MD5Bytes += CurStat.FileSize;
346
347 if (Fd == NULL && OpenFile() == false)
348 {
349 return false;
350 }
351 MD5Summation MD5;
352 if (Fd->Seek(0) == false || MD5.AddFD(Fd->Fd(),CurStat.FileSize) == false)
353 return false;
354
355 MD5Res = MD5.Result();
356 hex2bytes(CurStat.MD5, MD5Res.data(), sizeof(CurStat.MD5));
357 CurStat.Flags |= FlMD5;
358 return true;
359 }
360 /*}}}*/
361 // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
362 // ---------------------------------------------------------------------
363 /* */
364 bool CacheDB::GetSHA1(bool GenOnly)
365 {
366 // Try to read the control information out of the DB.
367 if ((CurStat.Flags & FlSHA1) == FlSHA1)
368 {
369 if (GenOnly == true)
370 return true;
371
372 SHA1Res = bytes2hex(CurStat.SHA1, sizeof(CurStat.SHA1));
373 return true;
374 }
375
376 Stats.SHA1Bytes += CurStat.FileSize;
377
378 if (Fd == NULL && OpenFile() == false)
379 {
380 return false;
381 }
382 SHA1Summation SHA1;
383 if (Fd->Seek(0) == false || SHA1.AddFD(Fd->Fd(),CurStat.FileSize) == false)
384 return false;
385
386 SHA1Res = SHA1.Result();
387 hex2bytes(CurStat.SHA1, SHA1Res.data(), sizeof(CurStat.SHA1));
388 CurStat.Flags |= FlSHA1;
389 return true;
390 }
391 /*}}}*/
392 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
393 // ---------------------------------------------------------------------
394 /* */
395 bool CacheDB::GetSHA256(bool GenOnly)
396 {
397 // Try to read the control information out of the DB.
398 if ((CurStat.Flags & FlSHA256) == FlSHA256)
399 {
400 if (GenOnly == true)
401 return true;
402
403 SHA256Res = bytes2hex(CurStat.SHA256, sizeof(CurStat.SHA256));
404 return true;
405 }
406
407 Stats.SHA256Bytes += CurStat.FileSize;
408
409 if (Fd == NULL && OpenFile() == false)
410 {
411 return false;
412 }
413 SHA256Summation SHA256;
414 if (Fd->Seek(0) == false || SHA256.AddFD(Fd->Fd(),CurStat.FileSize) == false)
415 return false;
416
417 SHA256Res = SHA256.Result();
418 hex2bytes(CurStat.SHA256, SHA256Res.data(), sizeof(CurStat.SHA256));
419 CurStat.Flags |= FlSHA256;
420 return true;
421 }
422 /*}}}*/
423 // CacheDB::Finish - Write back the cache structure /*{{{*/
424 // ---------------------------------------------------------------------
425 /* */
426 bool CacheDB::Finish()
427 {
428 // Optimize away some writes.
429 if (CurStat.Flags == OldStat.Flags &&
430 CurStat.mtime == OldStat.mtime)
431 return true;
432
433 // Write the stat information
434 CurStat.Flags = htonl(CurStat.Flags);
435 CurStat.FileSize = htonl(CurStat.FileSize);
436 InitQuery("st");
437 Put(&CurStat,sizeof(CurStat));
438 CurStat.Flags = ntohl(CurStat.Flags);
439 CurStat.FileSize = ntohl(CurStat.FileSize);
440
441 return true;
442 }
443 /*}}}*/
444 // CacheDB::Clean - Clean the Database /*{{{*/
445 // ---------------------------------------------------------------------
446 /* Tidy the database by removing files that no longer exist at all. */
447 bool CacheDB::Clean()
448 {
449 if (DBLoaded == false)
450 return true;
451
452 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
453 needs the lower one and 2.7.7 needs the upper.. */
454 DBC *Cursor;
455 if ((errno = Dbp->cursor(Dbp, NULL, &Cursor, 0)) != 0)
456 return _error->Error(_("Unable to get a cursor"));
457
458 DBT Key;
459 DBT Data;
460 memset(&Key,0,sizeof(Key));
461 memset(&Data,0,sizeof(Data));
462 while ((errno = Cursor->c_get(Cursor,&Key,&Data,DB_NEXT)) == 0)
463 {
464 const char *Colon = (char*)memrchr(Key.data, ':', Key.size);
465 if (Colon)
466 {
467 if (stringcmp(Colon + 1, (char *)Key.data+Key.size,"st") == 0 ||
468 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cl") == 0 ||
469 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cn") == 0)
470 {
471 if (FileExists(string((const char *)Key.data,Colon)) == true)
472 continue;
473 }
474 }
475
476 Cursor->c_del(Cursor,0);
477 }
478
479 return true;
480 }
481 /*}}}*/