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