]>
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>
24 #include <netinet/in.h> // htonl, etc
34 // CacheDB::ReadyDB - Ready the DB2 /*{{{*/
35 // ---------------------------------------------------------------------
36 /* This opens the DB2 file for caching package information */
37 bool CacheDB::ReadyDB(std::string
const &DB
)
41 ReadOnly
= _config
->FindB("APT::FTPArchive::ReadOnlyDB",false);
47 /* Check if the DB was disabled while running and deal with a
49 if (DBFailed() == true)
51 _error
->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile
.c_str());
52 rename(DBFile
.c_str(),(DBFile
+".old").c_str());
57 DBFile
= std::string();
62 db_create(&Dbp
, NULL
, 0);
63 if ((err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_BTREE
,
64 (ReadOnly
?DB_RDONLY
:DB_CREATE
),
67 if (err
== DB_OLD_VERSION
)
69 _error
->Warning(_("DB is old, attempting to upgrade %s"),DBFile
.c_str());
70 err
= Dbp
->upgrade(Dbp
, DB
.c_str(), 0);
72 err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_HASH
,
73 (ReadOnly
?DB_RDONLY
:DB_CREATE
), 0644);
76 // the database format has changed from DB_HASH to DB_BTREE in
80 _error
->Error(_("DB format is invalid. If you upgraded from an older version of apt, please remove and re-create the database."));
85 return _error
->Error(_("Unable to open DB file %s: %s"),DB
.c_str(), db_strerror(err
));
94 // CacheDB::OpenFile - Open the file /*{{{*/
95 // ---------------------------------------------------------------------
97 bool CacheDB::OpenFile()
99 Fd
= new FileFd(FileName
,FileFd::ReadOnly
);
100 if (_error
->PendingError() == true)
109 // CacheDB::GetFileStat - Get stats from the file /*{{{*/
110 // ---------------------------------------------------------------------
111 /* This gets the size from the database if it's there. If we need
112 * to look at the file, also get the mtime from the file. */
113 bool CacheDB::GetFileStat(bool const &doStat
)
115 if ((CurStat
.Flags
& FlSize
) == FlSize
&& doStat
== false)
117 /* Already worked out the file size */
121 /* Get it from the file. */
122 if (Fd
== NULL
&& OpenFile() == false)
128 if (fstat(Fd
->Fd(),&St
) != 0)
130 return _error
->Errno("fstat",
131 _("Failed to stat %s"),FileName
.c_str());
133 CurStat
.FileSize
= St
.st_size
;
134 CurStat
.mtime
= htonl(St
.st_mtime
);
135 CurStat
.Flags
|= FlSize
;
140 // CacheDB::GetCurStat - Set the CurStat variable. /*{{{*/
141 // ---------------------------------------------------------------------
142 /* Sets the CurStat variable. Either to 0 if no database is used
143 * or to the value in the database if one is used */
144 bool CacheDB::GetCurStat()
146 memset(&CurStat
,0,sizeof(CurStat
));
150 /* First see if there is anything about it
153 /* Get the flags (and mtime) */
155 // Ensure alignment of the returned structure
156 Data
.data
= &CurStat
;
157 Data
.ulen
= sizeof(CurStat
);
158 Data
.flags
= DB_DBT_USERMEM
;
163 CurStat
.Flags
= ntohl(CurStat
.Flags
);
164 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
169 // CacheDB::GetFileInfo - Get all the info about the file /*{{{*/
170 // ---------------------------------------------------------------------
171 bool CacheDB::GetFileInfo(std::string
const &FileName
, bool const &DoControl
, bool const &DoContents
,
172 bool const &GenContentsOnly
, bool const &DoMD5
, bool const &DoSHA1
,
173 bool const &DoSHA256
, bool const &DoSHA512
,
174 bool const &checkMtime
)
176 this->FileName
= FileName
;
178 if (GetCurStat() == false)
184 if (GetFileStat(checkMtime
) == false)
191 /* if mtime changed, update CurStat from disk */
192 if (checkMtime
== true && OldStat
.mtime
!= CurStat
.mtime
)
193 CurStat
.Flags
= FlSize
;
195 Stats
.Bytes
+= CurStat
.FileSize
;
198 if ((DoControl
&& LoadControl() == false)
199 || (DoContents
&& LoadContents(GenContentsOnly
) == false)
200 || (DoMD5
&& GetMD5(false) == false)
201 || (DoSHA1
&& GetSHA1(false) == false)
202 || (DoSHA256
&& GetSHA256(false) == false)
203 || (DoSHA512
&& GetSHA512(false) == false)
221 // CacheDB::LoadControl - Load Control information /*{{{*/
222 // ---------------------------------------------------------------------
224 bool CacheDB::LoadControl()
226 // Try to read the control information out of the DB.
227 if ((CurStat
.Flags
& FlControl
) == FlControl
)
229 // Lookup the control information
231 if (Get() == true && Control
.TakeControl(Data
.data
,Data
.size
) == true)
233 CurStat
.Flags
&= ~FlControl
;
236 if (Fd
== NULL
&& OpenFile() == false)
240 // Create a deb instance to read the archive
243 DebFile
= new debDebFile(*Fd
);
244 if (_error
->PendingError() == true)
249 if (Control
.Read(*DebFile
) == false)
252 if (Control
.Control
== 0)
253 return _error
->Error(_("Archive has no control record"));
255 // Write back the control information
257 if (Put(Control
.Control
,Control
.Length
) == true)
258 CurStat
.Flags
|= FlControl
;
262 // CacheDB::LoadContents - Load the File Listing /*{{{*/
263 // ---------------------------------------------------------------------
265 bool CacheDB::LoadContents(bool const &GenOnly
)
267 // Try to read the control information out of the DB.
268 if ((CurStat
.Flags
& FlContents
) == FlContents
)
273 // Lookup the contents information
277 if (Contents
.TakeContents(Data
.data
,Data
.size
) == true)
281 CurStat
.Flags
&= ~FlContents
;
284 if (Fd
== NULL
&& OpenFile() == false)
288 // Create a deb instance to read the archive
291 DebFile
= new debDebFile(*Fd
);
292 if (_error
->PendingError() == true)
296 if (Contents
.Read(*DebFile
) == false)
299 // Write back the control information
301 if (Put(Contents
.Data
,Contents
.CurSize
) == true)
302 CurStat
.Flags
|= FlContents
;
307 static std::string
bytes2hex(uint8_t *bytes
, size_t length
) {
311 space
.reserve(length
*2 + 1);
312 for (size_t i
= 0; i
< length
; i
++) {
313 snprintf(buf
, sizeof(buf
), "%02x", bytes
[i
]);
319 static inline unsigned char xdig2num(char const &dig
) {
320 if (isdigit(dig
)) return dig
- '0';
321 if ('a' <= dig
&& dig
<= 'f') return dig
- 'a' + 10;
322 if ('A' <= dig
&& dig
<= 'F') return dig
- 'A' + 10;
326 static void hex2bytes(uint8_t *bytes
, const char *hex
, int length
) {
327 while (length
-- > 0) {
329 if (isxdigit(hex
[0]) && isxdigit(hex
[1])) {
330 *bytes
= xdig2num(hex
[0]) * 16 + xdig2num(hex
[1]);
337 // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
338 // ---------------------------------------------------------------------
340 bool CacheDB::GetMD5(bool const &GenOnly
)
342 // Try to read the control information out of the DB.
343 if ((CurStat
.Flags
& FlMD5
) == FlMD5
)
348 MD5Res
= bytes2hex(CurStat
.MD5
, sizeof(CurStat
.MD5
));
352 Stats
.MD5Bytes
+= CurStat
.FileSize
;
354 if (Fd
== NULL
&& OpenFile() == false)
359 if (Fd
->Seek(0) == false || MD5
.AddFD(*Fd
, CurStat
.FileSize
) == false)
362 MD5Res
= MD5
.Result();
363 hex2bytes(CurStat
.MD5
, MD5Res
.data(), sizeof(CurStat
.MD5
));
364 CurStat
.Flags
|= FlMD5
;
368 // CacheDB::GetSHA1 - Get the SHA1 hash /*{{{*/
369 // ---------------------------------------------------------------------
371 bool CacheDB::GetSHA1(bool const &GenOnly
)
373 // Try to read the control information out of the DB.
374 if ((CurStat
.Flags
& FlSHA1
) == FlSHA1
)
379 SHA1Res
= bytes2hex(CurStat
.SHA1
, sizeof(CurStat
.SHA1
));
383 Stats
.SHA1Bytes
+= CurStat
.FileSize
;
385 if (Fd
== NULL
&& OpenFile() == false)
390 if (Fd
->Seek(0) == false || SHA1
.AddFD(*Fd
, CurStat
.FileSize
) == false)
393 SHA1Res
= SHA1
.Result();
394 hex2bytes(CurStat
.SHA1
, SHA1Res
.data(), sizeof(CurStat
.SHA1
));
395 CurStat
.Flags
|= FlSHA1
;
399 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
400 // ---------------------------------------------------------------------
402 bool CacheDB::GetSHA256(bool const &GenOnly
)
404 // Try to read the control information out of the DB.
405 if ((CurStat
.Flags
& FlSHA256
) == FlSHA256
)
410 SHA256Res
= bytes2hex(CurStat
.SHA256
, sizeof(CurStat
.SHA256
));
414 Stats
.SHA256Bytes
+= CurStat
.FileSize
;
416 if (Fd
== NULL
&& OpenFile() == false)
420 SHA256Summation SHA256
;
421 if (Fd
->Seek(0) == false || SHA256
.AddFD(*Fd
, CurStat
.FileSize
) == false)
424 SHA256Res
= SHA256
.Result();
425 hex2bytes(CurStat
.SHA256
, SHA256Res
.data(), sizeof(CurStat
.SHA256
));
426 CurStat
.Flags
|= FlSHA256
;
430 // CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
431 // ---------------------------------------------------------------------
433 bool CacheDB::GetSHA512(bool const &GenOnly
)
435 // Try to read the control information out of the DB.
436 if ((CurStat
.Flags
& FlSHA512
) == FlSHA512
)
441 SHA512Res
= bytes2hex(CurStat
.SHA512
, sizeof(CurStat
.SHA512
));
445 Stats
.SHA512Bytes
+= CurStat
.FileSize
;
447 if (Fd
== NULL
&& OpenFile() == false)
451 SHA512Summation SHA512
;
452 if (Fd
->Seek(0) == false || SHA512
.AddFD(*Fd
, CurStat
.FileSize
) == false)
455 SHA512Res
= SHA512
.Result();
456 hex2bytes(CurStat
.SHA512
, SHA512Res
.data(), sizeof(CurStat
.SHA512
));
457 CurStat
.Flags
|= FlSHA512
;
461 // CacheDB::Finish - Write back the cache structure /*{{{*/
462 // ---------------------------------------------------------------------
464 bool CacheDB::Finish()
466 // Optimize away some writes.
467 if (CurStat
.Flags
== OldStat
.Flags
&&
468 CurStat
.mtime
== OldStat
.mtime
)
471 // Write the stat information
472 CurStat
.Flags
= htonl(CurStat
.Flags
);
473 CurStat
.FileSize
= htonl(CurStat
.FileSize
);
475 Put(&CurStat
,sizeof(CurStat
));
476 CurStat
.Flags
= ntohl(CurStat
.Flags
);
477 CurStat
.FileSize
= ntohl(CurStat
.FileSize
);
482 // CacheDB::Clean - Clean the Database /*{{{*/
483 // ---------------------------------------------------------------------
484 /* Tidy the database by removing files that no longer exist at all. */
485 bool CacheDB::Clean()
487 if (DBLoaded
== false)
490 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
491 needs the lower one and 2.7.7 needs the upper.. */
493 if ((errno
= Dbp
->cursor(Dbp
, NULL
, &Cursor
, 0)) != 0)
494 return _error
->Error(_("Unable to get a cursor"));
498 memset(&Key
,0,sizeof(Key
));
499 memset(&Data
,0,sizeof(Data
));
500 while ((errno
= Cursor
->c_get(Cursor
,&Key
,&Data
,DB_NEXT
)) == 0)
502 const char *Colon
= (char*)memrchr(Key
.data
, ':', Key
.size
);
505 if (stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"st") == 0 ||
506 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cl") == 0 ||
507 stringcmp(Colon
+ 1, (char *)Key
.data
+Key
.size
,"cn") == 0)
509 if (FileExists(std::string((const char *)Key
.data
,Colon
)) == true)
514 Cursor
->c_del(Cursor
,0);
516 Dbp
->compact(Dbp
, NULL
, NULL
, NULL
, NULL
, DB_FREE_SPACE
, NULL
);