]>
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::ReadyDB - Ready the DB2 /*{{{*/
38 // ---------------------------------------------------------------------
39 /* This opens the DB2 file for caching package information */
40 bool CacheDB::ReadyDB(std::string
const &DB
)
44 ReadOnly
= _config
->FindB("APT::FTPArchive::ReadOnlyDB",false);
50 /* Check if the DB was disabled while running and deal with a
52 if (DBFailed() == true)
54 _error
->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile
.c_str());
55 rename(DBFile
.c_str(),(DBFile
+".old").c_str());
60 DBFile
= std::string();
65 db_create(&Dbp
, NULL
, 0);
66 if ((err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_BTREE
,
67 (ReadOnly
?DB_RDONLY
:DB_CREATE
),
70 if (err
== DB_OLD_VERSION
)
72 _error
->Warning(_("DB is old, attempting to upgrade %s"),DBFile
.c_str());
73 err
= Dbp
->upgrade(Dbp
, DB
.c_str(), 0);
75 err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_HASH
,
76 (ReadOnly
?DB_RDONLY
:DB_CREATE
), 0644);
79 // the database format has changed from DB_HASH to DB_BTREE in
83 _error
->Error(_("DB format is invalid. If you upgraded from an older version of apt, please remove and re-create the database."));
88 return _error
->Error(_("Unable to open DB file %s: %s"),DB
.c_str(), db_strerror(err
));
97 // CacheDB::OpenFile - Open the file /*{{{*/
98 // ---------------------------------------------------------------------
100 bool CacheDB::OpenFile()
102 // always close existing file first
106 Fd
= new FileFd(FileName
,FileFd::ReadOnly
);
107 if (_error
->PendingError() == true)
115 // CacheDB::CloseFile - Close the file /*{{{*/
116 void CacheDB::CloseFile()
125 // CacheDB::OpenDebFile - Open a debfile /*{{{*/
126 bool CacheDB::OpenDebFile()
128 // always close existing file first
131 // first open the fd, then pass it to the debDebFile
132 if(OpenFile() == false)
134 DebFile
= new debDebFile(*Fd
);
135 if (_error
->PendingError() == true)
140 // CacheDB::CloseDebFile - Close a debfile again /*{{{*/
141 void CacheDB::CloseDebFile()
152 // CacheDB::GetFileStat - Get stats from the file /*{{{*/
153 // ---------------------------------------------------------------------
154 /* This gets the size from the database if it's there. If we need
155 * to look at the file, also get the mtime from the file. */
156 bool CacheDB::GetFileStat(bool const &doStat
)
158 if ((CurStat
.Flags
& FlSize
) == FlSize
&& doStat
== false)
161 /* Get it from the file. */
162 if (OpenFile() == false)
167 if (fstat(Fd
->Fd(),&St
) != 0)
170 return _error
->Errno("fstat",
171 _("Failed to stat %s"),FileName
.c_str());
173 CurStat
.FileSize
= St
.st_size
;
174 CurStat
.mtime
= htonl(St
.st_mtime
);
175 CurStat
.Flags
|= FlSize
;
180 // CacheDB::GetCurStat - Set the CurStat variable. /*{{{*/
181 // ---------------------------------------------------------------------
182 /* Sets the CurStat variable. Either to 0 if no database is used
183 * or to the value in the database if one is used */
184 bool CacheDB::GetCurStat()
186 memset(&CurStat
,0,sizeof(CurStat
));
190 /* First see if there is anything about it
193 /* Get the flags (and mtime) */
195 // Ensure alignment of the returned structure
196 Data
.data
= &CurStat
;
197 Data
.ulen
= sizeof(CurStat
);
198 Data
.flags
= DB_DBT_USERMEM
;
203 CurStat
.Flags
= ntohl(CurStat
.Flags
);
204 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
209 // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
210 // ---------------------------------------------------------------------
211 bool CacheDB::GetFileInfo(std::string
const &FileName
, bool const &DoControl
, bool const &DoContents
,
212 bool const &GenContentsOnly
, bool const DoSource
, unsigned int const DoHashes
,
213 bool const &checkMtime
)
215 this->FileName
= FileName
;
217 if (GetCurStat() == false)
221 if (GetFileStat(checkMtime
) == false)
224 /* if mtime changed, update CurStat from disk */
225 if (checkMtime
== true && OldStat
.mtime
!= CurStat
.mtime
)
226 CurStat
.Flags
= FlSize
;
228 Stats
.Bytes
+= CurStat
.FileSize
;
231 if ((DoControl
&& LoadControl() == false)
232 || (DoContents
&& LoadContents(GenContentsOnly
) == false)
233 || (DoSource
&& LoadSource() == false)
234 || (DoHashes
!= 0 && GetHashes(false, DoHashes
) == false)
243 bool CacheDB::LoadSource() /*{{{*/
245 // Try to read the control information out of the DB.
246 if ((CurStat
.Flags
& FlSource
) == FlSource
)
248 // Lookup the control information
250 if (Get() == true && Dsc
.TakeDsc(Data
.data
, Data
.size
) == true)
254 CurStat
.Flags
&= ~FlSource
;
256 if (OpenFile() == false)
260 if (Dsc
.Read(FileName
) == false)
264 return _error
->Error(_("Failed to read .dsc"));
266 // Write back the control information
268 if (Put(Dsc
.Data
, Dsc
.Length
) == true)
269 CurStat
.Flags
|= FlSource
;
274 // CacheDB::LoadControl - Load Control information /*{{{*/
275 // ---------------------------------------------------------------------
277 bool CacheDB::LoadControl()
279 // Try to read the control information out of the DB.
280 if ((CurStat
.Flags
& FlControl
) == FlControl
)
282 // Lookup the control information
284 if (Get() == true && Control
.TakeControl(Data
.data
,Data
.size
) == true)
286 CurStat
.Flags
&= ~FlControl
;
289 if(OpenDebFile() == false)
293 if (Control
.Read(*DebFile
) == false)
296 if (Control
.Control
== 0)
297 return _error
->Error(_("Archive has no control record"));
299 // Write back the control information
301 if (Put(Control
.Control
,Control
.Length
) == true)
302 CurStat
.Flags
|= FlControl
;
306 // CacheDB::LoadContents - Load the File Listing /*{{{*/
307 // ---------------------------------------------------------------------
309 bool CacheDB::LoadContents(bool const &GenOnly
)
311 // Try to read the control information out of the DB.
312 if ((CurStat
.Flags
& FlContents
) == FlContents
)
317 // Lookup the contents information
321 if (Contents
.TakeContents(Data
.data
,Data
.size
) == true)
325 CurStat
.Flags
&= ~FlContents
;
328 if(OpenDebFile() == false)
332 if (Contents
.Read(*DebFile
) == false)
335 // Write back the control information
337 if (Put(Contents
.Data
,Contents
.CurSize
) == true)
338 CurStat
.Flags
|= FlContents
;
342 // CacheDB::GetHashes - Get the hashs /*{{{*/
343 static std::string
bytes2hex(uint8_t *bytes
, size_t length
) {
347 space
.reserve(length
*2 + 1);
348 for (size_t i
= 0; i
< length
; i
++) {
349 snprintf(buf
, sizeof(buf
), "%02x", bytes
[i
]);
355 static inline unsigned char xdig2num(char const &dig
) {
356 if (isdigit(dig
)) return dig
- '0';
357 if ('a' <= dig
&& dig
<= 'f') return dig
- 'a' + 10;
358 if ('A' <= dig
&& dig
<= 'F') return dig
- 'A' + 10;
362 static void hex2bytes(uint8_t *bytes
, const char *hex
, int length
) {
363 while (length
-- > 0) {
365 if (isxdigit(hex
[0]) && isxdigit(hex
[1])) {
366 *bytes
= xdig2num(hex
[0]) * 16 + xdig2num(hex
[1]);
372 bool CacheDB::GetHashes(bool const GenOnly
, unsigned int const DoHashes
)
374 unsigned int FlHashes
= DoHashes
& (Hashes::MD5SUM
| Hashes::SHA1SUM
| Hashes::SHA256SUM
| Hashes::SHA512SUM
);
379 if (OpenFile() == false)
383 if (Fd
->Seek(0) == false || hashes
.AddFD(*Fd
, CurStat
.FileSize
, FlHashes
) == false)
386 HashStringList hl
= hashes
.GetHashStringList();
387 for (HashStringList::const_iterator hs
= hl
.begin(); hs
!= hl
.end(); ++hs
)
389 HashesList
.push_back(*hs
);
390 if (strcasecmp(hs
->HashType().c_str(), "SHA512") == 0)
392 Stats
.SHA512Bytes
+= CurStat
.FileSize
;
393 hex2bytes(CurStat
.SHA512
, hs
->HashValue().data(), sizeof(CurStat
.SHA512
));
394 CurStat
.Flags
|= FlSHA512
;
396 else if (strcasecmp(hs
->HashType().c_str(), "SHA256") == 0)
398 Stats
.SHA256Bytes
+= CurStat
.FileSize
;
399 hex2bytes(CurStat
.SHA256
, hs
->HashValue().data(), sizeof(CurStat
.SHA256
));
400 CurStat
.Flags
|= FlSHA256
;
402 else if (strcasecmp(hs
->HashType().c_str(), "SHA1") == 0)
404 Stats
.SHA1Bytes
+= CurStat
.FileSize
;
405 hex2bytes(CurStat
.SHA1
, hs
->HashValue().data(), sizeof(CurStat
.SHA1
));
406 CurStat
.Flags
|= FlSHA1
;
408 else if (strcasecmp(hs
->HashType().c_str(), "MD5Sum") == 0)
410 Stats
.MD5Bytes
+= CurStat
.FileSize
;
411 hex2bytes(CurStat
.MD5
, hs
->HashValue().data(), sizeof(CurStat
.MD5
));
412 CurStat
.Flags
|= FlMD5
;
415 return _error
->Error("Got unknown unrequested hashtype %s", hs
->HashType().c_str());
421 return HashesList
.push_back(HashString("MD5Sum", bytes2hex(CurStat
.MD5
, sizeof(CurStat
.MD5
)))) &&
422 HashesList
.push_back(HashString("SHA1", bytes2hex(CurStat
.SHA1
, sizeof(CurStat
.SHA1
)))) &&
423 HashesList
.push_back(HashString("SHA256", bytes2hex(CurStat
.SHA256
, sizeof(CurStat
.SHA256
)))) &&
424 HashesList
.push_back(HashString("SHA512", bytes2hex(CurStat
.SHA512
, sizeof(CurStat
.SHA512
))));
427 // CacheDB::Finish - Write back the cache structure /*{{{*/
428 // ---------------------------------------------------------------------
430 bool CacheDB::Finish()
432 // Optimize away some writes.
433 if (CurStat
.Flags
== OldStat
.Flags
&&
434 CurStat
.mtime
== OldStat
.mtime
)
437 // Write the stat information
438 CurStat
.Flags
= htonl(CurStat
.Flags
);
439 CurStat
.FileSize
= htonl(CurStat
.FileSize
);
441 Put(&CurStat
,sizeof(CurStat
));
442 CurStat
.Flags
= ntohl(CurStat
.Flags
);
443 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
448 // CacheDB::Clean - Clean the Database /*{{{*/
449 // ---------------------------------------------------------------------
450 /* Tidy the database by removing files that no longer exist at all. */
451 bool CacheDB::Clean()
453 if (DBLoaded
== false)
456 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
457 needs the lower one and 2.7.7 needs the upper.. */
459 if ((errno
= Dbp
->cursor(Dbp
, NULL
, &Cursor
, 0)) != 0)
460 return _error
->Error(_("Unable to get a cursor"));
464 memset(&Key
,0,sizeof(Key
));
465 memset(&Data
,0,sizeof(Data
));
466 while ((errno
= Cursor
->c_get(Cursor
,&Key
,&Data
,DB_NEXT
)) == 0)
468 const char *Colon
= (char*)memrchr(Key
.data
, ':', Key
.size
);
471 if (stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"st") == 0 ||
472 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cl") == 0 ||
473 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cs") == 0 ||
474 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cn") == 0)
476 std::string FileName
= std::string((const char *)Key
.data
,Colon
);
477 if (FileExists(FileName
) == true) {
482 Cursor
->c_del(Cursor
,0);
484 int res
= Dbp
->compact(Dbp
, NULL
, NULL
, NULL
, NULL
, DB_FREE_SPACE
, NULL
);
486 _error
->Warning("compact failed with result %i", res
);
488 if(_config
->FindB("Debug::APT::FTPArchive::Clean", false) == true)
489 Dbp
->stat_print(Dbp
, 0);