]>
git.saurik.com Git - apt.git/blob - ftparchive/cachedb.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cachedb.cc,v 1.6 2003/02/10 07:34:41 doogie Exp $
4 /* ######################################################################
8 Simple uniform interface to a cache database.
10 ##################################################################### */
12 // Include Files /*{{{*/
14 #pragma implementation "cachedb.h"
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/md5.h>
22 #include <apt-pkg/strutl.h>
23 #include <apt-pkg/configuration.h>
25 #include <netinet/in.h> // htonl, etc
28 // CacheDB::ReadyDB - Ready the DB2 /*{{{*/
29 // ---------------------------------------------------------------------
30 /* This opens the DB2 file for caching package information */
31 bool CacheDB::ReadyDB(string DB
)
33 ReadOnly
= _config
->FindB("APT::FTPArchive::ReadOnlyDB",false);
39 /* Check if the DB was disabled while running and deal with a
41 if (DBFailed() == true)
43 _error
->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile
.c_str());
44 rename(DBFile
.c_str(),(DBFile
+".old").c_str());
54 if ((errno
= db_open(DB
.c_str(),DB_HASH
,
55 (ReadOnly
?DB_RDONLY
:DB_CREATE
),
59 return _error
->Errno("db_open",_("Unable to open DB2 file %s"),DB
.c_str());
67 // CacheDB::SetFile - Select a file to be working with /*{{{*/
68 // ---------------------------------------------------------------------
69 /* All future actions will be performed against this file */
70 bool CacheDB::SetFile(string FileName
,struct stat St
,FileFd
*Fd
)
74 this->FileName
= FileName
;
78 memset(&CurStat
,0,sizeof(CurStat
));
80 Stats
.Bytes
+= St
.st_size
;
83 if (DBLoaded
== false)
88 // Ensure alignment of the returned structure
90 Data
.ulen
= sizeof(CurStat
);
91 Data
.flags
= DB_DBT_USERMEM
;
92 // Lookup the stat info and confirm the file is unchanged
95 if (CurStat
.mtime
!= htonl(St
.st_mtime
))
97 CurStat
.mtime
= htonl(St
.st_mtime
);
99 _error
->Warning(_("File date has changed %s"),FileName
.c_str());
104 CurStat
.mtime
= htonl(St
.st_mtime
);
107 CurStat
.Flags
= ntohl(CurStat
.Flags
);
112 // CacheDB::LoadControl - Load Control information /*{{{*/
113 // ---------------------------------------------------------------------
115 bool CacheDB::LoadControl()
117 // Try to read the control information out of the DB.
118 if ((CurStat
.Flags
& FlControl
) == FlControl
)
120 // Lookup the control information
122 if (Get() == true && Control
.TakeControl(Data
.data
,Data
.size
) == true)
124 CurStat
.Flags
&= ~FlControl
;
127 // Create a deb instance to read the archive
130 DebFile
= new debDebFile(*Fd
);
131 if (_error
->PendingError() == true)
136 if (Control
.Read(*DebFile
) == false)
139 if (Control
.Control
== 0)
140 return _error
->Error(_("Archive has no control record"));
142 // Write back the control information
144 if (Put(Control
.Control
,Control
.Length
) == true)
145 CurStat
.Flags
|= FlControl
;
149 // CacheDB::LoadContents - Load the File Listing /*{{{*/
150 // ---------------------------------------------------------------------
152 bool CacheDB::LoadContents(bool GenOnly
)
154 // Try to read the control information out of the DB.
155 if ((CurStat
.Flags
& FlContents
) == FlContents
)
160 // Lookup the contents information
164 if (Contents
.TakeContents(Data
.data
,Data
.size
) == true)
168 CurStat
.Flags
&= ~FlContents
;
171 // Create a deb instance to read the archive
174 DebFile
= new debDebFile(*Fd
);
175 if (_error
->PendingError() == true)
179 if (Contents
.Read(*DebFile
) == false)
182 // Write back the control information
184 if (Put(Contents
.Data
,Contents
.CurSize
) == true)
185 CurStat
.Flags
|= FlContents
;
189 // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
190 // ---------------------------------------------------------------------
192 bool CacheDB::GetMD5(string
&MD5Res
,bool GenOnly
)
194 // Try to read the control information out of the DB.
195 if ((CurStat
.Flags
& FlMD5
) == FlMD5
)
203 MD5Res
= string((char *)Data
.data
,Data
.size
);
206 CurStat
.Flags
&= ~FlMD5
;
209 Stats
.MD5Bytes
+= FileStat
.st_size
;
212 if (Fd
->Seek(0) == false || MD5
.AddFD(Fd
->Fd(),FileStat
.st_size
) == false)
215 MD5Res
= MD5
.Result();
217 if (Put(MD5Res
.c_str(),MD5Res
.length()) == true)
218 CurStat
.Flags
|= FlMD5
;
222 // CacheDB::Finish - Write back the cache structure /*{{{*/
223 // ---------------------------------------------------------------------
225 bool CacheDB::Finish()
227 // Optimize away some writes.
228 if (CurStat
.Flags
== OldStat
.Flags
&&
229 CurStat
.mtime
== OldStat
.mtime
)
232 // Write the stat information
233 CurStat
.Flags
= htonl(CurStat
.Flags
);
235 Put(&CurStat
,sizeof(CurStat
));
236 CurStat
.Flags
= ntohl(CurStat
.Flags
);
240 // CacheDB::Clean - Clean the Database /*{{{*/
241 // ---------------------------------------------------------------------
242 /* Tidy the database by removing files that no longer exist at all. */
243 bool CacheDB::Clean()
245 if (DBLoaded
== false)
248 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
249 needs the lower one and 2.7.7 needs the upper.. */
250 #if DB_VERSION_MAJOR >= 2 && DB_VERSION_MINOR >= 7
252 if ((errno
= Dbp
->cursor(Dbp
,0,&Cursor
,0)) != 0)
253 return _error
->Error(_("Unable to get a cursor"));
256 if ((errno
= Dbp
->cursor(Dbp
,0,&Cursor
)) != 0)
257 return _error
->Error(_("Unable to get a cursor"));
262 memset(&Key
,0,sizeof(Key
));
263 memset(&Data
,0,sizeof(Data
));
264 while ((errno
= Cursor
->c_get(Cursor
,&Key
,&Data
,DB_NEXT
)) == 0)
266 const char *Colon
= (char *)Key
.data
;
267 for (; Colon
!= (char *)Key
.data
+Key
.size
&& *Colon
!= ':'; Colon
++);
268 if ((char *)Key
.data
+Key
.size
- Colon
> 2)
270 if (stringcmp((char *)Key
.data
,Colon
,"st") == 0 ||
271 stringcmp((char *)Key
.data
,Colon
,"cn") == 0 ||
272 stringcmp((char *)Key
.data
,Colon
,"m5") == 0 ||
273 stringcmp((char *)Key
.data
,Colon
,"cl") == 0)
275 if (FileExists(string(Colon
+1,(const char *)Key
.data
+Key
.size
)) == true)
280 Cursor
->c_del(Cursor
,0);