]>
git.saurik.com Git - apt.git/blob - ftparchive/cachedb.cc
0901492f7246394109e87c3f11c6691580acf715
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>
25 #include <netinet/in.h> // htonl, etc
35 CacheDB::CacheDB(std::string
const &DB
)
36 : Dbp(0), Fd(NULL
), DebFile(0)
48 // CacheDB::ReadyDB - Ready the DB2 /*{{{*/
49 // ---------------------------------------------------------------------
50 /* This opens the DB2 file for caching package information */
51 bool CacheDB::ReadyDB(std::string
const &DB
)
55 ReadOnly
= _config
->FindB("APT::FTPArchive::ReadOnlyDB",false);
61 /* Check if the DB was disabled while running and deal with a
63 if (DBFailed() == true)
65 _error
->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile
.c_str());
66 rename(DBFile
.c_str(),(DBFile
+".old").c_str());
71 DBFile
= std::string();
76 db_create(&Dbp
, NULL
, 0);
77 if ((err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_BTREE
,
78 (ReadOnly
?DB_RDONLY
:DB_CREATE
),
81 if (err
== DB_OLD_VERSION
)
83 _error
->Warning(_("DB is old, attempting to upgrade %s"),DBFile
.c_str());
84 err
= Dbp
->upgrade(Dbp
, DB
.c_str(), 0);
86 err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_HASH
,
87 (ReadOnly
?DB_RDONLY
:DB_CREATE
), 0644);
90 // the database format has changed from DB_HASH to DB_BTREE in
94 _error
->Error(_("DB format is invalid. If you upgraded from an older version of apt, please remove and re-create the database."));
99 return _error
->Error(_("Unable to open DB file %s: %s"),DB
.c_str(), db_strerror(err
));
108 // CacheDB::OpenFile - Open the file /*{{{*/
109 // ---------------------------------------------------------------------
111 bool CacheDB::OpenFile()
113 // always close existing file first
117 Fd
= new FileFd(FileName
,FileFd::ReadOnly
);
118 if (_error
->PendingError() == true)
126 // CacheDB::CloseFile - Close the file /*{{{*/
127 void CacheDB::CloseFile()
136 // CacheDB::OpenDebFile - Open a debfile /*{{{*/
137 bool CacheDB::OpenDebFile()
139 // always close existing file first
142 // first open the fd, then pass it to the debDebFile
143 if(OpenFile() == false)
145 DebFile
= new debDebFile(*Fd
);
146 if (_error
->PendingError() == true)
151 // CacheDB::CloseDebFile - Close a debfile again /*{{{*/
152 void CacheDB::CloseDebFile()
163 // CacheDB::GetFileStat - Get stats from the file /*{{{*/
164 // ---------------------------------------------------------------------
165 /* This gets the size from the database if it's there. If we need
166 * to look at the file, also get the mtime from the file. */
167 bool CacheDB::GetFileStat(bool const &doStat
)
169 if ((CurStat
.Flags
& FlSize
) == FlSize
&& doStat
== false)
172 /* Get it from the file. */
173 if (OpenFile() == false)
178 if (fstat(Fd
->Fd(),&St
) != 0)
181 return _error
->Errno("fstat",
182 _("Failed to stat %s"),FileName
.c_str());
184 CurStat
.FileSize
= St
.st_size
;
185 CurStat
.mtime
= htonl(St
.st_mtime
);
186 CurStat
.Flags
|= FlSize
;
191 // CacheDB::GetCurStatCompatOldFormat /*{{{*/
192 // ---------------------------------------------------------------------
193 /* Read the old (32bit FileSize) StateStore format from disk */
194 bool CacheDB::GetCurStatCompatOldFormat()
197 Data
.data
= &CurStatOldFormat
;
198 Data
.flags
= DB_DBT_USERMEM
;
199 Data
.ulen
= sizeof(CurStatOldFormat
);
204 CurStat
.Flags
= CurStatOldFormat
.Flags
;
205 CurStat
.mtime
= CurStatOldFormat
.mtime
;
206 CurStat
.FileSize
= CurStatOldFormat
.FileSize
;
207 memcpy(CurStat
.MD5
, CurStatOldFormat
.MD5
, sizeof(CurStat
.MD5
));
208 memcpy(CurStat
.SHA1
, CurStatOldFormat
.SHA1
, sizeof(CurStat
.SHA1
));
209 memcpy(CurStat
.SHA256
, CurStatOldFormat
.SHA256
, sizeof(CurStat
.SHA256
));
214 // CacheDB::GetCurStatCompatOldFormat /*{{{*/
215 // ---------------------------------------------------------------------
216 /* Read the new (64bit FileSize) StateStore format from disk */
217 bool CacheDB::GetCurStatCompatNewFormat()
220 Data
.data
= &CurStat
;
221 Data
.flags
= DB_DBT_USERMEM
;
222 Data
.ulen
= sizeof(CurStat
);
230 // CacheDB::GetCurStat - Set the CurStat variable. /*{{{*/
231 // ---------------------------------------------------------------------
232 /* Sets the CurStat variable. Either to 0 if no database is used
233 * or to the value in the database if one is used */
234 bool CacheDB::GetCurStat()
236 memset(&CurStat
,0,sizeof(CurStat
));
240 // do a first query to just get the size of the data on disk
242 Data
.data
= &CurStat
;
243 Data
.flags
= DB_DBT_USERMEM
;
249 // nothing needs to be done, we just have not data for this deb
251 // check if the record is written in the old format (32bit filesize)
252 else if(Data
.size
== sizeof(CurStatOldFormat
))
254 GetCurStatCompatOldFormat();
256 else if(Data
.size
== sizeof(CurStat
))
258 GetCurStatCompatNewFormat();
260 return _error
->Error("Cache record size mismatch (%ul)", Data
.size
);
263 CurStat
.Flags
= ntohl(CurStat
.Flags
);
264 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
269 // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
270 // ---------------------------------------------------------------------
271 bool CacheDB::GetFileInfo(std::string
const &FileName
, bool const &DoControl
,
272 bool const &DoContents
,
273 bool const &GenContentsOnly
,
274 bool const &DoSource
,
275 bool const &DoMD5
, bool const &DoSHA1
,
276 bool const &DoSHA256
, bool const &DoSHA512
,
277 bool const &checkMtime
)
280 this->FileName
= FileName
;
282 if (GetCurStat() == false)
286 if (GetFileStat(checkMtime
) == false)
289 /* if mtime changed, update CurStat from disk */
290 if (checkMtime
== true && OldStat
.mtime
!= CurStat
.mtime
)
291 CurStat
.Flags
= FlSize
;
293 Stats
.Bytes
+= CurStat
.FileSize
;
296 if ((DoControl
&& LoadControl() == false)
297 || (DoContents
&& LoadContents(GenContentsOnly
) == false)
298 || (DoSource
&& LoadSource() == false)
299 || (DoMD5
&& GetMD5(false) == false)
300 || (DoSHA1
&& GetSHA1(false) == false)
301 || (DoSHA256
&& GetSHA256(false) == false)
302 || (DoSHA512
&& GetSHA512(false) == false) )
311 bool CacheDB::LoadSource()
313 // Try to read the control information out of the DB.
314 if ((CurStat
.Flags
& FlSource
) == FlSource
)
316 // Lookup the control information
318 if (Get() == true && Dsc
.TakeDsc(Data
.data
, Data
.size
) == true)
322 CurStat
.Flags
&= ~FlSource
;
324 if (OpenFile() == false)
328 if (Dsc
.Read(FileName
) == false)
332 return _error
->Error(_("Failed to read .dsc"));
334 // Write back the control information
336 if (Put(Dsc
.Data
, Dsc
.Length
) == true)
337 CurStat
.Flags
|= FlSource
;
342 // CacheDB::LoadControl - Load Control information /*{{{*/
343 // ---------------------------------------------------------------------
345 bool CacheDB::LoadControl()
347 // Try to read the control information out of the DB.
348 if ((CurStat
.Flags
& FlControl
) == FlControl
)
350 // Lookup the control information
352 if (Get() == true && Control
.TakeControl(Data
.data
,Data
.size
) == true)
354 CurStat
.Flags
&= ~FlControl
;
357 if(OpenDebFile() == false)
361 if (Control
.Read(*DebFile
) == false)
364 if (Control
.Control
== 0)
365 return _error
->Error(_("Archive has no control record"));
367 // Write back the control information
369 if (Put(Control
.Control
,Control
.Length
) == true)
370 CurStat
.Flags
|= FlControl
;
374 // CacheDB::LoadContents - Load the File Listing /*{{{*/
375 // ---------------------------------------------------------------------
377 bool CacheDB::LoadContents(bool const &GenOnly
)
379 // Try to read the control information out of the DB.
380 if ((CurStat
.Flags
& FlContents
) == FlContents
)
385 // Lookup the contents information
389 if (Contents
.TakeContents(Data
.data
,Data
.size
) == true)
393 CurStat
.Flags
&= ~FlContents
;
396 if(OpenDebFile() == false)
400 if (Contents
.Read(*DebFile
) == false)
403 // Write back the control information
405 if (Put(Contents
.Data
,Contents
.CurSize
) == true)
406 CurStat
.Flags
|= FlContents
;
411 static std::string
bytes2hex(uint8_t *bytes
, size_t length
) {
415 space
.reserve(length
*2 + 1);
416 for (size_t i
= 0; i
< length
; i
++) {
417 snprintf(buf
, sizeof(buf
), "%02x", bytes
[i
]);
423 static inline unsigned char xdig2num(char const &dig
) {
424 if (isdigit(dig
)) return dig
- '0';
425 if ('a' <= dig
&& dig
<= 'f') return dig
- 'a' + 10;
426 if ('A' <= dig
&& dig
<= 'F') return dig
- 'A' + 10;
430 static void hex2bytes(uint8_t *bytes
, const char *hex
, int length
) {
431 while (length
-- > 0) {
433 if (isxdigit(hex
[0]) && isxdigit(hex
[1])) {
434 *bytes
= xdig2num(hex
[0]) * 16 + xdig2num(hex
[1]);
441 // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
442 // ---------------------------------------------------------------------
444 bool CacheDB::GetMD5(bool const &GenOnly
)
446 // Try to read the control information out of the DB.
447 if ((CurStat
.Flags
& FlMD5
) == FlMD5
)
452 MD5Res
= bytes2hex(CurStat
.MD5
, sizeof(CurStat
.MD5
));
456 Stats
.MD5Bytes
+= CurStat
.FileSize
;
458 if (OpenFile() == false)
462 if (Fd
->Seek(0) == false || MD5
.AddFD(*Fd
, CurStat
.FileSize
) == false)
465 MD5Res
= MD5
.Result();
466 hex2bytes(CurStat
.MD5
, MD5Res
.data(), sizeof(CurStat
.MD5
));
467 CurStat
.Flags
|= FlMD5
;
471 // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
472 // ---------------------------------------------------------------------
474 bool CacheDB::GetSHA1(bool const &GenOnly
)
476 // Try to read the control information out of the DB.
477 if ((CurStat
.Flags
& FlSHA1
) == FlSHA1
)
482 SHA1Res
= bytes2hex(CurStat
.SHA1
, sizeof(CurStat
.SHA1
));
486 Stats
.SHA1Bytes
+= CurStat
.FileSize
;
488 if (OpenFile() == false)
492 if (Fd
->Seek(0) == false || SHA1
.AddFD(*Fd
, CurStat
.FileSize
) == false)
495 SHA1Res
= SHA1
.Result();
496 hex2bytes(CurStat
.SHA1
, SHA1Res
.data(), sizeof(CurStat
.SHA1
));
497 CurStat
.Flags
|= FlSHA1
;
501 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
502 // ---------------------------------------------------------------------
504 bool CacheDB::GetSHA256(bool const &GenOnly
)
506 // Try to read the control information out of the DB.
507 if ((CurStat
.Flags
& FlSHA256
) == FlSHA256
)
512 SHA256Res
= bytes2hex(CurStat
.SHA256
, sizeof(CurStat
.SHA256
));
516 Stats
.SHA256Bytes
+= CurStat
.FileSize
;
518 if (OpenFile() == false)
521 SHA256Summation SHA256
;
522 if (Fd
->Seek(0) == false || SHA256
.AddFD(*Fd
, CurStat
.FileSize
) == false)
525 SHA256Res
= SHA256
.Result();
526 hex2bytes(CurStat
.SHA256
, SHA256Res
.data(), sizeof(CurStat
.SHA256
));
527 CurStat
.Flags
|= FlSHA256
;
531 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
532 // ---------------------------------------------------------------------
534 bool CacheDB::GetSHA512(bool const &GenOnly
)
536 // Try to read the control information out of the DB.
537 if ((CurStat
.Flags
& FlSHA512
) == FlSHA512
)
542 SHA512Res
= bytes2hex(CurStat
.SHA512
, sizeof(CurStat
.SHA512
));
546 Stats
.SHA512Bytes
+= CurStat
.FileSize
;
548 if (OpenFile() == false)
551 SHA512Summation SHA512
;
552 if (Fd
->Seek(0) == false || SHA512
.AddFD(*Fd
, CurStat
.FileSize
) == false)
555 SHA512Res
= SHA512
.Result();
556 hex2bytes(CurStat
.SHA512
, SHA512Res
.data(), sizeof(CurStat
.SHA512
));
557 CurStat
.Flags
|= FlSHA512
;
561 // CacheDB::Finish - Write back the cache structure /*{{{*/
562 // ---------------------------------------------------------------------
564 bool CacheDB::Finish()
566 // Optimize away some writes.
567 if (CurStat
.Flags
== OldStat
.Flags
&&
568 CurStat
.mtime
== OldStat
.mtime
)
571 // Write the stat information
572 CurStat
.Flags
= htonl(CurStat
.Flags
);
573 CurStat
.FileSize
= htonl(CurStat
.FileSize
);
575 Put(&CurStat
,sizeof(CurStat
));
576 CurStat
.Flags
= ntohl(CurStat
.Flags
);
577 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
582 // CacheDB::Clean - Clean the Database /*{{{*/
583 // ---------------------------------------------------------------------
584 /* Tidy the database by removing files that no longer exist at all. */
585 bool CacheDB::Clean()
587 if (DBLoaded
== false)
590 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
591 needs the lower one and 2.7.7 needs the upper.. */
593 if ((errno
= Dbp
->cursor(Dbp
, NULL
, &Cursor
, 0)) != 0)
594 return _error
->Error(_("Unable to get a cursor"));
598 memset(&Key
,0,sizeof(Key
));
599 memset(&Data
,0,sizeof(Data
));
600 while ((errno
= Cursor
->c_get(Cursor
,&Key
,&Data
,DB_NEXT
)) == 0)
602 const char *Colon
= (char*)memrchr(Key
.data
, ':', Key
.size
);
605 if (stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"st") == 0 ||
606 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cl") == 0 ||
607 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cs") == 0 ||
608 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cn") == 0)
610 std::string FileName
= std::string((const char *)Key
.data
,Colon
);
611 if (FileExists(FileName
) == true) {
616 Cursor
->c_del(Cursor
,0);
618 int res
= Dbp
->compact(Dbp
, NULL
, NULL
, NULL
, NULL
, DB_FREE_SPACE
, NULL
);
620 _error
->Warning("compact failed with result %i", res
);
622 if(_config
->FindB("Debug::APT::FTPArchive::Clean", false) == true)
623 Dbp
->stat_print(Dbp
, 0);