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 /*{{{*/
12 #include <apt-pkg/debindexfile.h>
13 #include <apt-pkg/debsrcrecords.h>
14 #include <apt-pkg/deblistparser.h>
15 #include <apt-pkg/debrecords.h>
16 #include <apt-pkg/sourcelist.h>
17 #include <apt-pkg/configuration.h>
18 #include <apt-pkg/progress.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/strutl.h>
21 #include <apt-pkg/acquire-item.h>
22 #include <apt-pkg/debmetaindex.h>
27 // SourcesIndex::debSourcesIndex - Constructor /*{{{*/
28 // ---------------------------------------------------------------------
30 debSourcesIndex::debSourcesIndex(string URI
,string Dist
,string Section
,bool Trusted
) :
31 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
)
35 // SourcesIndex::SourceInfo - Short 1 liner describing a source /*{{{*/
36 // ---------------------------------------------------------------------
37 /* The result looks like:
38 http://foo/debian/ stable/main src 1.1.1 (dsc) */
39 string
debSourcesIndex::SourceInfo(pkgSrcRecords::Parser
const &Record
,
40 pkgSrcRecords::File
const &File
) const
43 Res
= ::URI::NoUserPassword(URI
) + ' ';
44 if (Dist
[Dist
.size() - 1] == '/')
50 Res
+= Dist
+ '/' + Section
;
53 Res
+= Record
.Package();
55 Res
+= Record
.Version();
56 if (File
.Type
.empty() == false)
57 Res
+= " (" + File
.Type
+ ")";
61 // SourcesIndex::CreateSrcParser - Get a parser for the source files /*{{{*/
62 // ---------------------------------------------------------------------
64 pkgSrcRecords::Parser
*debSourcesIndex::CreateSrcParser() const
66 string SourcesURI
= _config
->FindDir("Dir::State::lists") +
67 URItoFileName(IndexURI("Sources"));
68 string SourcesURIgzip
= SourcesURI
+ ".gz";
69 if (!FileExists(SourcesURI
) && FileExists(SourcesURIgzip
))
70 SourcesURI
= SourcesURIgzip
;
72 return new debSrcRecordParser(SourcesURI
,this);
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::NoUserPassword(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 string s
= URItoFileName(IndexURI(Type
));
114 string sgzip
= s
+ ".gz";
115 if (!FileExists(s
) && FileExists(sgzip
))
121 string
debSourcesIndex::IndexURI(const char *Type
) const
124 if (Dist
[Dist
.size() - 1] == '/')
132 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
139 // SourcesIndex::Exists - Check if the index is available /*{{{*/
140 // ---------------------------------------------------------------------
142 bool debSourcesIndex::Exists() const
144 return FileExists(IndexFile("Sources"));
147 // SourcesIndex::Size - Return the size of the index /*{{{*/
148 // ---------------------------------------------------------------------
150 unsigned long debSourcesIndex::Size() const
152 unsigned long size
= 0;
154 /* we need to ignore errors here; if the lists are absent, just return 0 */
155 _error
->PushToStack();
157 FileFd f
= FileFd (IndexFile("Sources"), FileFd::ReadOnlyGzip
);
161 if (_error
->PendingError() == true)
163 _error
->RevertToStack();
169 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
170 // ---------------------------------------------------------------------
172 debPackagesIndex::debPackagesIndex(string
const &URI
, string
const &Dist
, string
const &Section
,
173 bool const &Trusted
, string
const &Arch
) :
174 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
), Architecture(Arch
)
176 if (Architecture
== "native")
177 Architecture
= _config
->Find("APT::Architecture");
180 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
181 // ---------------------------------------------------------------------
182 /* This is a shorter version that is designed to be < 60 chars or so */
183 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
185 string Res
= ::URI::NoUserPassword(URI
) + ' ';
186 if (Dist
[Dist
.size() - 1] == '/')
192 Res
+= Dist
+ '/' + Section
;
195 Res
+= Ver
.ParentPkg().Name();
197 if (Dist
[Dist
.size() - 1] != '/')
198 Res
.append(Ver
.Arch()).append(" ");
203 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
204 // ---------------------------------------------------------------------
205 /* This should help the user find the index in the sources.list and
206 in the filesystem for problem solving */
207 string
debPackagesIndex::Describe(bool Short
) const
211 snprintf(S
,sizeof(S
),"%s",Info("Packages").c_str());
213 snprintf(S
,sizeof(S
),"%s (%s)",Info("Packages").c_str(),
214 IndexFile("Packages").c_str());
218 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
219 // ---------------------------------------------------------------------
221 string
debPackagesIndex::Info(const char *Type
) const
223 string Info
= ::URI::NoUserPassword(URI
) + ' ';
224 if (Dist
[Dist
.size() - 1] == '/')
230 Info
+= Dist
+ '/' + Section
;
232 if (Dist
[Dist
.size() - 1] != '/')
233 Info
+= Architecture
+ " ";
238 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
239 // ---------------------------------------------------------------------
241 inline string
debPackagesIndex::IndexFile(const char *Type
) const
243 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
244 string sgzip
= s
+ ".gz";
245 if (!FileExists(s
) && FileExists(sgzip
))
250 string
debPackagesIndex::IndexURI(const char *Type
) const
253 if (Dist
[Dist
.size() - 1] == '/')
261 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
262 "/binary-" + Architecture
+ '/';
268 // PackagesIndex::Exists - Check if the index is available /*{{{*/
269 // ---------------------------------------------------------------------
271 bool debPackagesIndex::Exists() const
273 return FileExists(IndexFile("Packages"));
276 // PackagesIndex::Size - Return the size of the index /*{{{*/
277 // ---------------------------------------------------------------------
278 /* This is really only used for progress reporting. */
279 unsigned long debPackagesIndex::Size() const
281 unsigned long size
= 0;
283 /* we need to ignore errors here; if the lists are absent, just return 0 */
284 _error
->PushToStack();
286 FileFd f
= FileFd (IndexFile("Packages"), FileFd::ReadOnlyGzip
);
290 if (_error
->PendingError() == true)
292 _error
->RevertToStack();
297 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
298 // ---------------------------------------------------------------------
300 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
302 string PackageFile
= IndexFile("Packages");
303 FileFd
Pkg(PackageFile
,FileFd::ReadOnlyGzip
);
304 debListParser
Parser(&Pkg
, Architecture
);
306 if (_error
->PendingError() == true)
307 return _error
->Error("Problem opening %s",PackageFile
.c_str());
309 Prog
->SubProgress(0,Info("Packages"));
311 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
312 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
314 // Store the IMS information
315 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
316 pkgCacheGenerator::Dynamic
<pkgCache::PkgFileIterator
> DynFile(File
);
318 if (fstat(Pkg
.Fd(),&St
) != 0)
319 return _error
->Errno("fstat","Failed to stat");
320 File
->Size
= St
.st_size
;
321 File
->mtime
= St
.st_mtime
;
323 if (Gen
.MergeList(Parser
) == false)
324 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
326 // Check the release file
327 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("InRelease");
328 bool releaseExists
= false;
329 if (FileExists(ReleaseFile
) == true)
330 releaseExists
= true;
332 ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
334 if (releaseExists
== true || FileExists(ReleaseFile
) == true)
336 FileFd
Rel(ReleaseFile
,FileFd::ReadOnly
);
337 if (_error
->PendingError() == true)
339 Parser
.LoadReleaseInfo(File
,Rel
,Section
);
345 // PackagesIndex::FindInCache - Find this index /*{{{*/
346 // ---------------------------------------------------------------------
348 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
350 string FileName
= IndexFile("Packages");
351 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
352 for (; File
.end() == false; File
++)
354 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
358 if (stat(File
.FileName(),&St
) != 0)
360 if (_config
->FindB("Debug::pkgCacheGen", false))
361 std::clog
<< "PackagesIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
362 return pkgCache::PkgFileIterator(Cache
);
364 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
366 if (_config
->FindB("Debug::pkgCacheGen", false))
367 std::clog
<< "PackagesIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
368 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
369 << ") doesn't match for " << File
.FileName() << std::endl
;
370 return pkgCache::PkgFileIterator(Cache
);
379 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
380 // ---------------------------------------------------------------------
382 debTranslationsIndex::debTranslationsIndex(string URI
,string Dist
,string Section
,
383 char const * const Translation
) :
384 pkgIndexFile(true), URI(URI
), Dist(Dist
), Section(Section
),
385 Language(Translation
)
388 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
389 // ---------------------------------------------------------------------
391 inline string
debTranslationsIndex::IndexFile(const char *Type
) const
393 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
394 string sgzip
= s
+ ".gz";
395 if (!FileExists(s
) && FileExists(sgzip
))
400 string
debTranslationsIndex::IndexURI(const char *Type
) const
403 if (Dist
[Dist
.size() - 1] == '/')
411 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
412 "/i18n/Translation-";
418 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
419 // ---------------------------------------------------------------------
421 bool debTranslationsIndex::GetIndexes(pkgAcquire
*Owner
) const
423 if (TranslationsAvailable()) {
424 string
const TranslationFile
= string("Translation-").append(Language
);
425 new pkgAcqIndexTrans(Owner
, IndexURI(Language
),
426 Info(TranslationFile
.c_str()),
433 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
434 // ---------------------------------------------------------------------
435 /* This should help the user find the index in the sources.list and
436 in the filesystem for problem solving */
437 string
debTranslationsIndex::Describe(bool Short
) const
441 snprintf(S
,sizeof(S
),"%s",Info(TranslationFile().c_str()).c_str());
443 snprintf(S
,sizeof(S
),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
444 IndexFile(Language
).c_str());
448 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
449 // ---------------------------------------------------------------------
451 string
debTranslationsIndex::Info(const char *Type
) const
453 string Info
= ::URI::NoUserPassword(URI
) + ' ';
454 if (Dist
[Dist
.size() - 1] == '/')
460 Info
+= Dist
+ '/' + Section
;
466 bool debTranslationsIndex::HasPackages() const /*{{{*/
468 if(!TranslationsAvailable())
471 return FileExists(IndexFile(Language
));
474 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
475 // ---------------------------------------------------------------------
477 bool debTranslationsIndex::Exists() const
479 return FileExists(IndexFile(Language
));
482 // TranslationsIndex::Size - Return the size of the index /*{{{*/
483 // ---------------------------------------------------------------------
484 /* This is really only used for progress reporting. */
485 unsigned long debTranslationsIndex::Size() const
487 unsigned long size
= 0;
489 /* we need to ignore errors here; if the lists are absent, just return 0 */
490 _error
->PushToStack();
492 FileFd f
= FileFd (IndexFile(Language
), FileFd::ReadOnlyGzip
);
496 if (_error
->PendingError() == true)
498 _error
->RevertToStack();
503 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
504 // ---------------------------------------------------------------------
506 bool debTranslationsIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
508 // Check the translation file, if in use
509 string TranslationFile
= IndexFile(Language
);
510 if (TranslationsAvailable() && FileExists(TranslationFile
))
512 FileFd
Trans(TranslationFile
,FileFd::ReadOnlyGzip
);
513 debListParser
TransParser(&Trans
);
514 if (_error
->PendingError() == true)
518 Prog
->SubProgress(0, Info(TranslationFile
.c_str()));
519 if (Gen
.SelectFile(TranslationFile
,string(),*this) == false)
520 return _error
->Error("Problem with SelectFile %s",TranslationFile
.c_str());
522 // Store the IMS information
523 pkgCache::PkgFileIterator TransFile
= Gen
.GetCurFile();
525 if (fstat(Trans
.Fd(),&TransSt
) != 0)
526 return _error
->Errno("fstat","Failed to stat");
527 TransFile
->Size
= TransSt
.st_size
;
528 TransFile
->mtime
= TransSt
.st_mtime
;
530 if (Gen
.MergeList(TransParser
) == false)
531 return _error
->Error("Problem with MergeList %s",TranslationFile
.c_str());
537 // TranslationsIndex::FindInCache - Find this index /*{{{*/
538 // ---------------------------------------------------------------------
540 pkgCache::PkgFileIterator
debTranslationsIndex::FindInCache(pkgCache
&Cache
) const
542 string FileName
= IndexFile(Language
);
544 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
545 for (; File
.end() == false; File
++)
547 if (FileName
!= File
.FileName())
551 if (stat(File
.FileName(),&St
) != 0)
553 if (_config
->FindB("Debug::pkgCacheGen", false))
554 std::clog
<< "TranslationIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
555 return pkgCache::PkgFileIterator(Cache
);
557 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
559 if (_config
->FindB("Debug::pkgCacheGen", false))
560 std::clog
<< "TranslationIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
561 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
562 << ") doesn't match for " << File
.FileName() << std::endl
;
563 return pkgCache::PkgFileIterator(Cache
);
570 // StatusIndex::debStatusIndex - Constructor /*{{{*/
571 // ---------------------------------------------------------------------
573 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
577 // StatusIndex::Size - Return the size of the index /*{{{*/
578 // ---------------------------------------------------------------------
580 unsigned long debStatusIndex::Size() const
583 if (stat(File
.c_str(),&S
) != 0)
588 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
589 // ---------------------------------------------------------------------
591 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
593 FileFd
Pkg(File
,FileFd::ReadOnlyGzip
);
594 if (_error
->PendingError() == true)
596 debListParser
Parser(&Pkg
);
597 if (_error
->PendingError() == true)
601 Prog
->SubProgress(0,File
);
602 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
603 return _error
->Error("Problem with SelectFile %s",File
.c_str());
605 // Store the IMS information
606 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
608 if (fstat(Pkg
.Fd(),&St
) != 0)
609 return _error
->Errno("fstat","Failed to stat");
610 CFile
->Size
= St
.st_size
;
611 CFile
->mtime
= St
.st_mtime
;
612 CFile
->Archive
= Gen
.WriteUniqString("now");
614 if (Gen
.MergeList(Parser
) == false)
615 return _error
->Error("Problem with MergeList %s",File
.c_str());
619 // StatusIndex::FindInCache - Find this index /*{{{*/
620 // ---------------------------------------------------------------------
622 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
624 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
625 for (; File
.end() == false; File
++)
627 if (this->File
!= File
.FileName())
631 if (stat(File
.FileName(),&St
) != 0)
633 if (_config
->FindB("Debug::pkgCacheGen", false))
634 std::clog
<< "StatusIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
635 return pkgCache::PkgFileIterator(Cache
);
637 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
639 if (_config
->FindB("Debug::pkgCacheGen", false))
640 std::clog
<< "StatusIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
641 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
642 << ") doesn't match for " << File
.FileName() << std::endl
;
643 return pkgCache::PkgFileIterator(Cache
);
650 // StatusIndex::Exists - Check if the index is available /*{{{*/
651 // ---------------------------------------------------------------------
653 bool debStatusIndex::Exists() const
655 // Abort if the file does not exist.
660 // Index File types for Debian /*{{{*/
661 class debIFTypeSrc
: public pkgIndexFile::Type
665 debIFTypeSrc() {Label
= "Debian Source Index";};
667 class debIFTypePkg
: public pkgIndexFile::Type
671 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
673 return new debRecordParser(File
.FileName(),*File
.Cache());
675 debIFTypePkg() {Label
= "Debian Package Index";};
677 class debIFTypeTrans
: public debIFTypePkg
680 debIFTypeTrans() {Label
= "Debian Translation Index";};
682 class debIFTypeStatus
: public pkgIndexFile::Type
686 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
688 return new debRecordParser(File
.FileName(),*File
.Cache());
690 debIFTypeStatus() {Label
= "Debian dpkg status file";};
692 static debIFTypeSrc _apt_Src
;
693 static debIFTypePkg _apt_Pkg
;
694 static debIFTypeTrans _apt_Trans
;
695 static debIFTypeStatus _apt_Status
;
697 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
701 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
705 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
709 const pkgIndexFile::Type
*debStatusIndex::GetType() const