1
// -*- mode: cpp; mode: fold -*-
3
// $Id: cachedb.cc,v 1.2 2001/02/20 07:03:18 jgg Exp $
4
/* ######################################################################
8
Simple uniform interface to a cache database.
10
##################################################################### */
12
// Include Files /*{{{*/
14
#pragma implementation "cachedb.h"
19
#include <apt-pkg/error.h>
20
#include <apt-pkg/md5.h>
21
#include <apt-pkg/strutl.h>
22
#include <apt-pkg/configuration.h>
24
#include <netinet/in.h> // htonl, etc
27
// CacheDB::ReadyDB - Ready the DB2 /*{{{*/
28
// ---------------------------------------------------------------------
29
/* This opens the DB2 file for caching package information */
30
bool CacheDB::ReadyDB(string DB)
32
ReadOnly = _config->FindB("APT::FTPArchive::ReadOnlyDB",false);
38
/* Check if the DB was disabled while running and deal with a
40
if (DBFailed() == true)
42
_error->Warning("DB was corrupted, file renamed to %s.old",DBFile.c_str());
43
rename(DBFile.c_str(),(DBFile+".old").c_str());
53
if ((errno = db_open(DB.c_str(),DB_HASH,
54
(ReadOnly?DB_RDONLY:DB_CREATE),
58
return _error->Errno("db_open","Unable to open DB2 file %s",DB.c_str());
66
// CacheDB::SetFile - Select a file to be working with /*{{{*/
67
// ---------------------------------------------------------------------
68
/* All future actions will be performed against this file */
69
bool CacheDB::SetFile(string FileName,struct stat St,FileFd *Fd)
73
this->FileName = FileName;
77
memset(&CurStat,0,sizeof(CurStat));
79
Stats.Bytes += St.st_size;
82
if (DBLoaded == false)
87
// Ensure alignment of the returned structure
89
Data.ulen = sizeof(CurStat);
90
Data.flags = DB_DBT_USERMEM;
91
// Lookup the stat info and confirm the file is unchanged
94
if (CurStat.st_mtime != htonl(St.st_mtime))
96
CurStat.st_mtime = htonl(St.st_mtime);
98
_error->Warning("File date has changed %s",FileName.c_str());
103
CurStat.st_mtime = htonl(St.st_mtime);
106
CurStat.Flags = ntohl(CurStat.Flags);
111
// CacheDB::LoadControl - Load Control information /*{{{*/
112
// ---------------------------------------------------------------------
114
bool CacheDB::LoadControl()
116
// Try to read the control information out of the DB.
117
if ((CurStat.Flags & FlControl) == FlControl)
119
// Lookup the control information
121
if (Get() == true && Control.TakeControl(Data.data,Data.size) == true)
123
CurStat.Flags &= ~FlControl;
126
// Create a deb instance to read the archive
129
DebFile = new debDebFile(*Fd);
130
if (_error->PendingError() == true)
135
if (Control.Read(*DebFile) == false)
138
if (Control.Control == 0)
139
return _error->Error("Archive has no control record");
141
// Write back the control information
143
if (Put(Control.Control,Control.Length) == true)
144
CurStat.Flags |= FlControl;
148
// CacheDB::LoadContents - Load the File Listing /*{{{*/
149
// ---------------------------------------------------------------------
151
bool CacheDB::LoadContents(bool GenOnly)
153
// Try to read the control information out of the DB.
154
if ((CurStat.Flags & FlContents) == FlContents)
159
// Lookup the contents information
163
if (Contents.TakeContents(Data.data,Data.size) == true)
167
CurStat.Flags &= ~FlContents;
170
// Create a deb instance to read the archive
173
DebFile = new debDebFile(*Fd);
174
if (_error->PendingError() == true)
178
if (Contents.Read(*DebFile) == false)
181
// Write back the control information
183
if (Put(Contents.Data,Contents.CurSize) == true)
184
CurStat.Flags |= FlContents;
188
// CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
189
// ---------------------------------------------------------------------
191
bool CacheDB::GetMD5(string &MD5Res,bool GenOnly)
193
// Try to read the control information out of the DB.
194
if ((CurStat.Flags & FlMD5) == FlMD5)
202
MD5Res = string((char *)Data.data,Data.size);
205
CurStat.Flags &= ~FlMD5;
208
Stats.MD5Bytes += FileStat.st_size;
211
if (Fd->Seek(0) == false || MD5.AddFD(Fd->Fd(),FileStat.st_size) == false)
214
MD5Res = MD5.Result();
216
if (Put(MD5Res.begin(),MD5Res.length()) == true)
217
CurStat.Flags |= FlMD5;
221
// CacheDB::Finish - Write back the cache structure /*{{{*/
222
// ---------------------------------------------------------------------
224
bool CacheDB::Finish()
226
// Optimize away some writes.
227
if (CurStat.Flags == OldStat.Flags &&
228
CurStat.st_mtime == OldStat.st_mtime)
231
// Write the stat information
232
CurStat.Flags = htonl(CurStat.Flags);
234
Put(&CurStat,sizeof(CurStat));
235
CurStat.Flags = ntohl(CurStat.Flags);
239
// CacheDB::Clean - Clean the Database /*{{{*/
240
// ---------------------------------------------------------------------
241
/* Tidy the database by removing files that no longer exist at all. */
242
bool CacheDB::Clean()
244
if (DBLoaded == false)
247
/* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
248
needs the lower one and 2.7.7 needs the upper.. */
249
#if DB_VERSION_MAJOR >= 2 && DB_VERSION_MINOR >= 7
251
if ((errno = Dbp->cursor(Dbp,0,&Cursor,0)) != 0)
252
return _error->Error("Unable to get a cursor");
255
if ((errno = Dbp->cursor(Dbp,0,&Cursor)) != 0)
256
return _error->Error("Unable to get a cursor");
261
memset(&Key,0,sizeof(Key));
262
memset(&Data,0,sizeof(Data));
263
while ((errno = Cursor->c_get(Cursor,&Key,&Data,DB_NEXT)) == 0)
265
const char *Colon = (char *)Key.data;
266
for (; Colon != (char *)Key.data+Key.size && *Colon != ':'; Colon++);
267
if ((char *)Key.data+Key.size - Colon > 2)
269
if (stringcmp((char *)Key.data,Colon,"st") == 0 ||
270
stringcmp((char *)Key.data,Colon,"cn") == 0 ||
271
stringcmp((char *)Key.data,Colon,"m5") == 0 ||
272
stringcmp((char *)Key.data,Colon,"cl") == 0)
274
if (FileExists(string(Colon+1,(const char *)Key.data+Key.size)) == true)
279
Cursor->c_del(Cursor,0);