]>
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::ReadyDB - Ready the DB2 /*{{{*/
36 // ---------------------------------------------------------------------
37 /* This opens the DB2 file for caching package information */
38 bool CacheDB::ReadyDB(std::string
const &DB
)
42 ReadOnly
= _config
->FindB("APT::FTPArchive::ReadOnlyDB",false);
48 /* Check if the DB was disabled while running and deal with a
50 if (DBFailed() == true)
52 _error
->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile
.c_str());
53 rename(DBFile
.c_str(),(DBFile
+".old").c_str());
58 DBFile
= std::string();
63 db_create(&Dbp
, NULL
, 0);
64 if ((err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_BTREE
,
65 (ReadOnly
?DB_RDONLY
:DB_CREATE
),
68 if (err
== DB_OLD_VERSION
)
70 _error
->Warning(_("DB is old, attempting to upgrade %s"),DBFile
.c_str());
71 err
= Dbp
->upgrade(Dbp
, DB
.c_str(), 0);
73 err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_HASH
,
74 (ReadOnly
?DB_RDONLY
:DB_CREATE
), 0644);
77 // the database format has changed from DB_HASH to DB_BTREE in
81 _error
->Error(_("DB format is invalid. If you upgraded from an older version of apt, please remove and re-create the database."));
86 return _error
->Error(_("Unable to open DB file %s: %s"),DB
.c_str(), db_strerror(err
));
95 // CacheDB::OpenFile - Open the file /*{{{*/
96 // ---------------------------------------------------------------------
98 bool CacheDB::OpenFile()
100 // always close existing file first
104 Fd
= new FileFd(FileName
,FileFd::ReadOnly
);
105 if (_error
->PendingError() == true)
113 // CacheDB::CloseFile - Close the file /*{{{*/
114 void CacheDB::CloseFile()
123 // CacheDB::OpenDebFile - Open a debfile /*{{{*/
124 bool CacheDB::OpenDebFile()
126 // always close existing file first
129 // first open the fd, then pass it to the debDebFile
130 if(OpenFile() == false)
132 DebFile
= new debDebFile(*Fd
);
133 if (_error
->PendingError() == true)
138 // CacheDB::CloseDebFile - Close a debfile again /*{{{*/
139 void CacheDB::CloseDebFile()
150 // CacheDB::GetFileStat - Get stats from the file /*{{{*/
151 // ---------------------------------------------------------------------
152 /* This gets the size from the database if it's there. If we need
153 * to look at the file, also get the mtime from the file. */
154 bool CacheDB::GetFileStat(bool const &doStat
)
156 if ((CurStat
.Flags
& FlSize
) == FlSize
&& doStat
== false)
159 /* Get it from the file. */
160 if (OpenFile() == false)
165 if (fstat(Fd
->Fd(),&St
) != 0)
168 return _error
->Errno("fstat",
169 _("Failed to stat %s"),FileName
.c_str());
171 CurStat
.FileSize
= St
.st_size
;
172 CurStat
.mtime
= htonl(St
.st_mtime
);
173 CurStat
.Flags
|= FlSize
;
178 // CacheDB::GetCurStat - Set the CurStat variable. /*{{{*/
179 // ---------------------------------------------------------------------
180 /* Sets the CurStat variable. Either to 0 if no database is used
181 * or to the value in the database if one is used */
182 bool CacheDB::GetCurStat()
184 memset(&CurStat
,0,sizeof(CurStat
));
188 /* First see if there is anything about it
191 /* Get the flags (and mtime) */
193 // Ensure alignment of the returned structure
194 Data
.data
= &CurStat
;
195 Data
.ulen
= sizeof(CurStat
);
196 Data
.flags
= DB_DBT_USERMEM
;
201 CurStat
.Flags
= ntohl(CurStat
.Flags
);
202 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
207 // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
208 // ---------------------------------------------------------------------
209 bool CacheDB::GetFileInfo(std::string
const &FileName
, bool const &DoControl
,
210 bool const &DoContents
,
211 bool const &GenContentsOnly
,
212 bool const &DoSource
,
213 bool const &DoMD5
, bool const &DoSHA1
,
214 bool const &DoSHA256
, bool const &DoSHA512
,
215 bool const &checkMtime
)
218 this->FileName
= FileName
;
220 if (GetCurStat() == false)
224 if (GetFileStat(checkMtime
) == false)
227 /* if mtime changed, update CurStat from disk */
228 if (checkMtime
== true && OldStat
.mtime
!= CurStat
.mtime
)
229 CurStat
.Flags
= FlSize
;
231 Stats
.Bytes
+= CurStat
.FileSize
;
234 if ((DoControl
&& LoadControl() == false)
235 || (DoContents
&& LoadContents(GenContentsOnly
) == false)
236 || (DoSource
&& LoadSource() == false)
237 || (DoMD5
&& GetMD5(false) == false)
238 || (DoSHA1
&& GetSHA1(false) == false)
239 || (DoSHA256
&& GetSHA256(false) == false)
240 || (DoSHA512
&& GetSHA512(false) == false) )
249 bool CacheDB::LoadSource()
251 // Try to read the control information out of the DB.
252 if ((CurStat
.Flags
& FlSource
) == FlSource
)
254 // Lookup the control information
256 if (Get() == true && Dsc
.TakeDsc(Data
.data
, Data
.size
) == true)
260 CurStat
.Flags
&= ~FlSource
;
262 if (OpenFile() == false)
266 if (Dsc
.Read(FileName
) == false)
270 return _error
->Error(_("Failed to read .dsc"));
272 // Write back the control information
274 if (Put(Dsc
.Data
, Dsc
.Length
) == true)
275 CurStat
.Flags
|= FlSource
;
280 // CacheDB::LoadControl - Load Control information /*{{{*/
281 // ---------------------------------------------------------------------
283 bool CacheDB::LoadControl()
285 // Try to read the control information out of the DB.
286 if ((CurStat
.Flags
& FlControl
) == FlControl
)
288 // Lookup the control information
290 if (Get() == true && Control
.TakeControl(Data
.data
,Data
.size
) == true)
292 CurStat
.Flags
&= ~FlControl
;
295 if(OpenDebFile() == false)
299 if (Control
.Read(*DebFile
) == false)
302 if (Control
.Control
== 0)
303 return _error
->Error(_("Archive has no control record"));
305 // Write back the control information
307 if (Put(Control
.Control
,Control
.Length
) == true)
308 CurStat
.Flags
|= FlControl
;
312 // CacheDB::LoadContents - Load the File Listing /*{{{*/
313 // ---------------------------------------------------------------------
315 bool CacheDB::LoadContents(bool const &GenOnly
)
317 // Try to read the control information out of the DB.
318 if ((CurStat
.Flags
& FlContents
) == FlContents
)
323 // Lookup the contents information
327 if (Contents
.TakeContents(Data
.data
,Data
.size
) == true)
331 CurStat
.Flags
&= ~FlContents
;
334 if(OpenDebFile() == false)
338 if (Contents
.Read(*DebFile
) == false)
341 // Write back the control information
343 if (Put(Contents
.Data
,Contents
.CurSize
) == true)
344 CurStat
.Flags
|= FlContents
;
349 static std::string
bytes2hex(uint8_t *bytes
, size_t length
) {
353 space
.reserve(length
*2 + 1);
354 for (size_t i
= 0; i
< length
; i
++) {
355 snprintf(buf
, sizeof(buf
), "%02x", bytes
[i
]);
361 static inline unsigned char xdig2num(char const &dig
) {
362 if (isdigit(dig
)) return dig
- '0';
363 if ('a' <= dig
&& dig
<= 'f') return dig
- 'a' + 10;
364 if ('A' <= dig
&& dig
<= 'F') return dig
- 'A' + 10;
368 static void hex2bytes(uint8_t *bytes
, const char *hex
, int length
) {
369 while (length
-- > 0) {
371 if (isxdigit(hex
[0]) && isxdigit(hex
[1])) {
372 *bytes
= xdig2num(hex
[0]) * 16 + xdig2num(hex
[1]);
379 // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
380 // ---------------------------------------------------------------------
382 bool CacheDB::GetMD5(bool const &GenOnly
)
384 // Try to read the control information out of the DB.
385 if ((CurStat
.Flags
& FlMD5
) == FlMD5
)
390 MD5Res
= bytes2hex(CurStat
.MD5
, sizeof(CurStat
.MD5
));
394 Stats
.MD5Bytes
+= CurStat
.FileSize
;
396 if (OpenFile() == false)
400 if (Fd
->Seek(0) == false || MD5
.AddFD(*Fd
, CurStat
.FileSize
) == false)
403 MD5Res
= MD5
.Result();
404 hex2bytes(CurStat
.MD5
, MD5Res
.data(), sizeof(CurStat
.MD5
));
405 CurStat
.Flags
|= FlMD5
;
409 // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
410 // ---------------------------------------------------------------------
412 bool CacheDB::GetSHA1(bool const &GenOnly
)
414 // Try to read the control information out of the DB.
415 if ((CurStat
.Flags
& FlSHA1
) == FlSHA1
)
420 SHA1Res
= bytes2hex(CurStat
.SHA1
, sizeof(CurStat
.SHA1
));
424 Stats
.SHA1Bytes
+= CurStat
.FileSize
;
426 if (OpenFile() == false)
430 if (Fd
->Seek(0) == false || SHA1
.AddFD(*Fd
, CurStat
.FileSize
) == false)
433 SHA1Res
= SHA1
.Result();
434 hex2bytes(CurStat
.SHA1
, SHA1Res
.data(), sizeof(CurStat
.SHA1
));
435 CurStat
.Flags
|= FlSHA1
;
439 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
440 // ---------------------------------------------------------------------
442 bool CacheDB::GetSHA256(bool const &GenOnly
)
444 // Try to read the control information out of the DB.
445 if ((CurStat
.Flags
& FlSHA256
) == FlSHA256
)
450 SHA256Res
= bytes2hex(CurStat
.SHA256
, sizeof(CurStat
.SHA256
));
454 Stats
.SHA256Bytes
+= CurStat
.FileSize
;
456 if (OpenFile() == false)
459 SHA256Summation SHA256
;
460 if (Fd
->Seek(0) == false || SHA256
.AddFD(*Fd
, CurStat
.FileSize
) == false)
463 SHA256Res
= SHA256
.Result();
464 hex2bytes(CurStat
.SHA256
, SHA256Res
.data(), sizeof(CurStat
.SHA256
));
465 CurStat
.Flags
|= FlSHA256
;
469 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
470 // ---------------------------------------------------------------------
472 bool CacheDB::GetSHA512(bool const &GenOnly
)
474 // Try to read the control information out of the DB.
475 if ((CurStat
.Flags
& FlSHA512
) == FlSHA512
)
480 SHA512Res
= bytes2hex(CurStat
.SHA512
, sizeof(CurStat
.SHA512
));
484 Stats
.SHA512Bytes
+= CurStat
.FileSize
;
486 if (OpenFile() == false)
489 SHA512Summation SHA512
;
490 if (Fd
->Seek(0) == false || SHA512
.AddFD(*Fd
, CurStat
.FileSize
) == false)
493 SHA512Res
= SHA512
.Result();
494 hex2bytes(CurStat
.SHA512
, SHA512Res
.data(), sizeof(CurStat
.SHA512
));
495 CurStat
.Flags
|= FlSHA512
;
499 // CacheDB::Finish - Write back the cache structure /*{{{*/
500 // ---------------------------------------------------------------------
502 bool CacheDB::Finish()
504 // Optimize away some writes.
505 if (CurStat
.Flags
== OldStat
.Flags
&&
506 CurStat
.mtime
== OldStat
.mtime
)
509 // Write the stat information
510 CurStat
.Flags
= htonl(CurStat
.Flags
);
511 CurStat
.FileSize
= htonl(CurStat
.FileSize
);
513 Put(&CurStat
,sizeof(CurStat
));
514 CurStat
.Flags
= ntohl(CurStat
.Flags
);
515 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
520 // CacheDB::Clean - Clean the Database /*{{{*/
521 // ---------------------------------------------------------------------
522 /* Tidy the database by removing files that no longer exist at all. */
523 bool CacheDB::Clean()
525 if (DBLoaded
== false)
528 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
529 needs the lower one and 2.7.7 needs the upper.. */
531 if ((errno
= Dbp
->cursor(Dbp
, NULL
, &Cursor
, 0)) != 0)
532 return _error
->Error(_("Unable to get a cursor"));
536 memset(&Key
,0,sizeof(Key
));
537 memset(&Data
,0,sizeof(Data
));
538 while ((errno
= Cursor
->c_get(Cursor
,&Key
,&Data
,DB_NEXT
)) == 0)
540 const char *Colon
= (char*)memrchr(Key
.data
, ':', Key
.size
);
543 if (stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"st") == 0 ||
544 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cl") == 0 ||
545 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cs") == 0 ||
546 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cn") == 0)
548 std::string FileName
= std::string((const char *)Key
.data
,Colon
);
549 if (FileExists(FileName
) == true) {
554 Cursor
->c_del(Cursor
,0);
556 int res
= Dbp
->compact(Dbp
, NULL
, NULL
, NULL
, NULL
, DB_FREE_SPACE
, NULL
);
558 _error
->Warning("compact failed with result %i", res
);
560 if(_config
->FindB("Debug::APT::FTPArchive::Clean", false) == true)
561 Dbp
->stat_print(Dbp
, 0);