]>
Commit | Line | Data |
---|---|---|
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> |
b2e465d6 AL |
22 | |
23 | #include <netinet/in.h> // htonl, etc | |
ea542140 DK |
24 | |
25 | #include <apti18n.h> | |
26 | #include "cachedb.h" | |
b2e465d6 AL |
27 | /*}}}*/ |
28 | ||
29 | // CacheDB::ReadyDB - Ready the DB2 /*{{{*/ | |
30 | // --------------------------------------------------------------------- | |
31 | /* This opens the DB2 file for caching package information */ | |
8f3ba4e8 | 32 | bool CacheDB::ReadyDB(std::string const &DB) |
b2e465d6 | 33 | { |
c9569a1e AL |
34 | int err; |
35 | ||
b2e465d6 AL |
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 | { | |
dc738e7a | 46 | _error->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile.c_str()); |
b2e465d6 AL |
47 | rename(DBFile.c_str(),(DBFile+".old").c_str()); |
48 | } | |
49 | ||
50 | DBLoaded = false; | |
51 | Dbp = 0; | |
8f3ba4e8 | 52 | DBFile = std::string(); |
b2e465d6 AL |
53 | |
54 | if (DB.empty()) | |
55 | return true; | |
c9569a1e AL |
56 | |
57 | db_create(&Dbp, NULL, 0); | |
cde41ae8 | 58 | if ((err = Dbp->open(Dbp, NULL, DB.c_str(), NULL, DB_BTREE, |
b2e465d6 | 59 | (ReadOnly?DB_RDONLY:DB_CREATE), |
c9569a1e | 60 | 0644)) != 0) |
b2e465d6 | 61 | { |
c9569a1e AL |
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 | } | |
eb2bc4f2 MV |
71 | // the database format has changed from DB_HASH to DB_BTREE in |
72 | // apt 0.6.44 | |
73 | if (err == EINVAL) | |
74 | { | |
c6474fb6 | 75 | _error->Error(_("DB format is invalid. If you upgraded from an older version of apt, please remove and re-create the database.")); |
eb2bc4f2 | 76 | } |
c9569a1e AL |
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 | } | |
b2e465d6 AL |
82 | } |
83 | ||
84 | DBFile = DB; | |
85 | DBLoaded = true; | |
86 | return true; | |
87 | } | |
88 | /*}}}*/ | |
c6474fb6 | 89 | // CacheDB::OpenFile - Open the file /*{{{*/ |
b2e465d6 | 90 | // --------------------------------------------------------------------- |
cde41ae8 MV |
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. */ | |
ff574e76 | 108 | bool CacheDB::GetFileStat(bool const &doStat) |
cde41ae8 | 109 | { |
ff574e76 | 110 | if ((CurStat.Flags & FlSize) == FlSize && doStat == false) |
cde41ae8 MV |
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() | |
b2e465d6 | 140 | { |
b2e465d6 AL |
141 | memset(&CurStat,0,sizeof(CurStat)); |
142 | ||
cde41ae8 MV |
143 | if (DBLoaded) |
144 | { | |
c6474fb6 | 145 | /* First see if there is anything about it |
cde41ae8 | 146 | in the database */ |
b2e465d6 | 147 | |
cde41ae8 | 148 | /* Get the flags (and mtime) */ |
b2e465d6 | 149 | InitQuery("st"); |
b2e465d6 AL |
150 | // Ensure alignment of the returned structure |
151 | Data.data = &CurStat; | |
152 | Data.ulen = sizeof(CurStat); | |
153 | Data.flags = DB_DBT_USERMEM; | |
cde41ae8 | 154 | if (Get() == false) |
b2e465d6 | 155 | { |
b2e465d6 | 156 | CurStat.Flags = 0; |
b2e465d6 | 157 | } |
cde41ae8 MV |
158 | CurStat.Flags = ntohl(CurStat.Flags); |
159 | CurStat.FileSize = ntohl(CurStat.FileSize); | |
b2e465d6 | 160 | } |
cde41ae8 MV |
161 | return true; |
162 | } | |
163 | /*}}}*/ | |
164 | // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/ | |
165 | // --------------------------------------------------------------------- | |
8f3ba4e8 | 166 | bool CacheDB::GetFileInfo(std::string const &FileName, bool const &DoControl, bool const &DoContents, |
9209ec47 | 167 | bool const &GenContentsOnly, bool const &DoMD5, bool const &DoSHA1, |
9a961efc MV |
168 | bool const &DoSHA256, bool const &DoSHA512, |
169 | bool const &checkMtime) | |
cde41ae8 MV |
170 | { |
171 | this->FileName = FileName; | |
172 | ||
173 | if (GetCurStat() == false) | |
b2e465d6 | 174 | { |
cde41ae8 | 175 | return false; |
b2e465d6 | 176 | } |
b2e465d6 | 177 | OldStat = CurStat; |
ff574e76 DK |
178 | |
179 | if (GetFileStat(checkMtime) == false) | |
cde41ae8 MV |
180 | { |
181 | delete Fd; | |
182 | Fd = NULL; | |
183 | return false; | |
184 | } | |
185 | ||
ff574e76 DK |
186 | /* if mtime changed, update CurStat from disk */ |
187 | if (checkMtime == true && OldStat.mtime != CurStat.mtime) | |
188 | CurStat.Flags = FlSize; | |
189 | ||
cde41ae8 MV |
190 | Stats.Bytes += CurStat.FileSize; |
191 | Stats.Packages++; | |
192 | ||
2a5f8389 DK |
193 | if ((DoControl && LoadControl() == false) |
194 | || (DoContents && LoadContents(GenContentsOnly) == false) | |
195 | || (DoMD5 && GetMD5(false) == false) | |
196 | || (DoSHA1 && GetSHA1(false) == false) | |
9a961efc MV |
197 | || (DoSHA256 && GetSHA256(false) == false) |
198 | || (DoSHA512 && GetSHA512(false) == false) | |
199 | ) | |
cde41ae8 MV |
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 | ||
b2e465d6 AL |
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 | ||
cde41ae8 MV |
231 | if (Fd == NULL && OpenFile() == false) |
232 | { | |
233 | return false; | |
234 | } | |
b2e465d6 AL |
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) | |
dc738e7a | 248 | return _error->Error(_("Archive has no control record")); |
b2e465d6 AL |
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 | /* */ | |
9209ec47 | 260 | bool CacheDB::LoadContents(bool const &GenOnly) |
b2e465d6 AL |
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 | ||
cde41ae8 MV |
279 | if (Fd == NULL && OpenFile() == false) |
280 | { | |
281 | return false; | |
282 | } | |
b2e465d6 AL |
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 | /*}}}*/ | |
cde41ae8 | 301 | |
8f3ba4e8 | 302 | static std::string bytes2hex(uint8_t *bytes, size_t length) { |
76ef756a | 303 | char buf[3]; |
0fffbc8c | 304 | std::string space; |
76ef756a MV |
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; | |
cde41ae8 MV |
312 | } |
313 | ||
9209ec47 | 314 | static inline unsigned char xdig2num(char const &dig) { |
cde41ae8 MV |
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 | ||
b2e465d6 AL |
332 | // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/ |
333 | // --------------------------------------------------------------------- | |
334 | /* */ | |
9209ec47 | 335 | bool CacheDB::GetMD5(bool const &GenOnly) |
b2e465d6 AL |
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 | ||
cde41ae8 | 343 | MD5Res = bytes2hex(CurStat.MD5, sizeof(CurStat.MD5)); |
b2e465d6 AL |
344 | return true; |
345 | } | |
b2e465d6 | 346 | |
cde41ae8 | 347 | Stats.MD5Bytes += CurStat.FileSize; |
b2e465d6 | 348 | |
cde41ae8 MV |
349 | if (Fd == NULL && OpenFile() == false) |
350 | { | |
351 | return false; | |
352 | } | |
b2e465d6 | 353 | MD5Summation MD5; |
109eb151 | 354 | if (Fd->Seek(0) == false || MD5.AddFD(*Fd, CurStat.FileSize) == false) |
b2e465d6 AL |
355 | return false; |
356 | ||
357 | MD5Res = MD5.Result(); | |
cde41ae8 | 358 | hex2bytes(CurStat.MD5, MD5Res.data(), sizeof(CurStat.MD5)); |
b2e465d6 AL |
359 | CurStat.Flags |= FlMD5; |
360 | return true; | |
361 | } | |
362 | /*}}}*/ | |
cde41ae8 MV |
363 | // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/ |
364 | // --------------------------------------------------------------------- | |
365 | /* */ | |
9209ec47 | 366 | bool CacheDB::GetSHA1(bool const &GenOnly) |
cde41ae8 MV |
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; | |
109eb151 | 385 | if (Fd->Seek(0) == false || SHA1.AddFD(*Fd, CurStat.FileSize) == false) |
cde41ae8 MV |
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 | /* */ | |
9209ec47 | 397 | bool CacheDB::GetSHA256(bool const &GenOnly) |
cde41ae8 MV |
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; | |
109eb151 | 416 | if (Fd->Seek(0) == false || SHA256.AddFD(*Fd, CurStat.FileSize) == false) |
cde41ae8 MV |
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 | /*}}}*/ | |
9a961efc MV |
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; | |
109eb151 | 447 | if (Fd->Seek(0) == false || SHA512.AddFD(*Fd, CurStat.FileSize) == false) |
9a961efc MV |
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 | /*}}}*/ | |
b2e465d6 AL |
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 && | |
9bfe66dc | 463 | CurStat.mtime == OldStat.mtime) |
b2e465d6 AL |
464 | return true; |
465 | ||
466 | // Write the stat information | |
467 | CurStat.Flags = htonl(CurStat.Flags); | |
cde41ae8 | 468 | CurStat.FileSize = htonl(CurStat.FileSize); |
b2e465d6 AL |
469 | InitQuery("st"); |
470 | Put(&CurStat,sizeof(CurStat)); | |
471 | CurStat.Flags = ntohl(CurStat.Flags); | |
cde41ae8 MV |
472 | CurStat.FileSize = ntohl(CurStat.FileSize); |
473 | ||
b2e465d6 AL |
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.. */ | |
b2e465d6 | 487 | DBC *Cursor; |
c9569a1e | 488 | if ((errno = Dbp->cursor(Dbp, NULL, &Cursor, 0)) != 0) |
dc738e7a | 489 | return _error->Error(_("Unable to get a cursor")); |
b2e465d6 AL |
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 | { | |
592b401a MV |
497 | const char *Colon = (char*)memrchr(Key.data, ':', Key.size); |
498 | if (Colon) | |
b2e465d6 | 499 | { |
592b401a MV |
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) | |
b2e465d6 | 503 | { |
8f3ba4e8 | 504 | if (FileExists(std::string((const char *)Key.data,Colon)) == true) |
b2e465d6 AL |
505 | continue; |
506 | } | |
507 | } | |
508 | ||
509 | Cursor->c_del(Cursor,0); | |
510 | } | |
09fab244 | 511 | Dbp->compact(Dbp, NULL, NULL, NULL, NULL, DB_FREE_SPACE, NULL); |
b2e465d6 AL |
512 | |
513 | return true; | |
514 | } | |
515 | /*}}}*/ |