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