]>
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
) {
304 if (length
* 2 > sizeof(space
) - 1) length
= (sizeof(space
) - 1) / 2;
305 for (size_t i
= 0; i
< length
; i
++)
306 snprintf(&space
[i
*2], 3, "%02x", bytes
[i
]);
307 return std::string(space
);
310 static inline unsigned char xdig2num(char const &dig
) {
311 if (isdigit(dig
)) return dig
- '0';
312 if ('a' <= dig
&& dig
<= 'f') return dig
- 'a' + 10;
313 if ('A' <= dig
&& dig
<= 'F') return dig
- 'A' + 10;
317 static void hex2bytes(uint8_t *bytes
, const char *hex
, int length
) {
318 while (length
-- > 0) {
320 if (isxdigit(hex
[0]) && isxdigit(hex
[1])) {
321 *bytes
= xdig2num(hex
[0]) * 16 + xdig2num(hex
[1]);
328 // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
329 // ---------------------------------------------------------------------
331 bool CacheDB::GetMD5(bool const &GenOnly
)
333 // Try to read the control information out of the DB.
334 if ((CurStat
.Flags
& FlMD5
) == FlMD5
)
339 MD5Res
= bytes2hex(CurStat
.MD5
, sizeof(CurStat
.MD5
));
343 Stats
.MD5Bytes
+= CurStat
.FileSize
;
345 if (Fd
== NULL
&& OpenFile() == false)
350 if (Fd
->Seek(0) == false || MD5
.AddFD(Fd
->Fd(),CurStat
.FileSize
) == false)
353 MD5Res
= MD5
.Result();
354 hex2bytes(CurStat
.MD5
, MD5Res
.data(), sizeof(CurStat
.MD5
));
355 CurStat
.Flags
|= FlMD5
;
359 // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
360 // ---------------------------------------------------------------------
362 bool CacheDB::GetSHA1(bool const &GenOnly
)
364 // Try to read the control information out of the DB.
365 if ((CurStat
.Flags
& FlSHA1
) == FlSHA1
)
370 SHA1Res
= bytes2hex(CurStat
.SHA1
, sizeof(CurStat
.SHA1
));
374 Stats
.SHA1Bytes
+= CurStat
.FileSize
;
376 if (Fd
== NULL
&& OpenFile() == false)
381 if (Fd
->Seek(0) == false || SHA1
.AddFD(Fd
->Fd(),CurStat
.FileSize
) == false)
384 SHA1Res
= SHA1
.Result();
385 hex2bytes(CurStat
.SHA1
, SHA1Res
.data(), sizeof(CurStat
.SHA1
));
386 CurStat
.Flags
|= FlSHA1
;
390 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
391 // ---------------------------------------------------------------------
393 bool CacheDB::GetSHA256(bool const &GenOnly
)
395 // Try to read the control information out of the DB.
396 if ((CurStat
.Flags
& FlSHA256
) == FlSHA256
)
401 SHA256Res
= bytes2hex(CurStat
.SHA256
, sizeof(CurStat
.SHA256
));
405 Stats
.SHA256Bytes
+= CurStat
.FileSize
;
407 if (Fd
== NULL
&& OpenFile() == false)
411 SHA256Summation SHA256
;
412 if (Fd
->Seek(0) == false || SHA256
.AddFD(Fd
->Fd(),CurStat
.FileSize
) == false)
415 SHA256Res
= SHA256
.Result();
416 hex2bytes(CurStat
.SHA256
, SHA256Res
.data(), sizeof(CurStat
.SHA256
));
417 CurStat
.Flags
|= FlSHA256
;
421 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
422 // ---------------------------------------------------------------------
424 bool CacheDB::GetSHA512(bool const &GenOnly
)
426 // Try to read the control information out of the DB.
427 if ((CurStat
.Flags
& FlSHA512
) == FlSHA512
)
432 SHA512Res
= bytes2hex(CurStat
.SHA512
, sizeof(CurStat
.SHA512
));
436 Stats
.SHA512Bytes
+= CurStat
.FileSize
;
438 if (Fd
== NULL
&& OpenFile() == false)
442 SHA512Summation SHA512
;
443 if (Fd
->Seek(0) == false || SHA512
.AddFD(Fd
->Fd(),CurStat
.FileSize
) == false)
446 SHA512Res
= SHA512
.Result();
447 hex2bytes(CurStat
.SHA512
, SHA512Res
.data(), sizeof(CurStat
.SHA512
));
448 CurStat
.Flags
|= FlSHA512
;
452 // CacheDB::Finish - Write back the cache structure /*{{{*/
453 // ---------------------------------------------------------------------
455 bool CacheDB::Finish()
457 // Optimize away some writes.
458 if (CurStat
.Flags
== OldStat
.Flags
&&
459 CurStat
.mtime
== OldStat
.mtime
)
462 // Write the stat information
463 CurStat
.Flags
= htonl(CurStat
.Flags
);
464 CurStat
.FileSize
= htonl(CurStat
.FileSize
);
466 Put(&CurStat
,sizeof(CurStat
));
467 CurStat
.Flags
= ntohl(CurStat
.Flags
);
468 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
473 // CacheDB::Clean - Clean the Database /*{{{*/
474 // ---------------------------------------------------------------------
475 /* Tidy the database by removing files that no longer exist at all. */
476 bool CacheDB::Clean()
478 if (DBLoaded
== false)
481 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
482 needs the lower one and 2.7.7 needs the upper.. */
484 if ((errno
= Dbp
->cursor(Dbp
, NULL
, &Cursor
, 0)) != 0)
485 return _error
->Error(_("Unable to get a cursor"));
489 memset(&Key
,0,sizeof(Key
));
490 memset(&Data
,0,sizeof(Data
));
491 while ((errno
= Cursor
->c_get(Cursor
,&Key
,&Data
,DB_NEXT
)) == 0)
493 const char *Colon
= (char*)memrchr(Key
.data
, ':', Key
.size
);
496 if (stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"st") == 0 ||
497 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cl") == 0 ||
498 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cn") == 0)
500 if (FileExists(std::string((const char *)Key
.data
,Colon
)) == true)
505 Cursor
->c_del(Cursor
,0);
507 Dbp
->compact(Dbp
, NULL
, NULL
, NULL
, NULL
, DB_FREE_SPACE
, NULL
);