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 /*{{{*/
14 #include <apt-pkg/debindexfile.h>
15 #include <apt-pkg/debsrcrecords.h>
16 #include <apt-pkg/deblistparser.h>
17 #include <apt-pkg/debrecords.h>
18 #include <apt-pkg/configuration.h>
19 #include <apt-pkg/progress.h>
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/strutl.h>
22 #include <apt-pkg/acquire-item.h>
23 #include <apt-pkg/debmetaindex.h>
24 #include <apt-pkg/gpgv.h>
25 #include <apt-pkg/fileutl.h>
26 #include <apt-pkg/indexfile.h>
27 #include <apt-pkg/mmap.h>
28 #include <apt-pkg/pkgcache.h>
29 #include <apt-pkg/cacheiterators.h>
30 #include <apt-pkg/pkgcachegen.h>
31 #include <apt-pkg/pkgrecords.h>
32 #include <apt-pkg/srcrecords.h>
42 // SourcesIndex::debSourcesIndex - Constructor /*{{{*/
43 // ---------------------------------------------------------------------
45 debSourcesIndex::debSourcesIndex(string URI
,string Dist
,string Section
,bool Trusted
) :
46 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
)
50 // SourcesIndex::SourceInfo - Short 1 liner describing a source /*{{{*/
51 // ---------------------------------------------------------------------
52 /* The result looks like:
53 http://foo/debian/ stable/main src 1.1.1 (dsc) */
54 string
debSourcesIndex::SourceInfo(pkgSrcRecords::Parser
const &Record
,
55 pkgSrcRecords::File
const &File
) const
58 Res
= ::URI::NoUserPassword(URI
) + ' ';
59 if (Dist
[Dist
.size() - 1] == '/')
65 Res
+= Dist
+ '/' + Section
;
68 Res
+= Record
.Package();
70 Res
+= Record
.Version();
71 if (File
.Type
.empty() == false)
72 Res
+= " (" + File
.Type
+ ")";
76 // SourcesIndex::CreateSrcParser - Get a parser for the source files /*{{{*/
77 // ---------------------------------------------------------------------
79 pkgSrcRecords::Parser
*debSourcesIndex::CreateSrcParser() const
81 string SourcesURI
= _config
->FindDir("Dir::State::lists") +
82 URItoFileName(IndexURI("Sources"));
84 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
85 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
88 p
= SourcesURI
+ '.' + *t
;
90 return new debSrcRecordParser(p
, this);
92 if (FileExists(SourcesURI
))
93 return new debSrcRecordParser(SourcesURI
, this);
97 // SourcesIndex::Describe - Give a descriptive path to the index /*{{{*/
98 // ---------------------------------------------------------------------
100 string
debSourcesIndex::Describe(bool Short
) const
104 snprintf(S
,sizeof(S
),"%s",Info("Sources").c_str());
106 snprintf(S
,sizeof(S
),"%s (%s)",Info("Sources").c_str(),
107 IndexFile("Sources").c_str());
112 // SourcesIndex::Info - One liner describing the index URI /*{{{*/
113 // ---------------------------------------------------------------------
115 string
debSourcesIndex::Info(const char *Type
) const
117 string Info
= ::URI::NoUserPassword(URI
) + ' ';
118 if (Dist
[Dist
.size() - 1] == '/')
124 Info
+= Dist
+ '/' + Section
;
130 // SourcesIndex::Index* - Return the URI to the index files /*{{{*/
131 // ---------------------------------------------------------------------
133 inline string
debSourcesIndex::IndexFile(const char *Type
) const
135 string s
= URItoFileName(IndexURI(Type
));
137 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
138 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
140 string p
= s
+ '.' + *t
;
147 string
debSourcesIndex::IndexURI(const char *Type
) const
150 if (Dist
[Dist
.size() - 1] == '/')
158 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
165 // SourcesIndex::Exists - Check if the index is available /*{{{*/
166 // ---------------------------------------------------------------------
168 bool debSourcesIndex::Exists() const
170 return FileExists(IndexFile("Sources"));
173 // SourcesIndex::Size - Return the size of the index /*{{{*/
174 // ---------------------------------------------------------------------
176 unsigned long debSourcesIndex::Size() const
178 unsigned long size
= 0;
180 /* we need to ignore errors here; if the lists are absent, just return 0 */
181 _error
->PushToStack();
183 FileFd
f(IndexFile("Sources"), FileFd::ReadOnly
, FileFd::Extension
);
187 if (_error
->PendingError() == true)
189 _error
->RevertToStack();
195 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
196 // ---------------------------------------------------------------------
198 debPackagesIndex::debPackagesIndex(string
const &URI
, string
const &Dist
, string
const &Section
,
199 bool const &Trusted
, string
const &Arch
) :
200 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
), Architecture(Arch
)
202 if (Architecture
== "native")
203 Architecture
= _config
->Find("APT::Architecture");
206 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
207 // ---------------------------------------------------------------------
208 /* This is a shorter version that is designed to be < 60 chars or so */
209 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
211 string Res
= ::URI::NoUserPassword(URI
) + ' ';
212 if (Dist
[Dist
.size() - 1] == '/')
218 Res
+= Dist
+ '/' + Section
;
221 Res
+= Ver
.ParentPkg().Name();
223 if (Dist
[Dist
.size() - 1] != '/')
224 Res
.append(Ver
.Arch()).append(" ");
229 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
230 // ---------------------------------------------------------------------
231 /* This should help the user find the index in the sources.list and
232 in the filesystem for problem solving */
233 string
debPackagesIndex::Describe(bool Short
) const
237 snprintf(S
,sizeof(S
),"%s",Info("Packages").c_str());
239 snprintf(S
,sizeof(S
),"%s (%s)",Info("Packages").c_str(),
240 IndexFile("Packages").c_str());
244 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
245 // ---------------------------------------------------------------------
247 string
debPackagesIndex::Info(const char *Type
) const
249 string Info
= ::URI::NoUserPassword(URI
) + ' ';
250 if (Dist
[Dist
.size() - 1] == '/')
256 Info
+= Dist
+ '/' + Section
;
258 if (Dist
[Dist
.size() - 1] != '/')
259 Info
+= Architecture
+ " ";
264 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
265 // ---------------------------------------------------------------------
267 inline string
debPackagesIndex::IndexFile(const char *Type
) const
269 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
271 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
272 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
274 string p
= s
+ '.' + *t
;
280 string
debPackagesIndex::IndexURI(const char *Type
) const
283 if (Dist
[Dist
.size() - 1] == '/')
291 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
292 "/binary-" + Architecture
+ '/';
298 // PackagesIndex::Exists - Check if the index is available /*{{{*/
299 // ---------------------------------------------------------------------
301 bool debPackagesIndex::Exists() const
303 return FileExists(IndexFile("Packages"));
306 // PackagesIndex::Size - Return the size of the index /*{{{*/
307 // ---------------------------------------------------------------------
308 /* This is really only used for progress reporting. */
309 unsigned long debPackagesIndex::Size() const
311 unsigned long size
= 0;
313 /* we need to ignore errors here; if the lists are absent, just return 0 */
314 _error
->PushToStack();
316 FileFd
f(IndexFile("Packages"), FileFd::ReadOnly
, FileFd::Extension
);
320 if (_error
->PendingError() == true)
322 _error
->RevertToStack();
327 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
328 // ---------------------------------------------------------------------
330 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
332 string PackageFile
= IndexFile("Packages");
333 FileFd
Pkg(PackageFile
,FileFd::ReadOnly
, FileFd::Extension
);
334 debListParser
Parser(&Pkg
, Architecture
);
336 if (_error
->PendingError() == true)
337 return _error
->Error("Problem opening %s",PackageFile
.c_str());
339 Prog
->SubProgress(0,Info("Packages"));
341 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
342 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
344 // Store the IMS information
345 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
346 pkgCacheGenerator::Dynamic
<pkgCache::PkgFileIterator
> DynFile(File
);
347 File
->Size
= Pkg
.FileSize();
348 File
->mtime
= Pkg
.ModificationTime();
350 if (Gen
.MergeList(Parser
) == false)
351 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
353 // Check the release file
354 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("InRelease");
355 bool releaseExists
= false;
356 if (FileExists(ReleaseFile
) == true)
357 releaseExists
= true;
359 ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
361 if (releaseExists
== true || FileExists(ReleaseFile
) == true)
364 // Beware: The 'Release' file might be clearsigned in case the
365 // signature for an 'InRelease' file couldn't be checked
366 if (OpenMaybeClearSignedFile(ReleaseFile
, Rel
) == false)
369 if (_error
->PendingError() == true)
371 Parser
.LoadReleaseInfo(File
,Rel
,Section
);
377 // PackagesIndex::FindInCache - Find this index /*{{{*/
378 // ---------------------------------------------------------------------
380 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
382 string FileName
= IndexFile("Packages");
383 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
384 for (; File
.end() == false; ++File
)
386 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
390 if (stat(File
.FileName(),&St
) != 0)
392 if (_config
->FindB("Debug::pkgCacheGen", false))
393 std::clog
<< "PackagesIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
394 return pkgCache::PkgFileIterator(Cache
);
396 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
398 if (_config
->FindB("Debug::pkgCacheGen", false))
399 std::clog
<< "PackagesIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
400 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
401 << ") doesn't match for " << File
.FileName() << std::endl
;
402 return pkgCache::PkgFileIterator(Cache
);
411 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
412 // ---------------------------------------------------------------------
414 debTranslationsIndex::debTranslationsIndex(string URI
,string Dist
,string Section
,
415 char const * const Translation
) :
416 pkgIndexFile(true), URI(URI
), Dist(Dist
), Section(Section
),
417 Language(Translation
)
420 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
421 // ---------------------------------------------------------------------
423 inline string
debTranslationsIndex::IndexFile(const char *Type
) const
425 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
427 std::vector
<std::string
> types
= APT::Configuration::getCompressionTypes();
428 for (std::vector
<std::string
>::const_iterator t
= types
.begin(); t
!= types
.end(); ++t
)
430 string p
= s
+ '.' + *t
;
436 string
debTranslationsIndex::IndexURI(const char *Type
) const
439 if (Dist
[Dist
.size() - 1] == '/')
447 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
448 "/i18n/Translation-";
454 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
455 // ---------------------------------------------------------------------
457 bool debTranslationsIndex::GetIndexes(pkgAcquire
*Owner
) const
459 string
const TranslationFile
= string("Translation-").append(Language
);
460 new pkgAcqIndexTrans(Owner
, IndexURI(Language
),
461 Info(TranslationFile
.c_str()),
467 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
468 // ---------------------------------------------------------------------
469 /* This should help the user find the index in the sources.list and
470 in the filesystem for problem solving */
471 string
debTranslationsIndex::Describe(bool Short
) const
475 snprintf(S
,sizeof(S
),"%s",Info(TranslationFile().c_str()).c_str());
477 snprintf(S
,sizeof(S
),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
478 IndexFile(Language
).c_str());
482 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
483 // ---------------------------------------------------------------------
485 string
debTranslationsIndex::Info(const char *Type
) const
487 string Info
= ::URI::NoUserPassword(URI
) + ' ';
488 if (Dist
[Dist
.size() - 1] == '/')
494 Info
+= Dist
+ '/' + Section
;
500 bool debTranslationsIndex::HasPackages() const /*{{{*/
502 return FileExists(IndexFile(Language
));
505 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
506 // ---------------------------------------------------------------------
508 bool debTranslationsIndex::Exists() const
510 return FileExists(IndexFile(Language
));
513 // TranslationsIndex::Size - Return the size of the index /*{{{*/
514 // ---------------------------------------------------------------------
515 /* This is really only used for progress reporting. */
516 unsigned long debTranslationsIndex::Size() const
518 unsigned long size
= 0;
520 /* we need to ignore errors here; if the lists are absent, just return 0 */
521 _error
->PushToStack();
523 FileFd
f(IndexFile(Language
), FileFd::ReadOnly
, FileFd::Extension
);
527 if (_error
->PendingError() == true)
529 _error
->RevertToStack();
534 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
535 // ---------------------------------------------------------------------
537 bool debTranslationsIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
539 // Check the translation file, if in use
540 string TranslationFile
= IndexFile(Language
);
541 if (FileExists(TranslationFile
))
543 FileFd
Trans(TranslationFile
,FileFd::ReadOnly
, FileFd::Extension
);
544 debTranslationsParser
TransParser(&Trans
);
545 if (_error
->PendingError() == true)
549 Prog
->SubProgress(0, Info(TranslationFile
.c_str()));
550 if (Gen
.SelectFile(TranslationFile
,string(),*this) == false)
551 return _error
->Error("Problem with SelectFile %s",TranslationFile
.c_str());
553 // Store the IMS information
554 pkgCache::PkgFileIterator TransFile
= Gen
.GetCurFile();
555 TransFile
->Size
= Trans
.FileSize();
556 TransFile
->mtime
= Trans
.ModificationTime();
558 if (Gen
.MergeList(TransParser
) == false)
559 return _error
->Error("Problem with MergeList %s",TranslationFile
.c_str());
565 // TranslationsIndex::FindInCache - Find this index /*{{{*/
566 // ---------------------------------------------------------------------
568 pkgCache::PkgFileIterator
debTranslationsIndex::FindInCache(pkgCache
&Cache
) const
570 string FileName
= IndexFile(Language
);
572 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
573 for (; File
.end() == false; ++File
)
575 if (FileName
!= File
.FileName())
579 if (stat(File
.FileName(),&St
) != 0)
581 if (_config
->FindB("Debug::pkgCacheGen", false))
582 std::clog
<< "TranslationIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
583 return pkgCache::PkgFileIterator(Cache
);
585 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
587 if (_config
->FindB("Debug::pkgCacheGen", false))
588 std::clog
<< "TranslationIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
589 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
590 << ") doesn't match for " << File
.FileName() << std::endl
;
591 return pkgCache::PkgFileIterator(Cache
);
598 // StatusIndex::debStatusIndex - Constructor /*{{{*/
599 // ---------------------------------------------------------------------
601 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
605 // StatusIndex::Size - Return the size of the index /*{{{*/
606 // ---------------------------------------------------------------------
608 unsigned long debStatusIndex::Size() const
611 if (stat(File
.c_str(),&S
) != 0)
616 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
617 // ---------------------------------------------------------------------
619 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
621 FileFd
Pkg(File
,FileFd::ReadOnly
, FileFd::Extension
);
622 if (_error
->PendingError() == true)
624 debListParser
Parser(&Pkg
);
625 if (_error
->PendingError() == true)
629 Prog
->SubProgress(0,File
);
630 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
631 return _error
->Error("Problem with SelectFile %s",File
.c_str());
633 // Store the IMS information
634 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
635 CFile
->Size
= Pkg
.FileSize();
636 CFile
->mtime
= Pkg
.ModificationTime();
637 map_ptrloc
const storage
= Gen
.WriteUniqString("now");
638 CFile
->Archive
= storage
;
640 if (Gen
.MergeList(Parser
) == false)
641 return _error
->Error("Problem with MergeList %s",File
.c_str());
645 // StatusIndex::FindInCache - Find this index /*{{{*/
646 // ---------------------------------------------------------------------
648 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
650 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
651 for (; File
.end() == false; ++File
)
653 if (this->File
!= File
.FileName())
657 if (stat(File
.FileName(),&St
) != 0)
659 if (_config
->FindB("Debug::pkgCacheGen", false))
660 std::clog
<< "StatusIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
661 return pkgCache::PkgFileIterator(Cache
);
663 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
665 if (_config
->FindB("Debug::pkgCacheGen", false))
666 std::clog
<< "StatusIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
667 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
668 << ") doesn't match for " << File
.FileName() << std::endl
;
669 return pkgCache::PkgFileIterator(Cache
);
676 // StatusIndex::Exists - Check if the index is available /*{{{*/
677 // ---------------------------------------------------------------------
679 APT_CONST
bool debStatusIndex::Exists() const
681 // Abort if the file does not exist.
686 // Index File types for Debian /*{{{*/
687 class debIFTypeSrc
: public pkgIndexFile::Type
691 debIFTypeSrc() {Label
= "Debian Source Index";};
693 class debIFTypePkg
: public pkgIndexFile::Type
697 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
699 return new debRecordParser(File
.FileName(),*File
.Cache());
701 debIFTypePkg() {Label
= "Debian Package Index";};
703 class debIFTypeTrans
: public debIFTypePkg
706 debIFTypeTrans() {Label
= "Debian Translation Index";};
708 class debIFTypeStatus
: public pkgIndexFile::Type
712 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
714 return new debRecordParser(File
.FileName(),*File
.Cache());
716 debIFTypeStatus() {Label
= "Debian dpkg status file";};
718 static debIFTypeSrc _apt_Src
;
719 static debIFTypePkg _apt_Pkg
;
720 static debIFTypeTrans _apt_Trans
;
721 static debIFTypeStatus _apt_Status
;
723 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
727 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
731 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
735 const pkgIndexFile::Type
*debStatusIndex::GetType() const