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