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