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
const &URI
, string
const &Dist
, string
const &Section
,
153 bool const &Trusted
, string
const &Arch
) :
154 pkgIndexFile(Trusted
), URI(URI
), Dist(Dist
), Section(Section
), Architecture(Arch
)
156 if (Architecture
== "native")
157 Architecture
= _config
->Find("APT::Architecture");
160 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
161 // ---------------------------------------------------------------------
162 /* This is a shorter version that is designed to be < 60 chars or so */
163 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
165 string Res
= ::URI::NoUserPassword(URI
) + ' ';
166 if (Dist
[Dist
.size() - 1] == '/')
172 Res
+= Dist
+ '/' + Section
;
175 Res
+= Ver
.ParentPkg().Name();
183 // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
184 // ---------------------------------------------------------------------
185 /* This should help the user find the index in the sources.list and
186 in the filesystem for problem solving */
187 string
debPackagesIndex::Describe(bool Short
) const
191 snprintf(S
,sizeof(S
),"%s",Info("Packages").c_str());
193 snprintf(S
,sizeof(S
),"%s (%s)",Info("Packages").c_str(),
194 IndexFile("Packages").c_str());
198 // PackagesIndex::Info - One liner describing the index URI /*{{{*/
199 // ---------------------------------------------------------------------
201 string
debPackagesIndex::Info(const char *Type
) const
203 string Info
= ::URI::NoUserPassword(URI
) + ' ';
204 if (Dist
[Dist
.size() - 1] == '/')
210 Info
+= Dist
+ '/' + Section
;
212 Info
+= Architecture
;
218 // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
219 // ---------------------------------------------------------------------
221 inline string
debPackagesIndex::IndexFile(const char *Type
) const
223 return _config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
225 string
debPackagesIndex::IndexURI(const char *Type
) const
228 if (Dist
[Dist
.size() - 1] == '/')
236 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
237 "/binary-" + Architecture
+ '/';
243 // PackagesIndex::Exists - Check if the index is available /*{{{*/
244 // ---------------------------------------------------------------------
246 bool debPackagesIndex::Exists() const
248 return FileExists(IndexFile("Packages"));
251 // PackagesIndex::Size - Return the size of the index /*{{{*/
252 // ---------------------------------------------------------------------
253 /* This is really only used for progress reporting. */
254 unsigned long debPackagesIndex::Size() const
257 if (stat(IndexFile("Packages").c_str(),&S
) != 0)
262 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
263 // ---------------------------------------------------------------------
265 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
267 string PackageFile
= IndexFile("Packages");
268 FileFd
Pkg(PackageFile
,FileFd::ReadOnly
);
269 debListParser
Parser(&Pkg
, Architecture
);
270 if (_error
->PendingError() == true)
271 return _error
->Error("Problem opening %s",PackageFile
.c_str());
273 Prog
->SubProgress(0,Info("Packages"));
275 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
276 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
278 // Store the IMS information
279 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
280 pkgCacheGenerator::Dynamic
<pkgCache::PkgFileIterator
> DynFile(File
);
282 if (fstat(Pkg
.Fd(),&St
) != 0)
283 return _error
->Errno("fstat","Failed to stat");
284 File
->Size
= St
.st_size
;
285 File
->mtime
= St
.st_mtime
;
287 if (Gen
.MergeList(Parser
) == false)
288 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
290 // Check the release file
291 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
292 if (FileExists(ReleaseFile
) == true)
294 FileFd
Rel(ReleaseFile
,FileFd::ReadOnly
);
295 if (_error
->PendingError() == true)
297 Parser
.LoadReleaseInfo(File
,Rel
,Section
);
303 // PackagesIndex::FindInCache - Find this index /*{{{*/
304 // ---------------------------------------------------------------------
306 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
308 string FileName
= IndexFile("Packages");
309 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
310 for (; File
.end() == false; File
++)
312 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
316 if (stat(File
.FileName(),&St
) != 0)
318 if (_config
->FindB("Debug::pkgCacheGen", false))
319 std::clog
<< "PackagesIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
320 return pkgCache::PkgFileIterator(Cache
);
322 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
324 if (_config
->FindB("Debug::pkgCacheGen", false))
325 std::clog
<< "PackagesIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
326 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
327 << ") doesn't match for " << File
.FileName() << std::endl
;
328 return pkgCache::PkgFileIterator(Cache
);
337 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
338 // ---------------------------------------------------------------------
340 debTranslationsIndex::debTranslationsIndex(string URI
,string Dist
,string Section
,
341 char const * const Translation
) :
342 pkgIndexFile(true), URI(URI
), Dist(Dist
), Section(Section
),
343 Language(Translation
)
346 // TranslationIndex::Trans* - Return the URI to the translation files /*{{{*/
347 // ---------------------------------------------------------------------
349 inline string
debTranslationsIndex::IndexFile(const char *Type
) const
351 return _config
->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type
));
353 string
debTranslationsIndex::IndexURI(const char *Type
) const
356 if (Dist
[Dist
.size() - 1] == '/')
364 Res
= URI
+ "dists/" + Dist
+ '/' + Section
+
365 "/i18n/Translation-";
371 // TranslationsIndex::GetIndexes - Fetch the index files /*{{{*/
372 // ---------------------------------------------------------------------
374 bool debTranslationsIndex::GetIndexes(pkgAcquire
*Owner
) const
376 if (TranslationsAvailable()) {
377 string
const TranslationFile
= string("Translation-").append(Language
);
378 new pkgAcqIndexTrans(Owner
, IndexURI(Language
),
379 Info(TranslationFile
.c_str()),
386 // TranslationsIndex::Describe - Give a descriptive path to the index /*{{{*/
387 // ---------------------------------------------------------------------
388 /* This should help the user find the index in the sources.list and
389 in the filesystem for problem solving */
390 string
debTranslationsIndex::Describe(bool Short
) const
394 snprintf(S
,sizeof(S
),"%s",Info(TranslationFile().c_str()).c_str());
396 snprintf(S
,sizeof(S
),"%s (%s)",Info(TranslationFile().c_str()).c_str(),
397 IndexFile(Language
).c_str());
401 // TranslationsIndex::Info - One liner describing the index URI /*{{{*/
402 // ---------------------------------------------------------------------
404 string
debTranslationsIndex::Info(const char *Type
) const
406 string Info
= ::URI::NoUserPassword(URI
) + ' ';
407 if (Dist
[Dist
.size() - 1] == '/')
413 Info
+= Dist
+ '/' + Section
;
419 bool debTranslationsIndex::HasPackages() const /*{{{*/
421 if(!TranslationsAvailable())
424 return FileExists(IndexFile(Language
));
427 // TranslationsIndex::Exists - Check if the index is available /*{{{*/
428 // ---------------------------------------------------------------------
430 bool debTranslationsIndex::Exists() const
432 return FileExists(IndexFile(Language
));
435 // TranslationsIndex::Size - Return the size of the index /*{{{*/
436 // ---------------------------------------------------------------------
437 /* This is really only used for progress reporting. */
438 unsigned long debTranslationsIndex::Size() const
441 if (stat(IndexFile(Language
).c_str(),&S
) != 0)
446 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
447 // ---------------------------------------------------------------------
449 bool debTranslationsIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
451 // Check the translation file, if in use
452 string TranslationFile
= IndexFile(Language
);
453 if (TranslationsAvailable() && FileExists(TranslationFile
))
455 FileFd
Trans(TranslationFile
,FileFd::ReadOnly
);
456 debListParser
TransParser(&Trans
);
457 if (_error
->PendingError() == true)
461 Prog
->SubProgress(0, Info(TranslationFile
.c_str()));
462 if (Gen
.SelectFile(TranslationFile
,string(),*this) == false)
463 return _error
->Error("Problem with SelectFile %s",TranslationFile
.c_str());
465 // Store the IMS information
466 pkgCache::PkgFileIterator TransFile
= Gen
.GetCurFile();
468 if (fstat(Trans
.Fd(),&TransSt
) != 0)
469 return _error
->Errno("fstat","Failed to stat");
470 TransFile
->Size
= TransSt
.st_size
;
471 TransFile
->mtime
= TransSt
.st_mtime
;
473 if (Gen
.MergeList(TransParser
) == false)
474 return _error
->Error("Problem with MergeList %s",TranslationFile
.c_str());
480 // TranslationsIndex::FindInCache - Find this index /*{{{*/
481 // ---------------------------------------------------------------------
483 pkgCache::PkgFileIterator
debTranslationsIndex::FindInCache(pkgCache
&Cache
) const
485 string FileName
= IndexFile(Language
);
487 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
488 for (; File
.end() == false; File
++)
490 if (FileName
!= File
.FileName())
494 if (stat(File
.FileName(),&St
) != 0)
496 if (_config
->FindB("Debug::pkgCacheGen", false))
497 std::clog
<< "TranslationIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
498 return pkgCache::PkgFileIterator(Cache
);
500 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
502 if (_config
->FindB("Debug::pkgCacheGen", false))
503 std::clog
<< "TranslationIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
504 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
505 << ") doesn't match for " << File
.FileName() << std::endl
;
506 return pkgCache::PkgFileIterator(Cache
);
513 // StatusIndex::debStatusIndex - Constructor /*{{{*/
514 // ---------------------------------------------------------------------
516 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
520 // StatusIndex::Size - Return the size of the index /*{{{*/
521 // ---------------------------------------------------------------------
523 unsigned long debStatusIndex::Size() const
526 if (stat(File
.c_str(),&S
) != 0)
531 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
532 // ---------------------------------------------------------------------
534 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
536 FileFd
Pkg(File
,FileFd::ReadOnly
);
537 if (_error
->PendingError() == true)
539 debListParser
Parser(&Pkg
);
540 if (_error
->PendingError() == true)
544 Prog
->SubProgress(0,File
);
545 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
546 return _error
->Error("Problem with SelectFile %s",File
.c_str());
548 // Store the IMS information
549 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
551 if (fstat(Pkg
.Fd(),&St
) != 0)
552 return _error
->Errno("fstat","Failed to stat");
553 CFile
->Size
= St
.st_size
;
554 CFile
->mtime
= St
.st_mtime
;
555 CFile
->Archive
= Gen
.WriteUniqString("now");
557 if (Gen
.MergeList(Parser
) == false)
558 return _error
->Error("Problem with MergeList %s",File
.c_str());
562 // StatusIndex::FindInCache - Find this index /*{{{*/
563 // ---------------------------------------------------------------------
565 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
567 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
568 for (; File
.end() == false; File
++)
570 if (this->File
!= File
.FileName())
574 if (stat(File
.FileName(),&St
) != 0)
576 if (_config
->FindB("Debug::pkgCacheGen", false))
577 std::clog
<< "StatusIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
578 return pkgCache::PkgFileIterator(Cache
);
580 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
582 if (_config
->FindB("Debug::pkgCacheGen", false))
583 std::clog
<< "StatusIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
584 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
585 << ") doesn't match for " << File
.FileName() << std::endl
;
586 return pkgCache::PkgFileIterator(Cache
);
593 // StatusIndex::Exists - Check if the index is available /*{{{*/
594 // ---------------------------------------------------------------------
596 bool debStatusIndex::Exists() const
598 // Abort if the file does not exist.
603 // Index File types for Debian /*{{{*/
604 class debIFTypeSrc
: public pkgIndexFile::Type
608 debIFTypeSrc() {Label
= "Debian Source Index";};
610 class debIFTypePkg
: public pkgIndexFile::Type
614 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
616 return new debRecordParser(File
.FileName(),*File
.Cache());
618 debIFTypePkg() {Label
= "Debian Package Index";};
620 class debIFTypeTrans
: public debIFTypePkg
623 debIFTypeTrans() {Label
= "Debian Translation Index";};
625 class debIFTypeStatus
: public pkgIndexFile::Type
629 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
631 return new debRecordParser(File
.FileName(),*File
.Cache());
633 debIFTypeStatus() {Label
= "Debian dpkg status file";};
635 static debIFTypeSrc _apt_Src
;
636 static debIFTypePkg _apt_Pkg
;
637 static debIFTypeTrans _apt_Trans
;
638 static debIFTypeStatus _apt_Status
;
640 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
644 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
648 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
652 const pkgIndexFile::Type
*debStatusIndex::GetType() const