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"));
83 string SourcesURIgzip
= SourcesURI
+ ".gz";
85 if (!FileExists(SourcesURI
) && !FileExists(SourcesURIgzip
))
87 else if (!FileExists(SourcesURI
) && FileExists(SourcesURIgzip
))
88 SourcesURI
= SourcesURIgzip
;
90 return new debSrcRecordParser(SourcesURI
,this);
93 // SourcesIndex::Describe - Give a descriptive path to the index /*{{{*/
94 // ---------------------------------------------------------------------
96 string
debSourcesIndex::Describe(bool Short
) const
100 snprintf(S
,sizeof(S
),"%s",Info("Sources").c_str());
102 snprintf(S
,sizeof(S
),"%s (%s)",Info("Sources").c_str(),
103 IndexFile("Sources").c_str());
108 // SourcesIndex::Info - One liner describing the index URI /*{{{*/
109 // ---------------------------------------------------------------------
111 string
debSourcesIndex::Info(const char *Type
) const
113 string Info
= ::URI::NoUserPassword(URI
) + ' ';
114 if (Dist
[Dist
.size() - 1] == '/')
120 Info
+= Dist
+ '/' + Section
;
126 // SourcesIndex::Index* - Return the URI to the index files /*{{{*/
127 // ---------------------------------------------------------------------
129 inline string
debSourcesIndex::IndexFile(const char *Type
) const
131 string s
= URItoFileName(IndexURI(Type
));
132 string sgzip
= s
+ ".gz";
133 if (!FileExists(s
) && FileExists(sgzip
))
139 string
debSourcesIndex::IndexURI(const char *Type
) const
142 if (Dist
[Dist
.size() - 1] == '/')
150 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
157 // SourcesIndex::Exists - Check if the index is available /*{{{*/
158 // ---------------------------------------------------------------------
160 bool debSourcesIndex::Exists() const
162 return FileExists(IndexFile("Sources"));
165 // SourcesIndex::Size - Return the size of the index /*{{{*/
166 // ---------------------------------------------------------------------
168 unsigned long debSourcesIndex::Size() const
170 unsigned long size
= 0;
172 /* we need to ignore errors here; if the lists are absent, just return 0 */
173 _error
->PushToStack();
175 FileFd
f(IndexFile("Sources"), FileFd::ReadOnly
, FileFd::Extension
);
179 if (_error
->PendingError() == true)
181 _error
->RevertToStack();
187 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
188 // ---------------------------------------------------------------------
190 debPackagesIndex::debPackagesIndex(string
const &URI
, string
const &Dist
, string
const &Section
,
191 bool const &Trusted
, string
const &Arch
) :
192 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
), Architecture(Arch
)
194 if (Architecture
== "native")
195 Architecture
= _config
->Find("APT::Architecture");
198 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
199 // ---------------------------------------------------------------------
200 /* This is a shorter version that is designed to be < 60 chars or so */
201 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
203 string Res
= ::URI::NoUserPassword(URI
) + ' ';
204 if (Dist
[Dist
.size() - 1] == '/')
210 Res
+= Dist
+ '/' + Section
;
213 Res
+= Ver
.ParentPkg().Name();
215 if (Dist
[Dist
.size() - 1] != '/')
216 Res
.append(Ver
.Arch()).append(" ");
221 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
222 // ---------------------------------------------------------------------
223 /* This should help the user find the index in the sources.list and
224 in the filesystem for problem solving */
225 string
debPackagesIndex::Describe(bool Short
) const
229 snprintf(S
,sizeof(S
),"%s",Info("Packages").c_str());
231 snprintf(S
,sizeof(S
),"%s (%s)",Info("Packages").c_str(),
232 IndexFile("Packages").c_str());
236 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
237 // ---------------------------------------------------------------------
239 string
debPackagesIndex::Info(const char *Type
) const
241 string Info
= ::URI::NoUserPassword(URI
) + ' ';
242 if (Dist
[Dist
.size() - 1] == '/')
248 Info
+= Dist
+ '/' + Section
;
250 if (Dist
[Dist
.size() - 1] != '/')
251 Info
+= Architecture
+ " ";
256 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
257 // ---------------------------------------------------------------------
259 inline string
debPackagesIndex::IndexFile(const char *Type
) const
261 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
262 string sgzip
= s
+ ".gz";
263 if (!FileExists(s
) && FileExists(sgzip
))
268 string
debPackagesIndex::IndexURI(const char *Type
) const
271 if (Dist
[Dist
.size() - 1] == '/')
279 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
280 "/binary-" + Architecture
+ '/';
286 // PackagesIndex::Exists - Check if the index is available /*{{{*/
287 // ---------------------------------------------------------------------
289 bool debPackagesIndex::Exists() const
291 return FileExists(IndexFile("Packages"));
294 // PackagesIndex::Size - Return the size of the index /*{{{*/
295 // ---------------------------------------------------------------------
296 /* This is really only used for progress reporting. */
297 unsigned long debPackagesIndex::Size() const
299 unsigned long size
= 0;
301 /* we need to ignore errors here; if the lists are absent, just return 0 */
302 _error
->PushToStack();
304 FileFd
f(IndexFile("Packages"), FileFd::ReadOnly
, FileFd::Extension
);
308 if (_error
->PendingError() == true)
310 _error
->RevertToStack();
315 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
316 // ---------------------------------------------------------------------
318 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
320 string PackageFile
= IndexFile("Packages");
321 FileFd
Pkg(PackageFile
,FileFd::ReadOnly
, FileFd::Extension
);
322 debListParser
Parser(&Pkg
, Architecture
);
324 if (_error
->PendingError() == true)
325 return _error
->Error("Problem opening %s",PackageFile
.c_str());
327 Prog
->SubProgress(0,Info("Packages"));
329 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
330 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
332 // Store the IMS information
333 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
334 pkgCacheGenerator::Dynamic
<pkgCache::PkgFileIterator
> DynFile(File
);
335 File
->Size
= Pkg
.FileSize();
336 File
->mtime
= Pkg
.ModificationTime();
338 if (Gen
.MergeList(Parser
) == false)
339 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
341 // Check the release file
342 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("InRelease");
343 bool releaseExists
= false;
344 if (FileExists(ReleaseFile
) == true)
345 releaseExists
= true;
347 ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
349 if (releaseExists
== true || FileExists(ReleaseFile
) == true)
352 // Beware: The 'Release' file might be clearsigned in case the
353 // signature for an 'InRelease' file couldn't be checked
354 if (OpenMaybeClearSignedFile(ReleaseFile
, Rel
) == false)
357 if (_error
->PendingError() == true)
359 Parser
.LoadReleaseInfo(File
,Rel
,Section
);
365 // PackagesIndex::FindInCache - Find this index /*{{{*/
366 // ---------------------------------------------------------------------
368 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
370 string FileName
= IndexFile("Packages");
371 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
372 for (; File
.end() == false; ++File
)
374 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
378 if (stat(File
.FileName(),&St
) != 0)
380 if (_config
->FindB("Debug::pkgCacheGen", false))
381 std::clog
<< "PackagesIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
382 return pkgCache::PkgFileIterator(Cache
);
384 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
386 if (_config
->FindB("Debug::pkgCacheGen", false))
387 std::clog
<< "PackagesIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
388 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
389 << ") doesn't match for " << File
.FileName() << std::endl
;
390 return pkgCache::PkgFileIterator(Cache
);
399 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
400 // ---------------------------------------------------------------------
402 debTranslationsIndex::debTranslationsIndex(string URI
,string Dist
,string Section
,
403 char const * const Translation
) :
404 pkgIndexFile(true), URI(URI
), Dist(Dist
), Section(Section
),
405 Language(Translation
)
408 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
409 // ---------------------------------------------------------------------
411 inline string
debTranslationsIndex::IndexFile(const char *Type
) const
413 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
414 string sgzip
= s
+ ".gz";
415 if (!FileExists(s
) && FileExists(sgzip
))
420 string
debTranslationsIndex::IndexURI(const char *Type
) const
423 if (Dist
[Dist
.size() - 1] == '/')
431 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
432 "/i18n/Translation-";
438 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
439 // ---------------------------------------------------------------------
441 bool debTranslationsIndex::GetIndexes(pkgAcquire
*Owner
) const
443 string
const TranslationFile
= string("Translation-").append(Language
);
444 new pkgAcqIndexTrans(Owner
, IndexURI(Language
),
445 Info(TranslationFile
.c_str()),
451 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
452 // ---------------------------------------------------------------------
453 /* This should help the user find the index in the sources.list and
454 in the filesystem for problem solving */
455 string
debTranslationsIndex::Describe(bool Short
) const
459 snprintf(S
,sizeof(S
),"%s",Info(TranslationFile().c_str()).c_str());
461 snprintf(S
,sizeof(S
),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
462 IndexFile(Language
).c_str());
466 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
467 // ---------------------------------------------------------------------
469 string
debTranslationsIndex::Info(const char *Type
) const
471 string Info
= ::URI::NoUserPassword(URI
) + ' ';
472 if (Dist
[Dist
.size() - 1] == '/')
478 Info
+= Dist
+ '/' + Section
;
484 bool debTranslationsIndex::HasPackages() const /*{{{*/
486 return FileExists(IndexFile(Language
));
489 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
490 // ---------------------------------------------------------------------
492 bool debTranslationsIndex::Exists() const
494 return FileExists(IndexFile(Language
));
497 // TranslationsIndex::Size - Return the size of the index /*{{{*/
498 // ---------------------------------------------------------------------
499 /* This is really only used for progress reporting. */
500 unsigned long debTranslationsIndex::Size() const
502 unsigned long size
= 0;
504 /* we need to ignore errors here; if the lists are absent, just return 0 */
505 _error
->PushToStack();
507 FileFd
f(IndexFile(Language
), FileFd::ReadOnly
, FileFd::Extension
);
511 if (_error
->PendingError() == true)
513 _error
->RevertToStack();
518 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
519 // ---------------------------------------------------------------------
521 bool debTranslationsIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
523 // Check the translation file, if in use
524 string TranslationFile
= IndexFile(Language
);
525 if (FileExists(TranslationFile
))
527 FileFd
Trans(TranslationFile
,FileFd::ReadOnly
, FileFd::Extension
);
528 debTranslationsParser
TransParser(&Trans
);
529 if (_error
->PendingError() == true)
533 Prog
->SubProgress(0, Info(TranslationFile
.c_str()));
534 if (Gen
.SelectFile(TranslationFile
,string(),*this) == false)
535 return _error
->Error("Problem with SelectFile %s",TranslationFile
.c_str());
537 // Store the IMS information
538 pkgCache::PkgFileIterator TransFile
= Gen
.GetCurFile();
539 TransFile
->Size
= Trans
.FileSize();
540 TransFile
->mtime
= Trans
.ModificationTime();
542 if (Gen
.MergeList(TransParser
) == false)
543 return _error
->Error("Problem with MergeList %s",TranslationFile
.c_str());
549 // TranslationsIndex::FindInCache - Find this index /*{{{*/
550 // ---------------------------------------------------------------------
552 pkgCache::PkgFileIterator
debTranslationsIndex::FindInCache(pkgCache
&Cache
) const
554 string FileName
= IndexFile(Language
);
556 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
557 for (; File
.end() == false; ++File
)
559 if (FileName
!= File
.FileName())
563 if (stat(File
.FileName(),&St
) != 0)
565 if (_config
->FindB("Debug::pkgCacheGen", false))
566 std::clog
<< "TranslationIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
567 return pkgCache::PkgFileIterator(Cache
);
569 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
571 if (_config
->FindB("Debug::pkgCacheGen", false))
572 std::clog
<< "TranslationIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
573 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
574 << ") doesn't match for " << File
.FileName() << std::endl
;
575 return pkgCache::PkgFileIterator(Cache
);
582 // StatusIndex::debStatusIndex - Constructor /*{{{*/
583 // ---------------------------------------------------------------------
585 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
589 // StatusIndex::Size - Return the size of the index /*{{{*/
590 // ---------------------------------------------------------------------
592 unsigned long debStatusIndex::Size() const
595 if (stat(File
.c_str(),&S
) != 0)
600 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
601 // ---------------------------------------------------------------------
603 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
605 FileFd
Pkg(File
,FileFd::ReadOnly
, FileFd::Extension
);
606 if (_error
->PendingError() == true)
608 debListParser
Parser(&Pkg
);
609 if (_error
->PendingError() == true)
613 Prog
->SubProgress(0,File
);
614 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
615 return _error
->Error("Problem with SelectFile %s",File
.c_str());
617 // Store the IMS information
618 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
619 CFile
->Size
= Pkg
.FileSize();
620 CFile
->mtime
= Pkg
.ModificationTime();
621 map_ptrloc
const storage
= Gen
.WriteUniqString("now");
622 CFile
->Archive
= storage
;
624 if (Gen
.MergeList(Parser
) == false)
625 return _error
->Error("Problem with MergeList %s",File
.c_str());
629 // StatusIndex::FindInCache - Find this index /*{{{*/
630 // ---------------------------------------------------------------------
632 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
634 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
635 for (; File
.end() == false; ++File
)
637 if (this->File
!= File
.FileName())
641 if (stat(File
.FileName(),&St
) != 0)
643 if (_config
->FindB("Debug::pkgCacheGen", false))
644 std::clog
<< "StatusIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
645 return pkgCache::PkgFileIterator(Cache
);
647 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
649 if (_config
->FindB("Debug::pkgCacheGen", false))
650 std::clog
<< "StatusIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
651 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
652 << ") doesn't match for " << File
.FileName() << std::endl
;
653 return pkgCache::PkgFileIterator(Cache
);
660 // StatusIndex::Exists - Check if the index is available /*{{{*/
661 // ---------------------------------------------------------------------
663 APT_CONST
bool debStatusIndex::Exists() const
665 // Abort if the file does not exist.
670 // Index File types for Debian /*{{{*/
671 class debIFTypeSrc
: public pkgIndexFile::Type
675 debIFTypeSrc() {Label
= "Debian Source Index";};
677 class debIFTypePkg
: public pkgIndexFile::Type
681 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
683 return new debRecordParser(File
.FileName(),*File
.Cache());
685 debIFTypePkg() {Label
= "Debian Package Index";};
687 class debIFTypeTrans
: public debIFTypePkg
690 debIFTypeTrans() {Label
= "Debian Translation Index";};
692 class debIFTypeStatus
: public pkgIndexFile::Type
696 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
698 return new debRecordParser(File
.FileName(),*File
.Cache());
700 debIFTypeStatus() {Label
= "Debian dpkg status file";};
702 static debIFTypeSrc _apt_Src
;
703 static debIFTypePkg _apt_Pkg
;
704 static debIFTypeTrans _apt_Trans
;
705 static debIFTypeStatus _apt_Status
;
707 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
711 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
715 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
719 const pkgIndexFile::Type
*debStatusIndex::GetType() const