]>
git.saurik.com Git - apt.git/blob - ftparchive/cachedb.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cachedb.cc,v 1.7 2004/05/08 19:41:01 mdz Exp $
4 /* ######################################################################
8 Simple uniform interface to a cache database.
10 ##################################################################### */
12 // Include Files /*{{{*/
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 #include <apt-pkg/gpgv.h>
24 #include <apt-pkg/hashes.h>
26 #include <netinet/in.h> // htonl, etc
37 CacheDB::CacheDB(std::string
const &DB
)
38 : Dbp(0), Fd(NULL
), DebFile(0)
51 // CacheDB::ReadyDB - Ready the DB2 /*{{{*/
52 // ---------------------------------------------------------------------
53 /* This opens the DB2 file for caching package information */
54 bool CacheDB::ReadyDB(std::string
const &DB
)
58 ReadOnly
= _config
->FindB("APT::FTPArchive::ReadOnlyDB",false);
64 /* Check if the DB was disabled while running and deal with a
66 if (DBFailed() == true)
68 _error
->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile
.c_str());
69 rename(DBFile
.c_str(),(DBFile
+".old").c_str());
74 DBFile
= std::string();
79 db_create(&Dbp
, NULL
, 0);
80 if ((err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_BTREE
,
81 (ReadOnly
?DB_RDONLY
:DB_CREATE
),
84 if (err
== DB_OLD_VERSION
)
86 _error
->Warning(_("DB is old, attempting to upgrade %s"),DBFile
.c_str());
87 err
= Dbp
->upgrade(Dbp
, DB
.c_str(), 0);
89 err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_HASH
,
90 (ReadOnly
?DB_RDONLY
:DB_CREATE
), 0644);
93 // the database format has changed from DB_HASH to DB_BTREE in
97 _error
->Error(_("DB format is invalid. If you upgraded from an older version of apt, please remove and re-create the database."));
102 return _error
->Error(_("Unable to open DB file %s: %s"),DB
.c_str(), db_strerror(err
));
111 // CacheDB::OpenFile - Open the file /*{{{*/
112 // ---------------------------------------------------------------------
114 bool CacheDB::OpenFile()
116 // always close existing file first
120 Fd
= new FileFd(FileName
,FileFd::ReadOnly
);
121 if (_error
->PendingError() == true)
129 // CacheDB::CloseFile - Close the file /*{{{*/
130 void CacheDB::CloseFile()
139 // CacheDB::OpenDebFile - Open a debfile /*{{{*/
140 bool CacheDB::OpenDebFile()
142 // always close existing file first
145 // first open the fd, then pass it to the debDebFile
146 if(OpenFile() == false)
148 DebFile
= new debDebFile(*Fd
);
149 if (_error
->PendingError() == true)
154 // CacheDB::CloseDebFile - Close a debfile again /*{{{*/
155 void CacheDB::CloseDebFile()
166 // CacheDB::GetFileStat - Get stats from the file /*{{{*/
167 // ---------------------------------------------------------------------
168 /* This gets the size from the database if it's there. If we need
169 * to look at the file, also get the mtime from the file. */
170 bool CacheDB::GetFileStat(bool const &doStat
)
172 if ((CurStat
.Flags
& FlSize
) == FlSize
&& doStat
== false)
175 /* Get it from the file. */
176 if (OpenFile() == false)
181 if (fstat(Fd
->Fd(),&St
) != 0)
184 return _error
->Errno("fstat",
185 _("Failed to stat %s"),FileName
.c_str());
187 CurStat
.FileSize
= St
.st_size
;
188 CurStat
.mtime
= htonl(St
.st_mtime
);
189 CurStat
.Flags
|= FlSize
;
194 // CacheDB::GetCurStatCompatOldFormat /*{{{*/
195 // ---------------------------------------------------------------------
196 /* Read the old (32bit FileSize) StateStore format from disk */
197 bool CacheDB::GetCurStatCompatOldFormat()
200 Data
.data
= &CurStatOldFormat
;
201 Data
.flags
= DB_DBT_USERMEM
;
202 Data
.ulen
= sizeof(CurStatOldFormat
);
207 CurStat
.Flags
= CurStatOldFormat
.Flags
;
208 CurStat
.mtime
= CurStatOldFormat
.mtime
;
209 CurStat
.FileSize
= CurStatOldFormat
.FileSize
;
210 memcpy(CurStat
.MD5
, CurStatOldFormat
.MD5
, sizeof(CurStat
.MD5
));
211 memcpy(CurStat
.SHA1
, CurStatOldFormat
.SHA1
, sizeof(CurStat
.SHA1
));
212 memcpy(CurStat
.SHA256
, CurStatOldFormat
.SHA256
, sizeof(CurStat
.SHA256
));
217 // CacheDB::GetCurStatCompatOldFormat /*{{{*/
218 // ---------------------------------------------------------------------
219 /* Read the new (64bit FileSize) StateStore format from disk */
220 bool CacheDB::GetCurStatCompatNewFormat()
223 Data
.data
= &CurStat
;
224 Data
.flags
= DB_DBT_USERMEM
;
225 Data
.ulen
= sizeof(CurStat
);
233 // CacheDB::GetCurStat - Set the CurStat variable. /*{{{*/
234 // ---------------------------------------------------------------------
235 /* Sets the CurStat variable. Either to 0 if no database is used
236 * or to the value in the database if one is used */
237 bool CacheDB::GetCurStat()
239 memset(&CurStat
,0,sizeof(CurStat
));
243 // do a first query to just get the size of the data on disk
245 Data
.data
= &CurStat
;
246 Data
.flags
= DB_DBT_USERMEM
;
252 // nothing needs to be done, we just have not data for this deb
254 // check if the record is written in the old format (32bit filesize)
255 else if(Data
.size
== sizeof(CurStatOldFormat
))
257 GetCurStatCompatOldFormat();
259 else if(Data
.size
== sizeof(CurStat
))
261 GetCurStatCompatNewFormat();
263 return _error
->Error("Cache record size mismatch (%ul)", Data
.size
);
266 CurStat
.Flags
= ntohl(CurStat
.Flags
);
267 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
272 // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
273 // ---------------------------------------------------------------------
274 bool CacheDB::GetFileInfo(std::string
const &FileName
, bool const &DoControl
, bool const &DoContents
,
275 bool const &GenContentsOnly
, bool const DoSource
, unsigned int const DoHashes
,
276 bool const &checkMtime
)
278 this->FileName
= FileName
;
280 if (GetCurStat() == false)
284 if (GetFileStat(checkMtime
) == false)
287 /* if mtime changed, update CurStat from disk */
288 if (checkMtime
== true && OldStat
.mtime
!= CurStat
.mtime
)
289 CurStat
.Flags
= FlSize
;
291 Stats
.Bytes
+= CurStat
.FileSize
;
294 if ((DoControl
&& LoadControl() == false)
295 || (DoContents
&& LoadContents(GenContentsOnly
) == false)
296 || (DoSource
&& LoadSource() == false)
297 || (DoHashes
!= 0 && GetHashes(false, DoHashes
) == false)
306 bool CacheDB::LoadSource() /*{{{*/
308 // Try to read the control information out of the DB.
309 if ((CurStat
.Flags
& FlSource
) == FlSource
)
311 // Lookup the control information
313 if (Get() == true && Dsc
.TakeDsc(Data
.data
, Data
.size
) == true)
317 CurStat
.Flags
&= ~FlSource
;
319 if (OpenFile() == false)
323 if (Dsc
.Read(FileName
) == false)
327 return _error
->Error(_("Failed to read .dsc"));
329 // Write back the control information
331 if (Put(Dsc
.Data
.c_str(), Dsc
.Length
) == true)
332 CurStat
.Flags
|= FlSource
;
337 // CacheDB::LoadControl - Load Control information /*{{{*/
338 // ---------------------------------------------------------------------
340 bool CacheDB::LoadControl()
342 // Try to read the control information out of the DB.
343 if ((CurStat
.Flags
& FlControl
) == FlControl
)
345 // Lookup the control information
347 if (Get() == true && Control
.TakeControl(Data
.data
,Data
.size
) == true)
349 CurStat
.Flags
&= ~FlControl
;
352 if(OpenDebFile() == false)
356 if (Control
.Read(*DebFile
) == false)
359 if (Control
.Control
== 0)
360 return _error
->Error(_("Archive has no control record"));
362 // Write back the control information
364 if (Put(Control
.Control
,Control
.Length
) == true)
365 CurStat
.Flags
|= FlControl
;
369 // CacheDB::LoadContents - Load the File Listing /*{{{*/
370 // ---------------------------------------------------------------------
372 bool CacheDB::LoadContents(bool const &GenOnly
)
374 // Try to read the control information out of the DB.
375 if ((CurStat
.Flags
& FlContents
) == FlContents
)
380 // Lookup the contents information
384 if (Contents
.TakeContents(Data
.data
,Data
.size
) == true)
388 CurStat
.Flags
&= ~FlContents
;
391 if(OpenDebFile() == false)
395 if (Contents
.Read(*DebFile
) == false)
398 // Write back the control information
400 if (Put(Contents
.Data
,Contents
.CurSize
) == true)
401 CurStat
.Flags
|= FlContents
;
405 // CacheDB::GetHashes - Get the hashs /*{{{*/
406 static std::string
bytes2hex(uint8_t *bytes
, size_t length
) {
410 space
.reserve(length
*2 + 1);
411 for (size_t i
= 0; i
< length
; i
++) {
412 snprintf(buf
, sizeof(buf
), "%02x", bytes
[i
]);
418 static inline unsigned char xdig2num(char const &dig
) {
419 if (isdigit(dig
)) return dig
- '0';
420 if ('a' <= dig
&& dig
<= 'f') return dig
- 'a' + 10;
421 if ('A' <= dig
&& dig
<= 'F') return dig
- 'A' + 10;
425 static void hex2bytes(uint8_t *bytes
, const char *hex
, int length
) {
426 while (length
-- > 0) {
428 if (isxdigit(hex
[0]) && isxdigit(hex
[1])) {
429 *bytes
= xdig2num(hex
[0]) * 16 + xdig2num(hex
[1]);
435 bool CacheDB::GetHashes(bool const GenOnly
, unsigned int const DoHashes
)
437 unsigned int FlHashes
= DoHashes
& (Hashes::MD5SUM
| Hashes::SHA1SUM
| Hashes::SHA256SUM
| Hashes::SHA512SUM
);
442 if (OpenFile() == false)
445 Hashes
hashes(FlHashes
);
446 if (Fd
->Seek(0) == false || hashes
.AddFD(*Fd
, CurStat
.FileSize
) == false)
449 HashStringList hl
= hashes
.GetHashStringList();
450 for (HashStringList::const_iterator hs
= hl
.begin(); hs
!= hl
.end(); ++hs
)
452 HashesList
.push_back(*hs
);
453 if (strcasecmp(hs
->HashType().c_str(), "SHA512") == 0)
455 Stats
.SHA512Bytes
+= CurStat
.FileSize
;
456 hex2bytes(CurStat
.SHA512
, hs
->HashValue().data(), sizeof(CurStat
.SHA512
));
457 CurStat
.Flags
|= FlSHA512
;
459 else if (strcasecmp(hs
->HashType().c_str(), "SHA256") == 0)
461 Stats
.SHA256Bytes
+= CurStat
.FileSize
;
462 hex2bytes(CurStat
.SHA256
, hs
->HashValue().data(), sizeof(CurStat
.SHA256
));
463 CurStat
.Flags
|= FlSHA256
;
465 else if (strcasecmp(hs
->HashType().c_str(), "SHA1") == 0)
467 Stats
.SHA1Bytes
+= CurStat
.FileSize
;
468 hex2bytes(CurStat
.SHA1
, hs
->HashValue().data(), sizeof(CurStat
.SHA1
));
469 CurStat
.Flags
|= FlSHA1
;
471 else if (strcasecmp(hs
->HashType().c_str(), "MD5Sum") == 0)
473 Stats
.MD5Bytes
+= CurStat
.FileSize
;
474 hex2bytes(CurStat
.MD5
, hs
->HashValue().data(), sizeof(CurStat
.MD5
));
475 CurStat
.Flags
|= FlMD5
;
477 else if (strcasecmp(hs
->HashType().c_str(), "Checksum-FileSize") == 0)
479 // we store it in a different field already
482 return _error
->Error("Got unknown unrequested hashtype %s", hs
->HashType().c_str());
488 return HashesList
.push_back(HashString("MD5Sum", bytes2hex(CurStat
.MD5
, sizeof(CurStat
.MD5
)))) &&
489 HashesList
.push_back(HashString("SHA1", bytes2hex(CurStat
.SHA1
, sizeof(CurStat
.SHA1
)))) &&
490 HashesList
.push_back(HashString("SHA256", bytes2hex(CurStat
.SHA256
, sizeof(CurStat
.SHA256
)))) &&
491 HashesList
.push_back(HashString("SHA512", bytes2hex(CurStat
.SHA512
, sizeof(CurStat
.SHA512
))));
494 // CacheDB::Finish - Write back the cache structure /*{{{*/
495 // ---------------------------------------------------------------------
497 bool CacheDB::Finish()
499 // Optimize away some writes.
500 if (CurStat
.Flags
== OldStat
.Flags
&&
501 CurStat
.mtime
== OldStat
.mtime
)
504 // Write the stat information
505 CurStat
.Flags
= htonl(CurStat
.Flags
);
506 CurStat
.FileSize
= htonl(CurStat
.FileSize
);
508 Put(&CurStat
,sizeof(CurStat
));
509 CurStat
.Flags
= ntohl(CurStat
.Flags
);
510 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
515 // CacheDB::Clean - Clean the Database /*{{{*/
516 // ---------------------------------------------------------------------
517 /* Tidy the database by removing files that no longer exist at all. */
518 bool CacheDB::Clean()
520 if (DBLoaded
== false)
523 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
524 needs the lower one and 2.7.7 needs the upper.. */
526 if ((errno
= Dbp
->cursor(Dbp
, NULL
, &Cursor
, 0)) != 0)
527 return _error
->Error(_("Unable to get a cursor"));
531 memset(&Key
,0,sizeof(Key
));
532 memset(&Data
,0,sizeof(Data
));
533 while ((errno
= Cursor
->c_get(Cursor
,&Key
,&Data
,DB_NEXT
)) == 0)
535 const char *Colon
= (char*)memrchr(Key
.data
, ':', Key
.size
);
538 if (stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"st") == 0 ||
539 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cl") == 0 ||
540 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cs") == 0 ||
541 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cn") == 0)
543 std::string FileName
= std::string((const char *)Key
.data
,Colon
);
544 if (FileExists(FileName
) == true) {
549 Cursor
->c_del(Cursor
,0);
551 int res
= Dbp
->compact(Dbp
, NULL
, NULL
, NULL
, NULL
, DB_FREE_SPACE
, NULL
);
553 _error
->Warning("compact failed with result %i", res
);
555 if(_config
->FindB("Debug::APT::FTPArchive::Clean", false) == true)
556 Dbp
->stat_print(Dbp
, 0);