]>
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)
50 // CacheDB::ReadyDB - Ready the DB2 /*{{{*/
51 // ---------------------------------------------------------------------
52 /* This opens the DB2 file for caching package information */
53 bool CacheDB::ReadyDB(std::string
const &DB
)
57 ReadOnly
= _config
->FindB("APT::FTPArchive::ReadOnlyDB",false);
63 /* Check if the DB was disabled while running and deal with a
65 if (DBFailed() == true)
67 _error
->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile
.c_str());
68 rename(DBFile
.c_str(),(DBFile
+".old").c_str());
73 DBFile
= std::string();
78 db_create(&Dbp
, NULL
, 0);
79 if ((err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_BTREE
,
80 (ReadOnly
?DB_RDONLY
:DB_CREATE
),
83 if (err
== DB_OLD_VERSION
)
85 _error
->Warning(_("DB is old, attempting to upgrade %s"),DBFile
.c_str());
86 err
= Dbp
->upgrade(Dbp
, DB
.c_str(), 0);
88 err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_HASH
,
89 (ReadOnly
?DB_RDONLY
:DB_CREATE
), 0644);
92 // the database format has changed from DB_HASH to DB_BTREE in
96 _error
->Error(_("DB format is invalid. If you upgraded from an older version of apt, please remove and re-create the database."));
101 return _error
->Error(_("Unable to open DB file %s: %s"),DB
.c_str(), db_strerror(err
));
110 // CacheDB::OpenFile - Open the file /*{{{*/
111 // ---------------------------------------------------------------------
113 bool CacheDB::OpenFile()
115 // always close existing file first
119 Fd
= new FileFd(FileName
,FileFd::ReadOnly
);
120 if (_error
->PendingError() == true)
128 // CacheDB::CloseFile - Close the file /*{{{*/
129 void CacheDB::CloseFile()
138 // CacheDB::OpenDebFile - Open a debfile /*{{{*/
139 bool CacheDB::OpenDebFile()
141 // always close existing file first
144 // first open the fd, then pass it to the debDebFile
145 if(OpenFile() == false)
147 DebFile
= new debDebFile(*Fd
);
148 if (_error
->PendingError() == true)
153 // CacheDB::CloseDebFile - Close a debfile again /*{{{*/
154 void CacheDB::CloseDebFile()
165 // CacheDB::GetFileStat - Get stats from the file /*{{{*/
166 // ---------------------------------------------------------------------
167 /* This gets the size from the database if it's there. If we need
168 * to look at the file, also get the mtime from the file. */
169 bool CacheDB::GetFileStat(bool const &doStat
)
171 if ((CurStat
.Flags
& FlSize
) == FlSize
&& doStat
== false)
174 /* Get it from the file. */
175 if (OpenFile() == false)
180 if (fstat(Fd
->Fd(),&St
) != 0)
183 return _error
->Errno("fstat",
184 _("Failed to stat %s"),FileName
.c_str());
186 CurStat
.FileSize
= St
.st_size
;
187 CurStat
.mtime
= htonl(St
.st_mtime
);
188 CurStat
.Flags
|= FlSize
;
193 // CacheDB::GetCurStatCompatOldFormat /*{{{*/
194 // ---------------------------------------------------------------------
195 /* Read the old (32bit FileSize) StateStore format from disk */
196 bool CacheDB::GetCurStatCompatOldFormat()
199 Data
.data
= &CurStatOldFormat
;
200 Data
.flags
= DB_DBT_USERMEM
;
201 Data
.ulen
= sizeof(CurStatOldFormat
);
206 CurStat
.Flags
= CurStatOldFormat
.Flags
;
207 CurStat
.mtime
= CurStatOldFormat
.mtime
;
208 CurStat
.FileSize
= CurStatOldFormat
.FileSize
;
209 memcpy(CurStat
.MD5
, CurStatOldFormat
.MD5
, sizeof(CurStat
.MD5
));
210 memcpy(CurStat
.SHA1
, CurStatOldFormat
.SHA1
, sizeof(CurStat
.SHA1
));
211 memcpy(CurStat
.SHA256
, CurStatOldFormat
.SHA256
, sizeof(CurStat
.SHA256
));
216 // CacheDB::GetCurStatCompatOldFormat /*{{{*/
217 // ---------------------------------------------------------------------
218 /* Read the new (64bit FileSize) StateStore format from disk */
219 bool CacheDB::GetCurStatCompatNewFormat()
222 Data
.data
= &CurStat
;
223 Data
.flags
= DB_DBT_USERMEM
;
224 Data
.ulen
= sizeof(CurStat
);
232 // CacheDB::GetCurStat - Set the CurStat variable. /*{{{*/
233 // ---------------------------------------------------------------------
234 /* Sets the CurStat variable. Either to 0 if no database is used
235 * or to the value in the database if one is used */
236 bool CacheDB::GetCurStat()
238 memset(&CurStat
,0,sizeof(CurStat
));
242 // do a first query to just get the size of the data on disk
244 Data
.data
= &CurStat
;
245 Data
.flags
= DB_DBT_USERMEM
;
251 // nothing needs to be done, we just have not data for this deb
253 // check if the record is written in the old format (32bit filesize)
254 else if(Data
.size
== sizeof(CurStatOldFormat
))
256 GetCurStatCompatOldFormat();
258 else if(Data
.size
== sizeof(CurStat
))
260 GetCurStatCompatNewFormat();
262 return _error
->Error("Cache record size mismatch (%ul)", Data
.size
);
265 CurStat
.Flags
= ntohl(CurStat
.Flags
);
266 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
271 // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
272 // ---------------------------------------------------------------------
273 bool CacheDB::GetFileInfo(std::string
const &FileName
, bool const &DoControl
, bool const &DoContents
,
274 bool const &GenContentsOnly
, bool const DoSource
, unsigned int const DoHashes
,
275 bool const &checkMtime
)
277 this->FileName
= FileName
;
279 if (GetCurStat() == false)
283 if (GetFileStat(checkMtime
) == false)
286 /* if mtime changed, update CurStat from disk */
287 if (checkMtime
== true && OldStat
.mtime
!= CurStat
.mtime
)
288 CurStat
.Flags
= FlSize
;
290 Stats
.Bytes
+= CurStat
.FileSize
;
293 if ((DoControl
&& LoadControl() == false)
294 || (DoContents
&& LoadContents(GenContentsOnly
) == false)
295 || (DoSource
&& LoadSource() == false)
296 || (DoHashes
!= 0 && GetHashes(false, DoHashes
) == false)
305 bool CacheDB::LoadSource() /*{{{*/
307 // Try to read the control information out of the DB.
308 if ((CurStat
.Flags
& FlSource
) == FlSource
)
310 // Lookup the control information
312 if (Get() == true && Dsc
.TakeDsc(Data
.data
, Data
.size
) == true)
316 CurStat
.Flags
&= ~FlSource
;
318 if (OpenFile() == false)
322 if (Dsc
.Read(FileName
) == false)
326 return _error
->Error(_("Failed to read .dsc"));
328 // Write back the control information
330 if (Put(Dsc
.Data
.c_str(), Dsc
.Length
) == true)
331 CurStat
.Flags
|= FlSource
;
336 // CacheDB::LoadControl - Load Control information /*{{{*/
337 // ---------------------------------------------------------------------
339 bool CacheDB::LoadControl()
341 // Try to read the control information out of the DB.
342 if ((CurStat
.Flags
& FlControl
) == FlControl
)
344 // Lookup the control information
346 if (Get() == true && Control
.TakeControl(Data
.data
,Data
.size
) == true)
348 CurStat
.Flags
&= ~FlControl
;
351 if(OpenDebFile() == false)
355 if (Control
.Read(*DebFile
) == false)
358 if (Control
.Control
== 0)
359 return _error
->Error(_("Archive has no control record"));
361 // Write back the control information
363 if (Put(Control
.Control
,Control
.Length
) == true)
364 CurStat
.Flags
|= FlControl
;
368 // CacheDB::LoadContents - Load the File Listing /*{{{*/
369 // ---------------------------------------------------------------------
371 bool CacheDB::LoadContents(bool const &GenOnly
)
373 // Try to read the control information out of the DB.
374 if ((CurStat
.Flags
& FlContents
) == FlContents
)
379 // Lookup the contents information
383 if (Contents
.TakeContents(Data
.data
,Data
.size
) == true)
387 CurStat
.Flags
&= ~FlContents
;
390 if(OpenDebFile() == false)
394 if (Contents
.Read(*DebFile
) == false)
397 // Write back the control information
399 if (Put(Contents
.Data
,Contents
.CurSize
) == true)
400 CurStat
.Flags
|= FlContents
;
404 // CacheDB::GetHashes - Get the hashs /*{{{*/
405 static std::string
bytes2hex(uint8_t *bytes
, size_t length
) {
409 space
.reserve(length
*2 + 1);
410 for (size_t i
= 0; i
< length
; i
++) {
411 snprintf(buf
, sizeof(buf
), "%02x", bytes
[i
]);
417 static inline unsigned char xdig2num(char const &dig
) {
418 if (isdigit(dig
)) return dig
- '0';
419 if ('a' <= dig
&& dig
<= 'f') return dig
- 'a' + 10;
420 if ('A' <= dig
&& dig
<= 'F') return dig
- 'A' + 10;
424 static void hex2bytes(uint8_t *bytes
, const char *hex
, int length
) {
425 while (length
-- > 0) {
427 if (isxdigit(hex
[0]) && isxdigit(hex
[1])) {
428 *bytes
= xdig2num(hex
[0]) * 16 + xdig2num(hex
[1]);
434 bool CacheDB::GetHashes(bool const GenOnly
, unsigned int const DoHashes
)
436 unsigned int FlHashes
= DoHashes
& (Hashes::MD5SUM
| Hashes::SHA1SUM
| Hashes::SHA256SUM
| Hashes::SHA512SUM
);
441 if (OpenFile() == false)
445 if (Fd
->Seek(0) == false || hashes
.AddFD(*Fd
, CurStat
.FileSize
, FlHashes
) == false)
448 HashStringList hl
= hashes
.GetHashStringList();
449 for (HashStringList::const_iterator hs
= hl
.begin(); hs
!= hl
.end(); ++hs
)
451 HashesList
.push_back(*hs
);
452 if (strcasecmp(hs
->HashType().c_str(), "SHA512") == 0)
454 Stats
.SHA512Bytes
+= CurStat
.FileSize
;
455 hex2bytes(CurStat
.SHA512
, hs
->HashValue().data(), sizeof(CurStat
.SHA512
));
456 CurStat
.Flags
|= FlSHA512
;
458 else if (strcasecmp(hs
->HashType().c_str(), "SHA256") == 0)
460 Stats
.SHA256Bytes
+= CurStat
.FileSize
;
461 hex2bytes(CurStat
.SHA256
, hs
->HashValue().data(), sizeof(CurStat
.SHA256
));
462 CurStat
.Flags
|= FlSHA256
;
464 else if (strcasecmp(hs
->HashType().c_str(), "SHA1") == 0)
466 Stats
.SHA1Bytes
+= CurStat
.FileSize
;
467 hex2bytes(CurStat
.SHA1
, hs
->HashValue().data(), sizeof(CurStat
.SHA1
));
468 CurStat
.Flags
|= FlSHA1
;
470 else if (strcasecmp(hs
->HashType().c_str(), "MD5Sum") == 0)
472 Stats
.MD5Bytes
+= CurStat
.FileSize
;
473 hex2bytes(CurStat
.MD5
, hs
->HashValue().data(), sizeof(CurStat
.MD5
));
474 CurStat
.Flags
|= FlMD5
;
476 else if (strcasecmp(hs
->HashType().c_str(), "Checksum-FileSize") == 0)
478 // we store it in a different field already
481 return _error
->Error("Got unknown unrequested hashtype %s", hs
->HashType().c_str());
487 return HashesList
.push_back(HashString("MD5Sum", bytes2hex(CurStat
.MD5
, sizeof(CurStat
.MD5
)))) &&
488 HashesList
.push_back(HashString("SHA1", bytes2hex(CurStat
.SHA1
, sizeof(CurStat
.SHA1
)))) &&
489 HashesList
.push_back(HashString("SHA256", bytes2hex(CurStat
.SHA256
, sizeof(CurStat
.SHA256
)))) &&
490 HashesList
.push_back(HashString("SHA512", bytes2hex(CurStat
.SHA512
, sizeof(CurStat
.SHA512
))));
493 // CacheDB::Finish - Write back the cache structure /*{{{*/
494 // ---------------------------------------------------------------------
496 bool CacheDB::Finish()
498 // Optimize away some writes.
499 if (CurStat
.Flags
== OldStat
.Flags
&&
500 CurStat
.mtime
== OldStat
.mtime
)
503 // Write the stat information
504 CurStat
.Flags
= htonl(CurStat
.Flags
);
505 CurStat
.FileSize
= htonl(CurStat
.FileSize
);
507 Put(&CurStat
,sizeof(CurStat
));
508 CurStat
.Flags
= ntohl(CurStat
.Flags
);
509 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
514 // CacheDB::Clean - Clean the Database /*{{{*/
515 // ---------------------------------------------------------------------
516 /* Tidy the database by removing files that no longer exist at all. */
517 bool CacheDB::Clean()
519 if (DBLoaded
== false)
522 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
523 needs the lower one and 2.7.7 needs the upper.. */
525 if ((errno
= Dbp
->cursor(Dbp
, NULL
, &Cursor
, 0)) != 0)
526 return _error
->Error(_("Unable to get a cursor"));
530 memset(&Key
,0,sizeof(Key
));
531 memset(&Data
,0,sizeof(Data
));
532 while ((errno
= Cursor
->c_get(Cursor
,&Key
,&Data
,DB_NEXT
)) == 0)
534 const char *Colon
= (char*)memrchr(Key
.data
, ':', Key
.size
);
537 if (stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"st") == 0 ||
538 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cl") == 0 ||
539 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cs") == 0 ||
540 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cn") == 0)
542 std::string FileName
= std::string((const char *)Key
.data
,Colon
);
543 if (FileExists(FileName
) == true) {
548 Cursor
->c_del(Cursor
,0);
550 int res
= Dbp
->compact(Dbp
, NULL
, NULL
, NULL
, NULL
, DB_FREE_SPACE
, NULL
);
552 _error
->Warning("compact failed with result %i", res
);
554 if(_config
->FindB("Debug::APT::FTPArchive::Clean", false) == true)
555 Dbp
->stat_print(Dbp
, 0);