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
= URItoFileName(IndexURI("Sources"));
67 return new debSrcRecordParser(_config
->FindDir("Dir::State::lists") +
71 // SourcesIndex::Describe - Give a descriptive path to the index /*{{{*/
72 // ---------------------------------------------------------------------
74 string
debSourcesIndex::Describe(bool Short
) const
78 snprintf(S
,sizeof(S
),"%s",Info("Sources").c_str());
80 snprintf(S
,sizeof(S
),"%s (%s)",Info("Sources").c_str(),
81 IndexFile("Sources").c_str());
86 // SourcesIndex::Info - One liner describing the index URI /*{{{*/
87 // ---------------------------------------------------------------------
89 string
debSourcesIndex::Info(const char *Type
) const
91 string Info
= ::URI::NoUserPassword(URI
) + ' ';
92 if (Dist
[Dist
.size() - 1] == '/')
98 Info
+= Dist
+ '/' + Section
;
104 // SourcesIndex::Index* - Return the URI to the index files /*{{{*/
105 // ---------------------------------------------------------------------
107 inline string
debSourcesIndex::IndexFile(const char *Type
) const
109 string s
= URItoFileName(IndexURI(Type
));
110 string sgzip
= s
+ ".gz";
111 if (!FileExists(s
) && FileExists(sgzip
))
117 string
debSourcesIndex::IndexURI(const char *Type
) const
120 if (Dist
[Dist
.size() - 1] == '/')
128 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
135 // SourcesIndex::Exists - Check if the index is available /*{{{*/
136 // ---------------------------------------------------------------------
138 bool debSourcesIndex::Exists() const
140 return FileExists(IndexFile("Sources"));
143 // SourcesIndex::Size - Return the size of the index /*{{{*/
144 // ---------------------------------------------------------------------
146 unsigned long debSourcesIndex::Size() const
149 if (stat(IndexFile("Sources").c_str(),&S
) != 0)
155 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
156 // ---------------------------------------------------------------------
158 debPackagesIndex::debPackagesIndex(string URI
,string Dist
,string Section
,bool Trusted
) :
159 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
)
163 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
164 // ---------------------------------------------------------------------
165 /* This is a shorter version that is designed to be < 60 chars or so */
166 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
168 string Res
= ::URI::NoUserPassword(URI
) + ' ';
169 if (Dist
[Dist
.size() - 1] == '/')
175 Res
+= Dist
+ '/' + Section
;
178 Res
+= Ver
.ParentPkg().Name();
184 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
185 // ---------------------------------------------------------------------
186 /* This should help the user find the index in the sources.list and
187 in the filesystem for problem solving */
188 string
debPackagesIndex::Describe(bool Short
) const
192 snprintf(S
,sizeof(S
),"%s",Info("Packages").c_str());
194 snprintf(S
,sizeof(S
),"%s (%s)",Info("Packages").c_str(),
195 IndexFile("Packages").c_str());
199 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
200 // ---------------------------------------------------------------------
202 string
debPackagesIndex::Info(const char *Type
) const
204 string Info
= ::URI::NoUserPassword(URI
) + ' ';
205 if (Dist
[Dist
.size() - 1] == '/')
211 Info
+= Dist
+ '/' + Section
;
217 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
218 // ---------------------------------------------------------------------
220 inline string
debPackagesIndex::IndexFile(const char *Type
) const
222 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
223 string sgzip
= s
+ ".gz";
224 if (!FileExists(s
) && FileExists(sgzip
))
229 string
debPackagesIndex::IndexURI(const char *Type
) const
232 if (Dist
[Dist
.size() - 1] == '/')
240 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
241 "/binary-" + _config
->Find("APT::Architecture") + '/';
247 // PackagesIndex::Exists - Check if the index is available /*{{{*/
248 // ---------------------------------------------------------------------
250 bool debPackagesIndex::Exists() const
252 return FileExists(IndexFile("Packages"));
255 // PackagesIndex::Size - Return the size of the index /*{{{*/
256 // ---------------------------------------------------------------------
257 /* This is really only used for progress reporting. */
258 unsigned long debPackagesIndex::Size() const
261 if (stat(IndexFile("Packages").c_str(),&S
) != 0)
266 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
267 // ---------------------------------------------------------------------
269 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
&Prog
) const
271 string PackageFile
= IndexFile("Packages");
272 FileFd
Pkg(PackageFile
,FileFd::ReadOnly
);
273 debListParser
Parser(&Pkg
);
274 if (_error
->PendingError() == true)
275 return _error
->Error("Problem opening %s",PackageFile
.c_str());
277 Prog
.SubProgress(0,Info("Packages"));
279 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
280 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
282 // Store the IMS information
283 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
285 if (fstat(Pkg
.Fd(),&St
) != 0)
286 return _error
->Errno("fstat","Failed to stat");
287 File
->Size
= St
.st_size
;
288 File
->mtime
= St
.st_mtime
;
290 if (Gen
.MergeList(Parser
) == false)
291 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
293 // Check the release file
294 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
295 if (FileExists(ReleaseFile
) == true)
297 FileFd
Rel(ReleaseFile
,FileFd::ReadOnly
);
298 if (_error
->PendingError() == true)
300 Parser
.LoadReleaseInfo(File
,Rel
,Section
);
306 // PackagesIndex::FindInCache - Find this index /*{{{*/
307 // ---------------------------------------------------------------------
309 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
311 string FileName
= IndexFile("Packages");
312 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
313 for (; File
.end() == false; File
++)
315 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
319 if (stat(File
.FileName(),&St
) != 0)
321 if (_config
->FindB("Debug::pkgCacheGen", false))
322 std::clog
<< "PackagesIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
323 return pkgCache::PkgFileIterator(Cache
);
325 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
327 if (_config
->FindB("Debug::pkgCacheGen", false))
328 std::clog
<< "PackagesIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
329 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
330 << ") doesn't match for " << File
.FileName() << std::endl
;
331 return pkgCache::PkgFileIterator(Cache
);
340 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
341 // ---------------------------------------------------------------------
343 debTranslationsIndex::debTranslationsIndex(string URI
,string Dist
,string Section
,
344 char const * const Translation
) :
345 pkgIndexFile(true), URI(URI
), Dist(Dist
), Section(Section
),
346 Language(Translation
)
349 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
350 // ---------------------------------------------------------------------
352 inline string
debTranslationsIndex::IndexFile(const char *Type
) const
354 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
355 string sgzip
= s
+ ".gz";
356 if (!FileExists(s
) && FileExists(sgzip
))
361 string
debTranslationsIndex::IndexURI(const char *Type
) const
364 if (Dist
[Dist
.size() - 1] == '/')
372 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
373 "/i18n/Translation-";
379 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
380 // ---------------------------------------------------------------------
382 bool debTranslationsIndex::GetIndexes(pkgAcquire
*Owner
) const
384 if (TranslationsAvailable()) {
385 string
const TranslationFile
= string("Translation-").append(Language
);
386 new pkgAcqIndexTrans(Owner
, IndexURI(Language
),
387 Info(TranslationFile
.c_str()),
394 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
395 // ---------------------------------------------------------------------
396 /* This should help the user find the index in the sources.list and
397 in the filesystem for problem solving */
398 string
debTranslationsIndex::Describe(bool Short
) const
402 snprintf(S
,sizeof(S
),"%s",Info(TranslationFile().c_str()).c_str());
404 snprintf(S
,sizeof(S
),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
405 IndexFile(Language
).c_str());
409 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
410 // ---------------------------------------------------------------------
412 string
debTranslationsIndex::Info(const char *Type
) const
414 string Info
= ::URI::NoUserPassword(URI
) + ' ';
415 if (Dist
[Dist
.size() - 1] == '/')
421 Info
+= Dist
+ '/' + Section
;
427 bool debTranslationsIndex::HasPackages() const /*{{{*/
429 if(!TranslationsAvailable())
432 return FileExists(IndexFile(Language
));
435 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
436 // ---------------------------------------------------------------------
438 bool debTranslationsIndex::Exists() const
440 return FileExists(IndexFile(Language
));
443 // TranslationsIndex::Size - Return the size of the index /*{{{*/
444 // ---------------------------------------------------------------------
445 /* This is really only used for progress reporting. */
446 unsigned long debTranslationsIndex::Size() const
449 if (stat(IndexFile(Language
).c_str(),&S
) != 0)
454 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
455 // ---------------------------------------------------------------------
457 bool debTranslationsIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
&Prog
) const
459 // Check the translation file, if in use
460 string TranslationFile
= IndexFile(Language
);
461 if (TranslationsAvailable() && FileExists(TranslationFile
))
463 FileFd
Trans(TranslationFile
,FileFd::ReadOnly
);
464 debListParser
TransParser(&Trans
);
465 if (_error
->PendingError() == true)
468 Prog
.SubProgress(0, Info(TranslationFile
.c_str()));
469 if (Gen
.SelectFile(TranslationFile
,string(),*this) == false)
470 return _error
->Error("Problem with SelectFile %s",TranslationFile
.c_str());
472 // Store the IMS information
473 pkgCache::PkgFileIterator TransFile
= Gen
.GetCurFile();
475 if (fstat(Trans
.Fd(),&TransSt
) != 0)
476 return _error
->Errno("fstat","Failed to stat");
477 TransFile
->Size
= TransSt
.st_size
;
478 TransFile
->mtime
= TransSt
.st_mtime
;
480 if (Gen
.MergeList(TransParser
) == false)
481 return _error
->Error("Problem with MergeList %s",TranslationFile
.c_str());
487 // TranslationsIndex::FindInCache - Find this index /*{{{*/
488 // ---------------------------------------------------------------------
490 pkgCache::PkgFileIterator
debTranslationsIndex::FindInCache(pkgCache
&Cache
) const
492 string FileName
= IndexFile(Language
);
494 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
495 for (; File
.end() == false; File
++)
497 if (FileName
!= File
.FileName())
501 if (stat(File
.FileName(),&St
) != 0)
503 if (_config
->FindB("Debug::pkgCacheGen", false))
504 std::clog
<< "TranslationIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
505 return pkgCache::PkgFileIterator(Cache
);
507 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
509 if (_config
->FindB("Debug::pkgCacheGen", false))
510 std::clog
<< "TranslationIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
511 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
512 << ") doesn't match for " << File
.FileName() << std::endl
;
513 return pkgCache::PkgFileIterator(Cache
);
520 // StatusIndex::debStatusIndex - Constructor /*{{{*/
521 // ---------------------------------------------------------------------
523 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
527 // StatusIndex::Size - Return the size of the index /*{{{*/
528 // ---------------------------------------------------------------------
530 unsigned long debStatusIndex::Size() const
533 if (stat(File
.c_str(),&S
) != 0)
538 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
539 // ---------------------------------------------------------------------
541 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
&Prog
) const
543 FileFd
Pkg(File
,FileFd::ReadOnly
);
544 if (_error
->PendingError() == true)
546 debListParser
Parser(&Pkg
);
547 if (_error
->PendingError() == true)
550 Prog
.SubProgress(0,File
);
551 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
552 return _error
->Error("Problem with SelectFile %s",File
.c_str());
554 // Store the IMS information
555 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
557 if (fstat(Pkg
.Fd(),&St
) != 0)
558 return _error
->Errno("fstat","Failed to stat");
559 CFile
->Size
= St
.st_size
;
560 CFile
->mtime
= St
.st_mtime
;
561 CFile
->Archive
= Gen
.WriteUniqString("now");
563 if (Gen
.MergeList(Parser
) == false)
564 return _error
->Error("Problem with MergeList %s",File
.c_str());
568 // StatusIndex::FindInCache - Find this index /*{{{*/
569 // ---------------------------------------------------------------------
571 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
573 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
574 for (; File
.end() == false; File
++)
576 if (this->File
!= File
.FileName())
580 if (stat(File
.FileName(),&St
) != 0)
582 if (_config
->FindB("Debug::pkgCacheGen", false))
583 std::clog
<< "StatusIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
584 return pkgCache::PkgFileIterator(Cache
);
586 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
588 if (_config
->FindB("Debug::pkgCacheGen", false))
589 std::clog
<< "StatusIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
590 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
591 << ") doesn't match for " << File
.FileName() << std::endl
;
592 return pkgCache::PkgFileIterator(Cache
);
599 // StatusIndex::Exists - Check if the index is available /*{{{*/
600 // ---------------------------------------------------------------------
602 bool debStatusIndex::Exists() const
604 // Abort if the file does not exist.
609 // Index File types for Debian /*{{{*/
610 class debIFTypeSrc
: public pkgIndexFile::Type
614 debIFTypeSrc() {Label
= "Debian Source Index";};
616 class debIFTypePkg
: public pkgIndexFile::Type
620 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
622 return new debRecordParser(File
.FileName(),*File
.Cache());
624 debIFTypePkg() {Label
= "Debian Package Index";};
626 class debIFTypeTrans
: public debIFTypePkg
629 debIFTypeTrans() {Label
= "Debian Translation Index";};
631 class debIFTypeStatus
: public pkgIndexFile::Type
635 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
637 return new debRecordParser(File
.FileName(),*File
.Cache());
639 debIFTypeStatus() {Label
= "Debian dpkg status file";};
641 static debIFTypeSrc _apt_Src
;
642 static debIFTypePkg _apt_Pkg
;
643 static debIFTypeTrans _apt_Trans
;
644 static debIFTypeStatus _apt_Status
;
646 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
650 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
654 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
658 const pkgIndexFile::Type
*debStatusIndex::GetType() const