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