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