]> git.saurik.com Git - apt.git/blob - ftparchive/cachedb.cc
Merge EDSP 0.5 w/ multi-arch support for external solvers
[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::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 */
195 bool CacheDB::GetCurStat()
196 {
197 memset(&CurStat,0,sizeof(CurStat));
198
199 if (DBLoaded)
200 {
201 /* First see if there is anything about it
202 in the database */
203
204 /* Get the flags (and mtime) */
205 InitQueryStats();
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)
211 {
212 CurStat.Flags = 0;
213 }
214 CurStat.Flags = ntohl(CurStat.Flags);
215 CurStat.FileSize = ntohl(CurStat.FileSize);
216 }
217 return true;
218 }
219 /*}}}*/
220 // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
221 // ---------------------------------------------------------------------
222 bool 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,
228 bool const &checkMtime)
229 {
230 bool result = true;
231 this->FileName = FileName;
232
233 if (GetCurStat() == false)
234 return false;
235 OldStat = CurStat;
236
237 if (GetFileStat(checkMtime) == false)
238 return false;
239
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 }
257
258 return result;
259 }
260 /*}}}*/
261
262 bool 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
268 InitQuerySource();
269 if (Get() == true && Dsc.TakeDsc(Data.data, Data.size) == true)
270 {
271 return true;
272 }
273 CurStat.Flags &= ~FlSource;
274 }
275 if (OpenFile() == false)
276 return false;
277
278 Stats.Misses++;
279 if (Dsc.Read(FileName) == false)
280 return false;
281
282 if (Dsc.Data == 0)
283 return _error->Error(_("Failed to read .dsc"));
284
285 // Write back the control information
286 InitQuerySource();
287 if (Put(Dsc.Data, Dsc.Length) == true)
288 CurStat.Flags |= FlSource;
289
290 return true;
291 }
292
293 // CacheDB::LoadControl - Load Control information /*{{{*/
294 // ---------------------------------------------------------------------
295 /* */
296 bool 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
302 InitQueryControl();
303 if (Get() == true && Control.TakeControl(Data.data,Data.size) == true)
304 return true;
305 CurStat.Flags &= ~FlControl;
306 }
307
308 if(OpenDebFile() == false)
309 return false;
310
311 Stats.Misses++;
312 if (Control.Read(*DebFile) == false)
313 return false;
314
315 if (Control.Control == 0)
316 return _error->Error(_("Archive has no control record"));
317
318 // Write back the control information
319 InitQueryControl();
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 /* */
328 bool CacheDB::LoadContents(bool const &GenOnly)
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
337 InitQueryContent();
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
347 if(OpenDebFile() == false)
348 return false;
349
350 Stats.Misses++;
351 if (Contents.Read(*DebFile) == false)
352 return false;
353
354 // Write back the control information
355 InitQueryContent();
356 if (Put(Contents.Data,Contents.CurSize) == true)
357 CurStat.Flags |= FlContents;
358 return true;
359 }
360 /*}}}*/
361
362 static std::string bytes2hex(uint8_t *bytes, size_t length) {
363 char buf[3];
364 std::string space;
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;
372 }
373
374 static inline unsigned char xdig2num(char const &dig) {
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
381 static 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
392 // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
393 // ---------------------------------------------------------------------
394 /* */
395 bool CacheDB::GetMD5(bool const &GenOnly)
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
403 MD5Res = bytes2hex(CurStat.MD5, sizeof(CurStat.MD5));
404 return true;
405 }
406
407 Stats.MD5Bytes += CurStat.FileSize;
408
409 if (OpenFile() == false)
410 return false;
411
412 MD5Summation MD5;
413 if (Fd->Seek(0) == false || MD5.AddFD(*Fd, CurStat.FileSize) == false)
414 return false;
415
416 MD5Res = MD5.Result();
417 hex2bytes(CurStat.MD5, MD5Res.data(), sizeof(CurStat.MD5));
418 CurStat.Flags |= FlMD5;
419 return true;
420 }
421 /*}}}*/
422 // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
423 // ---------------------------------------------------------------------
424 /* */
425 bool CacheDB::GetSHA1(bool const &GenOnly)
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
439 if (OpenFile() == false)
440 return false;
441
442 SHA1Summation SHA1;
443 if (Fd->Seek(0) == false || SHA1.AddFD(*Fd, CurStat.FileSize) == false)
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 /* */
455 bool CacheDB::GetSHA256(bool const &GenOnly)
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
469 if (OpenFile() == false)
470 return false;
471
472 SHA256Summation SHA256;
473 if (Fd->Seek(0) == false || SHA256.AddFD(*Fd, CurStat.FileSize) == false)
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 /*}}}*/
482 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
483 // ---------------------------------------------------------------------
484 /* */
485 bool 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
499 if (OpenFile() == false)
500 return false;
501
502 SHA512Summation SHA512;
503 if (Fd->Seek(0) == false || SHA512.AddFD(*Fd, CurStat.FileSize) == false)
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 /*}}}*/
512 // CacheDB::Finish - Write back the cache structure /*{{{*/
513 // ---------------------------------------------------------------------
514 /* */
515 bool CacheDB::Finish()
516 {
517 // Optimize away some writes.
518 if (CurStat.Flags == OldStat.Flags &&
519 CurStat.mtime == OldStat.mtime)
520 return true;
521
522 // Write the stat information
523 CurStat.Flags = htonl(CurStat.Flags);
524 CurStat.FileSize = htonl(CurStat.FileSize);
525 InitQueryStats();
526 Put(&CurStat,sizeof(CurStat));
527 CurStat.Flags = ntohl(CurStat.Flags);
528 CurStat.FileSize = ntohl(CurStat.FileSize);
529
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. */
536 bool 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.. */
543 DBC *Cursor;
544 if ((errno = Dbp->cursor(Dbp, NULL, &Cursor, 0)) != 0)
545 return _error->Error(_("Unable to get a cursor"));
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 {
553 const char *Colon = (char*)memrchr(Key.data, ':', Key.size);
554 if (Colon)
555 {
556 if (stringcmp(Colon + 1, (char *)Key.data+Key.size,"st") == 0 ||
557 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cl") == 0 ||
558 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cs") == 0 ||
559 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cn") == 0)
560 {
561 std::string FileName = std::string((const char *)Key.data,Colon);
562 if (FileExists(FileName) == true) {
563 continue;
564 }
565 }
566 }
567 Cursor->c_del(Cursor,0);
568 }
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
576
577 return true;
578 }
579 /*}}}*/