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