]> git.saurik.com Git - apt.git/blob - ftparchive/cachedb.cc
merge from the expermental2 branch
[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 buf[3];
304 std::string space;
305
306 space.reserve(length*2 + 1);
307 for (size_t i = 0; i < length; i++) {
308 snprintf(buf, sizeof(buf), "%02x", bytes[i]);
309 space.append(buf);
310 }
311 return space;
312 }
313
314 static inline unsigned char xdig2num(char const &dig) {
315 if (isdigit(dig)) return dig - '0';
316 if ('a' <= dig && dig <= 'f') return dig - 'a' + 10;
317 if ('A' <= dig && dig <= 'F') return dig - 'A' + 10;
318 return 0;
319 }
320
321 static void hex2bytes(uint8_t *bytes, const char *hex, int length) {
322 while (length-- > 0) {
323 *bytes = 0;
324 if (isxdigit(hex[0]) && isxdigit(hex[1])) {
325 *bytes = xdig2num(hex[0]) * 16 + xdig2num(hex[1]);
326 hex += 2;
327 }
328 bytes++;
329 }
330 }
331
332 // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
333 // ---------------------------------------------------------------------
334 /* */
335 bool CacheDB::GetMD5(bool const &GenOnly)
336 {
337 // Try to read the control information out of the DB.
338 if ((CurStat.Flags & FlMD5) == FlMD5)
339 {
340 if (GenOnly == true)
341 return true;
342
343 MD5Res = bytes2hex(CurStat.MD5, sizeof(CurStat.MD5));
344 return true;
345 }
346
347 Stats.MD5Bytes += CurStat.FileSize;
348
349 if (Fd == NULL && OpenFile() == false)
350 {
351 return false;
352 }
353 MD5Summation MD5;
354 if (Fd->Seek(0) == false || MD5.AddFD(*Fd, CurStat.FileSize) == false)
355 return false;
356
357 MD5Res = MD5.Result();
358 hex2bytes(CurStat.MD5, MD5Res.data(), sizeof(CurStat.MD5));
359 CurStat.Flags |= FlMD5;
360 return true;
361 }
362 /*}}}*/
363 // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
364 // ---------------------------------------------------------------------
365 /* */
366 bool CacheDB::GetSHA1(bool const &GenOnly)
367 {
368 // Try to read the control information out of the DB.
369 if ((CurStat.Flags & FlSHA1) == FlSHA1)
370 {
371 if (GenOnly == true)
372 return true;
373
374 SHA1Res = bytes2hex(CurStat.SHA1, sizeof(CurStat.SHA1));
375 return true;
376 }
377
378 Stats.SHA1Bytes += CurStat.FileSize;
379
380 if (Fd == NULL && OpenFile() == false)
381 {
382 return false;
383 }
384 SHA1Summation SHA1;
385 if (Fd->Seek(0) == false || SHA1.AddFD(*Fd, CurStat.FileSize) == false)
386 return false;
387
388 SHA1Res = SHA1.Result();
389 hex2bytes(CurStat.SHA1, SHA1Res.data(), sizeof(CurStat.SHA1));
390 CurStat.Flags |= FlSHA1;
391 return true;
392 }
393 /*}}}*/
394 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
395 // ---------------------------------------------------------------------
396 /* */
397 bool CacheDB::GetSHA256(bool const &GenOnly)
398 {
399 // Try to read the control information out of the DB.
400 if ((CurStat.Flags & FlSHA256) == FlSHA256)
401 {
402 if (GenOnly == true)
403 return true;
404
405 SHA256Res = bytes2hex(CurStat.SHA256, sizeof(CurStat.SHA256));
406 return true;
407 }
408
409 Stats.SHA256Bytes += CurStat.FileSize;
410
411 if (Fd == NULL && OpenFile() == false)
412 {
413 return false;
414 }
415 SHA256Summation SHA256;
416 if (Fd->Seek(0) == false || SHA256.AddFD(*Fd, CurStat.FileSize) == false)
417 return false;
418
419 SHA256Res = SHA256.Result();
420 hex2bytes(CurStat.SHA256, SHA256Res.data(), sizeof(CurStat.SHA256));
421 CurStat.Flags |= FlSHA256;
422 return true;
423 }
424 /*}}}*/
425 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
426 // ---------------------------------------------------------------------
427 /* */
428 bool CacheDB::GetSHA512(bool const &GenOnly)
429 {
430 // Try to read the control information out of the DB.
431 if ((CurStat.Flags & FlSHA512) == FlSHA512)
432 {
433 if (GenOnly == true)
434 return true;
435
436 SHA512Res = bytes2hex(CurStat.SHA512, sizeof(CurStat.SHA512));
437 return true;
438 }
439
440 Stats.SHA512Bytes += CurStat.FileSize;
441
442 if (Fd == NULL && OpenFile() == false)
443 {
444 return false;
445 }
446 SHA512Summation SHA512;
447 if (Fd->Seek(0) == false || SHA512.AddFD(*Fd, CurStat.FileSize) == false)
448 return false;
449
450 SHA512Res = SHA512.Result();
451 hex2bytes(CurStat.SHA512, SHA512Res.data(), sizeof(CurStat.SHA512));
452 CurStat.Flags |= FlSHA512;
453 return true;
454 }
455 /*}}}*/
456 // CacheDB::Finish - Write back the cache structure /*{{{*/
457 // ---------------------------------------------------------------------
458 /* */
459 bool CacheDB::Finish()
460 {
461 // Optimize away some writes.
462 if (CurStat.Flags == OldStat.Flags &&
463 CurStat.mtime == OldStat.mtime)
464 return true;
465
466 // Write the stat information
467 CurStat.Flags = htonl(CurStat.Flags);
468 CurStat.FileSize = htonl(CurStat.FileSize);
469 InitQuery("st");
470 Put(&CurStat,sizeof(CurStat));
471 CurStat.Flags = ntohl(CurStat.Flags);
472 CurStat.FileSize = ntohl(CurStat.FileSize);
473
474 return true;
475 }
476 /*}}}*/
477 // CacheDB::Clean - Clean the Database /*{{{*/
478 // ---------------------------------------------------------------------
479 /* Tidy the database by removing files that no longer exist at all. */
480 bool CacheDB::Clean()
481 {
482 if (DBLoaded == false)
483 return true;
484
485 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
486 needs the lower one and 2.7.7 needs the upper.. */
487 DBC *Cursor;
488 if ((errno = Dbp->cursor(Dbp, NULL, &Cursor, 0)) != 0)
489 return _error->Error(_("Unable to get a cursor"));
490
491 DBT Key;
492 DBT Data;
493 memset(&Key,0,sizeof(Key));
494 memset(&Data,0,sizeof(Data));
495 while ((errno = Cursor->c_get(Cursor,&Key,&Data,DB_NEXT)) == 0)
496 {
497 const char *Colon = (char*)memrchr(Key.data, ':', Key.size);
498 if (Colon)
499 {
500 if (stringcmp(Colon + 1, (char *)Key.data+Key.size,"st") == 0 ||
501 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cl") == 0 ||
502 stringcmp(Colon + 1, (char *)Key.data+Key.size,"cn") == 0)
503 {
504 if (FileExists(std::string((const char *)Key.data,Colon)) == true)
505 continue;
506 }
507 }
508
509 Cursor->c_del(Cursor,0);
510 }
511 Dbp->compact(Dbp, NULL, NULL, NULL, NULL, DB_FREE_SPACE, NULL);
512
513 return true;
514 }
515 /*}}}*/