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