]> git.saurik.com Git - apt.git/commitdiff
refactor EDSP classes for better internal reuse
authorDavid Kalnischkies <david@kalnischkies.de>
Sun, 5 Jun 2016 11:05:30 +0000 (13:05 +0200)
committerDavid Kalnischkies <david@kalnischkies.de>
Sun, 5 Jun 2016 11:15:55 +0000 (13:15 +0200)
The classes are all marked as hidden, so changing them is no problem ABI
wise and will help with introducing protocols similar to EDSP.
The change has no observeable behavior difference, its just code
juggling.

Git-Dch: Ignore

apt-pkg/edsp/edspindexfile.cc
apt-pkg/edsp/edspindexfile.h
apt-pkg/edsp/edsplistparser.cc
apt-pkg/edsp/edsplistparser.h
apt-pkg/edsp/edspsystem.cc
apt-pkg/edsp/edspsystem.h

index 4e0f9c26a99c088afb12ad96bbd50d78e24c28c0..e2863a2cc4160cb2ab85927ae0a62113edb9079e 100644 (file)
 #include <string>
                                                                        /*}}}*/
 
-// EDSP Index                                                          /*{{{*/
-edspIndex::edspIndex(std::string const &File) : pkgDebianIndexRealFile(File, true), d(NULL)
+// EDSP-like Index                                                     /*{{{*/
+edspLikeIndex::edspLikeIndex(std::string const &File) : pkgDebianIndexRealFile(File, true)
 {
 }
-std::string edspIndex::GetComponent() const
-{
-   return "edsp";
-}
-std::string edspIndex::GetArchitecture() const
+std::string edspLikeIndex::GetArchitecture() const
 {
    return std::string();
 }
-bool edspIndex::HasPackages() const
+bool edspLikeIndex::HasPackages() const
 {
    return true;
 }
-bool edspIndex::Exists() const
+bool edspLikeIndex::Exists() const
 {
    return true;
 }
-uint8_t edspIndex::GetIndexFlags() const
+uint8_t edspLikeIndex::GetIndexFlags() const
 {
    return 0;
 }
-bool edspIndex::OpenListFile(FileFd &Pkg, std::string const &FileName)
+bool edspLikeIndex::OpenListFile(FileFd &Pkg, std::string const &FileName)
 {
    if (FileName.empty() == false && FileName != "/nonexistent/stdin")
       return pkgDebianIndexRealFile::OpenListFile(Pkg, FileName);
    if (Pkg.OpenDescriptor(STDIN_FILENO, FileFd::ReadOnly) == false)
       return _error->Error("Problem opening %s",FileName.c_str());
    return true;
+}
+                                                                       /*}}}*/
+// EDSP Index                                                          /*{{{*/
+edspIndex::edspIndex(std::string const &File) : edspLikeIndex(File)
+{
+}
+std::string edspIndex::GetComponent() const
+{
+   return "edsp";
 }
 pkgCacheListParser * edspIndex::CreateListParser(FileFd &Pkg)
 {
@@ -84,4 +89,5 @@ const pkgIndexFile::Type *edspIndex::GetType() const
 }
                                                                        /*}}}*/
 
+edspLikeIndex::~edspLikeIndex() {}
 edspIndex::~edspIndex() {}
index 4548bff3c76a72b96e4cc0a7f3f529bc5a35c6c8..26bd1232b7d8a575b5ba1e0d221d4164225798d4 100644 (file)
 class OpProgress;
 class pkgCacheGenerator;
 
-class APT_HIDDEN edspIndex : public pkgDebianIndexRealFile
+class APT_HIDDEN edspLikeIndex : public pkgDebianIndexRealFile
 {
-   /** \brief dpointer placeholder (for later in case we need it) */
-   void * const d;
-
 protected:
-   APT_HIDDEN virtual pkgCacheListParser * CreateListParser(FileFd &Pkg) APT_OVERRIDE;
    virtual bool OpenListFile(FileFd &Pkg, std::string const &File) APT_OVERRIDE;
    virtual uint8_t GetIndexFlags() const APT_OVERRIDE;
-   virtual std::string GetComponent() const APT_OVERRIDE;
    virtual std::string GetArchitecture() const APT_OVERRIDE;
-public:
 
-   virtual const Type *GetType() const APT_OVERRIDE APT_CONST;
+public:
    virtual bool Exists() const APT_OVERRIDE;
    virtual bool HasPackages() const APT_OVERRIDE;
 
+   edspLikeIndex(std::string const &File);
+   virtual ~edspLikeIndex();
+};
+
+class APT_HIDDEN edspIndex : public edspLikeIndex
+{
+protected:
+   APT_HIDDEN virtual pkgCacheListParser * CreateListParser(FileFd &Pkg) APT_OVERRIDE;
+   virtual std::string GetComponent() const APT_OVERRIDE;
+
+public:
+   virtual const Type *GetType() const APT_OVERRIDE APT_CONST;
+
    edspIndex(std::string const &File);
    virtual ~edspIndex();
 };
index f35000e0ee34bd1c7d11f18519b3dac6c3b8ee42..39a6e8a6e6650843f8e63e14c850d31274c8bb55 100644 (file)
 
                                                                        /*}}}*/
 
-class edspListParserPrivate                                            /*{{{*/
-{
-public:
-   FileFd extendedstates;
-   FileFd preferences;
-
-   edspListParserPrivate()
-   {
-      std::string const states = _config->FindFile("Dir::State::extended_states");
-      RemoveFile("edspListParserPrivate", states);
-      extendedstates.Open(states, FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive, 0600);
-      std::string const prefs = _config->FindFile("Dir::Etc::preferences");
-      RemoveFile("edspListParserPrivate", prefs);
-      preferences.Open(prefs, FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive, 0600);
-   }
-};
-                                                                       /*}}}*/
 // ListParser::edspListParser - Constructor                            /*{{{*/
-edspListParser::edspListParser(FileFd *File) : debListParser(File), d(new edspListParserPrivate())
+edspLikeListParser::edspLikeListParser(FileFd * const File) : debListParser(File)
+{
+}
+edspListParser::edspListParser(FileFd * const File) : edspLikeListParser(File)
 {
+   std::string const states = _config->FindFile("Dir::State::extended_states");
+   RemoveFile("edspListParserPrivate", states);
+   extendedstates.Open(states, FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive, 0600);
+   std::string const prefs = _config->FindFile("Dir::Etc::preferences");
+   RemoveFile("edspListParserPrivate", prefs);
+   preferences.Open(prefs, FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive, 0600);
 }
                                                                        /*}}}*/
 // ListParser::NewVersion - Fill in the version structure              /*{{{*/
-bool edspListParser::NewVersion(pkgCache::VerIterator &Ver)
+bool edspLikeListParser::NewVersion(pkgCache::VerIterator &Ver)
 {
    _system->SetVersionMapping(Ver->ID, Section.FindI("APT-ID", Ver->ID));
    return debListParser::NewVersion(Ver);
@@ -55,19 +47,17 @@ bool edspListParser::NewVersion(pkgCache::VerIterator &Ver)
 // ListParser::Description - Return the description string             /*{{{*/
 // ---------------------------------------------------------------------
 /* Sorry, no description for the resolvers… */
-std::vector<std::string> edspListParser::AvailableDescriptionLanguages()
+std::vector<std::string> edspLikeListParser::AvailableDescriptionLanguages()
 {
    return {};
 }
-MD5SumValue edspListParser::Description_md5()
+MD5SumValue edspLikeListParser::Description_md5()
 {
    return MD5SumValue("");
 }
                                                                        /*}}}*/
 // ListParser::VersionHash - Compute a unique hash for this version    /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-unsigned short edspListParser::VersionHash()
+unsigned short edspLikeListParser::VersionHash()
 {
    if (Section.Exists("APT-Hash") == true)
       return Section.FindI("APT-Hash");
@@ -76,6 +66,13 @@ unsigned short edspListParser::VersionHash()
    return 0;
 }
                                                                        /*}}}*/
+// ListParser::LoadReleaseInfo - Load the release information          /*{{{*/
+APT_CONST bool edspLikeListParser::LoadReleaseInfo(pkgCache::RlsFileIterator & /*FileI*/,
+                                   FileFd & /*File*/, std::string const &/*component*/)
+{
+   return true;
+}
+                                                                       /*}}}*/
 // ListParser::ParseStatus - Parse the status field                    /*{{{*/
 // ---------------------------------------------------------------------
 /* The Status: line here is not a normal dpkg one but just one which tells
@@ -102,7 +99,7 @@ bool edspListParser::ParseStatus(pkgCache::PkgIterator &Pkg,
    {
       std::string out;
       strprintf(out, "Package: %s\nArchitecture: %s\nAuto-Installed: 1\n\n", Pkg.Name(), Pkg.Arch());
-      if (d->extendedstates.Write(out.c_str(), out.length()) == false)
+      if (extendedstates.Write(out.c_str(), out.length()) == false)
         return false;
    }
 
@@ -111,7 +108,7 @@ bool edspListParser::ParseStatus(pkgCache::PkgIterator &Pkg,
    {
       std::string out;
       strprintf(out, "Package: %s\nPin: version %s\nPin-Priority: 9999\n\n", Pkg.FullName().c_str(), Ver.VerStr());
-      if (d->preferences.Write(out.c_str(), out.length()) == false)
+      if (preferences.Write(out.c_str(), out.length()) == false)
         return false;
    }
 
@@ -120,22 +117,13 @@ bool edspListParser::ParseStatus(pkgCache::PkgIterator &Pkg,
    {
       std::string out;
       strprintf(out, "Package: %s\nPin: version %s\nPin-Priority: %d\n\n", Pkg.FullName().c_str(), Ver.VerStr(), pinvalue);
-      if (d->preferences.Write(out.c_str(), out.length()) == false)
+      if (preferences.Write(out.c_str(), out.length()) == false)
         return false;
    }
 
    return true;
 }
                                                                        /*}}}*/
-// ListParser::LoadReleaseInfo - Load the release information          /*{{{*/
-APT_CONST bool edspListParser::LoadReleaseInfo(pkgCache::RlsFileIterator & /*FileI*/,
-                                   FileFd & /*File*/, std::string const &/*component*/)
-{
-   return true;
-}
-                                                                       /*}}}*/
-edspListParser::~edspListParser()                                      /*{{{*/
-{
-   delete d;
-}
-                                                                       /*}}}*/
+
+edspLikeListParser::~edspLikeListParser() {}
+edspListParser::~edspListParser() {}
index 691ac83ed96baa4014ffa0287e339a645ed2eeb3..7cd5ab2b36ef5d921fcfade24910ecfa2ccaafb7 100644 (file)
@@ -14,6 +14,7 @@
 #include <apt-pkg/deblistparser.h>
 #include <apt-pkg/md5.h>
 #include <apt-pkg/pkgcache.h>
+#include <apt-pkg/fileutl.h>
 
 #include <string>
 
 #include <apt-pkg/tagfile.h>
 #endif
 
-class FileFd;
-class edspListParserPrivate;
-
-class APT_HIDDEN edspListParser : public debListParser
+class APT_HIDDEN edspLikeListParser : public debListParser
 {
-   edspListParserPrivate * const d;
    public:
    virtual bool NewVersion(pkgCache::VerIterator &Ver) APT_OVERRIDE;
    virtual std::vector<std::string> AvailableDescriptionLanguages() APT_OVERRIDE;
@@ -38,12 +35,20 @@ class APT_HIDDEN edspListParser : public debListParser
    bool LoadReleaseInfo(pkgCache::RlsFileIterator &FileI,FileFd &File,
                        std::string const &section);
 
-   edspListParser(FileFd *File);
-   virtual ~edspListParser();
+   edspLikeListParser(FileFd *File);
+   virtual ~edspLikeListParser();
+};
 
-   protected:
+class APT_HIDDEN edspListParser : public edspLikeListParser
+{
+   FileFd extendedstates;
+   FileFd preferences;
+
+protected:
    virtual bool ParseStatus(pkgCache::PkgIterator &Pkg,pkgCache::VerIterator &Ver) APT_OVERRIDE;
 
+public:
+   edspListParser(FileFd *File);
+   virtual ~edspListParser();
 };
-
 #endif
index 0d967863ef86cc6b198a2a685ef5ced99f2df988..c3eea6f105909a3f35db438fc5a799ca3d658385 100644 (file)
 
                                                                        /*}}}*/
 
-class edspSystemPrivate {
-   std::string tempDir;
-   std::string tempStatesFile;
-   std::string tempPrefsFile;
-
-public:
-   edspSystemPrivate() {}
-
-   void Initialize(Configuration &Cnf)
-   {
-      DeInitialize();
-      Cnf.Set("Dir::State::extended_states", "/dev/null");
-      Cnf.Set("Dir::Etc::preferences", "/dev/null");
-      std::string const tmp = GetTempDir();
-      char tmpname[100];
-      snprintf(tmpname, sizeof(tmpname), "%s/apt-edsp-solver-XXXXXX", tmp.c_str());
-      if (NULL == mkdtemp(tmpname))
-        return;
-      tempDir = tmpname;
-      tempStatesFile = flCombine(tempDir, "extended_states");
-      Cnf.Set("Dir::State::extended_states", tempStatesFile);
-      tempPrefsFile = flCombine(tempDir, "apt_preferences");
-      Cnf.Set("Dir::Etc::preferences", tempPrefsFile);
-   }
-
-   void DeInitialize()
-   {
-      if (tempDir.empty())
-        return;
-
-      RemoveFile("DeInitialize", tempStatesFile);
-      RemoveFile("DeInitialize", tempPrefsFile);
-      rmdir(tempDir.c_str());
-   }
-
-   ~edspSystemPrivate() { DeInitialize(); }
-};
-// System::edspSystem - Constructor                                    /*{{{*/
-edspSystem::edspSystem() : pkgSystem("Debian APT solver interface", &debVS), d(new edspSystemPrivate()), StatusFile(NULL)
+// System::System - Constructor                                                /*{{{*/
+edspLikeSystem::edspLikeSystem(char const * const Label) : pkgSystem(Label, &debVS)
 {
 }
-                                                                       /*}}}*/
-// System::~debSystem - Destructor                                     /*{{{*/
-edspSystem::~edspSystem()
+edspSystem::edspSystem() : edspLikeSystem("Debian APT solver interface")
 {
-   delete StatusFile;
-   delete d;
 }
                                                                        /*}}}*/
 // System::Lock - Get the lock                                         /*{{{*/
-bool edspSystem::Lock()
+bool edspLikeSystem::Lock()
 {
    return true;
 }
                                                                        /*}}}*/
 // System::UnLock - Drop a lock                                                /*{{{*/
-bool edspSystem::UnLock(bool /*NoErrors*/)
+bool edspLikeSystem::UnLock(bool /*NoErrors*/)
 {
    return true;
 }
@@ -93,68 +52,89 @@ bool edspSystem::UnLock(bool /*NoErrors*/)
 // ---------------------------------------------------------------------
 /* we can't use edsp input as input for real installations - just a
    simulation can work, but everything else will fail bigtime */
-pkgPackageManager *edspSystem::CreatePM(pkgDepCache * /*Cache*/) const
+pkgPackageManager *edspLikeSystem::CreatePM(pkgDepCache * /*Cache*/) const
 {
-   return NULL;
+   return nullptr;
 }
                                                                        /*}}}*/
 // System::Initialize - Setup the configuration space..                        /*{{{*/
-bool edspSystem::Initialize(Configuration &Cnf)
+bool edspLikeSystem::Initialize(Configuration &Cnf)
 {
-   d->Initialize(Cnf);
    Cnf.Set("Dir::Etc::preferencesparts", "/dev/null");
    Cnf.Set("Dir::State::status","/dev/null");
    Cnf.Set("Dir::State::lists","/dev/null");
-
    Cnf.Set("Debug::NoLocking", "true");
    Cnf.Set("APT::Get::Simulate", "true");
-
-   if (StatusFile) {
-     delete StatusFile;
-     StatusFile = 0;
-   }
+   StatusFile.reset(nullptr);
+   return true;
+}
+bool edspSystem::Initialize(Configuration &Cnf)
+{
+   if (edspLikeSystem::Initialize(Cnf) == false)
+      return false;
+   std::string const tmp = GetTempDir();
+   char tmpname[300];
+   snprintf(tmpname, sizeof(tmpname), "%s/apt-edsp-solver-XXXXXX", tmp.c_str());
+   if (nullptr == mkdtemp(tmpname))
+      return false;
+   tempDir = tmpname;
+   tempStatesFile = flCombine(tempDir, "extended_states");
+   Cnf.Set("Dir::State::extended_states", tempStatesFile);
+   tempPrefsFile = flCombine(tempDir, "apt_preferences");
+   Cnf.Set("Dir::Etc::preferences", tempPrefsFile);
    return true;
 }
                                                                        /*}}}*/
 // System::ArchiveSupported - Is a file format supported               /*{{{*/
-bool edspSystem::ArchiveSupported(const char * /*Type*/)
+bool edspLikeSystem::ArchiveSupported(const char * /*Type*/)
 {
    return false;
 }
                                                                        /*}}}*/
 // System::Score - Never use the EDSP system automatically             /*{{{*/
-signed edspSystem::Score(Configuration const &)
+signed edspLikeSystem::Score(Configuration const &)
 {
    return -1000;
 }
                                                                        /*}}}*/
-bool edspSystem::AddStatusFiles(std::vector<pkgIndexFile *> &List)     /*{{{*/
-{
-   if (StatusFile == 0)
-   {
-      if (_config->Find("edsp::scenario", "") == "/nonexistent/stdin")
-        StatusFile = new edspIndex("/nonexistent/stdin");
-      else
-        StatusFile = new edspIndex(_config->FindFile("edsp::scenario"));
-   }
-   List.push_back(StatusFile);
-   return true;
-}
-                                                                       /*}}}*/
 // System::FindIndex - Get an index file for status files              /*{{{*/
-bool edspSystem::FindIndex(pkgCache::PkgFileIterator File,
+bool edspLikeSystem::FindIndex(pkgCache::PkgFileIterator File,
                          pkgIndexFile *&Found) const
 {
    if (StatusFile == 0)
       return false;
    if (StatusFile->FindInCache(*File.Cache()) == File)
    {
-      Found = StatusFile;
+      Found = StatusFile.get();
       return true;
    }
 
    return false;
 }
                                                                        /*}}}*/
+bool edspSystem::AddStatusFiles(std::vector<pkgIndexFile *> &List)     /*{{{*/
+{
+   if (StatusFile == nullptr)
+   {
+      if (_config->Find("edsp::scenario", "") == "/nonexistent/stdin")
+        StatusFile.reset(new edspIndex("/nonexistent/stdin"));
+      else
+        StatusFile.reset(new edspIndex(_config->FindFile("edsp::scenario")));
+   }
+   List.push_back(StatusFile.get());
+   return true;
+}
+                                                                       /*}}}*/
+
+edspLikeSystem::~edspLikeSystem() {}
+edspSystem::~edspSystem()
+{
+   if (tempDir.empty())
+      return;
+
+   RemoveFile("~edspSystem", tempStatesFile);
+   RemoveFile("~edspSystem", tempPrefsFile);
+   rmdir(tempDir.c_str());
+}
 
 APT_HIDDEN edspSystem edspSys;
index aa4298f01b19ab060e0032d8279a81bbb1ac89f6..2c429c3d6839368dbf6957763043bebaa08cea83 100644 (file)
@@ -14,6 +14,7 @@
 #include <apt-pkg/cacheiterators.h>
 #include <apt-pkg/pkgcache.h>
 
+#include <memory>
 #include <vector>
 
 #include <apt-pkg/macros.h>
@@ -22,28 +23,36 @@ class Configuration;
 class pkgDepCache;
 class pkgIndexFile;
 class pkgPackageManager;
-class edspIndex;
 
-class edspSystemPrivate;
-class APT_HIDDEN edspSystem : public pkgSystem
+class APT_HIDDEN edspLikeSystem : public pkgSystem
 {
-   /** \brief dpointer placeholder (for later in case we need it) */
-   edspSystemPrivate * const d;
-
-   edspIndex *StatusFile;
-
-   public:
+protected:
+   std::unique_ptr<pkgIndexFile> StatusFile;
 
+public:
    virtual bool Lock() APT_OVERRIDE APT_CONST;
    virtual bool UnLock(bool NoErrors = false) APT_OVERRIDE APT_CONST;
    virtual pkgPackageManager *CreatePM(pkgDepCache *Cache) const APT_OVERRIDE APT_CONST;
    virtual bool Initialize(Configuration &Cnf) APT_OVERRIDE;
    virtual bool ArchiveSupported(const char *Type) APT_OVERRIDE APT_CONST;
    virtual signed Score(Configuration const &Cnf) APT_OVERRIDE;
-   virtual bool AddStatusFiles(std::vector<pkgIndexFile *> &List) APT_OVERRIDE;
    virtual bool FindIndex(pkgCache::PkgFileIterator File,
                          pkgIndexFile *&Found) const APT_OVERRIDE;
 
+   edspLikeSystem(char const * const Label);
+   virtual ~edspLikeSystem();
+};
+
+class APT_HIDDEN edspSystem : public edspLikeSystem
+{
+   std::string tempDir;
+   std::string tempStatesFile;
+   std::string tempPrefsFile;
+
+public:
+   virtual bool Initialize(Configuration &Cnf) APT_OVERRIDE;
+   virtual bool AddStatusFiles(std::vector<pkgIndexFile *> &List) APT_OVERRIDE;
+
    edspSystem();
    virtual ~edspSystem();
 };