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";
70 if (!FileExists(SourcesURI
) && !FileExists(SourcesURIgzip
))
72 else if (!FileExists(SourcesURI
) && FileExists(SourcesURIgzip
))
73 SourcesURI
= SourcesURIgzip
;
75 return new debSrcRecordParser(SourcesURI
,this);
78 // SourcesIndex::Describe - Give a descriptive path to the index /*{{{*/
79 // ---------------------------------------------------------------------
81 string
debSourcesIndex::Describe(bool Short
) const
85 snprintf(S
,sizeof(S
),"%s",Info("Sources").c_str());
87 snprintf(S
,sizeof(S
),"%s (%s)",Info("Sources").c_str(),
88 IndexFile("Sources").c_str());
93 // SourcesIndex::Info - One liner describing the index URI /*{{{*/
94 // ---------------------------------------------------------------------
96 string
debSourcesIndex::Info(const char *Type
) const
98 string Info
= ::URI::NoUserPassword(URI
) + ' ';
99 if (Dist
[Dist
.size() - 1] == '/')
105 Info
+= Dist
+ '/' + Section
;
111 // SourcesIndex::Index* - Return the URI to the index files /*{{{*/
112 // ---------------------------------------------------------------------
114 inline string
debSourcesIndex::IndexFile(const char *Type
) const
116 string s
= URItoFileName(IndexURI(Type
));
117 string sgzip
= s
+ ".gz";
118 if (!FileExists(s
) && FileExists(sgzip
))
124 string
debSourcesIndex::IndexURI(const char *Type
) const
127 if (Dist
[Dist
.size() - 1] == '/')
135 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
142 // SourcesIndex::Exists - Check if the index is available /*{{{*/
143 // ---------------------------------------------------------------------
145 bool debSourcesIndex::Exists() const
147 return FileExists(IndexFile("Sources"));
150 // SourcesIndex::Size - Return the size of the index /*{{{*/
151 // ---------------------------------------------------------------------
153 unsigned long debSourcesIndex::Size() const
155 unsigned long size
= 0;
157 /* we need to ignore errors here; if the lists are absent, just return 0 */
158 _error
->PushToStack();
160 FileFd f
= FileFd (IndexFile("Sources"), FileFd::ReadOnlyGzip
);
164 if (_error
->PendingError() == true)
166 _error
->RevertToStack();
172 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
173 // ---------------------------------------------------------------------
175 debPackagesIndex::debPackagesIndex(string
const &URI
, string
const &Dist
, string
const &Section
,
176 bool const &Trusted
, string
const &Arch
) :
177 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
), Architecture(Arch
)
179 if (Architecture
== "native")
180 Architecture
= _config
->Find("APT::Architecture");
183 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
184 // ---------------------------------------------------------------------
185 /* This is a shorter version that is designed to be < 60 chars or so */
186 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
188 string Res
= ::URI::NoUserPassword(URI
) + ' ';
189 if (Dist
[Dist
.size() - 1] == '/')
195 Res
+= Dist
+ '/' + Section
;
198 Res
+= Ver
.ParentPkg().Name();
200 if (Dist
[Dist
.size() - 1] != '/')
201 Res
.append(Ver
.Arch()).append(" ");
206 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
207 // ---------------------------------------------------------------------
208 /* This should help the user find the index in the sources.list and
209 in the filesystem for problem solving */
210 string
debPackagesIndex::Describe(bool Short
) const
214 snprintf(S
,sizeof(S
),"%s",Info("Packages").c_str());
216 snprintf(S
,sizeof(S
),"%s (%s)",Info("Packages").c_str(),
217 IndexFile("Packages").c_str());
221 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
222 // ---------------------------------------------------------------------
224 string
debPackagesIndex::Info(const char *Type
) const
226 string Info
= ::URI::NoUserPassword(URI
) + ' ';
227 if (Dist
[Dist
.size() - 1] == '/')
233 Info
+= Dist
+ '/' + Section
;
235 if (Dist
[Dist
.size() - 1] != '/')
236 Info
+= Architecture
+ " ";
241 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
242 // ---------------------------------------------------------------------
244 inline string
debPackagesIndex::IndexFile(const char *Type
) const
246 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
247 string sgzip
= s
+ ".gz";
248 if (!FileExists(s
) && FileExists(sgzip
))
253 string
debPackagesIndex::IndexURI(const char *Type
) const
256 if (Dist
[Dist
.size() - 1] == '/')
264 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
265 "/binary-" + Architecture
+ '/';
271 // PackagesIndex::Exists - Check if the index is available /*{{{*/
272 // ---------------------------------------------------------------------
274 bool debPackagesIndex::Exists() const
276 return FileExists(IndexFile("Packages"));
279 // PackagesIndex::Size - Return the size of the index /*{{{*/
280 // ---------------------------------------------------------------------
281 /* This is really only used for progress reporting. */
282 unsigned long debPackagesIndex::Size() const
284 unsigned long size
= 0;
286 /* we need to ignore errors here; if the lists are absent, just return 0 */
287 _error
->PushToStack();
289 FileFd f
= FileFd (IndexFile("Packages"), FileFd::ReadOnlyGzip
);
293 if (_error
->PendingError() == true)
295 _error
->RevertToStack();
300 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
301 // ---------------------------------------------------------------------
303 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
305 string PackageFile
= IndexFile("Packages");
306 FileFd
Pkg(PackageFile
,FileFd::ReadOnlyGzip
);
307 debListParser
Parser(&Pkg
, Architecture
);
309 if (_error
->PendingError() == true)
310 return _error
->Error("Problem opening %s",PackageFile
.c_str());
312 Prog
->SubProgress(0,Info("Packages"));
314 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
315 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
317 // Store the IMS information
318 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
319 pkgCacheGenerator::Dynamic
<pkgCache::PkgFileIterator
> DynFile(File
);
321 if (fstat(Pkg
.Fd(),&St
) != 0)
322 return _error
->Errno("fstat","Failed to stat");
323 File
->Size
= St
.st_size
;
324 File
->mtime
= St
.st_mtime
;
326 if (Gen
.MergeList(Parser
) == false)
327 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
329 // Check the release file
330 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("InRelease");
331 bool releaseExists
= false;
332 if (FileExists(ReleaseFile
) == true)
333 releaseExists
= true;
335 ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
337 if (releaseExists
== true || FileExists(ReleaseFile
) == true)
339 FileFd
Rel(ReleaseFile
,FileFd::ReadOnly
);
340 if (_error
->PendingError() == true)
342 Parser
.LoadReleaseInfo(File
,Rel
,Section
);
348 // PackagesIndex::FindInCache - Find this index /*{{{*/
349 // ---------------------------------------------------------------------
351 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
353 string FileName
= IndexFile("Packages");
354 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
355 for (; File
.end() == false; File
++)
357 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
361 if (stat(File
.FileName(),&St
) != 0)
363 if (_config
->FindB("Debug::pkgCacheGen", false))
364 std::clog
<< "PackagesIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
365 return pkgCache::PkgFileIterator(Cache
);
367 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
369 if (_config
->FindB("Debug::pkgCacheGen", false))
370 std::clog
<< "PackagesIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
371 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
372 << ") doesn't match for " << File
.FileName() << std::endl
;
373 return pkgCache::PkgFileIterator(Cache
);
382 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
383 // ---------------------------------------------------------------------
385 debTranslationsIndex::debTranslationsIndex(string URI
,string Dist
,string Section
,
386 char const * const Translation
) :
387 pkgIndexFile(true), URI(URI
), Dist(Dist
), Section(Section
),
388 Language(Translation
)
391 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
392 // ---------------------------------------------------------------------
394 inline string
debTranslationsIndex::IndexFile(const char *Type
) const
396 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
397 string sgzip
= s
+ ".gz";
398 if (!FileExists(s
) && FileExists(sgzip
))
403 string
debTranslationsIndex::IndexURI(const char *Type
) const
406 if (Dist
[Dist
.size() - 1] == '/')
414 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
415 "/i18n/Translation-";
421 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
422 // ---------------------------------------------------------------------
424 bool debTranslationsIndex::GetIndexes(pkgAcquire
*Owner
) const
426 if (TranslationsAvailable()) {
427 string
const TranslationFile
= string("Translation-").append(Language
);
428 new pkgAcqIndexTrans(Owner
, IndexURI(Language
),
429 Info(TranslationFile
.c_str()),
436 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
437 // ---------------------------------------------------------------------
438 /* This should help the user find the index in the sources.list and
439 in the filesystem for problem solving */
440 string
debTranslationsIndex::Describe(bool Short
) const
444 snprintf(S
,sizeof(S
),"%s",Info(TranslationFile().c_str()).c_str());
446 snprintf(S
,sizeof(S
),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
447 IndexFile(Language
).c_str());
451 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
452 // ---------------------------------------------------------------------
454 string
debTranslationsIndex::Info(const char *Type
) const
456 string Info
= ::URI::NoUserPassword(URI
) + ' ';
457 if (Dist
[Dist
.size() - 1] == '/')
463 Info
+= Dist
+ '/' + Section
;
469 bool debTranslationsIndex::HasPackages() const /*{{{*/
471 if(!TranslationsAvailable())
474 return FileExists(IndexFile(Language
));
477 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
478 // ---------------------------------------------------------------------
480 bool debTranslationsIndex::Exists() const
482 return FileExists(IndexFile(Language
));
485 // TranslationsIndex::Size - Return the size of the index /*{{{*/
486 // ---------------------------------------------------------------------
487 /* This is really only used for progress reporting. */
488 unsigned long debTranslationsIndex::Size() const
490 unsigned long size
= 0;
492 /* we need to ignore errors here; if the lists are absent, just return 0 */
493 _error
->PushToStack();
495 FileFd f
= FileFd (IndexFile(Language
), FileFd::ReadOnlyGzip
);
499 if (_error
->PendingError() == true)
501 _error
->RevertToStack();
506 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
507 // ---------------------------------------------------------------------
509 bool debTranslationsIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
511 // Check the translation file, if in use
512 string TranslationFile
= IndexFile(Language
);
513 if (TranslationsAvailable() && FileExists(TranslationFile
))
515 FileFd
Trans(TranslationFile
,FileFd::ReadOnlyGzip
);
516 debListParser
TransParser(&Trans
);
517 if (_error
->PendingError() == true)
521 Prog
->SubProgress(0, Info(TranslationFile
.c_str()));
522 if (Gen
.SelectFile(TranslationFile
,string(),*this) == false)
523 return _error
->Error("Problem with SelectFile %s",TranslationFile
.c_str());
525 // Store the IMS information
526 pkgCache::PkgFileIterator TransFile
= Gen
.GetCurFile();
528 if (fstat(Trans
.Fd(),&TransSt
) != 0)
529 return _error
->Errno("fstat","Failed to stat");
530 TransFile
->Size
= TransSt
.st_size
;
531 TransFile
->mtime
= TransSt
.st_mtime
;
533 if (Gen
.MergeList(TransParser
) == false)
534 return _error
->Error("Problem with MergeList %s",TranslationFile
.c_str());
540 // TranslationsIndex::FindInCache - Find this index /*{{{*/
541 // ---------------------------------------------------------------------
543 pkgCache::PkgFileIterator
debTranslationsIndex::FindInCache(pkgCache
&Cache
) const
545 string FileName
= IndexFile(Language
);
547 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
548 for (; File
.end() == false; File
++)
550 if (FileName
!= File
.FileName())
554 if (stat(File
.FileName(),&St
) != 0)
556 if (_config
->FindB("Debug::pkgCacheGen", false))
557 std::clog
<< "TranslationIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
558 return pkgCache::PkgFileIterator(Cache
);
560 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
562 if (_config
->FindB("Debug::pkgCacheGen", false))
563 std::clog
<< "TranslationIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
564 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
565 << ") doesn't match for " << File
.FileName() << std::endl
;
566 return pkgCache::PkgFileIterator(Cache
);
573 // StatusIndex::debStatusIndex - Constructor /*{{{*/
574 // ---------------------------------------------------------------------
576 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
580 // StatusIndex::Size - Return the size of the index /*{{{*/
581 // ---------------------------------------------------------------------
583 unsigned long debStatusIndex::Size() const
586 if (stat(File
.c_str(),&S
) != 0)
591 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
592 // ---------------------------------------------------------------------
594 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
596 FileFd
Pkg(File
,FileFd::ReadOnlyGzip
);
597 if (_error
->PendingError() == true)
599 debListParser
Parser(&Pkg
);
600 if (_error
->PendingError() == true)
604 Prog
->SubProgress(0,File
);
605 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
606 return _error
->Error("Problem with SelectFile %s",File
.c_str());
608 // Store the IMS information
609 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
611 if (fstat(Pkg
.Fd(),&St
) != 0)
612 return _error
->Errno("fstat","Failed to stat");
613 CFile
->Size
= St
.st_size
;
614 CFile
->mtime
= St
.st_mtime
;
615 CFile
->Archive
= Gen
.WriteUniqString("now");
617 if (Gen
.MergeList(Parser
) == false)
618 return _error
->Error("Problem with MergeList %s",File
.c_str());
622 // StatusIndex::FindInCache - Find this index /*{{{*/
623 // ---------------------------------------------------------------------
625 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
627 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
628 for (; File
.end() == false; File
++)
630 if (this->File
!= File
.FileName())
634 if (stat(File
.FileName(),&St
) != 0)
636 if (_config
->FindB("Debug::pkgCacheGen", false))
637 std::clog
<< "StatusIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
638 return pkgCache::PkgFileIterator(Cache
);
640 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
642 if (_config
->FindB("Debug::pkgCacheGen", false))
643 std::clog
<< "StatusIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
644 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
645 << ") doesn't match for " << File
.FileName() << std::endl
;
646 return pkgCache::PkgFileIterator(Cache
);
653 // StatusIndex::Exists - Check if the index is available /*{{{*/
654 // ---------------------------------------------------------------------
656 bool debStatusIndex::Exists() const
658 // Abort if the file does not exist.
663 // Index File types for Debian /*{{{*/
664 class debIFTypeSrc
: public pkgIndexFile::Type
668 debIFTypeSrc() {Label
= "Debian Source Index";};
670 class debIFTypePkg
: public pkgIndexFile::Type
674 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
676 return new debRecordParser(File
.FileName(),*File
.Cache());
678 debIFTypePkg() {Label
= "Debian Package Index";};
680 class debIFTypeTrans
: public debIFTypePkg
683 debIFTypeTrans() {Label
= "Debian Translation Index";};
685 class debIFTypeStatus
: public pkgIndexFile::Type
689 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
691 return new debRecordParser(File
.FileName(),*File
.Cache());
693 debIFTypeStatus() {Label
= "Debian dpkg status file";};
695 static debIFTypeSrc _apt_Src
;
696 static debIFTypePkg _apt_Pkg
;
697 static debIFTypeTrans _apt_Trans
;
698 static debIFTypeStatus _apt_Status
;
700 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
704 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
708 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
712 const pkgIndexFile::Type
*debStatusIndex::GetType() const