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
153 if (stat(IndexFile("Sources").c_str(),&S
) != 0)
159 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
160 // ---------------------------------------------------------------------
162 debPackagesIndex::debPackagesIndex(string URI
,string Dist
,string Section
,bool Trusted
) :
163 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
)
167 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
168 // ---------------------------------------------------------------------
169 /* This is a shorter version that is designed to be < 60 chars or so */
170 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
172 string Res
= ::URI::NoUserPassword(URI
) + ' ';
173 if (Dist
[Dist
.size() - 1] == '/')
179 Res
+= Dist
+ '/' + Section
;
182 Res
+= Ver
.ParentPkg().Name();
188 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
189 // ---------------------------------------------------------------------
190 /* This should help the user find the index in the sources.list and
191 in the filesystem for problem solving */
192 string
debPackagesIndex::Describe(bool Short
) const
196 snprintf(S
,sizeof(S
),"%s",Info("Packages").c_str());
198 snprintf(S
,sizeof(S
),"%s (%s)",Info("Packages").c_str(),
199 IndexFile("Packages").c_str());
203 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
204 // ---------------------------------------------------------------------
206 string
debPackagesIndex::Info(const char *Type
) const
208 string Info
= ::URI::NoUserPassword(URI
) + ' ';
209 if (Dist
[Dist
.size() - 1] == '/')
215 Info
+= Dist
+ '/' + Section
;
221 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
222 // ---------------------------------------------------------------------
224 inline string
debPackagesIndex::IndexFile(const char *Type
) const
226 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
227 string sgzip
= s
+ ".gz";
228 if (!FileExists(s
) && FileExists(sgzip
))
233 string
debPackagesIndex::IndexURI(const char *Type
) const
236 if (Dist
[Dist
.size() - 1] == '/')
244 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
245 "/binary-" + _config
->Find("APT::Architecture") + '/';
251 // PackagesIndex::Exists - Check if the index is available /*{{{*/
252 // ---------------------------------------------------------------------
254 bool debPackagesIndex::Exists() const
256 return FileExists(IndexFile("Packages"));
259 // PackagesIndex::Size - Return the size of the index /*{{{*/
260 // ---------------------------------------------------------------------
261 /* This is really only used for progress reporting. */
262 unsigned long debPackagesIndex::Size() const
265 if (stat(IndexFile("Packages").c_str(),&S
) != 0)
270 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
271 // ---------------------------------------------------------------------
273 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
&Prog
) const
275 string PackageFile
= IndexFile("Packages");
276 FileFd
Pkg(PackageFile
,FileFd::ReadOnly
);
277 debListParser
Parser(&Pkg
);
278 if (_error
->PendingError() == true)
279 return _error
->Error("Problem opening %s",PackageFile
.c_str());
281 Prog
.SubProgress(0,Info("Packages"));
283 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
284 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
286 // Store the IMS information
287 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
289 if (fstat(Pkg
.Fd(),&St
) != 0)
290 return _error
->Errno("fstat","Failed to stat");
291 File
->Size
= St
.st_size
;
292 File
->mtime
= St
.st_mtime
;
294 if (Gen
.MergeList(Parser
) == false)
295 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
297 // Check the release file
298 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
299 if (FileExists(ReleaseFile
) == true)
301 FileFd
Rel(ReleaseFile
,FileFd::ReadOnly
);
302 if (_error
->PendingError() == true)
304 Parser
.LoadReleaseInfo(File
,Rel
,Section
);
310 // PackagesIndex::FindInCache - Find this index /*{{{*/
311 // ---------------------------------------------------------------------
313 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
315 string FileName
= IndexFile("Packages");
316 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
317 for (; File
.end() == false; File
++)
319 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
323 if (stat(File
.FileName(),&St
) != 0)
325 if (_config
->FindB("Debug::pkgCacheGen", false))
326 std::clog
<< "PackagesIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
327 return pkgCache::PkgFileIterator(Cache
);
329 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
331 if (_config
->FindB("Debug::pkgCacheGen", false))
332 std::clog
<< "PackagesIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
333 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
334 << ") doesn't match for " << File
.FileName() << std::endl
;
335 return pkgCache::PkgFileIterator(Cache
);
344 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
345 // ---------------------------------------------------------------------
347 debTranslationsIndex::debTranslationsIndex(string URI
,string Dist
,string Section
,
348 char const * const Translation
) :
349 pkgIndexFile(true), URI(URI
), Dist(Dist
), Section(Section
),
350 Language(Translation
)
353 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
354 // ---------------------------------------------------------------------
356 inline string
debTranslationsIndex::IndexFile(const char *Type
) const
358 string s
=_config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
359 string sgzip
= s
+ ".gz";
360 if (!FileExists(s
) && FileExists(sgzip
))
365 string
debTranslationsIndex::IndexURI(const char *Type
) const
368 if (Dist
[Dist
.size() - 1] == '/')
376 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
377 "/i18n/Translation-";
383 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
384 // ---------------------------------------------------------------------
386 bool debTranslationsIndex::GetIndexes(pkgAcquire
*Owner
) const
388 if (TranslationsAvailable()) {
389 string
const TranslationFile
= string("Translation-").append(Language
);
390 new pkgAcqIndexTrans(Owner
, IndexURI(Language
),
391 Info(TranslationFile
.c_str()),
398 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
399 // ---------------------------------------------------------------------
400 /* This should help the user find the index in the sources.list and
401 in the filesystem for problem solving */
402 string
debTranslationsIndex::Describe(bool Short
) const
406 snprintf(S
,sizeof(S
),"%s",Info(TranslationFile().c_str()).c_str());
408 snprintf(S
,sizeof(S
),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
409 IndexFile(Language
).c_str());
413 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
414 // ---------------------------------------------------------------------
416 string
debTranslationsIndex::Info(const char *Type
) const
418 string Info
= ::URI::NoUserPassword(URI
) + ' ';
419 if (Dist
[Dist
.size() - 1] == '/')
425 Info
+= Dist
+ '/' + Section
;
431 bool debTranslationsIndex::HasPackages() const /*{{{*/
433 if(!TranslationsAvailable())
436 return FileExists(IndexFile(Language
));
439 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
440 // ---------------------------------------------------------------------
442 bool debTranslationsIndex::Exists() const
444 return FileExists(IndexFile(Language
));
447 // TranslationsIndex::Size - Return the size of the index /*{{{*/
448 // ---------------------------------------------------------------------
449 /* This is really only used for progress reporting. */
450 unsigned long debTranslationsIndex::Size() const
453 if (stat(IndexFile(Language
).c_str(),&S
) != 0)
458 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
459 // ---------------------------------------------------------------------
461 bool debTranslationsIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
&Prog
) const
463 // Check the translation file, if in use
464 string TranslationFile
= IndexFile(Language
);
465 if (TranslationsAvailable() && FileExists(TranslationFile
))
467 FileFd
Trans(TranslationFile
,FileFd::ReadOnly
);
468 debListParser
TransParser(&Trans
);
469 if (_error
->PendingError() == true)
472 Prog
.SubProgress(0, Info(TranslationFile
.c_str()));
473 if (Gen
.SelectFile(TranslationFile
,string(),*this) == false)
474 return _error
->Error("Problem with SelectFile %s",TranslationFile
.c_str());
476 // Store the IMS information
477 pkgCache::PkgFileIterator TransFile
= Gen
.GetCurFile();
479 if (fstat(Trans
.Fd(),&TransSt
) != 0)
480 return _error
->Errno("fstat","Failed to stat");
481 TransFile
->Size
= TransSt
.st_size
;
482 TransFile
->mtime
= TransSt
.st_mtime
;
484 if (Gen
.MergeList(TransParser
) == false)
485 return _error
->Error("Problem with MergeList %s",TranslationFile
.c_str());
491 // TranslationsIndex::FindInCache - Find this index /*{{{*/
492 // ---------------------------------------------------------------------
494 pkgCache::PkgFileIterator
debTranslationsIndex::FindInCache(pkgCache
&Cache
) const
496 string FileName
= IndexFile(Language
);
498 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
499 for (; File
.end() == false; File
++)
501 if (FileName
!= File
.FileName())
505 if (stat(File
.FileName(),&St
) != 0)
507 if (_config
->FindB("Debug::pkgCacheGen", false))
508 std::clog
<< "TranslationIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
509 return pkgCache::PkgFileIterator(Cache
);
511 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
513 if (_config
->FindB("Debug::pkgCacheGen", false))
514 std::clog
<< "TranslationIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
515 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
516 << ") doesn't match for " << File
.FileName() << std::endl
;
517 return pkgCache::PkgFileIterator(Cache
);
524 // StatusIndex::debStatusIndex - Constructor /*{{{*/
525 // ---------------------------------------------------------------------
527 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
531 // StatusIndex::Size - Return the size of the index /*{{{*/
532 // ---------------------------------------------------------------------
534 unsigned long debStatusIndex::Size() const
537 if (stat(File
.c_str(),&S
) != 0)
542 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
543 // ---------------------------------------------------------------------
545 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
&Prog
) const
547 FileFd
Pkg(File
,FileFd::ReadOnly
);
548 if (_error
->PendingError() == true)
550 debListParser
Parser(&Pkg
);
551 if (_error
->PendingError() == true)
554 Prog
.SubProgress(0,File
);
555 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
556 return _error
->Error("Problem with SelectFile %s",File
.c_str());
558 // Store the IMS information
559 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
561 if (fstat(Pkg
.Fd(),&St
) != 0)
562 return _error
->Errno("fstat","Failed to stat");
563 CFile
->Size
= St
.st_size
;
564 CFile
->mtime
= St
.st_mtime
;
565 CFile
->Archive
= Gen
.WriteUniqString("now");
567 if (Gen
.MergeList(Parser
) == false)
568 return _error
->Error("Problem with MergeList %s",File
.c_str());
572 // StatusIndex::FindInCache - Find this index /*{{{*/
573 // ---------------------------------------------------------------------
575 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
577 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
578 for (; File
.end() == false; File
++)
580 if (this->File
!= File
.FileName())
584 if (stat(File
.FileName(),&St
) != 0)
586 if (_config
->FindB("Debug::pkgCacheGen", false))
587 std::clog
<< "StatusIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
588 return pkgCache::PkgFileIterator(Cache
);
590 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
592 if (_config
->FindB("Debug::pkgCacheGen", false))
593 std::clog
<< "StatusIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
594 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
595 << ") doesn't match for " << File
.FileName() << std::endl
;
596 return pkgCache::PkgFileIterator(Cache
);
603 // StatusIndex::Exists - Check if the index is available /*{{{*/
604 // ---------------------------------------------------------------------
606 bool debStatusIndex::Exists() const
608 // Abort if the file does not exist.
613 // Index File types for Debian /*{{{*/
614 class debIFTypeSrc
: public pkgIndexFile::Type
618 debIFTypeSrc() {Label
= "Debian Source Index";};
620 class debIFTypePkg
: public pkgIndexFile::Type
624 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
626 return new debRecordParser(File
.FileName(),*File
.Cache());
628 debIFTypePkg() {Label
= "Debian Package Index";};
630 class debIFTypeTrans
: public debIFTypePkg
633 debIFTypeTrans() {Label
= "Debian Translation Index";};
635 class debIFTypeStatus
: public pkgIndexFile::Type
639 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
641 return new debRecordParser(File
.FileName(),*File
.Cache());
643 debIFTypeStatus() {Label
= "Debian dpkg status file";};
645 static debIFTypeSrc _apt_Src
;
646 static debIFTypePkg _apt_Pkg
;
647 static debIFTypeTrans _apt_Trans
;
648 static debIFTypeStatus _apt_Status
;
650 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
654 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
658 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
662 const pkgIndexFile::Type
*debStatusIndex::GetType() const