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
)
161 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
162 // ---------------------------------------------------------------------
163 /* This is a shorter version that is designed to be < 60 chars or so */
164 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
166 string Res
= ::URI::SiteOnly(URI
) + ' ';
167 if (Dist
[Dist
.size() - 1] == '/')
173 Res
+= Dist
+ '/' + Section
;
176 Res
+= Ver
.ParentPkg().Name();
182 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
183 // ---------------------------------------------------------------------
184 /* This should help the user find the index in the sources.list and
185 in the filesystem for problem solving */
186 string
debPackagesIndex::Describe(bool Short
) const
190 snprintf(S
,sizeof(S
),"%s",Info("Packages").c_str());
192 snprintf(S
,sizeof(S
),"%s (%s)",Info("Packages").c_str(),
193 IndexFile("Packages").c_str());
197 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
198 // ---------------------------------------------------------------------
200 string
debPackagesIndex::Info(const char *Type
) const
202 string Info
= ::URI::SiteOnly(URI
) + ' ';
203 if (Dist
[Dist
.size() - 1] == '/')
209 Info
+= Dist
+ '/' + Section
;
215 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
216 // ---------------------------------------------------------------------
218 inline string
debPackagesIndex::IndexFile(const char *Type
) const
220 return _config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
222 string
debPackagesIndex::IndexURI(const char *Type
) const
225 if (Dist
[Dist
.size() - 1] == '/')
233 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
234 "/binary-" + _config
->Find("APT::Architecture") + '/';
240 // PackagesIndex::Exists - Check if the index is available /*{{{*/
241 // ---------------------------------------------------------------------
243 bool debPackagesIndex::Exists() const
245 return FileExists(IndexFile("Packages"));
248 // PackagesIndex::Size - Return the size of the index /*{{{*/
249 // ---------------------------------------------------------------------
250 /* This is really only used for progress reporting. */
251 unsigned long debPackagesIndex::Size() const
254 if (stat(IndexFile("Packages").c_str(),&S
) != 0)
259 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
260 // ---------------------------------------------------------------------
262 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
&Prog
) const
264 string PackageFile
= IndexFile("Packages");
265 FileFd
Pkg(PackageFile
,FileFd::ReadOnly
);
266 debListParser
Parser(&Pkg
);
267 if (_error
->PendingError() == true)
268 return _error
->Error("Problem opening %s",PackageFile
.c_str());
270 Prog
.SubProgress(0,Info("Packages"));
272 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
273 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
275 // Store the IMS information
276 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
278 if (fstat(Pkg
.Fd(),&St
) != 0)
279 return _error
->Errno("fstat","Failed to stat");
280 File
->Size
= St
.st_size
;
281 File
->mtime
= St
.st_mtime
;
283 if (Gen
.MergeList(Parser
) == false)
284 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
286 // Check the release file
287 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
288 if (FileExists(ReleaseFile
) == true)
290 FileFd
Rel(ReleaseFile
,FileFd::ReadOnly
);
291 if (_error
->PendingError() == true)
293 Parser
.LoadReleaseInfo(File
,Rel
,Section
);
299 // PackagesIndex::FindInCache - Find this index /*{{{*/
300 // ---------------------------------------------------------------------
302 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
304 string FileName
= IndexFile("Packages");
305 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
306 for (; File
.end() == false; File
++)
308 if (FileName
!= File
.FileName())
312 if (stat(File
.FileName(),&St
) != 0)
313 return pkgCache::PkgFileIterator(Cache
);
314 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
315 return pkgCache::PkgFileIterator(Cache
);
323 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
324 // ---------------------------------------------------------------------
326 debTranslationsIndex::debTranslationsIndex(string URI
,string Dist
,string Section
) :
327 pkgIndexFile(true), URI(URI
), Dist(Dist
), Section(Section
)
331 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
332 // ---------------------------------------------------------------------
334 inline string
debTranslationsIndex::IndexFile(const char *Type
) const
336 return _config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
338 string
debTranslationsIndex::IndexURI(const char *Type
) const
341 if (Dist
[Dist
.size() - 1] == '/')
349 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
350 "/i18n/Translation-";
356 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
357 // ---------------------------------------------------------------------
359 bool debTranslationsIndex::GetIndexes(pkgAcquire
*Owner
) const
361 if (TranslationsAvailable()) {
362 string TranslationFile
= "Translation-" + LanguageCode();
363 new pkgAcqIndexTrans(Owner
, IndexURI(LanguageCode().c_str()),
364 Info(TranslationFile
.c_str()),
371 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
372 // ---------------------------------------------------------------------
373 /* This should help the user find the index in the sources.list and
374 in the filesystem for problem solving */
375 string
debTranslationsIndex::Describe(bool Short
) const
379 snprintf(S
,sizeof(S
),"%s",Info(TranslationFile().c_str()).c_str());
381 snprintf(S
,sizeof(S
),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
382 IndexFile(LanguageCode().c_str()).c_str());
386 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
387 // ---------------------------------------------------------------------
389 string
debTranslationsIndex::Info(const char *Type
) const
391 string Info
= ::URI::SiteOnly(URI
) + ' ';
392 if (Dist
[Dist
.size() - 1] == '/')
398 Info
+= Dist
+ '/' + Section
;
404 bool debTranslationsIndex::HasPackages() const
406 if(!TranslationsAvailable())
409 return FileExists(IndexFile(LanguageCode().c_str()));
412 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
413 // ---------------------------------------------------------------------
415 bool debTranslationsIndex::Exists() const
417 return FileExists(IndexFile(LanguageCode().c_str()));
420 // TranslationsIndex::Size - Return the size of the index /*{{{*/
421 // ---------------------------------------------------------------------
422 /* This is really only used for progress reporting. */
423 unsigned long debTranslationsIndex::Size() const
426 if (stat(IndexFile(LanguageCode().c_str()).c_str(),&S
) != 0)
431 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
432 // ---------------------------------------------------------------------
434 bool debTranslationsIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
&Prog
) const
436 // Check the translation file, if in use
437 string TranslationFile
= IndexFile(LanguageCode().c_str());
438 if (TranslationsAvailable() && FileExists(TranslationFile
))
440 FileFd
Trans(TranslationFile
,FileFd::ReadOnly
);
441 debListParser
TransParser(&Trans
);
442 if (_error
->PendingError() == true)
445 Prog
.SubProgress(0, Info(TranslationFile
.c_str()));
446 if (Gen
.SelectFile(TranslationFile
,string(),*this) == false)
447 return _error
->Error("Problem with SelectFile %s",TranslationFile
.c_str());
449 // Store the IMS information
450 pkgCache::PkgFileIterator TransFile
= Gen
.GetCurFile();
452 if (fstat(Trans
.Fd(),&TransSt
) != 0)
453 return _error
->Errno("fstat","Failed to stat");
454 TransFile
->Size
= TransSt
.st_size
;
455 TransFile
->mtime
= TransSt
.st_mtime
;
457 if (Gen
.MergeList(TransParser
) == false)
458 return _error
->Error("Problem with MergeList %s",TranslationFile
.c_str());
464 // TranslationsIndex::FindInCache - Find this index /*{{{*/
465 // ---------------------------------------------------------------------
467 pkgCache::PkgFileIterator
debTranslationsIndex::FindInCache(pkgCache
&Cache
) const
469 string FileName
= IndexFile(LanguageCode().c_str());
471 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
472 for (; File
.end() == false; File
++)
474 if (FileName
!= File
.FileName())
478 if (stat(File
.FileName(),&St
) != 0)
479 return pkgCache::PkgFileIterator(Cache
);
480 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
481 return pkgCache::PkgFileIterator(Cache
);
487 // StatusIndex::debStatusIndex - Constructor /*{{{*/
488 // ---------------------------------------------------------------------
490 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
494 // StatusIndex::Size - Return the size of the index /*{{{*/
495 // ---------------------------------------------------------------------
497 unsigned long debStatusIndex::Size() const
500 if (stat(File
.c_str(),&S
) != 0)
505 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
506 // ---------------------------------------------------------------------
508 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
&Prog
) const
510 FileFd
Pkg(File
,FileFd::ReadOnly
);
511 if (_error
->PendingError() == true)
513 debListParser
Parser(&Pkg
);
514 if (_error
->PendingError() == true)
517 Prog
.SubProgress(0,File
);
518 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
519 return _error
->Error("Problem with SelectFile %s",File
.c_str());
521 // Store the IMS information
522 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
524 if (fstat(Pkg
.Fd(),&St
) != 0)
525 return _error
->Errno("fstat","Failed to stat");
526 CFile
->Size
= St
.st_size
;
527 CFile
->mtime
= St
.st_mtime
;
528 CFile
->Archive
= Gen
.WriteUniqString("now");
530 if (Gen
.MergeList(Parser
) == false)
531 return _error
->Error("Problem with MergeList %s",File
.c_str());
535 // StatusIndex::FindInCache - Find this index /*{{{*/
536 // ---------------------------------------------------------------------
538 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
540 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
541 for (; File
.end() == false; File
++)
543 if (this->File
!= File
.FileName())
547 if (stat(File
.FileName(),&St
) != 0)
548 return pkgCache::PkgFileIterator(Cache
);
549 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
550 return pkgCache::PkgFileIterator(Cache
);
556 // StatusIndex::Exists - Check if the index is available /*{{{*/
557 // ---------------------------------------------------------------------
559 bool debStatusIndex::Exists() const
561 // Abort if the file does not exist.
566 // Index File types for Debian /*{{{*/
567 class debIFTypeSrc
: public pkgIndexFile::Type
571 debIFTypeSrc() {Label
= "Debian Source Index";};
573 class debIFTypePkg
: public pkgIndexFile::Type
577 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
579 return new debRecordParser(File
.FileName(),*File
.Cache());
581 debIFTypePkg() {Label
= "Debian Package Index";};
583 class debIFTypeTrans
: public debIFTypePkg
586 debIFTypeTrans() {Label
= "Debian Translation Index";};
588 class debIFTypeStatus
: public pkgIndexFile::Type
592 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
594 return new debRecordParser(File
.FileName(),*File
.Cache());
596 debIFTypeStatus() {Label
= "Debian dpkg status file";};
598 static debIFTypeSrc _apt_Src
;
599 static debIFTypePkg _apt_Pkg
;
600 static debIFTypeTrans _apt_Trans
;
601 static debIFTypeStatus _apt_Status
;
603 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
607 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
611 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
615 const pkgIndexFile::Type
*debStatusIndex::GetType() const