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