]>
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 /*{{{*/
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
)
35 ReadOnly
= _config
->FindB("APT::FTPArchive::ReadOnlyDB",false);
41 /* Check if the DB was disabled while running and deal with a
43 if (DBFailed() == true)
45 _error
->Warning(_("DB was corrupted, file renamed to %s.old"),DBFile
.c_str());
46 rename(DBFile
.c_str(),(DBFile
+".old").c_str());
56 db_create(&Dbp
, NULL
, 0);
57 if ((err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_HASH
,
58 (ReadOnly
?DB_RDONLY
:DB_CREATE
),
61 if (err
== DB_OLD_VERSION
)
63 _error
->Warning(_("DB is old, attempting to upgrade %s"),DBFile
.c_str());
64 err
= Dbp
->upgrade(Dbp
, DB
.c_str(), 0);
66 err
= Dbp
->open(Dbp
, NULL
, DB
.c_str(), NULL
, DB_HASH
,
67 (ReadOnly
?DB_RDONLY
:DB_CREATE
), 0644);
73 return _error
->Error(_("Unable to open DB file %s: %s"),DB
.c_str(), db_strerror(err
));
82 // CacheDB::SetFile - Select a file to be working with /*{{{*/
83 // ---------------------------------------------------------------------
84 /* All future actions will be performed against this file */
85 bool CacheDB::SetFile(string FileName
,struct stat St
,FileFd
*Fd
)
89 this->FileName
= FileName
;
93 memset(&CurStat
,0,sizeof(CurStat
));
95 Stats
.Bytes
+= St
.st_size
;
98 if (DBLoaded
== false)
103 // Ensure alignment of the returned structure
104 Data
.data
= &CurStat
;
105 Data
.ulen
= sizeof(CurStat
);
106 Data
.flags
= DB_DBT_USERMEM
;
107 // Lookup the stat info and confirm the file is unchanged
110 if (CurStat
.mtime
!= htonl(St
.st_mtime
))
112 CurStat
.mtime
= htonl(St
.st_mtime
);
114 _error
->Warning(_("File date has changed %s"),FileName
.c_str());
119 CurStat
.mtime
= htonl(St
.st_mtime
);
122 CurStat
.Flags
= ntohl(CurStat
.Flags
);
127 // CacheDB::LoadControl - Load Control information /*{{{*/
128 // ---------------------------------------------------------------------
130 bool CacheDB::LoadControl()
132 // Try to read the control information out of the DB.
133 if ((CurStat
.Flags
& FlControl
) == FlControl
)
135 // Lookup the control information
137 if (Get() == true && Control
.TakeControl(Data
.data
,Data
.size
) == true)
139 CurStat
.Flags
&= ~FlControl
;
142 // Create a deb instance to read the archive
145 DebFile
= new debDebFile(*Fd
);
146 if (_error
->PendingError() == true)
151 if (Control
.Read(*DebFile
) == false)
154 if (Control
.Control
== 0)
155 return _error
->Error(_("Archive has no control record"));
157 // Write back the control information
159 if (Put(Control
.Control
,Control
.Length
) == true)
160 CurStat
.Flags
|= FlControl
;
164 // CacheDB::LoadContents - Load the File Listing /*{{{*/
165 // ---------------------------------------------------------------------
167 bool CacheDB::LoadContents(bool GenOnly
)
169 // Try to read the control information out of the DB.
170 if ((CurStat
.Flags
& FlContents
) == FlContents
)
175 // Lookup the contents information
179 if (Contents
.TakeContents(Data
.data
,Data
.size
) == true)
183 CurStat
.Flags
&= ~FlContents
;
186 // Create a deb instance to read the archive
189 DebFile
= new debDebFile(*Fd
);
190 if (_error
->PendingError() == true)
194 if (Contents
.Read(*DebFile
) == false)
197 // Write back the control information
199 if (Put(Contents
.Data
,Contents
.CurSize
) == true)
200 CurStat
.Flags
|= FlContents
;
204 // CacheDB::GetMD5 - Get the MD5 hash /*{{{*/
205 // ---------------------------------------------------------------------
207 bool CacheDB::GetMD5(string
&MD5Res
,bool GenOnly
)
209 // Try to read the control information out of the DB.
210 if ((CurStat
.Flags
& FlMD5
) == FlMD5
)
218 MD5Res
= string((char *)Data
.data
,Data
.size
);
221 CurStat
.Flags
&= ~FlMD5
;
224 Stats
.MD5Bytes
+= FileStat
.st_size
;
227 if (Fd
->Seek(0) == false || MD5
.AddFD(Fd
->Fd(),FileStat
.st_size
) == false)
230 MD5Res
= MD5
.Result();
232 if (Put(MD5Res
.c_str(),MD5Res
.length()) == true)
233 CurStat
.Flags
|= FlMD5
;
237 // CacheDB::Finish - Write back the cache structure /*{{{*/
238 // ---------------------------------------------------------------------
240 bool CacheDB::Finish()
242 // Optimize away some writes.
243 if (CurStat
.Flags
== OldStat
.Flags
&&
244 CurStat
.mtime
== OldStat
.mtime
)
247 // Write the stat information
248 CurStat
.Flags
= htonl(CurStat
.Flags
);
250 Put(&CurStat
,sizeof(CurStat
));
251 CurStat
.Flags
= ntohl(CurStat
.Flags
);
255 // CacheDB::Clean - Clean the Database /*{{{*/
256 // ---------------------------------------------------------------------
257 /* Tidy the database by removing files that no longer exist at all. */
258 bool CacheDB::Clean()
260 if (DBLoaded
== false)
263 /* I'm not sure what VERSION_MINOR should be here.. 2.4.14 certainly
264 needs the lower one and 2.7.7 needs the upper.. */
266 if ((errno
= Dbp
->cursor(Dbp
, NULL
, &Cursor
, 0)) != 0)
267 return _error
->Error(_("Unable to get a cursor"));
271 memset(&Key
,0,sizeof(Key
));
272 memset(&Data
,0,sizeof(Data
));
273 while ((errno
= Cursor
->c_get(Cursor
,&Key
,&Data
,DB_NEXT
)) == 0)
275 const char *Colon
= (char *)Key
.data
;
276 for (; Colon
!= (char *)Key
.data
+Key
.size
&& *Colon
!= ':'; Colon
++);
277 if ((char *)Key
.data
+Key
.size
- Colon
> 2)
279 if (stringcmp((char *)Key
.data
,Colon
,"st") == 0 ||
280 stringcmp((char *)Key
.data
,Colon
,"cn") == 0 ||
281 stringcmp((char *)Key
.data
,Colon
,"m5") == 0 ||
282 stringcmp((char *)Key
.data
,Colon
,"cl") == 0)
284 if (FileExists(string(Colon
+1,(const char *)Key
.data
+Key
.size
)) == true)
289 Cursor
->c_del(Cursor
,0);