without a filename we can't stat pipes
[ntk/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
AL
101 }
102
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
188 return true;
cde41ae8
MV
189}
190 /*}}}*/
191// CacheDB::GetCurStat - Set the CurStat variable. /*{{{*/
192// ---------------------------------------------------------------------
193/* Sets the CurStat variable. Either to 0 if no database is used
194 * or to the value in the database if one is used */
195bool CacheDB::GetCurStat()
b2e465d6 196{
b2e465d6
AL
197 memset(&CurStat,0,sizeof(CurStat));
198
215b0faf
MV
199 if (DBLoaded)
200 {
201 /* First see if there is anything about it
202 in the database */
203
204 /* Get the flags (and mtime) */
37497bd5 205 InitQueryStats();
215b0faf
MV
206 // Ensure alignment of the returned structure
207 Data.data = &CurStat;
208 Data.ulen = sizeof(CurStat);
209 Data.flags = DB_DBT_USERMEM;
210 if (Get() == false)
b2e465d6 211 {
b2e465d6 212 CurStat.Flags = 0;
b2e465d6 213 }
215b0faf
MV
214 CurStat.Flags = ntohl(CurStat.Flags);
215 CurStat.FileSize = ntohl(CurStat.FileSize);
b2e465d6 216 }
215b0faf 217 return true;
cde41ae8
MV
218}
219 /*}}}*/
220// CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
221// ---------------------------------------------------------------------
ce928105
MV
222bool CacheDB::GetFileInfo(std::string const &FileName, bool const &DoControl,
223 bool const &DoContents,
224 bool const &GenContentsOnly,
225 bool const &DoSource,
226 bool const &DoMD5, bool const &DoSHA1,
227 bool const &DoSHA256, bool const &DoSHA512,
9a961efc 228 bool const &checkMtime)
cde41ae8 229{
ce928105
MV
230 bool result = true;
231 this->FileName = FileName;
cde41ae8 232
ce928105 233 if (GetCurStat() == false)
ce928105 234 return false;
b2e465d6 235 OldStat = CurStat;
ff574e76 236
ce928105 237 if (GetFileStat(checkMtime) == false)
ce928105 238 return false;
cde41ae8 239
215b0faf
MV
240 /* if mtime changed, update CurStat from disk */
241 if (checkMtime == true && OldStat.mtime != CurStat.mtime)
242 CurStat.Flags = FlSize;
243
244 Stats.Bytes += CurStat.FileSize;
245 Stats.Packages++;
246
247 if ((DoControl && LoadControl() == false)
248 || (DoContents && LoadContents(GenContentsOnly) == false)
249 || (DoSource && LoadSource() == false)
250 || (DoMD5 && GetMD5(false) == false)
251 || (DoSHA1 && GetSHA1(false) == false)
252 || (DoSHA256 && GetSHA256(false) == false)
253 || (DoSHA512 && GetSHA512(false) == false) )
254 {
255 result = false;
256 }
ce928105 257
215b0faf 258 return result;
ce928105
MV
259}
260 /*}}}*/
261
262bool CacheDB::LoadSource()
263{
264 // Try to read the control information out of the DB.
265 if ((CurStat.Flags & FlSource) == FlSource)
266 {
267 // Lookup the control information
37497bd5 268 InitQuerySource();
ce928105 269 if (Get() == true && Dsc.TakeDsc(Data.data, Data.size) == true)
53ba4e2c 270 {
ce928105 271 return true;
53ba4e2c 272 }
ce928105
MV
273 CurStat.Flags &= ~FlSource;
274 }
215b0faf 275 if (OpenFile() == false)
ce928105 276 return false;
ce928105 277
ce928105
MV
278 Stats.Misses++;
279 if (Dsc.Read(FileName) == false)
280 return false;
cde41ae8 281
ce928105
MV
282 if (Dsc.Data == 0)
283 return _error->Error(_("Failed to read .dsc"));
284
285 // Write back the control information
37497bd5 286 InitQuerySource();
ce928105
MV
287 if (Put(Dsc.Data, Dsc.Length) == true)
288 CurStat.Flags |= FlSource;
cde41ae8 289
b2e465d6
AL
290 return true;
291}
ce928105 292
b2e465d6
AL
293// CacheDB::LoadControl - Load Control information /*{{{*/
294// ---------------------------------------------------------------------
295/* */
296bool CacheDB::LoadControl()
297{
298 // Try to read the control information out of the DB.
299 if ((CurStat.Flags & FlControl) == FlControl)
300 {
301 // Lookup the control information
37497bd5 302 InitQueryControl();
b2e465d6
AL
303 if (Get() == true && Control.TakeControl(Data.data,Data.size) == true)
304 return true;
305 CurStat.Flags &= ~FlControl;
306 }
307
215b0faf 308 if(OpenDebFile() == false)
cde41ae8 309 return false;
b2e465d6
AL
310
311 Stats.Misses++;
312 if (Control.Read(*DebFile) == false)
313 return false;
314
315 if (Control.Control == 0)
dc738e7a 316 return _error->Error(_("Archive has no control record"));
b2e465d6
AL
317
318 // Write back the control information
37497bd5 319 InitQueryControl();
b2e465d6
AL
320 if (Put(Control.Control,Control.Length) == true)
321 CurStat.Flags |= FlControl;
322 return true;
323}
324 /*}}}*/
325// CacheDB::LoadContents - Load the File Listing /*{{{*/
326// ---------------------------------------------------------------------
327/* */
9209ec47 328bool CacheDB::LoadContents(bool const &GenOnly)
b2e465d6
AL
329{
330 // Try to read the control information out of the DB.
331 if ((CurStat.Flags & FlContents) == FlContents)
332 {
333 if (GenOnly == true)
334 return true;
335
336 // Lookup the contents information
37497bd5 337 InitQueryContent();
b2e465d6
AL
338 if (Get() == true)
339 {
340 if (Contents.TakeContents(Data.data,Data.size) == true)
341 return true;
342 }
343
344 CurStat.Flags &= ~FlContents;
345 }
346
215b0faf 347 if(OpenDebFile() == false)
cde41ae8 348 return false;
b2e465d6 349
0a3b93fc 350 Stats.Misses++;
b2e465d6
AL
351 if (Contents.Read(*DebFile) == false)
352 return false;
353
354 // Write back the control information
37497bd5 355 InitQueryContent();
b2e465d6
AL
356 if (Put(Contents.Data,Contents.CurSize) == true)
357 CurStat.Flags |= FlContents;
358 return true;
359}
360 /*}}}*/
cde41ae8 361
8f3ba4e8 362static std::string bytes2hex(uint8_t *bytes, size_t length) {
76ef756a 363 char buf[3];
0fffbc8c 364 std::string space;
76ef756a
MV
365
366 space.reserve(length*2 + 1);
367 for (size_t i = 0; i < length; i++) {
368 snprintf(buf, sizeof(buf), "%02x", bytes[i]);
369 space.append(buf);
370 }
371 return space;
cde41ae8
MV
372}
373
9209ec47 374static inline unsigned char xdig2num(char const &dig) {
cde41ae8
MV
375 if (isdigit(dig)) return dig - '0';
376 if ('a' <= dig && dig <= 'f') return dig - 'a' + 10;
377 if ('A' <= dig && dig <= 'F') return dig - 'A' + 10;
378 return 0;
379}
380
381static void hex2bytes(uint8_t *bytes, const char *hex, int length) {
382 while (length-- > 0) {
383 *bytes = 0;
384 if (isxdigit(hex[0]) && isxdigit(hex[1])) {
385 *bytes = xdig2num(hex[0]) * 16 + xdig2num(hex[1]);
386 hex += 2;
387 }
388 bytes++;
389 }
390}
391
b2e465d6
AL
392// CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
393// ---------------------------------------------------------------------
394/* */
9209ec47 395bool CacheDB::GetMD5(bool const &GenOnly)
b2e465d6
AL
396{
397 // Try to read the control information out of the DB.
398 if ((CurStat.Flags & FlMD5) == FlMD5)
399 {
400 if (GenOnly == true)
401 return true;
402
cde41ae8 403 MD5Res = bytes2hex(CurStat.MD5, sizeof(CurStat.MD5));
b2e465d6 404 return true;
215b0faf 405 }
b2e465d6 406
cde41ae8 407 Stats.MD5Bytes += CurStat.FileSize;
b2e465d6 408
215b0faf 409 if (OpenFile() == false)
cde41ae8 410 return false;
215b0faf 411
b2e465d6 412 MD5Summation MD5;
109eb151 413 if (Fd->Seek(0) == false || MD5.AddFD(*Fd, CurStat.FileSize) == false)
b2e465d6
AL
414 return false;
415
416 MD5Res = MD5.Result();
cde41ae8 417 hex2bytes(CurStat.MD5, MD5Res.data(), sizeof(CurStat.MD5));
b2e465d6
AL
418 CurStat.Flags |= FlMD5;
419 return true;
420}
421 /*}}}*/
cde41ae8
MV
422// CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
423// ---------------------------------------------------------------------
424/* */
9209ec47 425bool CacheDB::GetSHA1(bool const &GenOnly)
cde41ae8
MV
426{
427 // Try to read the control information out of the DB.
428 if ((CurStat.Flags & FlSHA1) == FlSHA1)
429 {
430 if (GenOnly == true)
431 return true;
432
433 SHA1Res = bytes2hex(CurStat.SHA1, sizeof(CurStat.SHA1));
434 return true;
435 }
436
437 Stats.SHA1Bytes += CurStat.FileSize;
438
215b0faf 439 if (OpenFile() == false)
cde41ae8 440 return false;
215b0faf 441
cde41ae8 442 SHA1Summation SHA1;
109eb151 443 if (Fd->Seek(0) == false || SHA1.AddFD(*Fd, CurStat.FileSize) == false)
cde41ae8
MV
444 return false;
445
446 SHA1Res = SHA1.Result();
447 hex2bytes(CurStat.SHA1, SHA1Res.data(), sizeof(CurStat.SHA1));
448 CurStat.Flags |= FlSHA1;
449 return true;
450}
451 /*}}}*/
452// CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
453// ---------------------------------------------------------------------
454/* */
9209ec47 455bool CacheDB::GetSHA256(bool const &GenOnly)
cde41ae8
MV
456{
457 // Try to read the control information out of the DB.
458 if ((CurStat.Flags & FlSHA256) == FlSHA256)
459 {
460 if (GenOnly == true)
461 return true;
462
463 SHA256Res = bytes2hex(CurStat.SHA256, sizeof(CurStat.SHA256));
464 return true;
465 }
466
467 Stats.SHA256Bytes += CurStat.FileSize;
468
215b0faf 469 if (OpenFile() == false)
cde41ae8 470 return false;
215b0faf 471
cde41ae8 472 SHA256Summation SHA256;
109eb151 473 if (Fd->Seek(0) == false || SHA256.AddFD(*Fd, CurStat.FileSize) == false)
cde41ae8
MV
474 return false;
475
476 SHA256Res = SHA256.Result();
477 hex2bytes(CurStat.SHA256, SHA256Res.data(), sizeof(CurStat.SHA256));
478 CurStat.Flags |= FlSHA256;
479 return true;
480}
481 /*}}}*/
9a961efc
MV
482// CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
483// ---------------------------------------------------------------------
484/* */
485bool CacheDB::GetSHA512(bool const &GenOnly)
486{
487 // Try to read the control information out of the DB.
488 if ((CurStat.Flags & FlSHA512) == FlSHA512)
489 {
490 if (GenOnly == true)
491 return true;
492
493 SHA512Res = bytes2hex(CurStat.SHA512, sizeof(CurStat.SHA512));
494 return true;
495 }
496
497 Stats.SHA512Bytes += CurStat.FileSize;
498
215b0faf 499 if (OpenFile() == false)
9a961efc 500 return false;
215b0faf 501
9a961efc 502 SHA512Summation SHA512;
109eb151 503 if (Fd->Seek(0) == false || SHA512.AddFD(*Fd, CurStat.FileSize) == false)
9a961efc
MV
504 return false;
505
506 SHA512Res = SHA512.Result();
507 hex2bytes(CurStat.SHA512, SHA512Res.data(), sizeof(CurStat.SHA512));
508 CurStat.Flags |= FlSHA512;
509 return true;
510}
511 /*}}}*/
b2e465d6
AL
512// CacheDB::Finish - Write back the cache structure /*{{{*/
513// ---------------------------------------------------------------------
514/* */
515bool CacheDB::Finish()
516{
517 // Optimize away some writes.
518 if (CurStat.Flags == OldStat.Flags &&
9bfe66dc 519 CurStat.mtime == OldStat.mtime)
b2e465d6 520 return true;
cf6bbca0 521
b2e465d6
AL
522 // Write the stat information
523 CurStat.Flags = htonl(CurStat.Flags);
cde41ae8 524 CurStat.FileSize = htonl(CurStat.FileSize);
37497bd5 525 InitQueryStats();
b2e465d6
AL
526 Put(&CurStat,sizeof(CurStat));
527 CurStat.Flags = ntohl(CurStat.Flags);
cde41ae8
MV
528 CurStat.FileSize = ntohl(CurStat.FileSize);
529
b2e465d6
AL
530 return true;
531}
532 /*}}}*/
533// CacheDB::Clean - Clean the Database /*{{{*/
534// ---------------------------------------------------------------------
535/* Tidy the database by removing files that no longer exist at all. */
536bool CacheDB::Clean()
537{
538 if (DBLoaded == false)
539 return true;
540
541 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
542 needs the lower one and 2.7.7 needs the upper.. */
b2e465d6 543 DBC *Cursor;
c9569a1e 544 if ((errno = Dbp->cursor(Dbp, NULL, &Cursor, 0)) != 0)
dc738e7a 545 return _error->Error(_("Unable to get a cursor"));
b2e465d6
AL
546
547 DBT Key;
548 DBT Data;
549 memset(&Key,0,sizeof(Key));
550 memset(&Data,0,sizeof(Data));
551 while ((errno = Cursor->c_get(Cursor,&Key,&Data,DB_NEXT)) == 0)
552 {
592b401a
MV
553 const char *Colon = (char*)memrchr(Key.data, ':', Key.size);
554 if (Colon)
b2e465d6 555 {
592b401a
MV
556 if (stringcmp(Colon + 1, (char *)Key.data+Key.size,"st") == 0 ||
557 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cl") == 0 ||
53ba4e2c 558 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cs") == 0 ||
592b401a 559 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cn") == 0)
b2e465d6 560 {
53ba4e2c
MV
561 std::string FileName = std::string((const char *)Key.data,Colon);
562 if (FileExists(FileName) == true) {
563 continue;
564 }
b2e465d6
AL
565 }
566 }
b2e465d6
AL
567 Cursor->c_del(Cursor,0);
568 }
53ba4e2c
MV
569 int res = Dbp->compact(Dbp, NULL, NULL, NULL, NULL, DB_FREE_SPACE, NULL);
570 if (res < 0)
571 _error->Warning("compact failed with result %i", res);
572
573 if(_config->FindB("Debug::APT::FTPArchive::Clean", false) == true)
574 Dbp->stat_print(Dbp, 0);
575
b2e465d6
AL
576
577 return true;
578}
579 /*}}}*/