]>
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>
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::GetCurStat - Set the CurStat variable. /*{{{*/
192 // ---------------------------------------------------------------------
193 /* Sets the CurStat variable. Either to 0 if no database is used
194 * or to the value in the database if one is used */
195 bool CacheDB::GetCurStat()
197 memset(&CurStat
,0,sizeof(CurStat
));
201 /* First see if there is anything about it
204 /* Get the flags (and mtime) */
206 // Ensure alignment of the returned structure
207 Data
.data
= &CurStat
;
208 Data
.ulen
= sizeof(CurStat
);
209 Data
.flags
= DB_DBT_USERMEM
;
214 CurStat
.Flags
= ntohl(CurStat
.Flags
);
215 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
220 // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
221 // ---------------------------------------------------------------------
222 bool CacheDB::GetFileInfo(std::string
const &FileName
, bool const &DoControl
,
223 bool const &DoContents
,
224 bool const &GenContentsOnly
,
225 bool const &DoSource
,
226 bool const &DoMD5
, bool const &DoSHA1
,
227 bool const &DoSHA256
, bool const &DoSHA512
,
228 bool const &checkMtime
)
231 this->FileName
= FileName
;
233 if (GetCurStat() == false)
237 if (GetFileStat(checkMtime
) == false)
240 /* if mtime changed, update CurStat from disk */
241 if (checkMtime
== true && OldStat
.mtime
!= CurStat
.mtime
)
242 CurStat
.Flags
= FlSize
;
244 Stats
.Bytes
+= CurStat
.FileSize
;
247 if ((DoControl
&& LoadControl() == false)
248 || (DoContents
&& LoadContents(GenContentsOnly
) == false)
249 || (DoSource
&& LoadSource() == false)
250 || (DoMD5
&& GetMD5(false) == false)
251 || (DoSHA1
&& GetSHA1(false) == false)
252 || (DoSHA256
&& GetSHA256(false) == false)
253 || (DoSHA512
&& GetSHA512(false) == false) )
262 bool CacheDB::LoadSource()
264 // Try to read the control information out of the DB.
265 if ((CurStat
.Flags
& FlSource
) == FlSource
)
267 // Lookup the control information
269 if (Get() == true && Dsc
.TakeDsc(Data
.data
, Data
.size
) == true)
273 CurStat
.Flags
&= ~FlSource
;
275 if (OpenFile() == false)
279 if (Dsc
.Read(FileName
) == false)
283 return _error
->Error(_("Failed to read .dsc"));
285 // Write back the control information
287 if (Put(Dsc
.Data
, Dsc
.Length
) == true)
288 CurStat
.Flags
|= FlSource
;
293 // CacheDB::LoadControl - Load Control information /*{{{*/
294 // ---------------------------------------------------------------------
296 bool CacheDB::LoadControl()
298 // Try to read the control information out of the DB.
299 if ((CurStat
.Flags
& FlControl
) == FlControl
)
301 // Lookup the control information
303 if (Get() == true && Control
.TakeControl(Data
.data
,Data
.size
) == true)
305 CurStat
.Flags
&= ~FlControl
;
308 if(OpenDebFile() == false)
312 if (Control
.Read(*DebFile
) == false)
315 if (Control
.Control
== 0)
316 return _error
->Error(_("Archive has no control record"));
318 // Write back the control information
320 if (Put(Control
.Control
,Control
.Length
) == true)
321 CurStat
.Flags
|= FlControl
;
325 // CacheDB::LoadContents - Load the File Listing /*{{{*/
326 // ---------------------------------------------------------------------
328 bool CacheDB::LoadContents(bool const &GenOnly
)
330 // Try to read the control information out of the DB.
331 if ((CurStat
.Flags
& FlContents
) == FlContents
)
336 // Lookup the contents information
340 if (Contents
.TakeContents(Data
.data
,Data
.size
) == true)
344 CurStat
.Flags
&= ~FlContents
;
347 if(OpenDebFile() == false)
351 if (Contents
.Read(*DebFile
) == false)
354 // Write back the control information
356 if (Put(Contents
.Data
,Contents
.CurSize
) == true)
357 CurStat
.Flags
|= FlContents
;
362 static std::string
bytes2hex(uint8_t *bytes
, size_t length
) {
366 space
.reserve(length
*2 + 1);
367 for (size_t i
= 0; i
< length
; i
++) {
368 snprintf(buf
, sizeof(buf
), "%02x", bytes
[i
]);
374 static inline unsigned char xdig2num(char const &dig
) {
375 if (isdigit(dig
)) return dig
- '0';
376 if ('a' <= dig
&& dig
<= 'f') return dig
- 'a' + 10;
377 if ('A' <= dig
&& dig
<= 'F') return dig
- 'A' + 10;
381 static void hex2bytes(uint8_t *bytes
, const char *hex
, int length
) {
382 while (length
-- > 0) {
384 if (isxdigit(hex
[0]) && isxdigit(hex
[1])) {
385 *bytes
= xdig2num(hex
[0]) * 16 + xdig2num(hex
[1]);
392 // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
393 // ---------------------------------------------------------------------
395 bool CacheDB::GetMD5(bool const &GenOnly
)
397 // Try to read the control information out of the DB.
398 if ((CurStat
.Flags
& FlMD5
) == FlMD5
)
403 MD5Res
= bytes2hex(CurStat
.MD5
, sizeof(CurStat
.MD5
));
407 Stats
.MD5Bytes
+= CurStat
.FileSize
;
409 if (OpenFile() == false)
413 if (Fd
->Seek(0) == false || MD5
.AddFD(*Fd
, CurStat
.FileSize
) == false)
416 MD5Res
= MD5
.Result();
417 hex2bytes(CurStat
.MD5
, MD5Res
.data(), sizeof(CurStat
.MD5
));
418 CurStat
.Flags
|= FlMD5
;
422 // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
423 // ---------------------------------------------------------------------
425 bool CacheDB::GetSHA1(bool const &GenOnly
)
427 // Try to read the control information out of the DB.
428 if ((CurStat
.Flags
& FlSHA1
) == FlSHA1
)
433 SHA1Res
= bytes2hex(CurStat
.SHA1
, sizeof(CurStat
.SHA1
));
437 Stats
.SHA1Bytes
+= CurStat
.FileSize
;
439 if (OpenFile() == false)
443 if (Fd
->Seek(0) == false || SHA1
.AddFD(*Fd
, CurStat
.FileSize
) == false)
446 SHA1Res
= SHA1
.Result();
447 hex2bytes(CurStat
.SHA1
, SHA1Res
.data(), sizeof(CurStat
.SHA1
));
448 CurStat
.Flags
|= FlSHA1
;
452 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
453 // ---------------------------------------------------------------------
455 bool CacheDB::GetSHA256(bool const &GenOnly
)
457 // Try to read the control information out of the DB.
458 if ((CurStat
.Flags
& FlSHA256
) == FlSHA256
)
463 SHA256Res
= bytes2hex(CurStat
.SHA256
, sizeof(CurStat
.SHA256
));
467 Stats
.SHA256Bytes
+= CurStat
.FileSize
;
469 if (OpenFile() == false)
472 SHA256Summation SHA256
;
473 if (Fd
->Seek(0) == false || SHA256
.AddFD(*Fd
, CurStat
.FileSize
) == false)
476 SHA256Res
= SHA256
.Result();
477 hex2bytes(CurStat
.SHA256
, SHA256Res
.data(), sizeof(CurStat
.SHA256
));
478 CurStat
.Flags
|= FlSHA256
;
482 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
483 // ---------------------------------------------------------------------
485 bool CacheDB::GetSHA512(bool const &GenOnly
)
487 // Try to read the control information out of the DB.
488 if ((CurStat
.Flags
& FlSHA512
) == FlSHA512
)
493 SHA512Res
= bytes2hex(CurStat
.SHA512
, sizeof(CurStat
.SHA512
));
497 Stats
.SHA512Bytes
+= CurStat
.FileSize
;
499 if (OpenFile() == false)
502 SHA512Summation SHA512
;
503 if (Fd
->Seek(0) == false || SHA512
.AddFD(*Fd
, CurStat
.FileSize
) == false)
506 SHA512Res
= SHA512
.Result();
507 hex2bytes(CurStat
.SHA512
, SHA512Res
.data(), sizeof(CurStat
.SHA512
));
508 CurStat
.Flags
|= FlSHA512
;
512 // CacheDB::Finish - Write back the cache structure /*{{{*/
513 // ---------------------------------------------------------------------
515 bool CacheDB::Finish()
517 // Optimize away some writes.
518 if (CurStat
.Flags
== OldStat
.Flags
&&
519 CurStat
.mtime
== OldStat
.mtime
)
522 // Write the stat information
523 CurStat
.Flags
= htonl(CurStat
.Flags
);
524 CurStat
.FileSize
= htonl(CurStat
.FileSize
);
526 Put(&CurStat
,sizeof(CurStat
));
527 CurStat
.Flags
= ntohl(CurStat
.Flags
);
528 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
533 // CacheDB::Clean - Clean the Database /*{{{*/
534 // ---------------------------------------------------------------------
535 /* Tidy the database by removing files that no longer exist at all. */
536 bool CacheDB::Clean()
538 if (DBLoaded
== false)
541 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
542 needs the lower one and 2.7.7 needs the upper.. */
544 if ((errno
= Dbp
->cursor(Dbp
, NULL
, &Cursor
, 0)) != 0)
545 return _error
->Error(_("Unable to get a cursor"));
549 memset(&Key
,0,sizeof(Key
));
550 memset(&Data
,0,sizeof(Data
));
551 while ((errno
= Cursor
->c_get(Cursor
,&Key
,&Data
,DB_NEXT
)) == 0)
553 const char *Colon
= (char*)memrchr(Key
.data
, ':', Key
.size
);
556 if (stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"st") == 0 ||
557 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cl") == 0 ||
558 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cs") == 0 ||
559 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cn") == 0)
561 std::string FileName
= std::string((const char *)Key
.data
,Colon
);
562 if (FileExists(FileName
) == true) {
567 Cursor
->c_del(Cursor
,0);
569 int res
= Dbp
->compact(Dbp
, NULL
, NULL
, NULL
, NULL
, DB_FREE_SPACE
, NULL
);
571 _error
->Warning("compact failed with result %i", res
);
573 if(_config
->FindB("Debug::APT::FTPArchive::Clean", false) == true)
574 Dbp
->stat_print(Dbp
, 0);