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