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
152 FileFd f
= FileFd (IndexFile("Sources"), FileFd::ReadOnlyGzip
);
160 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
161 // ---------------------------------------------------------------------
163 debPackagesIndex::debPackagesIndex(string
const &URI
, string
const &Dist
, string
const &Section
,
164 bool const &Trusted
, string
const &Arch
) :
165 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
), Architecture(Arch
)
167 if (Architecture
== "native")
168 Architecture
= _config
->Find("APT::Architecture");
171 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
172 // ---------------------------------------------------------------------
173 /* This is a shorter version that is designed to be < 60 chars or so */
174 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
176 string Res
= ::URI::NoUserPassword(URI
) + ' ';
177 if (Dist
[Dist
.size() - 1] == '/')
183 Res
+= Dist
+ '/' + Section
;
186 Res
+= Ver
.ParentPkg().Name();
188 if (Dist
[Dist
.size() - 1] != '/')
189 Res
.append(Ver
.Arch()).append(" ");
194 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
195 // ---------------------------------------------------------------------
196 /* This should help the user find the index in the sources.list and
197 in the filesystem for problem solving */
198 string
debPackagesIndex::Describe(bool Short
) const
202 snprintf(S
,sizeof(S
),"%s",Info("Packages").c_str());
204 snprintf(S
,sizeof(S
),"%s (%s)",Info("Packages").c_str(),
205 IndexFile("Packages").c_str());
209 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
210 // ---------------------------------------------------------------------
212 string
debPackagesIndex::Info(const char *Type
) const
214 string Info
= ::URI::NoUserPassword(URI
) + ' ';
215 if (Dist
[Dist
.size() - 1] == '/')
221 Info
+= Dist
+ '/' + Section
;
223 if (Dist
[Dist
.size() - 1] != '/')
224 Info
+= Architecture
+ " ";
229 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
230 // ---------------------------------------------------------------------
232 inline string
debPackagesIndex::IndexFile(const char *Type
) const
234 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
235 string sgzip
= s
+ ".gz";
236 if (!FileExists(s
) && FileExists(sgzip
))
241 string
debPackagesIndex::IndexURI(const char *Type
) const
244 if (Dist
[Dist
.size() - 1] == '/')
252 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
253 "/binary-" + Architecture
+ '/';
259 // PackagesIndex::Exists - Check if the index is available /*{{{*/
260 // ---------------------------------------------------------------------
262 bool debPackagesIndex::Exists() const
264 return FileExists(IndexFile("Packages"));
267 // PackagesIndex::Size - Return the size of the index /*{{{*/
268 // ---------------------------------------------------------------------
269 /* This is really only used for progress reporting. */
270 unsigned long debPackagesIndex::Size() const
272 FileFd f
= FileFd (IndexFile("Packages"), FileFd::ReadOnlyGzip
);
279 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
280 // ---------------------------------------------------------------------
282 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
284 string PackageFile
= IndexFile("Packages");
285 FileFd
Pkg(PackageFile
,FileFd::ReadOnlyGzip
);
286 debListParser
Parser(&Pkg
, Architecture
);
288 if (_error
->PendingError() == true)
289 return _error
->Error("Problem opening %s",PackageFile
.c_str());
291 Prog
->SubProgress(0,Info("Packages"));
293 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
294 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
296 // Store the IMS information
297 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
298 pkgCacheGenerator::Dynamic
<pkgCache::PkgFileIterator
> DynFile(File
);
300 if (fstat(Pkg
.Fd(),&St
) != 0)
301 return _error
->Errno("fstat","Failed to stat");
302 File
->Size
= St
.st_size
;
303 File
->mtime
= St
.st_mtime
;
305 if (Gen
.MergeList(Parser
) == false)
306 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
308 // Check the release file
309 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
310 if (FileExists(ReleaseFile
) == true)
312 FileFd
Rel(ReleaseFile
,FileFd::ReadOnly
);
313 if (_error
->PendingError() == true)
315 Parser
.LoadReleaseInfo(File
,Rel
,Section
);
321 // PackagesIndex::FindInCache - Find this index /*{{{*/
322 // ---------------------------------------------------------------------
324 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
326 string FileName
= IndexFile("Packages");
327 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
328 for (; File
.end() == false; File
++)
330 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
334 if (stat(File
.FileName(),&St
) != 0)
336 if (_config
->FindB("Debug::pkgCacheGen", false))
337 std::clog
<< "PackagesIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
338 return pkgCache::PkgFileIterator(Cache
);
340 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
342 if (_config
->FindB("Debug::pkgCacheGen", false))
343 std::clog
<< "PackagesIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
344 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
345 << ") doesn't match for " << File
.FileName() << std::endl
;
346 return pkgCache::PkgFileIterator(Cache
);
355 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
356 // ---------------------------------------------------------------------
358 debTranslationsIndex::debTranslationsIndex(string URI
,string Dist
,string Section
,
359 char const * const Translation
) :
360 pkgIndexFile(true), URI(URI
), Dist(Dist
), Section(Section
),
361 Language(Translation
)
364 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
365 // ---------------------------------------------------------------------
367 inline string
debTranslationsIndex::IndexFile(const char *Type
) const
369 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
370 string sgzip
= s
+ ".gz";
371 if (!FileExists(s
) && FileExists(sgzip
))
376 string
debTranslationsIndex::IndexURI(const char *Type
) const
379 if (Dist
[Dist
.size() - 1] == '/')
387 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
388 "/i18n/Translation-";
394 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
395 // ---------------------------------------------------------------------
397 bool debTranslationsIndex::GetIndexes(pkgAcquire
*Owner
) const
399 if (TranslationsAvailable()) {
400 string
const TranslationFile
= string("Translation-").append(Language
);
401 new pkgAcqIndexTrans(Owner
, IndexURI(Language
),
402 Info(TranslationFile
.c_str()),
409 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
410 // ---------------------------------------------------------------------
411 /* This should help the user find the index in the sources.list and
412 in the filesystem for problem solving */
413 string
debTranslationsIndex::Describe(bool Short
) const
417 snprintf(S
,sizeof(S
),"%s",Info(TranslationFile().c_str()).c_str());
419 snprintf(S
,sizeof(S
),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
420 IndexFile(Language
).c_str());
424 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
425 // ---------------------------------------------------------------------
427 string
debTranslationsIndex::Info(const char *Type
) const
429 string Info
= ::URI::NoUserPassword(URI
) + ' ';
430 if (Dist
[Dist
.size() - 1] == '/')
436 Info
+= Dist
+ '/' + Section
;
442 bool debTranslationsIndex::HasPackages() const /*{{{*/
444 if(!TranslationsAvailable())
447 return FileExists(IndexFile(Language
));
450 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
451 // ---------------------------------------------------------------------
453 bool debTranslationsIndex::Exists() const
455 return FileExists(IndexFile(Language
));
458 // TranslationsIndex::Size - Return the size of the index /*{{{*/
459 // ---------------------------------------------------------------------
460 /* This is really only used for progress reporting. */
461 unsigned long debTranslationsIndex::Size() const
463 FileFd f
= FileFd (IndexFile(Language
), FileFd::ReadOnlyGzip
);
471 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
472 // ---------------------------------------------------------------------
474 bool debTranslationsIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
476 // Check the translation file, if in use
477 string TranslationFile
= IndexFile(Language
);
478 if (TranslationsAvailable() && FileExists(TranslationFile
))
480 FileFd
Trans(TranslationFile
,FileFd::ReadOnlyGzip
);
481 debListParser
TransParser(&Trans
);
482 if (_error
->PendingError() == true)
486 Prog
->SubProgress(0, Info(TranslationFile
.c_str()));
487 if (Gen
.SelectFile(TranslationFile
,string(),*this) == false)
488 return _error
->Error("Problem with SelectFile %s",TranslationFile
.c_str());
490 // Store the IMS information
491 pkgCache::PkgFileIterator TransFile
= Gen
.GetCurFile();
493 if (fstat(Trans
.Fd(),&TransSt
) != 0)
494 return _error
->Errno("fstat","Failed to stat");
495 TransFile
->Size
= TransSt
.st_size
;
496 TransFile
->mtime
= TransSt
.st_mtime
;
498 if (Gen
.MergeList(TransParser
) == false)
499 return _error
->Error("Problem with MergeList %s",TranslationFile
.c_str());
505 // TranslationsIndex::FindInCache - Find this index /*{{{*/
506 // ---------------------------------------------------------------------
508 pkgCache::PkgFileIterator
debTranslationsIndex::FindInCache(pkgCache
&Cache
) const
510 string FileName
= IndexFile(Language
);
512 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
513 for (; File
.end() == false; File
++)
515 if (FileName
!= File
.FileName())
519 if (stat(File
.FileName(),&St
) != 0)
521 if (_config
->FindB("Debug::pkgCacheGen", false))
522 std::clog
<< "TranslationIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
523 return pkgCache::PkgFileIterator(Cache
);
525 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
527 if (_config
->FindB("Debug::pkgCacheGen", false))
528 std::clog
<< "TranslationIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
529 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
530 << ") doesn't match for " << File
.FileName() << std::endl
;
531 return pkgCache::PkgFileIterator(Cache
);
538 // StatusIndex::debStatusIndex - Constructor /*{{{*/
539 // ---------------------------------------------------------------------
541 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
545 // StatusIndex::Size - Return the size of the index /*{{{*/
546 // ---------------------------------------------------------------------
548 unsigned long debStatusIndex::Size() const
551 if (stat(File
.c_str(),&S
) != 0)
556 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
557 // ---------------------------------------------------------------------
559 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
561 FileFd
Pkg(File
,FileFd::ReadOnlyGzip
);
562 if (_error
->PendingError() == true)
564 debListParser
Parser(&Pkg
);
565 if (_error
->PendingError() == true)
569 Prog
->SubProgress(0,File
);
570 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
571 return _error
->Error("Problem with SelectFile %s",File
.c_str());
573 // Store the IMS information
574 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
576 if (fstat(Pkg
.Fd(),&St
) != 0)
577 return _error
->Errno("fstat","Failed to stat");
578 CFile
->Size
= St
.st_size
;
579 CFile
->mtime
= St
.st_mtime
;
580 CFile
->Archive
= Gen
.WriteUniqString("now");
582 if (Gen
.MergeList(Parser
) == false)
583 return _error
->Error("Problem with MergeList %s",File
.c_str());
587 // StatusIndex::FindInCache - Find this index /*{{{*/
588 // ---------------------------------------------------------------------
590 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
592 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
593 for (; File
.end() == false; File
++)
595 if (this->File
!= File
.FileName())
599 if (stat(File
.FileName(),&St
) != 0)
601 if (_config
->FindB("Debug::pkgCacheGen", false))
602 std::clog
<< "StatusIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
603 return pkgCache::PkgFileIterator(Cache
);
605 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
607 if (_config
->FindB("Debug::pkgCacheGen", false))
608 std::clog
<< "StatusIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
609 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
610 << ") doesn't match for " << File
.FileName() << std::endl
;
611 return pkgCache::PkgFileIterator(Cache
);
618 // StatusIndex::Exists - Check if the index is available /*{{{*/
619 // ---------------------------------------------------------------------
621 bool debStatusIndex::Exists() const
623 // Abort if the file does not exist.
628 // Index File types for Debian /*{{{*/
629 class debIFTypeSrc
: public pkgIndexFile::Type
633 debIFTypeSrc() {Label
= "Debian Source Index";};
635 class debIFTypePkg
: public pkgIndexFile::Type
639 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
641 return new debRecordParser(File
.FileName(),*File
.Cache());
643 debIFTypePkg() {Label
= "Debian Package Index";};
645 class debIFTypeTrans
: public debIFTypePkg
648 debIFTypeTrans() {Label
= "Debian Translation Index";};
650 class debIFTypeStatus
: public pkgIndexFile::Type
654 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
656 return new debRecordParser(File
.FileName(),*File
.Cache());
658 debIFTypeStatus() {Label
= "Debian dpkg status file";};
660 static debIFTypeSrc _apt_Src
;
661 static debIFTypePkg _apt_Pkg
;
662 static debIFTypeTrans _apt_Trans
;
663 static debIFTypeStatus _apt_Status
;
665 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
669 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
673 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
677 const pkgIndexFile::Type
*debStatusIndex::GetType() const