summaryrefslogtreecommitdiff
path: root/apt-pkg/deb/debindexfile.cc
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2015-07-20 10:17:29 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2015-08-10 17:27:59 +0200
commitc9443c01208377f0cba9706412ea3a98ad97b56d (patch)
treee324eb72ee264cc41d92d5c7fc78af77351f6b88 /apt-pkg/deb/debindexfile.cc
parent5465192b9aeb1ccea778950ccf2d1b7b32f2cd91 (diff)
elimate duplicated code in pkgIndexFile subclasses
Trade deduplication of code for a bunch of new virtuals, so it is actually visible how the different indexes behave cleaning up the interface at large in the process. Git-Dch: Ignore
Diffstat (limited to 'apt-pkg/deb/debindexfile.cc')
-rw-r--r--apt-pkg/deb/debindexfile.cc416
1 files changed, 104 insertions, 312 deletions
diff --git a/apt-pkg/deb/debindexfile.cc b/apt-pkg/deb/debindexfile.cc
index e67233e5f..627cd36c5 100644
--- a/apt-pkg/deb/debindexfile.cc
+++ b/apt-pkg/deb/debindexfile.cc
@@ -36,27 +36,20 @@
#include <iostream>
#include <sstream>
#include <string>
+#include <memory>
#include <sys/stat.h>
/*}}}*/
-using std::string;
-
-// SourcesIndex::debSourcesIndex - Constructor /*{{{*/
-// ---------------------------------------------------------------------
-/* */
+// Sources Index /*{{{*/
debSourcesIndex::debSourcesIndex(IndexTarget const &Target,bool const Trusted) :
- pkgIndexTargetFile(Target, Trusted), d(NULL)
+ pkgDebianIndexTargetFile(Target, Trusted), d(NULL)
{
}
- /*}}}*/
-// SourcesIndex::SourceInfo - Short 1 liner describing a source /*{{{*/
-// ---------------------------------------------------------------------
-/* The result looks like:
- http://foo/debian/ stable/main src 1.1.1 (dsc) */
-string debSourcesIndex::SourceInfo(pkgSrcRecords::Parser const &Record,
+std::string debSourcesIndex::SourceInfo(pkgSrcRecords::Parser const &Record,
pkgSrcRecords::File const &File) const
{
- string Res = Target.Description;
+ // The result looks like: http://foo/debian/ stable/main src 1.1.1 (dsc)
+ std::string Res = Target.Description;
Res.erase(Target.Description.rfind(' '));
Res += " ";
@@ -67,294 +60,111 @@ string debSourcesIndex::SourceInfo(pkgSrcRecords::Parser const &Record,
Res += " (" + File.Type + ")";
return Res;
}
- /*}}}*/
-// SourcesIndex::CreateSrcParser - Get a parser for the source files /*{{{*/
-// ---------------------------------------------------------------------
-/* */
pkgSrcRecords::Parser *debSourcesIndex::CreateSrcParser() const
{
- string const SourcesURI = IndexFileName();
+ std::string const SourcesURI = IndexFileName();
if (FileExists(SourcesURI))
return new debSrcRecordParser(SourcesURI, this);
return NULL;
}
+bool debSourcesIndex::OpenListFile(FileFd &, std::string const &)
+{
+ return true;
+}
+pkgCacheListParser * debSourcesIndex::CreateListParser(FileFd &)
+{
+ return NULL;
+}
+uint8_t debSourcesIndex::GetIndexFlags() const
+{
+ return 0;
+}
/*}}}*/
-
-// PackagesIndex::debPackagesIndex - Contructor /*{{{*/
-// ---------------------------------------------------------------------
-/* */
+// Packages Index /*{{{*/
debPackagesIndex::debPackagesIndex(IndexTarget const &Target, bool const Trusted) :
- pkgIndexTargetFile(Target, Trusted), d(NULL)
+ pkgDebianIndexTargetFile(Target, Trusted), d(NULL)
{
}
- /*}}}*/
-// PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
-// ---------------------------------------------------------------------
-/* This is a shorter version that is designed to be < 60 chars or so */
-string debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver) const
+std::string debPackagesIndex::ArchiveInfo(pkgCache::VerIterator const &Ver) const
{
- std::string const Dist = Target.Option(IndexTarget::RELEASE);
- string Res = Target.Option(IndexTarget::SITE) + " " + Dist;
- std::string const Component = Target.Option(IndexTarget::COMPONENT);
- if (Component.empty() == false)
- Res += "/" + Component;
+ std::string Res = Target.Description;
+ Res.erase(Target.Description.rfind(' '));
Res += " ";
Res += Ver.ParentPkg().Name();
Res += " ";
+ std::string const Dist = Target.Option(IndexTarget::RELEASE);
if (Dist.empty() == false && Dist[Dist.size() - 1] != '/')
Res.append(Ver.Arch()).append(" ");
Res += Ver.VerStr();
return Res;
}
- /*}}}*/
-// PackagesIndex::Merge - Load the index file into a cache /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-bool debPackagesIndex::Merge(pkgCacheGenerator &Gen,OpProgress *Prog) const
-{
- string const PackageFile = IndexFileName();
- FileFd Pkg(PackageFile,FileFd::ReadOnly, FileFd::Extension);
- debListParser Parser(&Pkg, Target.Option(IndexTarget::ARCHITECTURE));
-
- if (_error->PendingError() == true)
- return _error->Error("Problem opening %s",PackageFile.c_str());
- if (Prog != NULL)
- Prog->SubProgress(0, Target.Description);
-
-
- std::string const URI = Target.Option(IndexTarget::REPO_URI);
- std::string Dist = Target.Option(IndexTarget::RELEASE);
- if (Dist.empty())
- Dist = "/";
- ::URI Tmp(URI);
- if (Gen.SelectFile(PackageFile, *this, Target.Option(IndexTarget::ARCHITECTURE), Target.Option(IndexTarget::COMPONENT)) == false)
- return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
-
- // Store the IMS information
- pkgCache::PkgFileIterator File = Gen.GetCurFile();
- pkgCacheGenerator::Dynamic<pkgCache::PkgFileIterator> DynFile(File);
- File->Size = Pkg.FileSize();
- File->mtime = Pkg.ModificationTime();
-
- if (Gen.MergeList(Parser) == false)
- return _error->Error("Problem with MergeList %s",PackageFile.c_str());
-
- return true;
-}
- /*}}}*/
-// PackagesIndex::FindInCache - Find this index /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-pkgCache::PkgFileIterator debPackagesIndex::FindInCache(pkgCache &Cache) const
+uint8_t debPackagesIndex::GetIndexFlags() const
{
- string const FileName = IndexFileName();
- pkgCache::PkgFileIterator File = Cache.FileBegin();
- for (; File.end() == false; ++File)
- {
- if (File.FileName() == NULL || FileName != File.FileName())
- continue;
-
- struct stat St;
- if (stat(File.FileName(),&St) != 0)
- {
- if (_config->FindB("Debug::pkgCacheGen", false))
- std::clog << "PackagesIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
- return pkgCache::PkgFileIterator(Cache);
- }
- if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
- {
- if (_config->FindB("Debug::pkgCacheGen", false))
- std::clog << "PackagesIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
- << ") or mtime (" << St.st_mtime << " <> " << File->mtime
- << ") doesn't match for " << File.FileName() << std::endl;
- return pkgCache::PkgFileIterator(Cache);
- }
- return File;
- }
-
- return File;
+ return 0;
}
/*}}}*/
-
-// TranslationsIndex::debTranslationsIndex - Contructor /*{{{*/
+// Translation-* Index /*{{{*/
debTranslationsIndex::debTranslationsIndex(IndexTarget const &Target) :
- pkgIndexTargetFile(Target, true), d(NULL)
+ pkgDebianIndexTargetFile(Target, true), d(NULL)
{}
- /*}}}*/
-bool debTranslationsIndex::HasPackages() const /*{{{*/
+bool debTranslationsIndex::HasPackages() const
{
return Exists();
}
- /*}}}*/
-// TranslationsIndex::Merge - Load the index file into a cache /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-bool debTranslationsIndex::Merge(pkgCacheGenerator &Gen,OpProgress *Prog) const
-{
- // Check the translation file, if in use
- string const TranslationFile = IndexFileName();
- if (FileExists(TranslationFile))
- {
- FileFd Trans(TranslationFile,FileFd::ReadOnly, FileFd::Extension);
- debTranslationsParser TransParser(&Trans);
- if (_error->PendingError() == true)
- return false;
-
- if (Prog != NULL)
- Prog->SubProgress(0, Target.Description);
- if (Gen.SelectFile(TranslationFile, *this, "", Target.Option(IndexTarget::COMPONENT), pkgCache::Flag::NotSource | pkgCache::Flag::NoPackages) == false)
- return _error->Error("Problem with SelectFile %s",TranslationFile.c_str());
-
- // Store the IMS information
- pkgCache::PkgFileIterator TransFile = Gen.GetCurFile();
- TransFile->Size = Trans.FileSize();
- TransFile->mtime = Trans.ModificationTime();
-
- if (Gen.MergeList(TransParser) == false)
- return _error->Error("Problem with MergeList %s",TranslationFile.c_str());
- }
-
+bool debTranslationsIndex::OpenListFile(FileFd &Pkg, std::string const &FileName)
+{
+ if (FileExists(FileName))
+ return pkgDebianIndexTargetFile::OpenListFile(Pkg, FileName);
return true;
}
- /*}}}*/
-// TranslationsIndex::FindInCache - Find this index /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-pkgCache::PkgFileIterator debTranslationsIndex::FindInCache(pkgCache &Cache) const
+uint8_t debTranslationsIndex::GetIndexFlags() const
{
- string FileName = IndexFileName();
-
- pkgCache::PkgFileIterator File = Cache.FileBegin();
- for (; File.end() == false; ++File)
- {
- if (FileName != File.FileName())
- continue;
-
- struct stat St;
- if (stat(File.FileName(),&St) != 0)
- {
- if (_config->FindB("Debug::pkgCacheGen", false))
- std::clog << "TranslationIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
- return pkgCache::PkgFileIterator(Cache);
- }
- if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
- {
- if (_config->FindB("Debug::pkgCacheGen", false))
- std::clog << "TranslationIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
- << ") or mtime (" << St.st_mtime << " <> " << File->mtime
- << ") doesn't match for " << File.FileName() << std::endl;
- return pkgCache::PkgFileIterator(Cache);
- }
- return File;
- }
- return File;
+ return pkgCache::Flag::NotSource | pkgCache::Flag::NoPackages;
}
- /*}}}*/
-
-// StatusIndex::debStatusIndex - Constructor /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-debStatusIndex::debStatusIndex(string File) : pkgIndexFile(true), d(NULL), File(File)
+std::string debTranslationsIndex::GetArchitecture() const
{
+ return std::string();
}
- /*}}}*/
-// StatusIndex::Size - Return the size of the index /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-unsigned long debStatusIndex::Size() const
+pkgCacheListParser * debTranslationsIndex::CreateListParser(FileFd &Pkg)
{
- struct stat S;
- if (stat(File.c_str(),&S) != 0)
- return 0;
- return S.st_size;
-}
- /*}}}*/
-// StatusIndex::Merge - Load the index file into a cache /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-bool debStatusIndex::Merge(pkgCacheGenerator &Gen,OpProgress *Prog) const
-{
- FileFd Pkg(File,FileFd::ReadOnly, FileFd::Extension);
- if (_error->PendingError() == true)
- return false;
- debListParser Parser(&Pkg);
- if (_error->PendingError() == true)
- return false;
-
- if (Prog != NULL)
- Prog->SubProgress(0,File);
- if (Gen.SelectFile(File, *this, "", "now", pkgCache::Flag::NotSource) == false)
- return _error->Error("Problem with SelectFile %s",File.c_str());
-
- // Store the IMS information
- pkgCache::PkgFileIterator CFile = Gen.GetCurFile();
- pkgCacheGenerator::Dynamic<pkgCache::PkgFileIterator> DynFile(CFile);
- CFile->Size = Pkg.FileSize();
- CFile->mtime = Pkg.ModificationTime();
-
- if (Gen.MergeList(Parser) == false)
- return _error->Error("Problem with MergeList %s",File.c_str());
- return true;
+ if (Pkg.IsOpen() == false)
+ return NULL;
+ _error->PushToStack();
+ pkgCacheListParser * const Parser = new debTranslationsParser(&Pkg);
+ bool const newError = _error->PendingError();
+ _error->MergeWithStack();
+ return newError ? NULL : Parser;
}
/*}}}*/
-// StatusIndex::FindInCache - Find this index /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-pkgCache::PkgFileIterator debStatusIndex::FindInCache(pkgCache &Cache) const
+// dpkg/status Index /*{{{*/
+debStatusIndex::debStatusIndex(std::string const &File) : pkgDebianIndexRealFile(File, true), d(NULL)
{
- pkgCache::PkgFileIterator File = Cache.FileBegin();
- for (; File.end() == false; ++File)
- {
- if (this->File != File.FileName())
- continue;
-
- struct stat St;
- if (stat(File.FileName(),&St) != 0)
- {
- if (_config->FindB("Debug::pkgCacheGen", false))
- std::clog << "StatusIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
- return pkgCache::PkgFileIterator(Cache);
- }
- if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
- {
- if (_config->FindB("Debug::pkgCacheGen", false))
- std::clog << "StatusIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
- << ") or mtime (" << St.st_mtime << " <> " << File->mtime
- << ") doesn't match for " << File.FileName() << std::endl;
- return pkgCache::PkgFileIterator(Cache);
- }
- return File;
- }
- return File;
}
- /*}}}*/
-// StatusIndex::Exists - Check if the index is available /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-APT_CONST bool debStatusIndex::Exists() const
+std::string debStatusIndex::GetArchitecture() const
{
- // Abort if the file does not exist.
- return true;
+ return std::string();
}
- /*}}}*/
-
-// debDebPkgFileIndex - Single .deb file /*{{{*/
-debDebPkgFileIndex::debDebPkgFileIndex(std::string const &DebFile)
- : pkgIndexFile(true), d(NULL), DebFile(DebFile)
+std::string debStatusIndex::GetComponent() const
{
- DebFileFullPath = flAbsPath(DebFile);
+ return "now";
}
-std::string debDebPkgFileIndex::ArchiveURI(std::string /*File*/) const
+uint8_t debStatusIndex::GetIndexFlags() const
{
- return "file:" + DebFileFullPath;
+ return pkgCache::Flag::NotSource;
}
-bool debDebPkgFileIndex::Exists() const
+ /*}}}*/
+// DebPkgFile Index - a single .deb file as an index /*{{{*/
+debDebPkgFileIndex::debDebPkgFileIndex(std::string const &DebFile)
+ : pkgDebianIndexRealFile(DebFile, true), d(NULL), DebFile(DebFile)
{
- return FileExists(DebFile);
}
bool debDebPkgFileIndex::GetContent(std::ostream &content, std::string const &debfile)
{
+ struct stat Buf;
+ if (stat(debfile.c_str(), &Buf) != 0)
+ return false;
+
// get the control data out of the deb file via dpkg-deb -I
std::string dpkg = _config->Find("Dir::Bin::dpkg","dpkg-deb");
std::vector<const char *> Args;
@@ -381,91 +191,73 @@ bool debDebPkgFileIndex::GetContent(std::ostream &content, std::string const &de
ExecWait(Child, "Popen");
content << "Filename: " << debfile << "\n";
- struct stat Buf;
- if (stat(debfile.c_str(), &Buf) != 0)
- return false;
content << "Size: " << Buf.st_size << "\n";
return true;
}
-bool debDebPkgFileIndex::Merge(pkgCacheGenerator& Gen, OpProgress* Prog) const
+bool debDebPkgFileIndex::OpenListFile(FileFd &Pkg, std::string const &FileName)
{
- if(Prog)
- Prog->SubProgress(0, "Reading deb file");
-
// write the control data to a tempfile
- SPtr<FileFd> DebControl = GetTempFile("deb-file-" + flNotDir(DebFile));
- if(DebControl == NULL)
+ if (GetTempFile("deb-file-" + flNotDir(FileName), true, &Pkg) == NULL)
return false;
std::ostringstream content;
- if (GetContent(content, DebFile) == false)
+ if (GetContent(content, FileName) == false)
return false;
std::string const contentstr = content.str();
if (contentstr.empty())
return true;
- DebControl->Write(contentstr.c_str(), contentstr.length());
- // rewind for the listparser
- DebControl->Seek(0);
-
- // and give it to the list parser
- debDebFileParser Parser(DebControl, DebFile);
- if(Gen.SelectFile(DebFile, *this, "", "now", pkgCache::Flag::LocalSource) == false)
- return _error->Error("Problem with SelectFile %s", DebFile.c_str());
-
- pkgCache::PkgFileIterator File = Gen.GetCurFile();
- File->Size = DebControl->Size();
- File->mtime = DebControl->ModificationTime();
-
- if (Gen.MergeList(Parser) == false)
- return _error->Error("Problem with MergeLister for %s", DebFile.c_str());
-
+ if (Pkg.Write(contentstr.c_str(), contentstr.length()) == false || Pkg.Seek(0) == false)
+ return false;
return true;
}
+pkgCacheListParser * debDebPkgFileIndex::CreateListParser(FileFd &Pkg)
+{
+ if (Pkg.IsOpen() == false)
+ return NULL;
+ _error->PushToStack();
+ pkgCacheListParser * const Parser = new debDebFileParser(&Pkg, DebFile);
+ bool const newError = _error->PendingError();
+ _error->MergeWithStack();
+ return newError ? NULL : Parser;
+}
+uint8_t debDebPkgFileIndex::GetIndexFlags() const
+{
+ return pkgCache::Flag::LocalSource;
+}
+std::string debDebPkgFileIndex::GetArchitecture() const
+{
+ return std::string();
+}
+std::string debDebPkgFileIndex::GetComponent() const
+{
+ return "local-deb";
+}
pkgCache::PkgFileIterator debDebPkgFileIndex::FindInCache(pkgCache &Cache) const
{
+ std::string const FileName = IndexFileName();
pkgCache::PkgFileIterator File = Cache.FileBegin();
for (; File.end() == false; ++File)
{
- if (File.FileName() == NULL || DebFile != File.FileName())
+ if (File.FileName() == NULL || FileName != File.FileName())
continue;
-
- return File;
+ // we can't do size checks here as file size != content size
+ return File;
}
return File;
}
-unsigned long debDebPkgFileIndex::Size() const
-{
- struct stat buf;
- if(stat(DebFile.c_str(), &buf) != 0)
- return 0;
- return buf.st_size;
-}
- /*}}}*/
-// debDscFileIndex - a .dsc file /*{{{*/
+ /*}}}*/
+// DscFile Index - a single .dsc file as an index /*{{{*/
debDscFileIndex::debDscFileIndex(std::string const &DscFile)
- : pkgIndexFile(true), d(NULL), DscFile(DscFile)
+ : pkgDebianIndexRealFile(DscFile, true), d(NULL)
{
}
-bool debDscFileIndex::Exists() const
-{
- return FileExists(DscFile);
-}
-
-unsigned long debDscFileIndex::Size() const
-{
- struct stat buf;
- if(stat(DscFile.c_str(), &buf) == 0)
- return buf.st_size;
- return 0;
-}
pkgSrcRecords::Parser *debDscFileIndex::CreateSrcParser() const
{
- if (!FileExists(DscFile))
+ if (Exists() == false)
return NULL;
-
- return new debDscRecordParser(DscFile,this);
+ return new debDscRecordParser(File, this);
}
/*}}}*/
@@ -478,7 +270,7 @@ class APT_HIDDEN debIFTypeSrc : public pkgIndexFile::Type
class APT_HIDDEN debIFTypePkg : public pkgIndexFile::Type
{
public:
- virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const APT_OVERRIDE
+ virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
{
return new debRecordParser(File.FileName(),*File.Cache());
};
@@ -492,7 +284,7 @@ class APT_HIDDEN debIFTypeTrans : public debIFTypePkg
class APT_HIDDEN debIFTypeStatus : public pkgIndexFile::Type
{
public:
- virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const APT_OVERRIDE
+ virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
{
return new debRecordParser(File.FileName(),*File.Cache());
};
@@ -501,7 +293,7 @@ class APT_HIDDEN debIFTypeStatus : public pkgIndexFile::Type
class APT_HIDDEN debIFTypeDebPkgFile : public pkgIndexFile::Type
{
public:
- virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const APT_OVERRIDE
+ virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
{
return new debDebFileRecordParser(File.FileName());
};
@@ -510,7 +302,7 @@ class APT_HIDDEN debIFTypeDebPkgFile : public pkgIndexFile::Type
class APT_HIDDEN debIFTypeDscFile : public pkgIndexFile::Type
{
public:
- virtual pkgSrcRecords::Parser *CreateSrcPkgParser(std::string DscFile) const APT_OVERRIDE
+ virtual pkgSrcRecords::Parser *CreateSrcPkgParser(std::string const &DscFile) const APT_OVERRIDE
{
return new debDscRecordParser(DscFile, NULL);
};
@@ -519,9 +311,9 @@ class APT_HIDDEN debIFTypeDscFile : public pkgIndexFile::Type
class APT_HIDDEN debIFTypeDebianSourceDir : public pkgIndexFile::Type
{
public:
- virtual pkgSrcRecords::Parser *CreateSrcPkgParser(std::string SourceDir) const APT_OVERRIDE
+ virtual pkgSrcRecords::Parser *CreateSrcPkgParser(std::string const &SourceDir) const APT_OVERRIDE
{
- return new debDscRecordParser(SourceDir + string("/debian/control"), NULL);
+ return new debDscRecordParser(SourceDir + std::string("/debian/control"), NULL);
};
debIFTypeDebianSourceDir() {Label = "Debian control file";};
};