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