Add compat mode for old (32bit FileSize) CacheDB (LP: #1274466)
[ntk/apt.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 #include <config.h>
14
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/md5.h>
17 #include <apt-pkg/sha1.h>
18 #include <apt-pkg/sha2.h>
19 #include <apt-pkg/strutl.h>
20 #include <apt-pkg/configuration.h>
21 #include <apt-pkg/fileutl.h>
22 #include <apt-pkg/debfile.h>
23 #include <apt-pkg/gpgv.h>
24
25 #include <netinet/in.h> // htonl, etc
26 #include <ctype.h>
27 #include <stddef.h>
28 #include <sys/stat.h>
29
30 #include "cachedb.h"
31
32 #include <apti18n.h>
33 /*}}}*/
34
35 CacheDB::CacheDB(std::string const &DB)
36 : Dbp(0), Fd(NULL), DebFile(0)
37 {
38 TmpKey[0]='\0';
39 ReadyDB(DB);
40 };
41
42 CacheDB::~CacheDB()
43 {
44 ReadyDB();
45 delete DebFile;
46 };
47
48 // CacheDB::ReadyDB - Ready the DB2 /*{{{*/
49 // ---------------------------------------------------------------------
50 /* This opens the DB2 file for caching package information */
51 bool CacheDB::ReadyDB(std::string const &DB)
52 {
53 int err;
54
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 {
65 _error->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile.c_str());
66 rename(DBFile.c_str(),(DBFile+".old").c_str());
67 }
68
69 DBLoaded = false;
70 Dbp = 0;
71 DBFile = std::string();
72
73 if (DB.empty())
74 return true;
75
76 db_create(&Dbp, NULL, 0);
77 if ((err = Dbp->open(Dbp, NULL, DB.c_str(), NULL, DB_BTREE,
78 (ReadOnly?DB_RDONLY:DB_CREATE),
79 0644)) != 0)
80 {
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 }
90 // the database format has changed from DB_HASH to DB_BTREE in
91 // apt 0.6.44
92 if (err == EINVAL)
93 {
94 _error->Error(_("DB format is invalid. If you upgraded from an older version of apt, please remove and re-create the database."));
95 }
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 }
101 }
102
103 DBFile = DB;
104 DBLoaded = true;
105 return true;
106 }
107 /*}}}*/
108 // CacheDB::OpenFile - Open the file /*{{{*/
109 // ---------------------------------------------------------------------
110 /* */
111 bool CacheDB::OpenFile()
112 {
113 // always close existing file first
114 CloseFile();
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;
124 }
125 /*}}}*/
126 // CacheDB::CloseFile - Close the file /*{{{*/
127 void CacheDB::CloseFile()
128 {
129 if(Fd != NULL)
130 {
131 delete Fd;
132 Fd = NULL;
133 }
134 }
135 /*}}}*/
136 // CacheDB::OpenDebFile - Open a debfile /*{{{*/
137 bool CacheDB::OpenDebFile()
138 {
139 // always close existing file first
140 CloseDebFile();
141
142 // first open the fd, then pass it to the debDebFile
143 if(OpenFile() == false)
144 return false;
145 DebFile = new debDebFile(*Fd);
146 if (_error->PendingError() == true)
147 return false;
148 return true;
149 }
150 /*}}}*/
151 // CacheDB::CloseDebFile - Close a debfile again /*{{{*/
152 void CacheDB::CloseDebFile()
153 {
154 CloseFile();
155
156 if(DebFile != NULL)
157 {
158 delete DebFile;
159 DebFile = NULL;
160 }
161 }
162 /*}}}*/
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. */
167 bool CacheDB::GetFileStat(bool const &doStat)
168 {
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;
189 }
190 /*}}}*/
191 // CacheDB::GetCurStatCompatOldFormat /*{{{*/
192 // ---------------------------------------------------------------------
193 /* Read the old (32bit FileSize) StateStore format from disk */
194 bool 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 */
217 bool 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 }
227 return true;
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 */
234 bool CacheDB::GetCurStat()
235 {
236 memset(&CurStat,0,sizeof(CurStat));
237
238 if (DBLoaded)
239 {
240 // do a first query to just get the size of the data on disk
241 InitQueryStats();
242 Data.data = &CurStat;
243 Data.flags = DB_DBT_USERMEM;
244 Data.ulen = 0;
245 Get();
246
247 if (Data.size == 0)
248 {
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
263 CurStat.Flags = ntohl(CurStat.Flags);
264 CurStat.FileSize = ntohl(CurStat.FileSize);
265 }
266 return true;
267 }
268 /*}}}*/
269 // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
270 // ---------------------------------------------------------------------
271 bool 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,
277 bool const &checkMtime)
278 {
279 bool result = true;
280 this->FileName = FileName;
281
282 if (GetCurStat() == false)
283 return false;
284 OldStat = CurStat;
285
286 if (GetFileStat(checkMtime) == false)
287 return false;
288
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 }
306
307 return result;
308 }
309 /*}}}*/
310
311 bool 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
317 InitQuerySource();
318 if (Get() == true && Dsc.TakeDsc(Data.data, Data.size) == true)
319 {
320 return true;
321 }
322 CurStat.Flags &= ~FlSource;
323 }
324 if (OpenFile() == false)
325 return false;
326
327 Stats.Misses++;
328 if (Dsc.Read(FileName) == false)
329 return false;
330
331 if (Dsc.Data == 0)
332 return _error->Error(_("Failed to read .dsc"));
333
334 // Write back the control information
335 InitQuerySource();
336 if (Put(Dsc.Data, Dsc.Length) == true)
337 CurStat.Flags |= FlSource;
338
339 return true;
340 }
341
342 // CacheDB::LoadControl - Load Control information /*{{{*/
343 // ---------------------------------------------------------------------
344 /* */
345 bool 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
351 InitQueryControl();
352 if (Get() == true && Control.TakeControl(Data.data,Data.size) == true)
353 return true;
354 CurStat.Flags &= ~FlControl;
355 }
356
357 if(OpenDebFile() == false)
358 return false;
359
360 Stats.Misses++;
361 if (Control.Read(*DebFile) == false)
362 return false;
363
364 if (Control.Control == 0)
365 return _error->Error(_("Archive has no control record"));
366
367 // Write back the control information
368 InitQueryControl();
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 /* */
377 bool CacheDB::LoadContents(bool const &GenOnly)
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
386 InitQueryContent();
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
396 if(OpenDebFile() == false)
397 return false;
398
399 Stats.Misses++;
400 if (Contents.Read(*DebFile) == false)
401 return false;
402
403 // Write back the control information
404 InitQueryContent();
405 if (Put(Contents.Data,Contents.CurSize) == true)
406 CurStat.Flags |= FlContents;
407 return true;
408 }
409 /*}}}*/
410
411 static std::string bytes2hex(uint8_t *bytes, size_t length) {
412 char buf[3];
413 std::string space;
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;
421 }
422
423 static inline unsigned char xdig2num(char const &dig) {
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
430 static 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
441 // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
442 // ---------------------------------------------------------------------
443 /* */
444 bool CacheDB::GetMD5(bool const &GenOnly)
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
452 MD5Res = bytes2hex(CurStat.MD5, sizeof(CurStat.MD5));
453 return true;
454 }
455
456 Stats.MD5Bytes += CurStat.FileSize;
457
458 if (OpenFile() == false)
459 return false;
460
461 MD5Summation MD5;
462 if (Fd->Seek(0) == false || MD5.AddFD(*Fd, CurStat.FileSize) == false)
463 return false;
464
465 MD5Res = MD5.Result();
466 hex2bytes(CurStat.MD5, MD5Res.data(), sizeof(CurStat.MD5));
467 CurStat.Flags |= FlMD5;
468 return true;
469 }
470 /*}}}*/
471 // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
472 // ---------------------------------------------------------------------
473 /* */
474 bool CacheDB::GetSHA1(bool const &GenOnly)
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
488 if (OpenFile() == false)
489 return false;
490
491 SHA1Summation SHA1;
492 if (Fd->Seek(0) == false || SHA1.AddFD(*Fd, CurStat.FileSize) == false)
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 /* */
504 bool CacheDB::GetSHA256(bool const &GenOnly)
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
518 if (OpenFile() == false)
519 return false;
520
521 SHA256Summation SHA256;
522 if (Fd->Seek(0) == false || SHA256.AddFD(*Fd, CurStat.FileSize) == false)
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 /*}}}*/
531 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
532 // ---------------------------------------------------------------------
533 /* */
534 bool 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
548 if (OpenFile() == false)
549 return false;
550
551 SHA512Summation SHA512;
552 if (Fd->Seek(0) == false || SHA512.AddFD(*Fd, CurStat.FileSize) == false)
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 /*}}}*/
561 // CacheDB::Finish - Write back the cache structure /*{{{*/
562 // ---------------------------------------------------------------------
563 /* */
564 bool CacheDB::Finish()
565 {
566 // Optimize away some writes.
567 if (CurStat.Flags == OldStat.Flags &&
568 CurStat.mtime == OldStat.mtime)
569 return true;
570
571 // Write the stat information
572 CurStat.Flags = htonl(CurStat.Flags);
573 CurStat.FileSize = htonl(CurStat.FileSize);
574 InitQueryStats();
575 Put(&CurStat,sizeof(CurStat));
576 CurStat.Flags = ntohl(CurStat.Flags);
577 CurStat.FileSize = ntohl(CurStat.FileSize);
578
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. */
585 bool 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.. */
592 DBC *Cursor;
593 if ((errno = Dbp->cursor(Dbp, NULL, &Cursor, 0)) != 0)
594 return _error->Error(_("Unable to get a cursor"));
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 {
602 const char *Colon = (char*)memrchr(Key.data, ':', Key.size);
603 if (Colon)
604 {
605 if (stringcmp(Colon + 1, (char *)Key.data+Key.size,"st") == 0 ||
606 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cl") == 0 ||
607 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cs") == 0 ||
608 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cn") == 0)
609 {
610 std::string FileName = std::string((const char *)Key.data,Colon);
611 if (FileExists(FileName) == true) {
612 continue;
613 }
614 }
615 }
616 Cursor->c_del(Cursor,0);
617 }
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
625
626 return true;
627 }
628 /*}}}*/