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/sourcelist.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/progress.h>
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/strutl.h>
23 #include <apt-pkg/acquire-item.h>
24 #include <apt-pkg/debmetaindex.h>
31 // SourcesIndex::debSourcesIndex - Constructor /*{{{*/
32 // ---------------------------------------------------------------------
34 debSourcesIndex::debSourcesIndex(string URI
,string Dist
,string Section
,bool Trusted
) :
35 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
)
39 // SourcesIndex::SourceInfo - Short 1 liner describing a source /*{{{*/
40 // ---------------------------------------------------------------------
41 /* The result looks like:
42 http://foo/debian/ stable/main src 1.1.1 (dsc) */
43 string
debSourcesIndex::SourceInfo(pkgSrcRecords::Parser
const &Record
,
44 pkgSrcRecords::File
const &File
) const
47 Res
= ::URI::NoUserPassword(URI
) + ' ';
48 if (Dist
[Dist
.size() - 1] == '/')
54 Res
+= Dist
+ '/' + Section
;
57 Res
+= Record
.Package();
59 Res
+= Record
.Version();
60 if (File
.Type
.empty() == false)
61 Res
+= " (" + File
.Type
+ ")";
65 // SourcesIndex::CreateSrcParser - Get a parser for the source files /*{{{*/
66 // ---------------------------------------------------------------------
68 pkgSrcRecords::Parser
*debSourcesIndex::CreateSrcParser() const
70 string SourcesURI
= _config
->FindDir("Dir::State::lists") +
71 URItoFileName(IndexURI("Sources"));
72 string SourcesURIgzip
= SourcesURI
+ ".gz";
74 if (!FileExists(SourcesURI
) && !FileExists(SourcesURIgzip
))
76 else if (!FileExists(SourcesURI
) && FileExists(SourcesURIgzip
))
77 SourcesURI
= SourcesURIgzip
;
79 return new debSrcRecordParser(SourcesURI
,this);
82 // SourcesIndex::Describe - Give a descriptive path to the index /*{{{*/
83 // ---------------------------------------------------------------------
85 string
debSourcesIndex::Describe(bool Short
) const
89 snprintf(S
,sizeof(S
),"%s",Info("Sources").c_str());
91 snprintf(S
,sizeof(S
),"%s (%s)",Info("Sources").c_str(),
92 IndexFile("Sources").c_str());
97 // SourcesIndex::Info - One liner describing the index URI /*{{{*/
98 // ---------------------------------------------------------------------
100 string
debSourcesIndex::Info(const char *Type
) const
102 string Info
= ::URI::NoUserPassword(URI
) + ' ';
103 if (Dist
[Dist
.size() - 1] == '/')
109 Info
+= Dist
+ '/' + Section
;
115 // SourcesIndex::Index* - Return the URI to the index files /*{{{*/
116 // ---------------------------------------------------------------------
118 inline string
debSourcesIndex::IndexFile(const char *Type
) const
120 string s
= URItoFileName(IndexURI(Type
));
121 string sgzip
= s
+ ".gz";
122 if (!FileExists(s
) && FileExists(sgzip
))
128 string
debSourcesIndex::IndexURI(const char *Type
) const
131 if (Dist
[Dist
.size() - 1] == '/')
139 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
146 // SourcesIndex::Exists - Check if the index is available /*{{{*/
147 // ---------------------------------------------------------------------
149 bool debSourcesIndex::Exists() const
151 return FileExists(IndexFile("Sources"));
154 // SourcesIndex::Size - Return the size of the index /*{{{*/
155 // ---------------------------------------------------------------------
157 unsigned long debSourcesIndex::Size() const
159 unsigned long size
= 0;
161 /* we need to ignore errors here; if the lists are absent, just return 0 */
162 _error
->PushToStack();
164 FileFd f
= FileFd (IndexFile("Sources"), FileFd::ReadOnly
, FileFd::Extension
);
168 if (_error
->PendingError() == true)
170 _error
->RevertToStack();
176 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
177 // ---------------------------------------------------------------------
179 debPackagesIndex::debPackagesIndex(string
const &URI
, string
const &Dist
, string
const &Section
,
180 bool const &Trusted
, string
const &Arch
) :
181 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
), Architecture(Arch
)
183 if (Architecture
== "native")
184 Architecture
= _config
->Find("APT::Architecture");
187 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
188 // ---------------------------------------------------------------------
189 /* This is a shorter version that is designed to be < 60 chars or so */
190 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
192 string Res
= ::URI::NoUserPassword(URI
) + ' ';
193 if (Dist
[Dist
.size() - 1] == '/')
199 Res
+= Dist
+ '/' + Section
;
202 Res
+= Ver
.ParentPkg().Name();
204 if (Dist
[Dist
.size() - 1] != '/')
205 Res
.append(Ver
.Arch()).append(" ");
210 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
211 // ---------------------------------------------------------------------
212 /* This should help the user find the index in the sources.list and
213 in the filesystem for problem solving */
214 string
debPackagesIndex::Describe(bool Short
) const
218 snprintf(S
,sizeof(S
),"%s",Info("Packages").c_str());
220 snprintf(S
,sizeof(S
),"%s (%s)",Info("Packages").c_str(),
221 IndexFile("Packages").c_str());
225 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
226 // ---------------------------------------------------------------------
228 string
debPackagesIndex::Info(const char *Type
) const
230 string Info
= ::URI::NoUserPassword(URI
) + ' ';
231 if (Dist
[Dist
.size() - 1] == '/')
237 Info
+= Dist
+ '/' + Section
;
239 if (Dist
[Dist
.size() - 1] != '/')
240 Info
+= Architecture
+ " ";
245 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
246 // ---------------------------------------------------------------------
248 inline string
debPackagesIndex::IndexFile(const char *Type
) const
250 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
251 string sgzip
= s
+ ".gz";
252 if (!FileExists(s
) && FileExists(sgzip
))
257 string
debPackagesIndex::IndexURI(const char *Type
) const
260 if (Dist
[Dist
.size() - 1] == '/')
268 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
269 "/binary-" + Architecture
+ '/';
275 // PackagesIndex::Exists - Check if the index is available /*{{{*/
276 // ---------------------------------------------------------------------
278 bool debPackagesIndex::Exists() const
280 return FileExists(IndexFile("Packages"));
283 // PackagesIndex::Size - Return the size of the index /*{{{*/
284 // ---------------------------------------------------------------------
285 /* This is really only used for progress reporting. */
286 unsigned long debPackagesIndex::Size() const
288 unsigned long size
= 0;
290 /* we need to ignore errors here; if the lists are absent, just return 0 */
291 _error
->PushToStack();
293 FileFd f
= FileFd (IndexFile("Packages"), FileFd::ReadOnly
, FileFd::Extension
);
297 if (_error
->PendingError() == true)
299 _error
->RevertToStack();
304 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
305 // ---------------------------------------------------------------------
307 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
309 string PackageFile
= IndexFile("Packages");
310 FileFd
Pkg(PackageFile
,FileFd::ReadOnly
, FileFd::Extension
);
311 debListParser
Parser(&Pkg
, Architecture
);
313 if (_error
->PendingError() == true)
314 return _error
->Error("Problem opening %s",PackageFile
.c_str());
316 Prog
->SubProgress(0,Info("Packages"));
318 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
319 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
321 // Store the IMS information
322 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
323 pkgCacheGenerator::Dynamic
<pkgCache::PkgFileIterator
> DynFile(File
);
324 File
->Size
= Pkg
.FileSize();
325 File
->mtime
= Pkg
.ModificationTime();
327 if (Gen
.MergeList(Parser
) == false)
328 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
330 // Check the release file
331 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("InRelease");
332 bool releaseExists
= false;
333 if (FileExists(ReleaseFile
) == true)
334 releaseExists
= true;
336 ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
338 if (releaseExists
== true || FileExists(ReleaseFile
) == true)
340 FileFd
Rel(ReleaseFile
,FileFd::ReadOnly
);
341 if (_error
->PendingError() == true)
343 Parser
.LoadReleaseInfo(File
,Rel
,Section
);
349 // PackagesIndex::FindInCache - Find this index /*{{{*/
350 // ---------------------------------------------------------------------
352 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
354 string FileName
= IndexFile("Packages");
355 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
356 for (; File
.end() == false; ++File
)
358 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
362 if (stat(File
.FileName(),&St
) != 0)
364 if (_config
->FindB("Debug::pkgCacheGen", false))
365 std::clog
<< "PackagesIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
366 return pkgCache::PkgFileIterator(Cache
);
368 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
370 if (_config
->FindB("Debug::pkgCacheGen", false))
371 std::clog
<< "PackagesIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
372 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
373 << ") doesn't match for " << File
.FileName() << std::endl
;
374 return pkgCache::PkgFileIterator(Cache
);
383 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
384 // ---------------------------------------------------------------------
386 debTranslationsIndex::debTranslationsIndex(string URI
,string Dist
,string Section
,
387 char const * const Translation
) :
388 pkgIndexFile(true), URI(URI
), Dist(Dist
), Section(Section
),
389 Language(Translation
)
392 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
393 // ---------------------------------------------------------------------
395 inline string
debTranslationsIndex::IndexFile(const char *Type
) const
397 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
398 string sgzip
= s
+ ".gz";
399 if (!FileExists(s
) && FileExists(sgzip
))
404 string
debTranslationsIndex::IndexURI(const char *Type
) const
407 if (Dist
[Dist
.size() - 1] == '/')
415 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
416 "/i18n/Translation-";
422 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
423 // ---------------------------------------------------------------------
425 bool debTranslationsIndex::GetIndexes(pkgAcquire
*Owner
) const
427 string
const TranslationFile
= string("Translation-").append(Language
);
428 new pkgAcqIndexTrans(Owner
, IndexURI(Language
),
429 Info(TranslationFile
.c_str()),
435 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
436 // ---------------------------------------------------------------------
437 /* This should help the user find the index in the sources.list and
438 in the filesystem for problem solving */
439 string
debTranslationsIndex::Describe(bool Short
) const
443 snprintf(S
,sizeof(S
),"%s",Info(TranslationFile().c_str()).c_str());
445 snprintf(S
,sizeof(S
),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
446 IndexFile(Language
).c_str());
450 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
451 // ---------------------------------------------------------------------
453 string
debTranslationsIndex::Info(const char *Type
) const
455 string Info
= ::URI::NoUserPassword(URI
) + ' ';
456 if (Dist
[Dist
.size() - 1] == '/')
462 Info
+= Dist
+ '/' + Section
;
468 bool debTranslationsIndex::HasPackages() const /*{{{*/
470 return FileExists(IndexFile(Language
));
473 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
474 // ---------------------------------------------------------------------
476 bool debTranslationsIndex::Exists() const
478 return FileExists(IndexFile(Language
));
481 // TranslationsIndex::Size - Return the size of the index /*{{{*/
482 // ---------------------------------------------------------------------
483 /* This is really only used for progress reporting. */
484 unsigned long debTranslationsIndex::Size() const
486 unsigned long size
= 0;
488 /* we need to ignore errors here; if the lists are absent, just return 0 */
489 _error
->PushToStack();
491 FileFd f
= FileFd (IndexFile(Language
), FileFd::ReadOnly
, FileFd::Extension
);
495 if (_error
->PendingError() == true)
497 _error
->RevertToStack();
502 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
503 // ---------------------------------------------------------------------
505 bool debTranslationsIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
507 // Check the translation file, if in use
508 string TranslationFile
= IndexFile(Language
);
509 if (FileExists(TranslationFile
))
511 FileFd
Trans(TranslationFile
,FileFd::ReadOnly
, FileFd::Extension
);
512 debListParser
TransParser(&Trans
);
513 if (_error
->PendingError() == true)
517 Prog
->SubProgress(0, Info(TranslationFile
.c_str()));
518 if (Gen
.SelectFile(TranslationFile
,string(),*this) == false)
519 return _error
->Error("Problem with SelectFile %s",TranslationFile
.c_str());
521 // Store the IMS information
522 pkgCache::PkgFileIterator TransFile
= Gen
.GetCurFile();
523 TransFile
->Size
= Trans
.FileSize();
524 TransFile
->mtime
= Trans
.ModificationTime();
526 if (Gen
.MergeList(TransParser
) == false)
527 return _error
->Error("Problem with MergeList %s",TranslationFile
.c_str());
533 // TranslationsIndex::FindInCache - Find this index /*{{{*/
534 // ---------------------------------------------------------------------
536 pkgCache::PkgFileIterator
debTranslationsIndex::FindInCache(pkgCache
&Cache
) const
538 string FileName
= IndexFile(Language
);
540 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
541 for (; File
.end() == false; ++File
)
543 if (FileName
!= File
.FileName())
547 if (stat(File
.FileName(),&St
) != 0)
549 if (_config
->FindB("Debug::pkgCacheGen", false))
550 std::clog
<< "TranslationIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
551 return pkgCache::PkgFileIterator(Cache
);
553 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
555 if (_config
->FindB("Debug::pkgCacheGen", false))
556 std::clog
<< "TranslationIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
557 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
558 << ") doesn't match for " << File
.FileName() << std::endl
;
559 return pkgCache::PkgFileIterator(Cache
);
566 // StatusIndex::debStatusIndex - Constructor /*{{{*/
567 // ---------------------------------------------------------------------
569 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
573 // StatusIndex::Size - Return the size of the index /*{{{*/
574 // ---------------------------------------------------------------------
576 unsigned long debStatusIndex::Size() const
579 if (stat(File
.c_str(),&S
) != 0)
584 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
585 // ---------------------------------------------------------------------
587 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
589 FileFd
Pkg(File
,FileFd::ReadOnly
, FileFd::Extension
);
590 if (_error
->PendingError() == true)
592 debListParser
Parser(&Pkg
);
593 if (_error
->PendingError() == true)
597 Prog
->SubProgress(0,File
);
598 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
599 return _error
->Error("Problem with SelectFile %s",File
.c_str());
601 // Store the IMS information
602 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
603 CFile
->Size
= Pkg
.FileSize();
604 CFile
->mtime
= Pkg
.ModificationTime();
605 CFile
->Archive
= Gen
.WriteUniqString("now");
607 if (Gen
.MergeList(Parser
) == false)
608 return _error
->Error("Problem with MergeList %s",File
.c_str());
612 // StatusIndex::FindInCache - Find this index /*{{{*/
613 // ---------------------------------------------------------------------
615 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
617 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
618 for (; File
.end() == false; ++File
)
620 if (this->File
!= File
.FileName())
624 if (stat(File
.FileName(),&St
) != 0)
626 if (_config
->FindB("Debug::pkgCacheGen", false))
627 std::clog
<< "StatusIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
628 return pkgCache::PkgFileIterator(Cache
);
630 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
632 if (_config
->FindB("Debug::pkgCacheGen", false))
633 std::clog
<< "StatusIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
634 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
635 << ") doesn't match for " << File
.FileName() << std::endl
;
636 return pkgCache::PkgFileIterator(Cache
);
643 // StatusIndex::Exists - Check if the index is available /*{{{*/
644 // ---------------------------------------------------------------------
646 bool debStatusIndex::Exists() const
648 // Abort if the file does not exist.
653 // Index File types for Debian /*{{{*/
654 class debIFTypeSrc
: public pkgIndexFile::Type
658 debIFTypeSrc() {Label
= "Debian Source Index";};
660 class debIFTypePkg
: public pkgIndexFile::Type
664 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
666 return new debRecordParser(File
.FileName(),*File
.Cache());
668 debIFTypePkg() {Label
= "Debian Package Index";};
670 class debIFTypeTrans
: public debIFTypePkg
673 debIFTypeTrans() {Label
= "Debian Translation Index";};
675 class debIFTypeStatus
: public pkgIndexFile::Type
679 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
681 return new debRecordParser(File
.FileName(),*File
.Cache());
683 debIFTypeStatus() {Label
= "Debian dpkg status file";};
685 static debIFTypeSrc _apt_Src
;
686 static debIFTypePkg _apt_Pkg
;
687 static debIFTypeTrans _apt_Trans
;
688 static debIFTypeStatus _apt_Status
;
690 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
694 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
698 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
702 const pkgIndexFile::Type
*debStatusIndex::GetType() const