]> git.saurik.com Git - apt.git/blob - ftparchive/cachedb.cc
update symbols files to 1.1.4 state of affairs
[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 FlHashes = DoHashes & (Hashes::MD5SUM | Hashes::SHA1SUM | Hashes::SHA256SUM | Hashes::SHA512SUM);
438 HashesList.clear();
439
440 if (FlHashes != 0)
441 {
442 if (OpenFile() == false)
443 return false;
444
445 Hashes hashes(FlHashes);
446 if (Fd->Seek(0) == false || hashes.AddFD(*Fd, CurStat.FileSize) == false)
447 return false;
448
449 HashStringList hl = hashes.GetHashStringList();
450 for (HashStringList::const_iterator hs = hl.begin(); hs != hl.end(); ++hs)
451 {
452 HashesList.push_back(*hs);
453 if (strcasecmp(hs->HashType().c_str(), "SHA512") == 0)
454 {
455 Stats.SHA512Bytes += CurStat.FileSize;
456 hex2bytes(CurStat.SHA512, hs->HashValue().data(), sizeof(CurStat.SHA512));
457 CurStat.Flags |= FlSHA512;
458 }
459 else if (strcasecmp(hs->HashType().c_str(), "SHA256") == 0)
460 {
461 Stats.SHA256Bytes += CurStat.FileSize;
462 hex2bytes(CurStat.SHA256, hs->HashValue().data(), sizeof(CurStat.SHA256));
463 CurStat.Flags |= FlSHA256;
464 }
465 else if (strcasecmp(hs->HashType().c_str(), "SHA1") == 0)
466 {
467 Stats.SHA1Bytes += CurStat.FileSize;
468 hex2bytes(CurStat.SHA1, hs->HashValue().data(), sizeof(CurStat.SHA1));
469 CurStat.Flags |= FlSHA1;
470 }
471 else if (strcasecmp(hs->HashType().c_str(), "MD5Sum") == 0)
472 {
473 Stats.MD5Bytes += CurStat.FileSize;
474 hex2bytes(CurStat.MD5, hs->HashValue().data(), sizeof(CurStat.MD5));
475 CurStat.Flags |= FlMD5;
476 }
477 else if (strcasecmp(hs->HashType().c_str(), "Checksum-FileSize") == 0)
478 {
479 // we store it in a different field already
480 }
481 else
482 return _error->Error("Got unknown unrequested hashtype %s", hs->HashType().c_str());
483 }
484 }
485 if (GenOnly == true)
486 return true;
487
488 bool ret = true;
489 #define PUSH_BACK_HASH(FLAG, TYPE, VALUE) \
490 if ((CurStat.Flags & FLAG) == FLAG) \
491 ret &= HashesList.push_back(HashString(TYPE, bytes2hex(VALUE, sizeof(VALUE))));
492 PUSH_BACK_HASH(FlMD5, "MD5Sum", CurStat.MD5);
493 PUSH_BACK_HASH(FlSHA1, "SHA1", CurStat.SHA1);
494 PUSH_BACK_HASH(FlSHA256, "SHA256", CurStat.SHA256);
495 PUSH_BACK_HASH(FlSHA512, "SHA512", CurStat.SHA512);
496 return ret;
497 }
498 /*}}}*/
499 // CacheDB::Finish - Write back the cache structure /*{{{*/
500 // ---------------------------------------------------------------------
501 /* */
502 bool CacheDB::Finish()
503 {
504 // Optimize away some writes.
505 if (CurStat.Flags == OldStat.Flags &&
506 CurStat.mtime == OldStat.mtime)
507 return true;
508
509 // Write the stat information
510 CurStat.Flags = htonl(CurStat.Flags);
511 CurStat.FileSize = htonl(CurStat.FileSize);
512 InitQueryStats();
513 Put(&CurStat,sizeof(CurStat));
514 CurStat.Flags = ntohl(CurStat.Flags);
515 CurStat.FileSize = ntohl(CurStat.FileSize);
516
517 return true;
518 }
519 /*}}}*/
520 // CacheDB::Clean - Clean the Database /*{{{*/
521 // ---------------------------------------------------------------------
522 /* Tidy the database by removing files that no longer exist at all. */
523 bool CacheDB::Clean()
524 {
525 if (DBLoaded == false)
526 return true;
527
528 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
529 needs the lower one and 2.7.7 needs the upper.. */
530 DBC *Cursor;
531 if ((errno = Dbp->cursor(Dbp, NULL, &Cursor, 0)) != 0)
532 return _error->Error(_("Unable to get a cursor"));
533
534 DBT Key;
535 DBT Data;
536 memset(&Key,0,sizeof(Key));
537 memset(&Data,0,sizeof(Data));
538 while ((errno = Cursor->c_get(Cursor,&Key,&Data,DB_NEXT)) == 0)
539 {
540 const char *Colon = (char*)memrchr(Key.data, ':', Key.size);
541 if (Colon)
542 {
543 if (stringcmp(Colon + 1, (char *)Key.data+Key.size,"st") == 0 ||
544 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cl") == 0 ||
545 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cs") == 0 ||
546 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cn") == 0)
547 {
548 std::string FileName = std::string((const char *)Key.data,Colon);
549 if (FileExists(FileName) == true) {
550 continue;
551 }
552 }
553 }
554 Cursor->c_del(Cursor,0);
555 }
556 int res = Dbp->compact(Dbp, NULL, NULL, NULL, NULL, DB_FREE_SPACE, NULL);
557 if (res < 0)
558 _error->Warning("compact failed with result %i", res);
559
560 if(_config->FindB("Debug::APT::FTPArchive::Clean", false) == true)
561 Dbp->stat_print(Dbp, 0);
562
563
564 return true;
565 }
566 /*}}}*/