use new Popen()
[ntk/apt.git] / ftparchive / cachedb.cc
CommitLineData
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>
453b82a3 22#include <apt-pkg/debfile.h>
a00a9b44 23
b2e465d6 24#include <netinet/in.h> // htonl, etc
453b82a3
DK
25#include <ctype.h>
26#include <stddef.h>
27#include <sys/stat.h>
ea542140 28
ea542140 29#include "cachedb.h"
a00a9b44
DK
30
31#include <apti18n.h>
b2e465d6
AL
32 /*}}}*/
33
34// CacheDB::ReadyDB - Ready the DB2 /*{{{*/
35// ---------------------------------------------------------------------
36/* This opens the DB2 file for caching package information */
8f3ba4e8 37bool CacheDB::ReadyDB(std::string const &DB)
b2e465d6 38{
c9569a1e
AL
39 int err;
40
b2e465d6
AL
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 {
dc738e7a 51 _error->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile.c_str());
b2e465d6
AL
52 rename(DBFile.c_str(),(DBFile+".old").c_str());
53 }
54
55 DBLoaded = false;
56 Dbp = 0;
8f3ba4e8 57 DBFile = std::string();
b2e465d6
AL
58
59 if (DB.empty())
60 return true;
c9569a1e
AL
61
62 db_create(&Dbp, NULL, 0);
cde41ae8 63 if ((err = Dbp->open(Dbp, NULL, DB.c_str(), NULL, DB_BTREE,
b2e465d6 64 (ReadOnly?DB_RDONLY:DB_CREATE),
c9569a1e 65 0644)) != 0)
b2e465d6 66 {
c9569a1e
AL
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 }
eb2bc4f2
MV
76 // the database format has changed from DB_HASH to DB_BTREE in
77 // apt 0.6.44
78 if (err == EINVAL)
79 {
c6474fb6 80 _error->Error(_("DB format is invalid. If you upgraded from an older version of apt, please remove and re-create the database."));
eb2bc4f2 81 }
c9569a1e
AL
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 }
b2e465d6
AL
87 }
88
89 DBFile = DB;
90 DBLoaded = true;
91 return true;
92}
93 /*}}}*/
c6474fb6 94// CacheDB::OpenFile - Open the file /*{{{*/
b2e465d6 95// ---------------------------------------------------------------------
cde41ae8
MV
96/* */
97bool 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. */
ff574e76 113bool CacheDB::GetFileStat(bool const &doStat)
cde41ae8 114{
ff574e76 115 if ((CurStat.Flags & FlSize) == FlSize && doStat == false)
cde41ae8
MV
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 */
144bool CacheDB::GetCurStat()
b2e465d6 145{
b2e465d6
AL
146 memset(&CurStat,0,sizeof(CurStat));
147
cde41ae8
MV
148 if (DBLoaded)
149 {
c6474fb6 150 /* First see if there is anything about it
cde41ae8 151 in the database */
b2e465d6 152
cde41ae8 153 /* Get the flags (and mtime) */
b2e465d6 154 InitQuery("st");
b2e465d6
AL
155 // Ensure alignment of the returned structure
156 Data.data = &CurStat;
157 Data.ulen = sizeof(CurStat);
158 Data.flags = DB_DBT_USERMEM;
cde41ae8 159 if (Get() == false)
b2e465d6 160 {
b2e465d6 161 CurStat.Flags = 0;
b2e465d6 162 }
cde41ae8
MV
163 CurStat.Flags = ntohl(CurStat.Flags);
164 CurStat.FileSize = ntohl(CurStat.FileSize);
b2e465d6 165 }
cde41ae8
MV
166 return true;
167}
168 /*}}}*/
169// CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
170// ---------------------------------------------------------------------
8f3ba4e8 171bool CacheDB::GetFileInfo(std::string const &FileName, bool const &DoControl, bool const &DoContents,
9209ec47 172 bool const &GenContentsOnly, bool const &DoMD5, bool const &DoSHA1,
9a961efc
MV
173 bool const &DoSHA256, bool const &DoSHA512,
174 bool const &checkMtime)
cde41ae8
MV
175{
176 this->FileName = FileName;
177
178 if (GetCurStat() == false)
b2e465d6 179 {
cde41ae8 180 return false;
b2e465d6 181 }
b2e465d6 182 OldStat = CurStat;
ff574e76
DK
183
184 if (GetFileStat(checkMtime) == false)
cde41ae8
MV
185 {
186 delete Fd;
187 Fd = NULL;
188 return false;
189 }
190
ff574e76
DK
191 /* if mtime changed, update CurStat from disk */
192 if (checkMtime == true && OldStat.mtime != CurStat.mtime)
193 CurStat.Flags = FlSize;
194
cde41ae8
MV
195 Stats.Bytes += CurStat.FileSize;
196 Stats.Packages++;
197
2a5f8389
DK
198 if ((DoControl && LoadControl() == false)
199 || (DoContents && LoadContents(GenContentsOnly) == false)
200 || (DoMD5 && GetMD5(false) == false)
201 || (DoSHA1 && GetSHA1(false) == false)
9a961efc
MV
202 || (DoSHA256 && GetSHA256(false) == false)
203 || (DoSHA512 && GetSHA512(false) == false)
204 )
cde41ae8
MV
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
b2e465d6
AL
218 return true;
219}
220 /*}}}*/
221// CacheDB::LoadControl - Load Control information /*{{{*/
222// ---------------------------------------------------------------------
223/* */
224bool 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
cde41ae8
MV
236 if (Fd == NULL && OpenFile() == false)
237 {
238 return false;
239 }
b2e465d6
AL
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)
dc738e7a 253 return _error->Error(_("Archive has no control record"));
b2e465d6
AL
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/* */
9209ec47 265bool CacheDB::LoadContents(bool const &GenOnly)
b2e465d6
AL
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
cde41ae8
MV
284 if (Fd == NULL && OpenFile() == false)
285 {
286 return false;
287 }
b2e465d6
AL
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 /*}}}*/
cde41ae8 306
8f3ba4e8 307static std::string bytes2hex(uint8_t *bytes, size_t length) {
76ef756a 308 char buf[3];
0fffbc8c 309 std::string space;
76ef756a
MV
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;
cde41ae8
MV
317}
318
9209ec47 319static inline unsigned char xdig2num(char const &dig) {
cde41ae8
MV
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
326static 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
b2e465d6
AL
337// CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
338// ---------------------------------------------------------------------
339/* */
9209ec47 340bool CacheDB::GetMD5(bool const &GenOnly)
b2e465d6
AL
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
cde41ae8 348 MD5Res = bytes2hex(CurStat.MD5, sizeof(CurStat.MD5));
b2e465d6
AL
349 return true;
350 }
b2e465d6 351
cde41ae8 352 Stats.MD5Bytes += CurStat.FileSize;
b2e465d6 353
cde41ae8
MV
354 if (Fd == NULL && OpenFile() == false)
355 {
356 return false;
357 }
b2e465d6 358 MD5Summation MD5;
109eb151 359 if (Fd->Seek(0) == false || MD5.AddFD(*Fd, CurStat.FileSize) == false)
b2e465d6
AL
360 return false;
361
362 MD5Res = MD5.Result();
cde41ae8 363 hex2bytes(CurStat.MD5, MD5Res.data(), sizeof(CurStat.MD5));
b2e465d6
AL
364 CurStat.Flags |= FlMD5;
365 return true;
366}
367 /*}}}*/
cde41ae8
MV
368// CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
369// ---------------------------------------------------------------------
370/* */
9209ec47 371bool CacheDB::GetSHA1(bool const &GenOnly)
cde41ae8
MV
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;
109eb151 390 if (Fd->Seek(0) == false || SHA1.AddFD(*Fd, CurStat.FileSize) == false)
cde41ae8
MV
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/* */
9209ec47 402bool CacheDB::GetSHA256(bool const &GenOnly)
cde41ae8
MV
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;
109eb151 421 if (Fd->Seek(0) == false || SHA256.AddFD(*Fd, CurStat.FileSize) == false)
cde41ae8
MV
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 /*}}}*/
9a961efc
MV
430// CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
431// ---------------------------------------------------------------------
432/* */
433bool 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;
109eb151 452 if (Fd->Seek(0) == false || SHA512.AddFD(*Fd, CurStat.FileSize) == false)
9a961efc
MV
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 /*}}}*/
b2e465d6
AL
461// CacheDB::Finish - Write back the cache structure /*{{{*/
462// ---------------------------------------------------------------------
463/* */
464bool CacheDB::Finish()
465{
466 // Optimize away some writes.
467 if (CurStat.Flags == OldStat.Flags &&
9bfe66dc 468 CurStat.mtime == OldStat.mtime)
b2e465d6
AL
469 return true;
470
471 // Write the stat information
472 CurStat.Flags = htonl(CurStat.Flags);
cde41ae8 473 CurStat.FileSize = htonl(CurStat.FileSize);
b2e465d6
AL
474 InitQuery("st");
475 Put(&CurStat,sizeof(CurStat));
476 CurStat.Flags = ntohl(CurStat.Flags);
cde41ae8
MV
477 CurStat.FileSize = ntohl(CurStat.FileSize);
478
b2e465d6
AL
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. */
485bool 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.. */
b2e465d6 492 DBC *Cursor;
c9569a1e 493 if ((errno = Dbp->cursor(Dbp, NULL, &Cursor, 0)) != 0)
dc738e7a 494 return _error->Error(_("Unable to get a cursor"));
b2e465d6
AL
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 {
592b401a
MV
502 const char *Colon = (char*)memrchr(Key.data, ':', Key.size);
503 if (Colon)
b2e465d6 504 {
592b401a
MV
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)
b2e465d6 508 {
8f3ba4e8 509 if (FileExists(std::string((const char *)Key.data,Colon)) == true)
b2e465d6
AL
510 continue;
511 }
512 }
513
514 Cursor->c_del(Cursor,0);
515 }
09fab244 516 Dbp->compact(Dbp, NULL, NULL, NULL, NULL, DB_FREE_SPACE, NULL);
b2e465d6
AL
517
518 return true;
519}
520 /*}}}*/