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
153 if (stat(IndexFile("Sources").c_str(),&S
) != 0)
159 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
160 // ---------------------------------------------------------------------
162 debPackagesIndex::debPackagesIndex(string
const &URI
, string
const &Dist
, string
const &Section
,
163 bool const &Trusted
, string
const &Arch
) :
164 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
), Architecture(Arch
)
166 if (Architecture
== "native")
167 Architecture
= _config
->Find("APT::Architecture");
170 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
171 // ---------------------------------------------------------------------
172 /* This is a shorter version that is designed to be < 60 chars or so */
173 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
175 string Res
= ::URI::NoUserPassword(URI
) + ' ';
176 if (Dist
[Dist
.size() - 1] == '/')
182 Res
+= Dist
+ '/' + Section
;
185 Res
+= Ver
.ParentPkg().Name();
187 if (Dist
[Dist
.size() - 1] != '/')
188 Res
.append(Ver
.Arch()).append(" ");
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::NoUserPassword(URI
) + ' ';
214 if (Dist
[Dist
.size() - 1] == '/')
220 Info
+= Dist
+ '/' + Section
;
222 if (Dist
[Dist
.size() - 1] != '/')
223 Info
+= Architecture
+ " ";
228 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
229 // ---------------------------------------------------------------------
231 inline string
debPackagesIndex::IndexFile(const char *Type
) const
233 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
234 string sgzip
= s
+ ".gz";
235 if (!FileExists(s
) && FileExists(sgzip
))
240 string
debPackagesIndex::IndexURI(const char *Type
) const
243 if (Dist
[Dist
.size() - 1] == '/')
251 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
252 "/binary-" + Architecture
+ '/';
258 // PackagesIndex::Exists - Check if the index is available /*{{{*/
259 // ---------------------------------------------------------------------
261 bool debPackagesIndex::Exists() const
263 return FileExists(IndexFile("Packages"));
266 // PackagesIndex::Size - Return the size of the index /*{{{*/
267 // ---------------------------------------------------------------------
268 /* This is really only used for progress reporting. */
269 unsigned long debPackagesIndex::Size() const
272 if (stat(IndexFile("Packages").c_str(),&S
) != 0)
277 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
278 // ---------------------------------------------------------------------
280 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
282 string PackageFile
= IndexFile("Packages");
283 FileFd
Pkg(PackageFile
,FileFd::ReadOnlyGzip
);
284 debListParser
Parser(&Pkg
, Architecture
);
286 if (_error
->PendingError() == true)
287 return _error
->Error("Problem opening %s",PackageFile
.c_str());
289 Prog
->SubProgress(0,Info("Packages"));
291 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
292 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
294 // Store the IMS information
295 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
296 pkgCacheGenerator::Dynamic
<pkgCache::PkgFileIterator
> DynFile(File
);
298 if (fstat(Pkg
.Fd(),&St
) != 0)
299 return _error
->Errno("fstat","Failed to stat");
300 File
->Size
= St
.st_size
;
301 File
->mtime
= St
.st_mtime
;
303 if (Gen
.MergeList(Parser
) == false)
304 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
306 // Check the release file
307 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
308 if (FileExists(ReleaseFile
) == true)
310 FileFd
Rel(ReleaseFile
,FileFd::ReadOnly
);
311 if (_error
->PendingError() == true)
313 Parser
.LoadReleaseInfo(File
,Rel
,Section
);
319 // PackagesIndex::FindInCache - Find this index /*{{{*/
320 // ---------------------------------------------------------------------
322 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
324 string FileName
= IndexFile("Packages");
325 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
326 for (; File
.end() == false; File
++)
328 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
332 if (stat(File
.FileName(),&St
) != 0)
334 if (_config
->FindB("Debug::pkgCacheGen", false))
335 std::clog
<< "PackagesIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
336 return pkgCache::PkgFileIterator(Cache
);
338 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
340 if (_config
->FindB("Debug::pkgCacheGen", false))
341 std::clog
<< "PackagesIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
342 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
343 << ") doesn't match for " << File
.FileName() << std::endl
;
344 return pkgCache::PkgFileIterator(Cache
);
353 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
354 // ---------------------------------------------------------------------
356 debTranslationsIndex::debTranslationsIndex(string URI
,string Dist
,string Section
,
357 char const * const Translation
) :
358 pkgIndexFile(true), URI(URI
), Dist(Dist
), Section(Section
),
359 Language(Translation
)
362 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
363 // ---------------------------------------------------------------------
365 inline string
debTranslationsIndex::IndexFile(const char *Type
) const
367 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
368 string sgzip
= s
+ ".gz";
369 if (!FileExists(s
) && FileExists(sgzip
))
374 string
debTranslationsIndex::IndexURI(const char *Type
) const
377 if (Dist
[Dist
.size() - 1] == '/')
385 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
386 "/i18n/Translation-";
392 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
393 // ---------------------------------------------------------------------
395 bool debTranslationsIndex::GetIndexes(pkgAcquire
*Owner
) const
397 if (TranslationsAvailable()) {
398 string
const TranslationFile
= string("Translation-").append(Language
);
399 new pkgAcqIndexTrans(Owner
, IndexURI(Language
),
400 Info(TranslationFile
.c_str()),
407 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
408 // ---------------------------------------------------------------------
409 /* This should help the user find the index in the sources.list and
410 in the filesystem for problem solving */
411 string
debTranslationsIndex::Describe(bool Short
) const
415 snprintf(S
,sizeof(S
),"%s",Info(TranslationFile().c_str()).c_str());
417 snprintf(S
,sizeof(S
),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
418 IndexFile(Language
).c_str());
422 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
423 // ---------------------------------------------------------------------
425 string
debTranslationsIndex::Info(const char *Type
) const
427 string Info
= ::URI::NoUserPassword(URI
) + ' ';
428 if (Dist
[Dist
.size() - 1] == '/')
434 Info
+= Dist
+ '/' + Section
;
440 bool debTranslationsIndex::HasPackages() const /*{{{*/
442 if(!TranslationsAvailable())
445 return FileExists(IndexFile(Language
));
448 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
449 // ---------------------------------------------------------------------
451 bool debTranslationsIndex::Exists() const
453 return FileExists(IndexFile(Language
));
456 // TranslationsIndex::Size - Return the size of the index /*{{{*/
457 // ---------------------------------------------------------------------
458 /* This is really only used for progress reporting. */
459 unsigned long debTranslationsIndex::Size() const
462 if (stat(IndexFile(Language
).c_str(),&S
) != 0)
467 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
468 // ---------------------------------------------------------------------
470 bool debTranslationsIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
472 // Check the translation file, if in use
473 string TranslationFile
= IndexFile(Language
);
474 if (TranslationsAvailable() && FileExists(TranslationFile
))
476 FileFd
Trans(TranslationFile
,FileFd::ReadOnlyGzip
);
477 debListParser
TransParser(&Trans
);
478 if (_error
->PendingError() == true)
482 Prog
->SubProgress(0, Info(TranslationFile
.c_str()));
483 if (Gen
.SelectFile(TranslationFile
,string(),*this) == false)
484 return _error
->Error("Problem with SelectFile %s",TranslationFile
.c_str());
486 // Store the IMS information
487 pkgCache::PkgFileIterator TransFile
= Gen
.GetCurFile();
489 if (fstat(Trans
.Fd(),&TransSt
) != 0)
490 return _error
->Errno("fstat","Failed to stat");
491 TransFile
->Size
= TransSt
.st_size
;
492 TransFile
->mtime
= TransSt
.st_mtime
;
494 if (Gen
.MergeList(TransParser
) == false)
495 return _error
->Error("Problem with MergeList %s",TranslationFile
.c_str());
501 // TranslationsIndex::FindInCache - Find this index /*{{{*/
502 // ---------------------------------------------------------------------
504 pkgCache::PkgFileIterator
debTranslationsIndex::FindInCache(pkgCache
&Cache
) const
506 string FileName
= IndexFile(Language
);
508 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
509 for (; File
.end() == false; File
++)
511 if (FileName
!= File
.FileName())
515 if (stat(File
.FileName(),&St
) != 0)
517 if (_config
->FindB("Debug::pkgCacheGen", false))
518 std::clog
<< "TranslationIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
519 return pkgCache::PkgFileIterator(Cache
);
521 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
523 if (_config
->FindB("Debug::pkgCacheGen", false))
524 std::clog
<< "TranslationIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
525 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
526 << ") doesn't match for " << File
.FileName() << std::endl
;
527 return pkgCache::PkgFileIterator(Cache
);
534 // StatusIndex::debStatusIndex - Constructor /*{{{*/
535 // ---------------------------------------------------------------------
537 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
541 // StatusIndex::Size - Return the size of the index /*{{{*/
542 // ---------------------------------------------------------------------
544 unsigned long debStatusIndex::Size() const
547 if (stat(File
.c_str(),&S
) != 0)
552 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
553 // ---------------------------------------------------------------------
555 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
557 FileFd
Pkg(File
,FileFd::ReadOnlyGzip
);
558 if (_error
->PendingError() == true)
560 debListParser
Parser(&Pkg
);
561 if (_error
->PendingError() == true)
565 Prog
->SubProgress(0,File
);
566 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
567 return _error
->Error("Problem with SelectFile %s",File
.c_str());
569 // Store the IMS information
570 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
572 if (fstat(Pkg
.Fd(),&St
) != 0)
573 return _error
->Errno("fstat","Failed to stat");
574 CFile
->Size
= St
.st_size
;
575 CFile
->mtime
= St
.st_mtime
;
576 CFile
->Archive
= Gen
.WriteUniqString("now");
578 if (Gen
.MergeList(Parser
) == false)
579 return _error
->Error("Problem with MergeList %s",File
.c_str());
583 // StatusIndex::FindInCache - Find this index /*{{{*/
584 // ---------------------------------------------------------------------
586 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
588 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
589 for (; File
.end() == false; File
++)
591 if (this->File
!= File
.FileName())
595 if (stat(File
.FileName(),&St
) != 0)
597 if (_config
->FindB("Debug::pkgCacheGen", false))
598 std::clog
<< "StatusIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
599 return pkgCache::PkgFileIterator(Cache
);
601 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
603 if (_config
->FindB("Debug::pkgCacheGen", false))
604 std::clog
<< "StatusIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
605 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
606 << ") doesn't match for " << File
.FileName() << std::endl
;
607 return pkgCache::PkgFileIterator(Cache
);
614 // StatusIndex::Exists - Check if the index is available /*{{{*/
615 // ---------------------------------------------------------------------
617 bool debStatusIndex::Exists() const
619 // Abort if the file does not exist.
624 // Index File types for Debian /*{{{*/
625 class debIFTypeSrc
: public pkgIndexFile::Type
629 debIFTypeSrc() {Label
= "Debian Source Index";};
631 class debIFTypePkg
: public pkgIndexFile::Type
635 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
637 return new debRecordParser(File
.FileName(),*File
.Cache());
639 debIFTypePkg() {Label
= "Debian Package Index";};
641 class debIFTypeTrans
: public debIFTypePkg
644 debIFTypeTrans() {Label
= "Debian Translation Index";};
646 class debIFTypeStatus
: public pkgIndexFile::Type
650 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
652 return new debRecordParser(File
.FileName(),*File
.Cache());
654 debIFTypeStatus() {Label
= "Debian dpkg status file";};
656 static debIFTypeSrc _apt_Src
;
657 static debIFTypePkg _apt_Pkg
;
658 static debIFTypeTrans _apt_Trans
;
659 static debIFTypeStatus _apt_Status
;
661 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
665 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
669 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
673 const pkgIndexFile::Type
*debStatusIndex::GetType() const