]>
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>
23 #include <netinet/in.h> // htonl, etc
29 // CacheDB::ReadyDB - Ready the DB2 /*{{{*/
30 // ---------------------------------------------------------------------
31 /* This opens the DB2 file for caching package information */
32 bool CacheDB::ReadyDB(std::string
const &DB
)
36 ReadOnly
= _config
->FindB("APT::FTPArchive::ReadOnlyDB",false);
42 /* Check if the DB was disabled while running and deal with a
44 if (DBFailed() == true)
46 _error
->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile
.c_str());
47 rename(DBFile
.c_str(),(DBFile
+".old").c_str());
52 DBFile
= std::string();
57 db_create(&Dbp
, NULL
, 0);
58 if ((err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_BTREE
,
59 (ReadOnly
?DB_RDONLY
:DB_CREATE
),
62 if (err
== DB_OLD_VERSION
)
64 _error
->Warning(_("DB is old, attempting to upgrade %s"),DBFile
.c_str());
65 err
= Dbp
->upgrade(Dbp
, DB
.c_str(), 0);
67 err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_HASH
,
68 (ReadOnly
?DB_RDONLY
:DB_CREATE
), 0644);
71 // the database format has changed from DB_HASH to DB_BTREE in
75 _error
->Error(_("DB format is invalid. If you upgraded from an older version of apt, please remove and re-create the database."));
80 return _error
->Error(_("Unable to open DB file %s: %s"),DB
.c_str(), db_strerror(err
));
89 // CacheDB::OpenFile - Open the file /*{{{*/
90 // ---------------------------------------------------------------------
92 bool CacheDB::OpenFile()
94 Fd
= new FileFd(FileName
,FileFd::ReadOnly
);
95 if (_error
->PendingError() == true)
104 // CacheDB::GetFileStat - Get stats from the file /*{{{*/
105 // ---------------------------------------------------------------------
106 /* This gets the size from the database if it's there. If we need
107 * to look at the file, also get the mtime from the file. */
108 bool CacheDB::GetFileStat(bool const &doStat
)
110 if ((CurStat
.Flags
& FlSize
) == FlSize
&& doStat
== false)
112 /* Already worked out the file size */
116 /* Get it from the file. */
117 if (Fd
== NULL
&& OpenFile() == false)
123 if (fstat(Fd
->Fd(),&St
) != 0)
125 return _error
->Errno("fstat",
126 _("Failed to stat %s"),FileName
.c_str());
128 CurStat
.FileSize
= St
.st_size
;
129 CurStat
.mtime
= htonl(St
.st_mtime
);
130 CurStat
.Flags
|= FlSize
;
135 // CacheDB::GetCurStat - Set the CurStat variable. /*{{{*/
136 // ---------------------------------------------------------------------
137 /* Sets the CurStat variable. Either to 0 if no database is used
138 * or to the value in the database if one is used */
139 bool CacheDB::GetCurStat()
141 memset(&CurStat
,0,sizeof(CurStat
));
145 /* First see if there is anything about it
148 /* Get the flags (and mtime) */
150 // Ensure alignment of the returned structure
151 Data
.data
= &CurStat
;
152 Data
.ulen
= sizeof(CurStat
);
153 Data
.flags
= DB_DBT_USERMEM
;
158 CurStat
.Flags
= ntohl(CurStat
.Flags
);
159 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
164 // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
165 // ---------------------------------------------------------------------
166 bool CacheDB::GetFileInfo(std::string
const &FileName
, bool const &DoControl
, bool const &DoContents
,
167 bool const &GenContentsOnly
, bool const &DoMD5
, bool const &DoSHA1
,
168 bool const &DoSHA256
, bool const &DoSHA512
,
169 bool const &checkMtime
)
171 this->FileName
= FileName
;
173 if (GetCurStat() == false)
179 if (GetFileStat(checkMtime
) == false)
186 /* if mtime changed, update CurStat from disk */
187 if (checkMtime
== true && OldStat
.mtime
!= CurStat
.mtime
)
188 CurStat
.Flags
= FlSize
;
190 Stats
.Bytes
+= CurStat
.FileSize
;
193 if ((DoControl
&& LoadControl() == false)
194 || (DoContents
&& LoadContents(GenContentsOnly
) == false)
195 || (DoMD5
&& GetMD5(false) == false)
196 || (DoSHA1
&& GetSHA1(false) == false)
197 || (DoSHA256
&& GetSHA256(false) == false)
198 || (DoSHA512
&& GetSHA512(false) == false)
216 // CacheDB::LoadControl - Load Control information /*{{{*/
217 // ---------------------------------------------------------------------
219 bool CacheDB::LoadControl()
221 // Try to read the control information out of the DB.
222 if ((CurStat
.Flags
& FlControl
) == FlControl
)
224 // Lookup the control information
226 if (Get() == true && Control
.TakeControl(Data
.data
,Data
.size
) == true)
228 CurStat
.Flags
&= ~FlControl
;
231 if (Fd
== NULL
&& OpenFile() == false)
235 // Create a deb instance to read the archive
238 DebFile
= new debDebFile(*Fd
);
239 if (_error
->PendingError() == true)
244 if (Control
.Read(*DebFile
) == false)
247 if (Control
.Control
== 0)
248 return _error
->Error(_("Archive has no control record"));
250 // Write back the control information
252 if (Put(Control
.Control
,Control
.Length
) == true)
253 CurStat
.Flags
|= FlControl
;
257 // CacheDB::LoadContents - Load the File Listing /*{{{*/
258 // ---------------------------------------------------------------------
260 bool CacheDB::LoadContents(bool const &GenOnly
)
262 // Try to read the control information out of the DB.
263 if ((CurStat
.Flags
& FlContents
) == FlContents
)
268 // Lookup the contents information
272 if (Contents
.TakeContents(Data
.data
,Data
.size
) == true)
276 CurStat
.Flags
&= ~FlContents
;
279 if (Fd
== NULL
&& OpenFile() == false)
283 // Create a deb instance to read the archive
286 DebFile
= new debDebFile(*Fd
);
287 if (_error
->PendingError() == true)
291 if (Contents
.Read(*DebFile
) == false)
294 // Write back the control information
296 if (Put(Contents
.Data
,Contents
.CurSize
) == true)
297 CurStat
.Flags
|= FlContents
;
302 static std::string
bytes2hex(uint8_t *bytes
, size_t length
) {
306 space
.reserve(length
*2 + 1);
307 for (size_t i
= 0; i
< length
; i
++) {
308 snprintf(buf
, sizeof(buf
), "%02x", bytes
[i
]);
314 static inline unsigned char xdig2num(char const &dig
) {
315 if (isdigit(dig
)) return dig
- '0';
316 if ('a' <= dig
&& dig
<= 'f') return dig
- 'a' + 10;
317 if ('A' <= dig
&& dig
<= 'F') return dig
- 'A' + 10;
321 static void hex2bytes(uint8_t *bytes
, const char *hex
, int length
) {
322 while (length
-- > 0) {
324 if (isxdigit(hex
[0]) && isxdigit(hex
[1])) {
325 *bytes
= xdig2num(hex
[0]) * 16 + xdig2num(hex
[1]);
332 // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
333 // ---------------------------------------------------------------------
335 bool CacheDB::GetMD5(bool const &GenOnly
)
337 // Try to read the control information out of the DB.
338 if ((CurStat
.Flags
& FlMD5
) == FlMD5
)
343 MD5Res
= bytes2hex(CurStat
.MD5
, sizeof(CurStat
.MD5
));
347 Stats
.MD5Bytes
+= CurStat
.FileSize
;
349 if (Fd
== NULL
&& OpenFile() == false)
354 if (Fd
->Seek(0) == false || MD5
.AddFD(*Fd
, CurStat
.FileSize
) == false)
357 MD5Res
= MD5
.Result();
358 hex2bytes(CurStat
.MD5
, MD5Res
.data(), sizeof(CurStat
.MD5
));
359 CurStat
.Flags
|= FlMD5
;
363 // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
364 // ---------------------------------------------------------------------
366 bool CacheDB::GetSHA1(bool const &GenOnly
)
368 // Try to read the control information out of the DB.
369 if ((CurStat
.Flags
& FlSHA1
) == FlSHA1
)
374 SHA1Res
= bytes2hex(CurStat
.SHA1
, sizeof(CurStat
.SHA1
));
378 Stats
.SHA1Bytes
+= CurStat
.FileSize
;
380 if (Fd
== NULL
&& OpenFile() == false)
385 if (Fd
->Seek(0) == false || SHA1
.AddFD(*Fd
, CurStat
.FileSize
) == false)
388 SHA1Res
= SHA1
.Result();
389 hex2bytes(CurStat
.SHA1
, SHA1Res
.data(), sizeof(CurStat
.SHA1
));
390 CurStat
.Flags
|= FlSHA1
;
394 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
395 // ---------------------------------------------------------------------
397 bool CacheDB::GetSHA256(bool const &GenOnly
)
399 // Try to read the control information out of the DB.
400 if ((CurStat
.Flags
& FlSHA256
) == FlSHA256
)
405 SHA256Res
= bytes2hex(CurStat
.SHA256
, sizeof(CurStat
.SHA256
));
409 Stats
.SHA256Bytes
+= CurStat
.FileSize
;
411 if (Fd
== NULL
&& OpenFile() == false)
415 SHA256Summation SHA256
;
416 if (Fd
->Seek(0) == false || SHA256
.AddFD(*Fd
, CurStat
.FileSize
) == false)
419 SHA256Res
= SHA256
.Result();
420 hex2bytes(CurStat
.SHA256
, SHA256Res
.data(), sizeof(CurStat
.SHA256
));
421 CurStat
.Flags
|= FlSHA256
;
425 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
426 // ---------------------------------------------------------------------
428 bool CacheDB::GetSHA512(bool const &GenOnly
)
430 // Try to read the control information out of the DB.
431 if ((CurStat
.Flags
& FlSHA512
) == FlSHA512
)
436 SHA512Res
= bytes2hex(CurStat
.SHA512
, sizeof(CurStat
.SHA512
));
440 Stats
.SHA512Bytes
+= CurStat
.FileSize
;
442 if (Fd
== NULL
&& OpenFile() == false)
446 SHA512Summation SHA512
;
447 if (Fd
->Seek(0) == false || SHA512
.AddFD(*Fd
, CurStat
.FileSize
) == false)
450 SHA512Res
= SHA512
.Result();
451 hex2bytes(CurStat
.SHA512
, SHA512Res
.data(), sizeof(CurStat
.SHA512
));
452 CurStat
.Flags
|= FlSHA512
;
456 // CacheDB::Finish - Write back the cache structure /*{{{*/
457 // ---------------------------------------------------------------------
459 bool CacheDB::Finish()
461 // Optimize away some writes.
462 if (CurStat
.Flags
== OldStat
.Flags
&&
463 CurStat
.mtime
== OldStat
.mtime
)
466 // Write the stat information
467 CurStat
.Flags
= htonl(CurStat
.Flags
);
468 CurStat
.FileSize
= htonl(CurStat
.FileSize
);
470 Put(&CurStat
,sizeof(CurStat
));
471 CurStat
.Flags
= ntohl(CurStat
.Flags
);
472 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
477 // CacheDB::Clean - Clean the Database /*{{{*/
478 // ---------------------------------------------------------------------
479 /* Tidy the database by removing files that no longer exist at all. */
480 bool CacheDB::Clean()
482 if (DBLoaded
== false)
485 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
486 needs the lower one and 2.7.7 needs the upper.. */
488 if ((errno
= Dbp
->cursor(Dbp
, NULL
, &Cursor
, 0)) != 0)
489 return _error
->Error(_("Unable to get a cursor"));
493 memset(&Key
,0,sizeof(Key
));
494 memset(&Data
,0,sizeof(Data
));
495 while ((errno
= Cursor
->c_get(Cursor
,&Key
,&Data
,DB_NEXT
)) == 0)
497 const char *Colon
= (char*)memrchr(Key
.data
, ':', Key
.size
);
500 if (stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"st") == 0 ||
501 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cl") == 0 ||
502 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cn") == 0)
504 if (FileExists(std::string((const char *)Key
.data
,Colon
)) == true)
509 Cursor
->c_del(Cursor
,0);
511 Dbp
->compact(Dbp
, NULL
, NULL
, NULL
, NULL
, DB_FREE_SPACE
, NULL
);