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
= URItoFileName(IndexURI("Sources"));
67 return new debSrcRecordParser(_config
->FindDir("Dir::State::lists") +
71 // SourcesIndex::Describe - Give a descriptive path to the index /*{{{*/
72 // ---------------------------------------------------------------------
74 string
debSourcesIndex::Describe(bool Short
) const
78 snprintf(S
,sizeof(S
),"%s",Info("Sources").c_str());
80 snprintf(S
,sizeof(S
),"%s (%s)",Info("Sources").c_str(),
81 IndexFile("Sources").c_str());
86 // SourcesIndex::Info - One liner describing the index URI /*{{{*/
87 // ---------------------------------------------------------------------
89 string
debSourcesIndex::Info(const char *Type
) const
91 string Info
= ::URI::NoUserPassword(URI
) + ' ';
92 if (Dist
[Dist
.size() - 1] == '/')
98 Info
+= Dist
+ '/' + Section
;
104 // SourcesIndex::Index* - Return the URI to the index files /*{{{*/
105 // ---------------------------------------------------------------------
107 inline string
debSourcesIndex::IndexFile(const char *Type
) const
109 return URItoFileName(IndexURI(Type
));
111 string
debSourcesIndex::IndexURI(const char *Type
) const
114 if (Dist
[Dist
.size() - 1] == '/')
122 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
129 // SourcesIndex::Exists - Check if the index is available /*{{{*/
130 // ---------------------------------------------------------------------
132 bool debSourcesIndex::Exists() const
134 return FileExists(IndexFile("Sources"));
137 // SourcesIndex::Size - Return the size of the index /*{{{*/
138 // ---------------------------------------------------------------------
140 unsigned long debSourcesIndex::Size() const
143 if (stat(IndexFile("Sources").c_str(),&S
) != 0)
149 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
150 // ---------------------------------------------------------------------
152 debPackagesIndex::debPackagesIndex(string URI
,string Dist
,string Section
,bool Trusted
) :
153 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
)
157 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
158 // ---------------------------------------------------------------------
159 /* This is a shorter version that is designed to be < 60 chars or so */
160 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
162 string Res
= ::URI::NoUserPassword(URI
) + ' ';
163 if (Dist
[Dist
.size() - 1] == '/')
169 Res
+= Dist
+ '/' + Section
;
172 Res
+= Ver
.ParentPkg().Name();
178 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
179 // ---------------------------------------------------------------------
180 /* This should help the user find the index in the sources.list and
181 in the filesystem for problem solving */
182 string
debPackagesIndex::Describe(bool Short
) const
186 snprintf(S
,sizeof(S
),"%s",Info("Packages").c_str());
188 snprintf(S
,sizeof(S
),"%s (%s)",Info("Packages").c_str(),
189 IndexFile("Packages").c_str());
193 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
194 // ---------------------------------------------------------------------
196 string
debPackagesIndex::Info(const char *Type
) const
198 string Info
= ::URI::NoUserPassword(URI
) + ' ';
199 if (Dist
[Dist
.size() - 1] == '/')
205 Info
+= Dist
+ '/' + Section
;
211 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
212 // ---------------------------------------------------------------------
214 inline string
debPackagesIndex::IndexFile(const char *Type
) const
216 return _config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
218 string
debPackagesIndex::IndexURI(const char *Type
) const
221 if (Dist
[Dist
.size() - 1] == '/')
229 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
230 "/binary-" + _config
->Find("APT::Architecture") + '/';
236 // PackagesIndex::Exists - Check if the index is available /*{{{*/
237 // ---------------------------------------------------------------------
239 bool debPackagesIndex::Exists() const
241 return FileExists(IndexFile("Packages"));
244 // PackagesIndex::Size - Return the size of the index /*{{{*/
245 // ---------------------------------------------------------------------
246 /* This is really only used for progress reporting. */
247 unsigned long debPackagesIndex::Size() const
250 if (stat(IndexFile("Packages").c_str(),&S
) != 0)
255 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
256 // ---------------------------------------------------------------------
258 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
&Prog
) const
260 string PackageFile
= IndexFile("Packages");
261 FileFd
Pkg(PackageFile
,FileFd::ReadOnly
);
262 debListParser
Parser(&Pkg
);
263 if (_error
->PendingError() == true)
264 return _error
->Error("Problem opening %s",PackageFile
.c_str());
266 Prog
.SubProgress(0,Info("Packages"));
268 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
269 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
271 // Store the IMS information
272 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
274 if (fstat(Pkg
.Fd(),&St
) != 0)
275 return _error
->Errno("fstat","Failed to stat");
276 File
->Size
= St
.st_size
;
277 File
->mtime
= St
.st_mtime
;
279 if (Gen
.MergeList(Parser
) == false)
280 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
282 // Check the release file
283 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
284 if (FileExists(ReleaseFile
) == true)
286 FileFd
Rel(ReleaseFile
,FileFd::ReadOnly
);
287 if (_error
->PendingError() == true)
289 Parser
.LoadReleaseInfo(File
,Rel
,Section
);
295 // PackagesIndex::FindInCache - Find this index /*{{{*/
296 // ---------------------------------------------------------------------
298 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
300 string FileName
= IndexFile("Packages");
301 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
302 for (; File
.end() == false; File
++)
304 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
308 if (stat(File
.FileName(),&St
) != 0)
310 if (_config
->FindB("Debug::pkgCacheGen", false))
311 std::clog
<< "PackagesIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
312 return pkgCache::PkgFileIterator(Cache
);
314 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
316 if (_config
->FindB("Debug::pkgCacheGen", false))
317 std::clog
<< "PackagesIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
318 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
319 << ") doesn't match for " << File
.FileName() << std::endl
;
320 return pkgCache::PkgFileIterator(Cache
);
329 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
330 // ---------------------------------------------------------------------
332 debTranslationsIndex::debTranslationsIndex(string URI
,string Dist
,string Section
,
333 char const * const Translation
) :
334 pkgIndexFile(true), URI(URI
), Dist(Dist
), Section(Section
),
335 Language(Translation
)
338 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
339 // ---------------------------------------------------------------------
341 inline string
debTranslationsIndex::IndexFile(const char *Type
) const
343 return _config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
345 string
debTranslationsIndex::IndexURI(const char *Type
) const
348 if (Dist
[Dist
.size() - 1] == '/')
356 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
357 "/i18n/Translation-";
363 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
364 // ---------------------------------------------------------------------
366 bool debTranslationsIndex::GetIndexes(pkgAcquire
*Owner
) const
368 if (TranslationsAvailable()) {
369 string
const TranslationFile
= string("Translation-").append(Language
);
370 new pkgAcqIndexTrans(Owner
, IndexURI(Language
),
371 Info(TranslationFile
.c_str()),
378 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
379 // ---------------------------------------------------------------------
380 /* This should help the user find the index in the sources.list and
381 in the filesystem for problem solving */
382 string
debTranslationsIndex::Describe(bool Short
) const
386 snprintf(S
,sizeof(S
),"%s",Info(TranslationFile().c_str()).c_str());
388 snprintf(S
,sizeof(S
),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
389 IndexFile(Language
).c_str());
393 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
394 // ---------------------------------------------------------------------
396 string
debTranslationsIndex::Info(const char *Type
) const
398 string Info
= ::URI::NoUserPassword(URI
) + ' ';
399 if (Dist
[Dist
.size() - 1] == '/')
405 Info
+= Dist
+ '/' + Section
;
411 bool debTranslationsIndex::HasPackages() const /*{{{*/
413 if(!TranslationsAvailable())
416 return FileExists(IndexFile(Language
));
419 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
420 // ---------------------------------------------------------------------
422 bool debTranslationsIndex::Exists() const
424 return FileExists(IndexFile(Language
));
427 // TranslationsIndex::Size - Return the size of the index /*{{{*/
428 // ---------------------------------------------------------------------
429 /* This is really only used for progress reporting. */
430 unsigned long debTranslationsIndex::Size() const
433 if (stat(IndexFile(Language
).c_str(),&S
) != 0)
438 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
439 // ---------------------------------------------------------------------
441 bool debTranslationsIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
&Prog
) const
443 // Check the translation file, if in use
444 string TranslationFile
= IndexFile(Language
);
445 if (TranslationsAvailable() && FileExists(TranslationFile
))
447 FileFd
Trans(TranslationFile
,FileFd::ReadOnly
);
448 debListParser
TransParser(&Trans
);
449 if (_error
->PendingError() == true)
452 Prog
.SubProgress(0, Info(TranslationFile
.c_str()));
453 if (Gen
.SelectFile(TranslationFile
,string(),*this) == false)
454 return _error
->Error("Problem with SelectFile %s",TranslationFile
.c_str());
456 // Store the IMS information
457 pkgCache::PkgFileIterator TransFile
= Gen
.GetCurFile();
459 if (fstat(Trans
.Fd(),&TransSt
) != 0)
460 return _error
->Errno("fstat","Failed to stat");
461 TransFile
->Size
= TransSt
.st_size
;
462 TransFile
->mtime
= TransSt
.st_mtime
;
464 if (Gen
.MergeList(TransParser
) == false)
465 return _error
->Error("Problem with MergeList %s",TranslationFile
.c_str());
471 // TranslationsIndex::FindInCache - Find this index /*{{{*/
472 // ---------------------------------------------------------------------
474 pkgCache::PkgFileIterator
debTranslationsIndex::FindInCache(pkgCache
&Cache
) const
476 string FileName
= IndexFile(Language
);
478 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
479 for (; File
.end() == false; File
++)
481 if (FileName
!= File
.FileName())
485 if (stat(File
.FileName(),&St
) != 0)
487 if (_config
->FindB("Debug::pkgCacheGen", false))
488 std::clog
<< "TranslationIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
489 return pkgCache::PkgFileIterator(Cache
);
491 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
493 if (_config
->FindB("Debug::pkgCacheGen", false))
494 std::clog
<< "TranslationIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
495 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
496 << ") doesn't match for " << File
.FileName() << std::endl
;
497 return pkgCache::PkgFileIterator(Cache
);
504 // StatusIndex::debStatusIndex - Constructor /*{{{*/
505 // ---------------------------------------------------------------------
507 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
511 // StatusIndex::Size - Return the size of the index /*{{{*/
512 // ---------------------------------------------------------------------
514 unsigned long debStatusIndex::Size() const
517 if (stat(File
.c_str(),&S
) != 0)
522 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
523 // ---------------------------------------------------------------------
525 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
&Prog
) const
527 FileFd
Pkg(File
,FileFd::ReadOnly
);
528 if (_error
->PendingError() == true)
530 debListParser
Parser(&Pkg
);
531 if (_error
->PendingError() == true)
534 Prog
.SubProgress(0,File
);
535 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
536 return _error
->Error("Problem with SelectFile %s",File
.c_str());
538 // Store the IMS information
539 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
541 if (fstat(Pkg
.Fd(),&St
) != 0)
542 return _error
->Errno("fstat","Failed to stat");
543 CFile
->Size
= St
.st_size
;
544 CFile
->mtime
= St
.st_mtime
;
545 CFile
->Archive
= Gen
.WriteUniqString("now");
547 if (Gen
.MergeList(Parser
) == false)
548 return _error
->Error("Problem with MergeList %s",File
.c_str());
552 // StatusIndex::FindInCache - Find this index /*{{{*/
553 // ---------------------------------------------------------------------
555 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
557 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
558 for (; File
.end() == false; File
++)
560 if (this->File
!= File
.FileName())
564 if (stat(File
.FileName(),&St
) != 0)
566 if (_config
->FindB("Debug::pkgCacheGen", false))
567 std::clog
<< "StatusIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
568 return pkgCache::PkgFileIterator(Cache
);
570 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
572 if (_config
->FindB("Debug::pkgCacheGen", false))
573 std::clog
<< "StatusIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
574 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
575 << ") doesn't match for " << File
.FileName() << std::endl
;
576 return pkgCache::PkgFileIterator(Cache
);
583 // StatusIndex::Exists - Check if the index is available /*{{{*/
584 // ---------------------------------------------------------------------
586 bool debStatusIndex::Exists() const
588 // Abort if the file does not exist.
593 // Index File types for Debian /*{{{*/
594 class debIFTypeSrc
: public pkgIndexFile::Type
598 debIFTypeSrc() {Label
= "Debian Source Index";};
600 class debIFTypePkg
: public pkgIndexFile::Type
604 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
606 return new debRecordParser(File
.FileName(),*File
.Cache());
608 debIFTypePkg() {Label
= "Debian Package Index";};
610 class debIFTypeTrans
: public debIFTypePkg
613 debIFTypeTrans() {Label
= "Debian Translation Index";};
615 class debIFTypeStatus
: public pkgIndexFile::Type
619 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
621 return new debRecordParser(File
.FileName(),*File
.Cache());
623 debIFTypeStatus() {Label
= "Debian dpkg status file";};
625 static debIFTypeSrc _apt_Src
;
626 static debIFTypePkg _apt_Pkg
;
627 static debIFTypeTrans _apt_Trans
;
628 static debIFTypeStatus _apt_Status
;
630 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
634 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
638 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
642 const pkgIndexFile::Type
*debStatusIndex::GetType() const