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 /*{{{*/
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/md5.h>
18 #include <apt-pkg/sha1.h>
19 #include <apt-pkg/sha2.h>
20 #include <apt-pkg/strutl.h>
21 #include <apt-pkg/configuration.h>
23 #include <netinet/in.h> // htonl, etc
26 // CacheDB::ReadyDB - Ready the DB2 /*{{{*/
27 // ---------------------------------------------------------------------
28 /* This opens the DB2 file for caching package information */
29 bool CacheDB::ReadyDB(string
const &DB
)
33 ReadOnly
= _config
->FindB("APT::FTPArchive::ReadOnlyDB",false);
39 /* Check if the DB was disabled while running and deal with a
41 if (DBFailed() == true)
43 _error
->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile
.c_str());
44 rename(DBFile
.c_str(),(DBFile
+".old").c_str());
54 db_create(&Dbp
, NULL
, 0);
55 if ((err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_BTREE
,
56 (ReadOnly
?DB_RDONLY
:DB_CREATE
),
59 if (err
== DB_OLD_VERSION
)
61 _error
->Warning(_("DB is old, attempting to upgrade %s"),DBFile
.c_str());
62 err
= Dbp
->upgrade(Dbp
, DB
.c_str(), 0);
64 err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_HASH
,
65 (ReadOnly
?DB_RDONLY
:DB_CREATE
), 0644);
68 // the database format has changed from DB_HASH to DB_BTREE in
72 _error
->Error(_("DB format is invalid. If you upgraded from an older version of apt, please remove and re-create the database."));
77 return _error
->Error(_("Unable to open DB file %s: %s"),DB
.c_str(), db_strerror(err
));
86 // CacheDB::OpenFile - Open the file /*{{{*/
87 // ---------------------------------------------------------------------
89 bool CacheDB::OpenFile()
91 Fd
= new FileFd(FileName
,FileFd::ReadOnly
);
92 if (_error
->PendingError() == true)
101 // CacheDB::GetFileStat - Get stats from the file /*{{{*/
102 // ---------------------------------------------------------------------
103 /* This gets the size from the database if it's there. If we need
104 * to look at the file, also get the mtime from the file. */
105 bool CacheDB::GetFileStat(bool const &doStat
)
107 if ((CurStat
.Flags
& FlSize
) == FlSize
&& doStat
== false)
109 /* Already worked out the file size */
113 /* Get it from the file. */
114 if (Fd
== NULL
&& OpenFile() == false)
120 if (fstat(Fd
->Fd(),&St
) != 0)
122 return _error
->Errno("fstat",
123 _("Failed to stat %s"),FileName
.c_str());
125 CurStat
.FileSize
= St
.st_size
;
126 CurStat
.mtime
= htonl(St
.st_mtime
);
127 CurStat
.Flags
|= FlSize
;
132 // CacheDB::GetCurStat - Set the CurStat variable. /*{{{*/
133 // ---------------------------------------------------------------------
134 /* Sets the CurStat variable. Either to 0 if no database is used
135 * or to the value in the database if one is used */
136 bool CacheDB::GetCurStat()
138 memset(&CurStat
,0,sizeof(CurStat
));
142 /* First see if there is anything about it
145 /* Get the flags (and mtime) */
147 // Ensure alignment of the returned structure
148 Data
.data
= &CurStat
;
149 Data
.ulen
= sizeof(CurStat
);
150 Data
.flags
= DB_DBT_USERMEM
;
155 CurStat
.Flags
= ntohl(CurStat
.Flags
);
156 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
161 // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
162 // ---------------------------------------------------------------------
163 bool CacheDB::GetFileInfo(string
const &FileName
, bool const &DoControl
, bool const &DoContents
,
164 bool const &GenContentsOnly
, bool const &DoMD5
, bool const &DoSHA1
,
165 bool const &DoSHA256
, bool const &DoSHA512
,
166 bool const &checkMtime
)
168 this->FileName
= FileName
;
170 if (GetCurStat() == false)
176 if (GetFileStat(checkMtime
) == false)
183 /* if mtime changed, update CurStat from disk */
184 if (checkMtime
== true && OldStat
.mtime
!= CurStat
.mtime
)
185 CurStat
.Flags
= FlSize
;
187 Stats
.Bytes
+= CurStat
.FileSize
;
190 if ((DoControl
&& LoadControl() == false)
191 || (DoContents
&& LoadContents(GenContentsOnly
) == false)
192 || (DoMD5
&& GetMD5(false) == false)
193 || (DoSHA1
&& GetSHA1(false) == false)
194 || (DoSHA256
&& GetSHA256(false) == false)
195 || (DoSHA512
&& GetSHA512(false) == false)
213 // CacheDB::LoadControl - Load Control information /*{{{*/
214 // ---------------------------------------------------------------------
216 bool CacheDB::LoadControl()
218 // Try to read the control information out of the DB.
219 if ((CurStat
.Flags
& FlControl
) == FlControl
)
221 // Lookup the control information
223 if (Get() == true && Control
.TakeControl(Data
.data
,Data
.size
) == true)
225 CurStat
.Flags
&= ~FlControl
;
228 if (Fd
== NULL
&& OpenFile() == false)
232 // Create a deb instance to read the archive
235 DebFile
= new debDebFile(*Fd
);
236 if (_error
->PendingError() == true)
241 if (Control
.Read(*DebFile
) == false)
244 if (Control
.Control
== 0)
245 return _error
->Error(_("Archive has no control record"));
247 // Write back the control information
249 if (Put(Control
.Control
,Control
.Length
) == true)
250 CurStat
.Flags
|= FlControl
;
254 // CacheDB::LoadContents - Load the File Listing /*{{{*/
255 // ---------------------------------------------------------------------
257 bool CacheDB::LoadContents(bool const &GenOnly
)
259 // Try to read the control information out of the DB.
260 if ((CurStat
.Flags
& FlContents
) == FlContents
)
265 // Lookup the contents information
269 if (Contents
.TakeContents(Data
.data
,Data
.size
) == true)
273 CurStat
.Flags
&= ~FlContents
;
276 if (Fd
== NULL
&& OpenFile() == false)
280 // Create a deb instance to read the archive
283 DebFile
= new debDebFile(*Fd
);
284 if (_error
->PendingError() == true)
288 if (Contents
.Read(*DebFile
) == false)
291 // Write back the control information
293 if (Put(Contents
.Data
,Contents
.CurSize
) == true)
294 CurStat
.Flags
|= FlContents
;
299 static string
bytes2hex(uint8_t *bytes
, size_t length
) {
303 space
.reserve(length
*2 + 1);
304 for (size_t i
= 0; i
< length
; i
++) {
305 snprintf(buf
, sizeof(buf
), "%02x", bytes
[i
]);
311 static inline unsigned char xdig2num(char const &dig
) {
312 if (isdigit(dig
)) return dig
- '0';
313 if ('a' <= dig
&& dig
<= 'f') return dig
- 'a' + 10;
314 if ('A' <= dig
&& dig
<= 'F') return dig
- 'A' + 10;
318 static void hex2bytes(uint8_t *bytes
, const char *hex
, int length
) {
319 while (length
-- > 0) {
321 if (isxdigit(hex
[0]) && isxdigit(hex
[1])) {
322 *bytes
= xdig2num(hex
[0]) * 16 + xdig2num(hex
[1]);
329 // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
330 // ---------------------------------------------------------------------
332 bool CacheDB::GetMD5(bool const &GenOnly
)
334 // Try to read the control information out of the DB.
335 if ((CurStat
.Flags
& FlMD5
) == FlMD5
)
340 MD5Res
= bytes2hex(CurStat
.MD5
, sizeof(CurStat
.MD5
));
344 Stats
.MD5Bytes
+= CurStat
.FileSize
;
346 if (Fd
== NULL
&& OpenFile() == false)
351 if (Fd
->Seek(0) == false || MD5
.AddFD(Fd
->Fd(),CurStat
.FileSize
) == false)
354 MD5Res
= MD5
.Result();
355 hex2bytes(CurStat
.MD5
, MD5Res
.data(), sizeof(CurStat
.MD5
));
356 CurStat
.Flags
|= FlMD5
;
360 // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
361 // ---------------------------------------------------------------------
363 bool CacheDB::GetSHA1(bool const &GenOnly
)
365 // Try to read the control information out of the DB.
366 if ((CurStat
.Flags
& FlSHA1
) == FlSHA1
)
371 SHA1Res
= bytes2hex(CurStat
.SHA1
, sizeof(CurStat
.SHA1
));
375 Stats
.SHA1Bytes
+= CurStat
.FileSize
;
377 if (Fd
== NULL
&& OpenFile() == false)
382 if (Fd
->Seek(0) == false || SHA1
.AddFD(Fd
->Fd(),CurStat
.FileSize
) == false)
385 SHA1Res
= SHA1
.Result();
386 hex2bytes(CurStat
.SHA1
, SHA1Res
.data(), sizeof(CurStat
.SHA1
));
387 CurStat
.Flags
|= FlSHA1
;
391 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
392 // ---------------------------------------------------------------------
394 bool CacheDB::GetSHA256(bool const &GenOnly
)
396 // Try to read the control information out of the DB.
397 if ((CurStat
.Flags
& FlSHA256
) == FlSHA256
)
402 SHA256Res
= bytes2hex(CurStat
.SHA256
, sizeof(CurStat
.SHA256
));
406 Stats
.SHA256Bytes
+= CurStat
.FileSize
;
408 if (Fd
== NULL
&& OpenFile() == false)
412 SHA256Summation SHA256
;
413 if (Fd
->Seek(0) == false || SHA256
.AddFD(Fd
->Fd(),CurStat
.FileSize
) == false)
416 SHA256Res
= SHA256
.Result();
417 hex2bytes(CurStat
.SHA256
, SHA256Res
.data(), sizeof(CurStat
.SHA256
));
418 CurStat
.Flags
|= FlSHA256
;
422 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
423 // ---------------------------------------------------------------------
425 bool CacheDB::GetSHA512(bool const &GenOnly
)
427 // Try to read the control information out of the DB.
428 if ((CurStat
.Flags
& FlSHA512
) == FlSHA512
)
433 SHA512Res
= bytes2hex(CurStat
.SHA512
, sizeof(CurStat
.SHA512
));
437 Stats
.SHA512Bytes
+= CurStat
.FileSize
;
439 if (Fd
== NULL
&& OpenFile() == false)
443 SHA512Summation SHA512
;
444 if (Fd
->Seek(0) == false || SHA512
.AddFD(Fd
->Fd(),CurStat
.FileSize
) == false)
447 SHA512Res
= SHA512
.Result();
448 hex2bytes(CurStat
.SHA512
, SHA512Res
.data(), sizeof(CurStat
.SHA512
));
449 CurStat
.Flags
|= FlSHA512
;
453 // CacheDB::Finish - Write back the cache structure /*{{{*/
454 // ---------------------------------------------------------------------
456 bool CacheDB::Finish()
458 // Optimize away some writes.
459 if (CurStat
.Flags
== OldStat
.Flags
&&
460 CurStat
.mtime
== OldStat
.mtime
)
463 // Write the stat information
464 CurStat
.Flags
= htonl(CurStat
.Flags
);
465 CurStat
.FileSize
= htonl(CurStat
.FileSize
);
467 Put(&CurStat
,sizeof(CurStat
));
468 CurStat
.Flags
= ntohl(CurStat
.Flags
);
469 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
474 // CacheDB::Clean - Clean the Database /*{{{*/
475 // ---------------------------------------------------------------------
476 /* Tidy the database by removing files that no longer exist at all. */
477 bool CacheDB::Clean()
479 if (DBLoaded
== false)
482 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
483 needs the lower one and 2.7.7 needs the upper.. */
485 if ((errno
= Dbp
->cursor(Dbp
, NULL
, &Cursor
, 0)) != 0)
486 return _error
->Error(_("Unable to get a cursor"));
490 memset(&Key
,0,sizeof(Key
));
491 memset(&Data
,0,sizeof(Data
));
492 while ((errno
= Cursor
->c_get(Cursor
,&Key
,&Data
,DB_NEXT
)) == 0)
494 const char *Colon
= (char*)memrchr(Key
.data
, ':', Key
.size
);
497 if (stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"st") == 0 ||
498 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cl") == 0 ||
499 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cn") == 0)
501 if (FileExists(string((const char *)Key
.data
,Colon
)) == true)
506 Cursor
->c_del(Cursor
,0);
508 Dbp
->compact(Dbp
, NULL
, NULL
, NULL
, NULL
, DB_FREE_SPACE
, NULL
);