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>
29 // SourcesIndex::debSourcesIndex - Constructor /*{{{*/
30 // ---------------------------------------------------------------------
32 debSourcesIndex::debSourcesIndex(string URI
,string Dist
,string Section
,bool Trusted
) :
33 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
)
37 // SourcesIndex::SourceInfo - Short 1 liner describing a source /*{{{*/
38 // ---------------------------------------------------------------------
39 /* The result looks like:
40 http://foo/debian/ stable/main src 1.1.1 (dsc) */
41 string
debSourcesIndex::SourceInfo(pkgSrcRecords::Parser
const &Record
,
42 pkgSrcRecords::File
const &File
) const
45 Res
= ::URI::NoUserPassword(URI
) + ' ';
46 if (Dist
[Dist
.size() - 1] == '/')
52 Res
+= Dist
+ '/' + Section
;
55 Res
+= Record
.Package();
57 Res
+= Record
.Version();
58 if (File
.Type
.empty() == false)
59 Res
+= " (" + File
.Type
+ ")";
63 // SourcesIndex::CreateSrcParser - Get a parser for the source files /*{{{*/
64 // ---------------------------------------------------------------------
66 pkgSrcRecords::Parser
*debSourcesIndex::CreateSrcParser() const
68 string SourcesURI
= _config
->FindDir("Dir::State::lists") +
69 URItoFileName(IndexURI("Sources"));
70 string SourcesURIgzip
= SourcesURI
+ ".gz";
72 if (!FileExists(SourcesURI
) && !FileExists(SourcesURIgzip
))
74 else if (!FileExists(SourcesURI
) && FileExists(SourcesURIgzip
))
75 SourcesURI
= SourcesURIgzip
;
77 return new debSrcRecordParser(SourcesURI
,this);
80 // SourcesIndex::Describe - Give a descriptive path to the index /*{{{*/
81 // ---------------------------------------------------------------------
83 string
debSourcesIndex::Describe(bool Short
) const
87 snprintf(S
,sizeof(S
),"%s",Info("Sources").c_str());
89 snprintf(S
,sizeof(S
),"%s (%s)",Info("Sources").c_str(),
90 IndexFile("Sources").c_str());
95 // SourcesIndex::Info - One liner describing the index URI /*{{{*/
96 // ---------------------------------------------------------------------
98 string
debSourcesIndex::Info(const char *Type
) const
100 string Info
= ::URI::NoUserPassword(URI
) + ' ';
101 if (Dist
[Dist
.size() - 1] == '/')
107 Info
+= Dist
+ '/' + Section
;
113 // SourcesIndex::Index* - Return the URI to the index files /*{{{*/
114 // ---------------------------------------------------------------------
116 inline string
debSourcesIndex::IndexFile(const char *Type
) const
118 string s
= URItoFileName(IndexURI(Type
));
119 string sgzip
= s
+ ".gz";
120 if (!FileExists(s
) && FileExists(sgzip
))
126 string
debSourcesIndex::IndexURI(const char *Type
) const
129 if (Dist
[Dist
.size() - 1] == '/')
137 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
144 // SourcesIndex::Exists - Check if the index is available /*{{{*/
145 // ---------------------------------------------------------------------
147 bool debSourcesIndex::Exists() const
149 return FileExists(IndexFile("Sources"));
152 // SourcesIndex::Size - Return the size of the index /*{{{*/
153 // ---------------------------------------------------------------------
155 unsigned long debSourcesIndex::Size() const
157 unsigned long size
= 0;
159 /* we need to ignore errors here; if the lists are absent, just return 0 */
160 _error
->PushToStack();
162 FileFd f
= FileFd (IndexFile("Sources"), FileFd::ReadOnly
, FileFd::Extension
);
166 if (_error
->PendingError() == true)
168 _error
->RevertToStack();
174 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
175 // ---------------------------------------------------------------------
177 debPackagesIndex::debPackagesIndex(string
const &URI
, string
const &Dist
, string
const &Section
,
178 bool const &Trusted
, string
const &Arch
) :
179 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
), Architecture(Arch
)
181 if (Architecture
== "native")
182 Architecture
= _config
->Find("APT::Architecture");
185 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
186 // ---------------------------------------------------------------------
187 /* This is a shorter version that is designed to be < 60 chars or so */
188 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
190 string Res
= ::URI::NoUserPassword(URI
) + ' ';
191 if (Dist
[Dist
.size() - 1] == '/')
197 Res
+= Dist
+ '/' + Section
;
200 Res
+= Ver
.ParentPkg().Name();
202 if (Dist
[Dist
.size() - 1] != '/')
203 Res
.append(Ver
.Arch()).append(" ");
208 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
209 // ---------------------------------------------------------------------
210 /* This should help the user find the index in the sources.list and
211 in the filesystem for problem solving */
212 string
debPackagesIndex::Describe(bool Short
) const
216 snprintf(S
,sizeof(S
),"%s",Info("Packages").c_str());
218 snprintf(S
,sizeof(S
),"%s (%s)",Info("Packages").c_str(),
219 IndexFile("Packages").c_str());
223 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
224 // ---------------------------------------------------------------------
226 string
debPackagesIndex::Info(const char *Type
) const
228 string Info
= ::URI::NoUserPassword(URI
) + ' ';
229 if (Dist
[Dist
.size() - 1] == '/')
235 Info
+= Dist
+ '/' + Section
;
237 if (Dist
[Dist
.size() - 1] != '/')
238 Info
+= Architecture
+ " ";
243 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
244 // ---------------------------------------------------------------------
246 inline string
debPackagesIndex::IndexFile(const char *Type
) const
248 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
249 string sgzip
= s
+ ".gz";
250 if (!FileExists(s
) && FileExists(sgzip
))
255 string
debPackagesIndex::IndexURI(const char *Type
) const
258 if (Dist
[Dist
.size() - 1] == '/')
266 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
267 "/binary-" + Architecture
+ '/';
273 // PackagesIndex::Exists - Check if the index is available /*{{{*/
274 // ---------------------------------------------------------------------
276 bool debPackagesIndex::Exists() const
278 return FileExists(IndexFile("Packages"));
281 // PackagesIndex::Size - Return the size of the index /*{{{*/
282 // ---------------------------------------------------------------------
283 /* This is really only used for progress reporting. */
284 unsigned long debPackagesIndex::Size() const
286 unsigned long size
= 0;
288 /* we need to ignore errors here; if the lists are absent, just return 0 */
289 _error
->PushToStack();
291 FileFd f
= FileFd (IndexFile("Packages"), FileFd::ReadOnly
, FileFd::Extension
);
295 if (_error
->PendingError() == true)
297 _error
->RevertToStack();
302 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
303 // ---------------------------------------------------------------------
305 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
307 string PackageFile
= IndexFile("Packages");
308 FileFd
Pkg(PackageFile
,FileFd::ReadOnly
, FileFd::Extension
);
309 debListParser
Parser(&Pkg
, Architecture
);
311 if (_error
->PendingError() == true)
312 return _error
->Error("Problem opening %s",PackageFile
.c_str());
314 Prog
->SubProgress(0,Info("Packages"));
316 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
317 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
319 // Store the IMS information
320 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
321 pkgCacheGenerator::Dynamic
<pkgCache::PkgFileIterator
> DynFile(File
);
322 // FIXME: Get this info from FileFd instead
324 if (fstat(Pkg
.Fd(),&St
) != 0)
325 return _error
->Errno("fstat","Failed to stat");
326 File
->Size
= St
.st_size
;
327 File
->mtime
= St
.st_mtime
;
329 if (Gen
.MergeList(Parser
) == false)
330 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
332 // Check the release file
333 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("InRelease");
334 bool releaseExists
= false;
335 if (FileExists(ReleaseFile
) == true)
336 releaseExists
= true;
338 ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
340 if (releaseExists
== true || FileExists(ReleaseFile
) == true)
342 FileFd
Rel(ReleaseFile
,FileFd::ReadOnly
);
343 if (_error
->PendingError() == true)
345 Parser
.LoadReleaseInfo(File
,Rel
,Section
);
351 // PackagesIndex::FindInCache - Find this index /*{{{*/
352 // ---------------------------------------------------------------------
354 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
356 string FileName
= IndexFile("Packages");
357 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
358 for (; File
.end() == false; ++File
)
360 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
364 if (stat(File
.FileName(),&St
) != 0)
366 if (_config
->FindB("Debug::pkgCacheGen", false))
367 std::clog
<< "PackagesIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
368 return pkgCache::PkgFileIterator(Cache
);
370 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
372 if (_config
->FindB("Debug::pkgCacheGen", false))
373 std::clog
<< "PackagesIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
374 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
375 << ") doesn't match for " << File
.FileName() << std::endl
;
376 return pkgCache::PkgFileIterator(Cache
);
385 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
386 // ---------------------------------------------------------------------
388 debTranslationsIndex::debTranslationsIndex(string URI
,string Dist
,string Section
,
389 char const * const Translation
) :
390 pkgIndexFile(true), URI(URI
), Dist(Dist
), Section(Section
),
391 Language(Translation
)
394 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
395 // ---------------------------------------------------------------------
397 inline string
debTranslationsIndex::IndexFile(const char *Type
) const
399 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
400 string sgzip
= s
+ ".gz";
401 if (!FileExists(s
) && FileExists(sgzip
))
406 string
debTranslationsIndex::IndexURI(const char *Type
) const
409 if (Dist
[Dist
.size() - 1] == '/')
417 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
418 "/i18n/Translation-";
424 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
425 // ---------------------------------------------------------------------
427 bool debTranslationsIndex::GetIndexes(pkgAcquire
*Owner
) const
429 string
const TranslationFile
= string("Translation-").append(Language
);
430 new pkgAcqIndexTrans(Owner
, IndexURI(Language
),
431 Info(TranslationFile
.c_str()),
437 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
438 // ---------------------------------------------------------------------
439 /* This should help the user find the index in the sources.list and
440 in the filesystem for problem solving */
441 string
debTranslationsIndex::Describe(bool Short
) const
445 snprintf(S
,sizeof(S
),"%s",Info(TranslationFile().c_str()).c_str());
447 snprintf(S
,sizeof(S
),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
448 IndexFile(Language
).c_str());
452 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
453 // ---------------------------------------------------------------------
455 string
debTranslationsIndex::Info(const char *Type
) const
457 string Info
= ::URI::NoUserPassword(URI
) + ' ';
458 if (Dist
[Dist
.size() - 1] == '/')
464 Info
+= Dist
+ '/' + Section
;
470 bool debTranslationsIndex::HasPackages() const /*{{{*/
472 return FileExists(IndexFile(Language
));
475 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
476 // ---------------------------------------------------------------------
478 bool debTranslationsIndex::Exists() const
480 return FileExists(IndexFile(Language
));
483 // TranslationsIndex::Size - Return the size of the index /*{{{*/
484 // ---------------------------------------------------------------------
485 /* This is really only used for progress reporting. */
486 unsigned long debTranslationsIndex::Size() const
488 unsigned long size
= 0;
490 /* we need to ignore errors here; if the lists are absent, just return 0 */
491 _error
->PushToStack();
493 FileFd f
= FileFd (IndexFile(Language
), FileFd::ReadOnly
, FileFd::Extension
);
497 if (_error
->PendingError() == true)
499 _error
->RevertToStack();
504 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
505 // ---------------------------------------------------------------------
507 bool debTranslationsIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
509 // Check the translation file, if in use
510 string TranslationFile
= IndexFile(Language
);
511 if (FileExists(TranslationFile
))
513 FileFd
Trans(TranslationFile
,FileFd::ReadOnly
, FileFd::Extension
);
514 debListParser
TransParser(&Trans
);
515 if (_error
->PendingError() == true)
519 Prog
->SubProgress(0, Info(TranslationFile
.c_str()));
520 if (Gen
.SelectFile(TranslationFile
,string(),*this) == false)
521 return _error
->Error("Problem with SelectFile %s",TranslationFile
.c_str());
523 // Store the IMS information
524 pkgCache::PkgFileIterator TransFile
= Gen
.GetCurFile();
526 if (fstat(Trans
.Fd(),&TransSt
) != 0)
527 return _error
->Errno("fstat","Failed to stat");
528 TransFile
->Size
= TransSt
.st_size
;
529 TransFile
->mtime
= TransSt
.st_mtime
;
531 if (Gen
.MergeList(TransParser
) == false)
532 return _error
->Error("Problem with MergeList %s",TranslationFile
.c_str());
538 // TranslationsIndex::FindInCache - Find this index /*{{{*/
539 // ---------------------------------------------------------------------
541 pkgCache::PkgFileIterator
debTranslationsIndex::FindInCache(pkgCache
&Cache
) const
543 string FileName
= IndexFile(Language
);
545 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
546 for (; File
.end() == false; ++File
)
548 if (FileName
!= File
.FileName())
552 if (stat(File
.FileName(),&St
) != 0)
554 if (_config
->FindB("Debug::pkgCacheGen", false))
555 std::clog
<< "TranslationIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
556 return pkgCache::PkgFileIterator(Cache
);
558 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
560 if (_config
->FindB("Debug::pkgCacheGen", false))
561 std::clog
<< "TranslationIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
562 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
563 << ") doesn't match for " << File
.FileName() << std::endl
;
564 return pkgCache::PkgFileIterator(Cache
);
571 // StatusIndex::debStatusIndex - Constructor /*{{{*/
572 // ---------------------------------------------------------------------
574 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
578 // StatusIndex::Size - Return the size of the index /*{{{*/
579 // ---------------------------------------------------------------------
581 unsigned long debStatusIndex::Size() const
584 if (stat(File
.c_str(),&S
) != 0)
589 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
590 // ---------------------------------------------------------------------
592 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
594 FileFd
Pkg(File
,FileFd::ReadOnly
, FileFd::Extension
);
595 if (_error
->PendingError() == true)
597 debListParser
Parser(&Pkg
);
598 if (_error
->PendingError() == true)
602 Prog
->SubProgress(0,File
);
603 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
604 return _error
->Error("Problem with SelectFile %s",File
.c_str());
606 // Store the IMS information
607 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
609 if (fstat(Pkg
.Fd(),&St
) != 0)
610 return _error
->Errno("fstat","Failed to stat");
611 CFile
->Size
= St
.st_size
;
612 CFile
->mtime
= St
.st_mtime
;
613 CFile
->Archive
= Gen
.WriteUniqString("now");
615 if (Gen
.MergeList(Parser
) == false)
616 return _error
->Error("Problem with MergeList %s",File
.c_str());
620 // StatusIndex::FindInCache - Find this index /*{{{*/
621 // ---------------------------------------------------------------------
623 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
625 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
626 for (; File
.end() == false; ++File
)
628 if (this->File
!= File
.FileName())
632 if (stat(File
.FileName(),&St
) != 0)
634 if (_config
->FindB("Debug::pkgCacheGen", false))
635 std::clog
<< "StatusIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
636 return pkgCache::PkgFileIterator(Cache
);
638 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
640 if (_config
->FindB("Debug::pkgCacheGen", false))
641 std::clog
<< "StatusIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
642 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
643 << ") doesn't match for " << File
.FileName() << std::endl
;
644 return pkgCache::PkgFileIterator(Cache
);
651 // StatusIndex::Exists - Check if the index is available /*{{{*/
652 // ---------------------------------------------------------------------
654 bool debStatusIndex::Exists() const
656 // Abort if the file does not exist.
661 // Index File types for Debian /*{{{*/
662 class debIFTypeSrc
: public pkgIndexFile::Type
666 debIFTypeSrc() {Label
= "Debian Source Index";};
668 class debIFTypePkg
: public pkgIndexFile::Type
672 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
674 return new debRecordParser(File
.FileName(),*File
.Cache());
676 debIFTypePkg() {Label
= "Debian Package Index";};
678 class debIFTypeTrans
: public debIFTypePkg
681 debIFTypeTrans() {Label
= "Debian Translation Index";};
683 class debIFTypeStatus
: public pkgIndexFile::Type
687 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
689 return new debRecordParser(File
.FileName(),*File
.Cache());
691 debIFTypeStatus() {Label
= "Debian dpkg status file";};
693 static debIFTypeSrc _apt_Src
;
694 static debIFTypePkg _apt_Pkg
;
695 static debIFTypeTrans _apt_Trans
;
696 static debIFTypeStatus _apt_Status
;
698 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
702 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
706 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
710 const pkgIndexFile::Type
*debStatusIndex::GetType() const