1 // -*- mode: cpp; mode: fold -*-
3 // $Id: debindexfile.cc,v 1.5.2.3 2004/01/04 19:11:00 mdz Exp $
4 /* ######################################################################
6 Debian Specific sources.list types and the three sorts of Debian
9 ##################################################################### */
11 // Include Files /*{{{*/
13 #pragma implementation "apt-pkg/debindexfile.h"
16 #include <apt-pkg/debindexfile.h>
17 #include <apt-pkg/debsrcrecords.h>
18 #include <apt-pkg/deblistparser.h>
19 #include <apt-pkg/debrecords.h>
20 #include <apt-pkg/sourcelist.h>
21 #include <apt-pkg/configuration.h>
22 #include <apt-pkg/progress.h>
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/strutl.h>
25 #include <apt-pkg/acquire-item.h>
26 #include <apt-pkg/debmetaindex.h>
31 // SourcesIndex::debSourcesIndex - Constructor /*{{{*/
32 // ---------------------------------------------------------------------
34 debSourcesIndex::debSourcesIndex(string URI
,string Dist
,string Section
,bool Trusted
) :
35 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
)
39 // SourcesIndex::SourceInfo - Short 1 liner describing a source /*{{{*/
40 // ---------------------------------------------------------------------
41 /* The result looks like:
42 http://foo/ stable/main src 1.1.1 (dsc) */
43 string
debSourcesIndex::SourceInfo(pkgSrcRecords::Parser
const &Record
,
44 pkgSrcRecords::File
const &File
) const
47 Res
= ::URI::SiteOnly(URI
) + ' ';
48 if (Dist
[Dist
.size() - 1] == '/')
54 Res
+= Dist
+ '/' + Section
;
57 Res
+= Record
.Package();
59 Res
+= Record
.Version();
60 if (File
.Type
.empty() == false)
61 Res
+= " (" + File
.Type
+ ")";
65 // SourcesIndex::CreateSrcParser - Get a parser for the source files /*{{{*/
66 // ---------------------------------------------------------------------
68 pkgSrcRecords::Parser
*debSourcesIndex::CreateSrcParser() const
70 string SourcesURI
= URItoFileName(IndexURI("Sources"));
71 return new debSrcRecordParser(_config
->FindDir("Dir::State::lists") +
75 // SourcesIndex::Describe - Give a descriptive path to the index /*{{{*/
76 // ---------------------------------------------------------------------
78 string
debSourcesIndex::Describe(bool Short
) const
82 snprintf(S
,sizeof(S
),"%s",Info("Sources").c_str());
84 snprintf(S
,sizeof(S
),"%s (%s)",Info("Sources").c_str(),
85 IndexFile("Sources").c_str());
90 // SourcesIndex::Info - One liner describing the index URI /*{{{*/
91 // ---------------------------------------------------------------------
93 string
debSourcesIndex::Info(const char *Type
) const
95 string Info
= ::URI::SiteOnly(URI
) + ' ';
96 if (Dist
[Dist
.size() - 1] == '/')
102 Info
+= Dist
+ '/' + Section
;
108 // SourcesIndex::Index* - Return the URI to the index files /*{{{*/
109 // ---------------------------------------------------------------------
111 inline string
debSourcesIndex::IndexFile(const char *Type
) const
113 return URItoFileName(IndexURI(Type
));
115 string
debSourcesIndex::IndexURI(const char *Type
) const
118 if (Dist
[Dist
.size() - 1] == '/')
126 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
133 // SourcesIndex::Exists - Check if the index is available /*{{{*/
134 // ---------------------------------------------------------------------
136 bool debSourcesIndex::Exists() const
138 return FileExists(IndexFile("Sources"));
141 // SourcesIndex::Size - Return the size of the index /*{{{*/
142 // ---------------------------------------------------------------------
144 unsigned long debSourcesIndex::Size() const
147 if (stat(IndexFile("Sources").c_str(),&S
) != 0)
153 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
154 // ---------------------------------------------------------------------
156 debPackagesIndex::debPackagesIndex(string URI
,string Dist
,string Section
,bool Trusted
) :
157 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
)
162 string
debPackagesIndex::ArchiveURI(string File
) const
164 // FIXME: EVIL! Remove as soon as pdiff support is offical
165 string remap
= _config
->Find("APT::URL-Remap::"+URI
,"");
172 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
173 // ---------------------------------------------------------------------
174 /* This is a shorter version that is designed to be < 60 chars or so */
175 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
177 string Res
= ::URI::SiteOnly(URI
) + ' ';
178 if (Dist
[Dist
.size() - 1] == '/')
184 Res
+= Dist
+ '/' + Section
;
187 Res
+= Ver
.ParentPkg().Name();
193 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
194 // ---------------------------------------------------------------------
195 /* This should help the user find the index in the sources.list and
196 in the filesystem for problem solving */
197 string
debPackagesIndex::Describe(bool Short
) const
201 snprintf(S
,sizeof(S
),"%s",Info("Packages").c_str());
203 snprintf(S
,sizeof(S
),"%s (%s)",Info("Packages").c_str(),
204 IndexFile("Packages").c_str());
208 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
209 // ---------------------------------------------------------------------
211 string
debPackagesIndex::Info(const char *Type
) const
213 string Info
= ::URI::SiteOnly(URI
) + ' ';
214 if (Dist
[Dist
.size() - 1] == '/')
220 Info
+= Dist
+ '/' + Section
;
226 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
227 // ---------------------------------------------------------------------
229 inline string
debPackagesIndex::IndexFile(const char *Type
) const
231 return _config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
233 string
debPackagesIndex::IndexURI(const char *Type
) const
236 if (Dist
[Dist
.size() - 1] == '/')
244 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
245 "/binary-" + _config
->Find("APT::Architecture") + '/';
251 // PackagesIndex::Exists - Check if the index is available /*{{{*/
252 // ---------------------------------------------------------------------
254 bool debPackagesIndex::Exists() const
256 return FileExists(IndexFile("Packages"));
259 // PackagesIndex::Size - Return the size of the index /*{{{*/
260 // ---------------------------------------------------------------------
261 /* This is really only used for progress reporting. */
262 unsigned long debPackagesIndex::Size() const
265 if (stat(IndexFile("Packages").c_str(),&S
) != 0)
270 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
271 // ---------------------------------------------------------------------
273 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
&Prog
) const
275 string PackageFile
= IndexFile("Packages");
276 FileFd
Pkg(PackageFile
,FileFd::ReadOnly
);
277 debListParser
Parser(&Pkg
);
278 if (_error
->PendingError() == true)
279 return _error
->Error("Problem opening %s",PackageFile
.c_str());
281 Prog
.SubProgress(0,Info("Packages"));
283 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
284 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
286 // Store the IMS information
287 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
289 if (fstat(Pkg
.Fd(),&St
) != 0)
290 return _error
->Errno("fstat","Failed to stat");
291 File
->Size
= St
.st_size
;
292 File
->mtime
= St
.st_mtime
;
294 if (Gen
.MergeList(Parser
) == false)
295 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
297 // Check the release file
298 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
299 if (FileExists(ReleaseFile
) == true)
301 FileFd
Rel(ReleaseFile
,FileFd::ReadOnly
);
302 if (_error
->PendingError() == true)
304 Parser
.LoadReleaseInfo(File
,Rel
,Section
);
310 // PackagesIndex::FindInCache - Find this index /*{{{*/
311 // ---------------------------------------------------------------------
313 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
315 string FileName
= IndexFile("Packages");
316 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
317 for (; File
.end() == false; File
++)
319 if (FileName
!= File
.FileName())
323 if (stat(File
.FileName(),&St
) != 0)
324 return pkgCache::PkgFileIterator(Cache
);
325 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
326 return pkgCache::PkgFileIterator(Cache
);
334 // StatusIndex::debStatusIndex - Constructor /*{{{*/
335 // ---------------------------------------------------------------------
337 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
341 // StatusIndex::Size - Return the size of the index /*{{{*/
342 // ---------------------------------------------------------------------
344 unsigned long debStatusIndex::Size() const
347 if (stat(File
.c_str(),&S
) != 0)
352 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
353 // ---------------------------------------------------------------------
355 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
&Prog
) const
357 FileFd
Pkg(File
,FileFd::ReadOnly
);
358 if (_error
->PendingError() == true)
360 debListParser
Parser(&Pkg
);
361 if (_error
->PendingError() == true)
364 Prog
.SubProgress(0,File
);
365 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
366 return _error
->Error("Problem with SelectFile %s",File
.c_str());
368 // Store the IMS information
369 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
371 if (fstat(Pkg
.Fd(),&St
) != 0)
372 return _error
->Errno("fstat","Failed to stat");
373 CFile
->Size
= St
.st_size
;
374 CFile
->mtime
= St
.st_mtime
;
375 CFile
->Archive
= Gen
.WriteUniqString("now");
377 if (Gen
.MergeList(Parser
) == false)
378 return _error
->Error("Problem with MergeList %s",File
.c_str());
382 // StatusIndex::FindInCache - Find this index /*{{{*/
383 // ---------------------------------------------------------------------
385 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
387 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
388 for (; File
.end() == false; File
++)
390 if (this->File
!= File
.FileName())
394 if (stat(File
.FileName(),&St
) != 0)
395 return pkgCache::PkgFileIterator(Cache
);
396 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
397 return pkgCache::PkgFileIterator(Cache
);
403 // StatusIndex::Exists - Check if the index is available /*{{{*/
404 // ---------------------------------------------------------------------
406 bool debStatusIndex::Exists() const
408 // Abort if the file does not exist.
413 // Index File types for Debian /*{{{*/
414 class debIFTypeSrc
: public pkgIndexFile::Type
418 debIFTypeSrc() {Label
= "Debian Source Index";};
420 class debIFTypePkg
: public pkgIndexFile::Type
424 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
426 return new debRecordParser(File
.FileName(),*File
.Cache());
428 debIFTypePkg() {Label
= "Debian Package Index";};
430 class debIFTypeStatus
: public pkgIndexFile::Type
434 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
436 return new debRecordParser(File
.FileName(),*File
.Cache());
438 debIFTypeStatus() {Label
= "Debian dpkg status file";};
440 static debIFTypeSrc _apt_Src
;
441 static debIFTypePkg _apt_Pkg
;
442 static debIFTypeStatus _apt_Status
;
444 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
448 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
452 const pkgIndexFile::Type
*debStatusIndex::GetType() const