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