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