]>
git.saurik.com Git - apt.git/blob - ftparchive/cachedb.cc
f6cbad668f660729b62155f608253bba4f3c9ab6
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 Fd
= new FileFd(FileName
,FileFd::ReadOnly
);
101 if (_error
->PendingError() == true)
109 void CacheDB::CloseFile()
115 bool CacheDB::OpenDebFile()
117 DebFile
= new debDebFile(*Fd
);
118 if (_error
->PendingError() == true)
123 void CacheDB::CloseDebFile()
129 // CacheDB::GetFileStat - Get stats from the file /*{{{*/
130 // ---------------------------------------------------------------------
131 /* This gets the size from the database if it's there. If we need
132 * to look at the file, also get the mtime from the file. */
133 bool CacheDB::GetFileStat(bool const &doStat
)
135 if ((CurStat
.Flags
& FlSize
) == FlSize
&& doStat
== false)
137 /* Already worked out the file size */
141 /* Get it from the file. */
142 if (Fd
== NULL
&& OpenFile() == false)
148 if (fstat(Fd
->Fd(),&St
) != 0)
150 return _error
->Errno("fstat",
151 _("Failed to stat %s"),FileName
.c_str());
153 CurStat
.FileSize
= St
.st_size
;
154 CurStat
.mtime
= htonl(St
.st_mtime
);
155 CurStat
.Flags
|= FlSize
;
160 // CacheDB::GetCurStat - Set the CurStat variable. /*{{{*/
161 // ---------------------------------------------------------------------
162 /* Sets the CurStat variable. Either to 0 if no database is used
163 * or to the value in the database if one is used */
164 bool CacheDB::GetCurStat()
166 memset(&CurStat
,0,sizeof(CurStat
));
170 /* First see if there is anything about it
173 /* Get the flags (and mtime) */
175 // Ensure alignment of the returned structure
176 Data
.data
= &CurStat
;
177 Data
.ulen
= sizeof(CurStat
);
178 Data
.flags
= DB_DBT_USERMEM
;
183 CurStat
.Flags
= ntohl(CurStat
.Flags
);
184 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
189 // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
190 // ---------------------------------------------------------------------
191 bool CacheDB::GetFileInfo(std::string
const &FileName
, bool const &DoControl
,
192 bool const &DoContents
,
193 bool const &GenContentsOnly
,
194 bool const &DoSource
,
195 bool const &DoMD5
, bool const &DoSHA1
,
196 bool const &DoSHA256
, bool const &DoSHA512
,
197 bool const &checkMtime
)
200 this->FileName
= FileName
;
202 if (GetCurStat() == false)
208 if (GetFileStat(checkMtime
) == false)
214 /* if mtime changed, update CurStat from disk */
215 if (checkMtime
== true && OldStat
.mtime
!= CurStat
.mtime
)
216 CurStat
.Flags
= FlSize
;
218 Stats
.Bytes
+= CurStat
.FileSize
;
221 if ((DoControl
&& LoadControl() == false)
222 || (DoContents
&& LoadContents(GenContentsOnly
) == false)
223 || (DoSource
&& LoadSource() == false)
224 || (DoMD5
&& GetMD5(false) == false)
225 || (DoSHA1
&& GetSHA1(false) == false)
226 || (DoSHA256
&& GetSHA256(false) == false)
227 || (DoSHA512
&& GetSHA512(false) == false)
240 bool CacheDB::LoadSource()
242 // Try to read the control information out of the DB.
243 if ((CurStat
.Flags
& FlSource
) == FlSource
)
245 // Lookup the control information
247 if (Get() == true && Dsc
.TakeDsc(Data
.data
, Data
.size
) == true)
249 CurStat
.Flags
&= ~FlSource
;
252 if (Fd
== NULL
&& OpenFile() == false)
257 // Read the .dsc file
260 if(OpenFile() == false)
265 if (Dsc
.Read(FileName
) == false)
269 return _error
->Error(_("Failed to read .dsc"));
271 // Write back the control information
273 if (Put(Dsc
.Data
, Dsc
.Length
) == true)
274 CurStat
.Flags
|= FlSource
;
279 // CacheDB::LoadControl - Load Control information /*{{{*/
280 // ---------------------------------------------------------------------
282 bool CacheDB::LoadControl()
284 // Try to read the control information out of the DB.
285 if ((CurStat
.Flags
& FlControl
) == FlControl
)
287 // Lookup the control information
289 if (Get() == true && Control
.TakeControl(Data
.data
,Data
.size
) == true)
291 CurStat
.Flags
&= ~FlControl
;
294 if (Fd
== NULL
&& OpenFile() == false)
298 // Create a deb instance to read the archive
301 if(OpenDebFile() == false)
306 if (Control
.Read(*DebFile
) == false)
309 if (Control
.Control
== 0)
310 return _error
->Error(_("Archive has no control record"));
312 // Write back the control information
314 if (Put(Control
.Control
,Control
.Length
) == true)
315 CurStat
.Flags
|= FlControl
;
319 // CacheDB::LoadContents - Load the File Listing /*{{{*/
320 // ---------------------------------------------------------------------
322 bool CacheDB::LoadContents(bool const &GenOnly
)
324 // Try to read the control information out of the DB.
325 if ((CurStat
.Flags
& FlContents
) == FlContents
)
330 // Lookup the contents information
334 if (Contents
.TakeContents(Data
.data
,Data
.size
) == true)
338 CurStat
.Flags
&= ~FlContents
;
341 if (Fd
== NULL
&& OpenFile() == false)
345 // Create a deb instance to read the archive
348 if(OpenDebFile() == false)
353 if (Contents
.Read(*DebFile
) == false)
356 // Write back the control information
358 if (Put(Contents
.Data
,Contents
.CurSize
) == true)
359 CurStat
.Flags
|= FlContents
;
364 static std::string
bytes2hex(uint8_t *bytes
, size_t length
) {
368 space
.reserve(length
*2 + 1);
369 for (size_t i
= 0; i
< length
; i
++) {
370 snprintf(buf
, sizeof(buf
), "%02x", bytes
[i
]);
376 static inline unsigned char xdig2num(char const &dig
) {
377 if (isdigit(dig
)) return dig
- '0';
378 if ('a' <= dig
&& dig
<= 'f') return dig
- 'a' + 10;
379 if ('A' <= dig
&& dig
<= 'F') return dig
- 'A' + 10;
383 static void hex2bytes(uint8_t *bytes
, const char *hex
, int length
) {
384 while (length
-- > 0) {
386 if (isxdigit(hex
[0]) && isxdigit(hex
[1])) {
387 *bytes
= xdig2num(hex
[0]) * 16 + xdig2num(hex
[1]);
394 // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
395 // ---------------------------------------------------------------------
397 bool CacheDB::GetMD5(bool const &GenOnly
)
399 // Try to read the control information out of the DB.
400 if ((CurStat
.Flags
& FlMD5
) == FlMD5
)
405 MD5Res
= bytes2hex(CurStat
.MD5
, sizeof(CurStat
.MD5
));
409 Stats
.MD5Bytes
+= CurStat
.FileSize
;
411 if (Fd
== NULL
&& OpenFile() == false)
416 if (Fd
->Seek(0) == false || MD5
.AddFD(*Fd
, CurStat
.FileSize
) == false)
419 MD5Res
= MD5
.Result();
420 hex2bytes(CurStat
.MD5
, MD5Res
.data(), sizeof(CurStat
.MD5
));
421 CurStat
.Flags
|= FlMD5
;
425 // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
426 // ---------------------------------------------------------------------
428 bool CacheDB::GetSHA1(bool const &GenOnly
)
430 // Try to read the control information out of the DB.
431 if ((CurStat
.Flags
& FlSHA1
) == FlSHA1
)
436 SHA1Res
= bytes2hex(CurStat
.SHA1
, sizeof(CurStat
.SHA1
));
440 Stats
.SHA1Bytes
+= CurStat
.FileSize
;
442 if (Fd
== NULL
&& OpenFile() == false)
447 if (Fd
->Seek(0) == false || SHA1
.AddFD(*Fd
, CurStat
.FileSize
) == false)
450 SHA1Res
= SHA1
.Result();
451 hex2bytes(CurStat
.SHA1
, SHA1Res
.data(), sizeof(CurStat
.SHA1
));
452 CurStat
.Flags
|= FlSHA1
;
456 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
457 // ---------------------------------------------------------------------
459 bool CacheDB::GetSHA256(bool const &GenOnly
)
461 // Try to read the control information out of the DB.
462 if ((CurStat
.Flags
& FlSHA256
) == FlSHA256
)
467 SHA256Res
= bytes2hex(CurStat
.SHA256
, sizeof(CurStat
.SHA256
));
471 Stats
.SHA256Bytes
+= CurStat
.FileSize
;
473 if (Fd
== NULL
&& OpenFile() == false)
477 SHA256Summation SHA256
;
478 if (Fd
->Seek(0) == false || SHA256
.AddFD(*Fd
, CurStat
.FileSize
) == false)
481 SHA256Res
= SHA256
.Result();
482 hex2bytes(CurStat
.SHA256
, SHA256Res
.data(), sizeof(CurStat
.SHA256
));
483 CurStat
.Flags
|= FlSHA256
;
487 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
488 // ---------------------------------------------------------------------
490 bool CacheDB::GetSHA512(bool const &GenOnly
)
492 // Try to read the control information out of the DB.
493 if ((CurStat
.Flags
& FlSHA512
) == FlSHA512
)
498 SHA512Res
= bytes2hex(CurStat
.SHA512
, sizeof(CurStat
.SHA512
));
502 Stats
.SHA512Bytes
+= CurStat
.FileSize
;
504 if (Fd
== NULL
&& OpenFile() == false)
508 SHA512Summation SHA512
;
509 if (Fd
->Seek(0) == false || SHA512
.AddFD(*Fd
, CurStat
.FileSize
) == false)
512 SHA512Res
= SHA512
.Result();
513 hex2bytes(CurStat
.SHA512
, SHA512Res
.data(), sizeof(CurStat
.SHA512
));
514 CurStat
.Flags
|= FlSHA512
;
518 // CacheDB::Finish - Write back the cache structure /*{{{*/
519 // ---------------------------------------------------------------------
521 bool CacheDB::Finish()
523 // Optimize away some writes.
524 if (CurStat
.Flags
== OldStat
.Flags
&&
525 CurStat
.mtime
== OldStat
.mtime
)
528 // Write the stat information
529 CurStat
.Flags
= htonl(CurStat
.Flags
);
530 CurStat
.FileSize
= htonl(CurStat
.FileSize
);
532 Put(&CurStat
,sizeof(CurStat
));
533 CurStat
.Flags
= ntohl(CurStat
.Flags
);
534 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
539 // CacheDB::Clean - Clean the Database /*{{{*/
540 // ---------------------------------------------------------------------
541 /* Tidy the database by removing files that no longer exist at all. */
542 bool CacheDB::Clean()
544 if (DBLoaded
== false)
547 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
548 needs the lower one and 2.7.7 needs the upper.. */
550 if ((errno
= Dbp
->cursor(Dbp
, NULL
, &Cursor
, 0)) != 0)
551 return _error
->Error(_("Unable to get a cursor"));
555 memset(&Key
,0,sizeof(Key
));
556 memset(&Data
,0,sizeof(Data
));
557 while ((errno
= Cursor
->c_get(Cursor
,&Key
,&Data
,DB_NEXT
)) == 0)
559 const char *Colon
= (char*)memrchr(Key
.data
, ':', Key
.size
);
562 if (stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"st") == 0 ||
563 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cl") == 0 ||
564 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cn") == 0)
566 if (FileExists(std::string((const char *)Key
.data
,Colon
)) == true)
571 Cursor
->c_del(Cursor
,0);
573 Dbp
->compact(Dbp
, NULL
, NULL
, NULL
, NULL
, DB_FREE_SPACE
, NULL
);