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/configuration.h>
19 #include <apt-pkg/progress.h>
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/strutl.h>
22 #include <apt-pkg/acquire-item.h>
23 #include <apt-pkg/debmetaindex.h>
24 #include <apt-pkg/gpgv.h>
25 #include <apt-pkg/fileutl.h>
26 #include <apt-pkg/indexfile.h>
27 #include <apt-pkg/mmap.h>
28 #include <apt-pkg/pkgcache.h>
29 #include <apt-pkg/cacheiterators.h>
30 #include <apt-pkg/pkgcachegen.h>
31 #include <apt-pkg/pkgrecords.h>
32 #include <apt-pkg/srcrecords.h>
33 #include <apt-pkg/sptr.h>
44 // SourcesIndex::debSourcesIndex - Constructor /*{{{*/
45 // ---------------------------------------------------------------------
47 debSourcesIndex::debSourcesIndex(IndexTarget
const &Target
,bool const Trusted
) :
48 pkgIndexTargetFile(Target
, Trusted
)
52 // SourcesIndex::SourceInfo - Short 1 liner describing a source /*{{{*/
53 // ---------------------------------------------------------------------
54 /* The result looks like:
55 http://foo/debian/ stable/main src 1.1.1 (dsc) */
56 string
debSourcesIndex::SourceInfo(pkgSrcRecords::Parser
const &Record
,
57 pkgSrcRecords::File
const &File
) const
59 string Res
= Target
.Description
;
60 Res
.erase(Target
.Description
.rfind(' '));
63 Res
+= Record
.Package();
65 Res
+= Record
.Version();
66 if (File
.Type
.empty() == false)
67 Res
+= " (" + File
.Type
+ ")";
71 // SourcesIndex::CreateSrcParser - Get a parser for the source files /*{{{*/
72 // ---------------------------------------------------------------------
74 pkgSrcRecords::Parser
*debSourcesIndex::CreateSrcParser() const
76 string
const SourcesURI
= IndexFileName();
77 if (FileExists(SourcesURI
))
78 return new debSrcRecordParser(SourcesURI
, this);
83 // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
84 // ---------------------------------------------------------------------
86 debPackagesIndex::debPackagesIndex(IndexTarget
const &Target
, bool const Trusted
) :
87 pkgIndexTargetFile(Target
, Trusted
)
91 // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
92 // ---------------------------------------------------------------------
93 /* This is a shorter version that is designed to be < 60 chars or so */
94 string
debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver
) const
96 std::string
const Dist
= Target
.Option("RELEASE");
97 string Res
= Target
.Option("SITE") + " " + Dist
;
98 std::string
const Component
= Target
.Option("COMPONENT");
99 if (Component
.empty() == false)
100 Res
+= "/" + Component
;
103 Res
+= Ver
.ParentPkg().Name();
105 if (Dist
.empty() == false && Dist
[Dist
.size() - 1] != '/')
106 Res
.append(Ver
.Arch()).append(" ");
111 // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
112 // ---------------------------------------------------------------------
114 bool debPackagesIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
116 string
const PackageFile
= IndexFileName();
117 FileFd
Pkg(PackageFile
,FileFd::ReadOnly
, FileFd::Extension
);
118 debListParser
Parser(&Pkg
, Target
.Option("ARCHITECTURE"));
120 if (_error
->PendingError() == true)
121 return _error
->Error("Problem opening %s",PackageFile
.c_str());
123 Prog
->SubProgress(0, Target
.Description
);
126 std::string
const URI
= Target
.Option("REPO_URI");
127 std::string Dist
= Target
.Option("RELEASE");
131 if (Gen
.SelectFile(PackageFile
,Tmp
.Host
,*this) == false)
132 return _error
->Error("Problem with SelectFile %s",PackageFile
.c_str());
134 // Store the IMS information
135 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
136 pkgCacheGenerator::Dynamic
<pkgCache::PkgFileIterator
> DynFile(File
);
137 File
->Size
= Pkg
.FileSize();
138 File
->mtime
= Pkg
.ModificationTime();
140 if (Gen
.MergeList(Parser
) == false)
141 return _error
->Error("Problem with MergeList %s",PackageFile
.c_str());
143 // Check the release file
144 string ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("InRelease");
145 bool releaseExists
= false;
146 if (FileExists(ReleaseFile
) == true)
147 releaseExists
= true;
149 ReleaseFile
= debReleaseIndex(URI
,Dist
).MetaIndexFile("Release");
151 if (releaseExists
== true || FileExists(ReleaseFile
) == true)
154 // Beware: The 'Release' file might be clearsigned in case the
155 // signature for an 'InRelease' file couldn't be checked
156 if (OpenMaybeClearSignedFile(ReleaseFile
, Rel
) == false)
159 if (_error
->PendingError() == true)
161 Parser
.LoadReleaseInfo(File
, Rel
, Target
.Option("COMPONENT"));
167 // PackagesIndex::FindInCache - Find this index /*{{{*/
168 // ---------------------------------------------------------------------
170 pkgCache::PkgFileIterator
debPackagesIndex::FindInCache(pkgCache
&Cache
) const
172 string
const FileName
= IndexFileName();
173 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
174 for (; File
.end() == false; ++File
)
176 if (File
.FileName() == NULL
|| FileName
!= File
.FileName())
180 if (stat(File
.FileName(),&St
) != 0)
182 if (_config
->FindB("Debug::pkgCacheGen", false))
183 std::clog
<< "PackagesIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
184 return pkgCache::PkgFileIterator(Cache
);
186 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
188 if (_config
->FindB("Debug::pkgCacheGen", false))
189 std::clog
<< "PackagesIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
190 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
191 << ") doesn't match for " << File
.FileName() << std::endl
;
192 return pkgCache::PkgFileIterator(Cache
);
201 // TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
202 debTranslationsIndex::debTranslationsIndex(IndexTarget
const &Target
) :
203 pkgIndexTargetFile(Target
, true)
206 bool debTranslationsIndex::HasPackages() const /*{{{*/
211 // TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
212 // ---------------------------------------------------------------------
214 bool debTranslationsIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
216 // Check the translation file, if in use
217 string
const TranslationFile
= IndexFileName();
218 if (FileExists(TranslationFile
))
220 FileFd
Trans(TranslationFile
,FileFd::ReadOnly
, FileFd::Extension
);
221 debTranslationsParser
TransParser(&Trans
);
222 if (_error
->PendingError() == true)
226 Prog
->SubProgress(0, Target
.Description
);
227 if (Gen
.SelectFile(TranslationFile
,string(),*this) == false)
228 return _error
->Error("Problem with SelectFile %s",TranslationFile
.c_str());
230 // Store the IMS information
231 pkgCache::PkgFileIterator TransFile
= Gen
.GetCurFile();
232 TransFile
->Size
= Trans
.FileSize();
233 TransFile
->mtime
= Trans
.ModificationTime();
235 if (Gen
.MergeList(TransParser
) == false)
236 return _error
->Error("Problem with MergeList %s",TranslationFile
.c_str());
242 // TranslationsIndex::FindInCache - Find this index /*{{{*/
243 // ---------------------------------------------------------------------
245 pkgCache::PkgFileIterator
debTranslationsIndex::FindInCache(pkgCache
&Cache
) const
247 string FileName
= IndexFileName();
249 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
250 for (; File
.end() == false; ++File
)
252 if (FileName
!= File
.FileName())
256 if (stat(File
.FileName(),&St
) != 0)
258 if (_config
->FindB("Debug::pkgCacheGen", false))
259 std::clog
<< "TranslationIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
260 return pkgCache::PkgFileIterator(Cache
);
262 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
264 if (_config
->FindB("Debug::pkgCacheGen", false))
265 std::clog
<< "TranslationIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
266 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
267 << ") doesn't match for " << File
.FileName() << std::endl
;
268 return pkgCache::PkgFileIterator(Cache
);
276 // StatusIndex::debStatusIndex - Constructor /*{{{*/
277 // ---------------------------------------------------------------------
279 debStatusIndex::debStatusIndex(string File
) : pkgIndexFile(true), File(File
)
283 // StatusIndex::Size - Return the size of the index /*{{{*/
284 // ---------------------------------------------------------------------
286 unsigned long debStatusIndex::Size() const
289 if (stat(File
.c_str(),&S
) != 0)
294 // StatusIndex::Merge - Load the index file into a cache /*{{{*/
295 // ---------------------------------------------------------------------
297 bool debStatusIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
*Prog
) const
299 FileFd
Pkg(File
,FileFd::ReadOnly
, FileFd::Extension
);
300 if (_error
->PendingError() == true)
302 debListParser
Parser(&Pkg
);
303 if (_error
->PendingError() == true)
307 Prog
->SubProgress(0,File
);
308 if (Gen
.SelectFile(File
,string(),*this,pkgCache::Flag::NotSource
) == false)
309 return _error
->Error("Problem with SelectFile %s",File
.c_str());
311 // Store the IMS information
312 pkgCache::PkgFileIterator CFile
= Gen
.GetCurFile();
313 CFile
->Size
= Pkg
.FileSize();
314 CFile
->mtime
= Pkg
.ModificationTime();
315 map_stringitem_t
const storage
= Gen
.StoreString(pkgCacheGenerator::MIXED
, "now");
316 CFile
->Archive
= storage
;
318 if (Gen
.MergeList(Parser
) == false)
319 return _error
->Error("Problem with MergeList %s",File
.c_str());
323 // StatusIndex::FindInCache - Find this index /*{{{*/
324 // ---------------------------------------------------------------------
326 pkgCache::PkgFileIterator
debStatusIndex::FindInCache(pkgCache
&Cache
) const
328 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
329 for (; File
.end() == false; ++File
)
331 if (this->File
!= File
.FileName())
335 if (stat(File
.FileName(),&St
) != 0)
337 if (_config
->FindB("Debug::pkgCacheGen", false))
338 std::clog
<< "StatusIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
339 return pkgCache::PkgFileIterator(Cache
);
341 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
343 if (_config
->FindB("Debug::pkgCacheGen", false))
344 std::clog
<< "StatusIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
345 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
346 << ") doesn't match for " << File
.FileName() << std::endl
;
347 return pkgCache::PkgFileIterator(Cache
);
354 // StatusIndex::Exists - Check if the index is available /*{{{*/
355 // ---------------------------------------------------------------------
357 APT_CONST
bool debStatusIndex::Exists() const
359 // Abort if the file does not exist.
364 // debDebPkgFile - Single .deb file /*{{{*/
365 debDebPkgFileIndex::debDebPkgFileIndex(std::string DebFile
)
366 : pkgIndexFile(true), DebFile(DebFile
)
368 DebFileFullPath
= flAbsPath(DebFile
);
371 std::string
debDebPkgFileIndex::ArchiveURI(std::string
/*File*/) const
373 return "file:" + DebFileFullPath
;
376 bool debDebPkgFileIndex::Exists() const
378 return FileExists(DebFile
);
380 bool debDebPkgFileIndex::GetContent(std::ostream
&content
, std::string
const &debfile
)
382 // get the control data out of the deb file via dpkg-deb -I
383 std::string dpkg
= _config
->Find("Dir::Bin::dpkg","dpkg-deb");
384 std::vector
<const char *> Args
;
385 Args
.push_back(dpkg
.c_str());
386 Args
.push_back("-I");
387 Args
.push_back(debfile
.c_str());
388 Args
.push_back("control");
389 Args
.push_back(NULL
);
392 if(Popen((const char**)&Args
[0], PipeFd
, Child
, FileFd::ReadOnly
) == false)
393 return _error
->Error("Popen failed");
397 unsigned long long actual
= 0;
398 if (PipeFd
.Read(buffer
, sizeof(buffer
)-1, &actual
) == false)
399 return _error
->Errno("read", "Failed to read dpkg pipe");
402 buffer
[actual
] = '\0';
405 ExecWait(Child
, "Popen");
407 content
<< "Filename: " << debfile
<< "\n";
409 if (stat(debfile
.c_str(), &Buf
) != 0)
411 content
<< "Size: " << Buf
.st_size
<< "\n";
415 bool debDebPkgFileIndex::Merge(pkgCacheGenerator
& Gen
, OpProgress
* Prog
) const
418 Prog
->SubProgress(0, "Reading deb file");
420 // write the control data to a tempfile
421 SPtr
<FileFd
> DebControl
= GetTempFile("deb-file-" + flNotDir(DebFile
));
422 if(DebControl
== NULL
)
424 std::ostringstream content
;
425 if (GetContent(content
, DebFile
) == false)
427 std::string
const contentstr
= content
.str();
428 DebControl
->Write(contentstr
.c_str(), contentstr
.length());
429 // rewind for the listparser
432 // and give it to the list parser
433 debDebFileParser
Parser(DebControl
, DebFile
);
434 if(Gen
.SelectFile(DebFile
, "local", *this, pkgCache::Flag::LocalSource
) == false)
435 return _error
->Error("Problem with SelectFile %s", DebFile
.c_str());
437 pkgCache::PkgFileIterator File
= Gen
.GetCurFile();
438 File
->Size
= DebControl
->Size();
439 File
->mtime
= DebControl
->ModificationTime();
441 if (Gen
.MergeList(Parser
) == false)
442 return _error
->Error("Problem with MergeLister for %s", DebFile
.c_str());
446 pkgCache::PkgFileIterator
debDebPkgFileIndex::FindInCache(pkgCache
&Cache
) const
448 pkgCache::PkgFileIterator File
= Cache
.FileBegin();
449 for (; File
.end() == false; ++File
)
451 if (File
.FileName() == NULL
|| DebFile
!= File
.FileName())
459 unsigned long debDebPkgFileIndex::Size() const
462 if(stat(DebFile
.c_str(), &buf
) != 0)
468 // debDscFileIndex stuff
469 debDscFileIndex::debDscFileIndex(std::string
&DscFile
)
470 : pkgIndexFile(true), DscFile(DscFile
)
474 bool debDscFileIndex::Exists() const
476 return FileExists(DscFile
);
479 unsigned long debDscFileIndex::Size() const
482 if(stat(DscFile
.c_str(), &buf
) == 0)
487 // DscFileIndex::CreateSrcParser - Get a parser for the .dsc file /*{{{*/
488 pkgSrcRecords::Parser
*debDscFileIndex::CreateSrcParser() const
490 if (!FileExists(DscFile
))
493 return new debDscRecordParser(DscFile
,this);
496 // Index File types for Debian /*{{{*/
497 class APT_HIDDEN debIFTypeSrc
: public pkgIndexFile::Type
501 debIFTypeSrc() {Label
= "Debian Source Index";};
503 class APT_HIDDEN debIFTypePkg
: public pkgIndexFile::Type
507 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
509 return new debRecordParser(File
.FileName(),*File
.Cache());
511 debIFTypePkg() {Label
= "Debian Package Index";};
513 class APT_HIDDEN debIFTypeTrans
: public debIFTypePkg
516 debIFTypeTrans() {Label
= "Debian Translation Index";};
518 class APT_HIDDEN debIFTypeStatus
: public pkgIndexFile::Type
522 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
524 return new debRecordParser(File
.FileName(),*File
.Cache());
526 debIFTypeStatus() {Label
= "Debian dpkg status file";};
528 class APT_HIDDEN debIFTypeDebPkgFile
: public pkgIndexFile::Type
531 virtual pkgRecords::Parser
*CreatePkgParser(pkgCache::PkgFileIterator File
) const
533 return new debDebFileRecordParser(File
.FileName());
535 debIFTypeDebPkgFile() {Label
= "deb Package file";};
537 class APT_HIDDEN debIFTypeDscFile
: public pkgIndexFile::Type
540 virtual pkgSrcRecords::Parser
*CreateSrcPkgParser(std::string DscFile
) const
542 return new debDscRecordParser(DscFile
, NULL
);
544 debIFTypeDscFile() {Label
= "dsc File Source Index";};
546 class APT_HIDDEN debIFTypeDebianSourceDir
: public pkgIndexFile::Type
549 virtual pkgSrcRecords::Parser
*CreateSrcPkgParser(std::string SourceDir
) const
551 return new debDscRecordParser(SourceDir
+ string("/debian/control"), NULL
);
553 debIFTypeDebianSourceDir() {Label
= "debian/control File Source Index";};
556 APT_HIDDEN debIFTypeSrc _apt_Src
;
557 APT_HIDDEN debIFTypePkg _apt_Pkg
;
558 APT_HIDDEN debIFTypeTrans _apt_Trans
;
559 APT_HIDDEN debIFTypeStatus _apt_Status
;
560 APT_HIDDEN debIFTypeDebPkgFile _apt_DebPkgFile
;
561 // file based pseudo indexes
562 APT_HIDDEN debIFTypeDscFile _apt_DscFile
;
563 APT_HIDDEN debIFTypeDebianSourceDir _apt_DebianSourceDir
;
565 const pkgIndexFile::Type
*debSourcesIndex::GetType() const
569 const pkgIndexFile::Type
*debPackagesIndex::GetType() const
573 const pkgIndexFile::Type
*debTranslationsIndex::GetType() const
577 const pkgIndexFile::Type
*debStatusIndex::GetType() const
581 const pkgIndexFile::Type
*debDebPkgFileIndex::GetType() const
583 return &_apt_DebPkgFile
;
585 const pkgIndexFile::Type
*debDscFileIndex::GetType() const
587 return &_apt_DscFile
;
589 const pkgIndexFile::Type
*debDebianSourceDirIndex::GetType() const
591 return &_apt_DebianSourceDir
;
595 debStatusIndex::~debStatusIndex() {}
596 debPackagesIndex::~debPackagesIndex() {}
597 debTranslationsIndex::~debTranslationsIndex() {}
598 debSourcesIndex::~debSourcesIndex() {}
600 debDebPkgFileIndex::~debDebPkgFileIndex() {}