Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / zypp_detail / ZYppImpl.cc
index 4a6ea44..8af290a 100644 (file)
  *
 */
 
-#include <sys/utsname.h>
 #include <iostream>
-//#include "zypp/base/Logger.h"
+#include "zypp/TmpPath.h"
+#include "zypp/base/Logger.h"
+#include "zypp/base/String.h"
 
 #include "zypp/zypp_detail/ZYppImpl.h"
+#include "zypp/target/TargetImpl.h"
+#include "zypp/ZYpp.h"
+#include "zypp/DiskUsageCounter.h"
+#include "zypp/ZConfig.h"
+#include "zypp/sat/Pool.h"
+#include "zypp/PoolItem.h"
+
+#include "zypp/ZYppCallbacks.h"        // JobReport::instance
 
 using std::endl;
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
 { /////////////////////////////////////////////////////////////////
-  ///////////////////////////////////////////////////////////////////
-  namespace zypp_detail
-  { /////////////////////////////////////////////////////////////////
 
-    inline Locale defaultTextLocale()
+  ///////////////////////////////////////////////////////////////////
+  namespace media
+  {
+    ScopedDisableMediaChangeReport::ScopedDisableMediaChangeReport( bool condition_r )
     {
-      Locale ret( "en" );
-      char * envlist[] = { "LC_ALL", "LC_CTYPE", "LANG", NULL };
-      for ( char ** envvar = envlist; *envvar; ++envvar )
-        {
-          char * envlang = getenv( *envvar );
-          if ( envlang )
-            {
-              std::string envstr( envlang );
-              if ( envstr != "POSIX" && envstr != "C" )
-                {
-                  Locale lang( envlang );
-                  if ( lang != Locale::noCode )
-                    {
-                      ret = lang;
-                      break;
-                    }
-                }
-            }
-        }
-      return ret;
+      static weak_ptr<callback::TempConnect<media::MediaChangeReport> > globalguard;
+      if ( condition_r && ! (_guard = globalguard.lock()) )
+      {
+       // aquire a new one....
+       _guard.reset( new callback::TempConnect<media::MediaChangeReport>() );
+       globalguard = _guard;
+      }
     }
+  } // namespace media
+  ///////////////////////////////////////////////////////////////////
+
+  callback::SendReport<JobReport> & JobReport::instance()
+  {
+    static callback::SendReport<JobReport> _report;
+    return _report;
+  }
+
+
+  ///////////////////////////////////////////////////////////////////
+  namespace zypp_detail
+  { /////////////////////////////////////////////////////////////////
 
     ///////////////////////////////////////////////////////////////////
     //
@@ -55,22 +64,12 @@ namespace zypp
     // METHOD TYPE : Constructor
     //
     ZYppImpl::ZYppImpl()
-    : _textLocale( defaultTextLocale() )
-    , _pool()
-    , _sourceFeed( _pool )
-    , _resolver( new Resolver(_pool.accessor()) )
+    : _target(0)
+    , _resolver( new Resolver( ResPool::instance()) )
     {
-      MIL << "defaultTextLocale: '" << _textLocale << "'" << endl;
-
-      struct utsname buf;
-      if (uname (&buf) < 0) {
-       ERR << "Can't determine system architecture" << endl;
-      }
-      else {
-       MIL << "System architecture is '" << buf.machine << "'" << endl;
-       _architecture = Arch(buf.machine);
-      }
-
+      ZConfig::instance().about( MIL );
+      MIL << "Initializing keyring..." << std::endl;
+      _keyring = new KeyRing(tmpPath());
     }
 
     ///////////////////////////////////////////////////////////////////
@@ -79,22 +78,37 @@ namespace zypp
     // METHOD TYPE : Destructor
     //
     ZYppImpl::~ZYppImpl()
+    {}
+
+    //------------------------------------------------------------------------
+    // add/remove resolvables
+
+    DiskUsageCounter::MountPointSet ZYppImpl::diskUsage()
     {
+      if ( ! _disk_usage )
+      {
+        setPartitions( DiskUsageCounter::detectMountPoints() );
+      }
+      return _disk_usage->disk_usage(pool());
     }
 
-    void ZYppImpl::addResolvables (const ResStore& store, bool installed)
+    void ZYppImpl::setPartitions(const DiskUsageCounter::MountPointSet &mp)
     {
-       _pool.insert(store.begin(), store.end(), installed);
+      _disk_usage.reset(new DiskUsageCounter());
+      _disk_usage->setMountPoints(mp);
     }
 
-    void ZYppImpl::removeResolvables (const ResStore& store)
+    DiskUsageCounter::MountPointSet ZYppImpl::getPartitions() const
     {
-        for (ResStore::iterator it = store.begin(); it != store.end(); it++)
-       {
-           _pool.erase(*it);
-       }
+      if (_disk_usage)
+        return _disk_usage->getMountPoints();
+      else
+        return DiskUsageCounter::detectMountPoints();
     }
 
+    //------------------------------------------------------------------------
+    // target
+
     Target_Ptr ZYppImpl::target() const
     {
       if (! _target)
@@ -102,54 +116,97 @@ namespace zypp
       return _target;
      }
 
-    void ZYppImpl::initTarget(const Pathname & root, bool commit_only)
+    void ZYppImpl::initializeTarget( const Pathname & root, bool doRebuild_r )
     {
-      if (_target)
-       _target = Target_Ptr();
-      _target = new Target(root);
-      if (!commit_only)
-      {
-       _target->enableStorage(root);
-       addResolvables (_target->resolvables(), true);
+      MIL << "initTarget( " << root << (doRebuild_r?", rebuilddb":"") << ")" << endl;
+      if (_target) {
+          if (_target->root() == root) {
+              MIL << "Repeated call to initializeTarget()" << endl;
+              return;
+          }
+
+          _target->unload();
+
       }
+      _target = new Target( root, doRebuild_r );
+      _target->buildCache();
     }
 
     void ZYppImpl::finishTarget()
     {
-#warning not removing target resolvables for now
-//      if (_target)
-//     removeResolvables (_target->resolvables());
+      if (_target)
+          _target->unload();
+
       _target = 0;
     }
 
+    //------------------------------------------------------------------------
+    // commit
+
     /** \todo Remove workflow from target, lot's of it could be done here,
-    * and target used for transact. */
-    ZYpp::CommitResult ZYppImpl::commit( int medianr_r )
+     * and target used for transact. */
+    ZYppCommitResult ZYppImpl::commit( const ZYppCommitPolicy & policy_r )
     {
-      MIL << "Attempt to commit (medianr " << medianr_r << ")" << endl;
+      setenv( "ZYPP_IS_RUNNING", str::numstring(getpid()).c_str(), 1 );
+
+      if ( getenv("ZYPP_TESTSUITE_FAKE_ARCH") )
+      {
+        ZYPP_THROW( Exception("ZYPP_TESTSUITE_FAKE_ARCH set. Commit not allowed and disabled.") );
+      }
+
+      MIL << "Attempt to commit (" << policy_r << ")" << endl;
       if (! _target)
        ZYPP_THROW( Exception("Target not initialized.") );
 
-      ZYpp::CommitResult res;
-      // must redirect to Target::Impl. This kind of commit should not be
-      // in the Target interface.
-      res._result = _target->commit( pool(), medianr_r,
-                                     res._errors, res._remaining, res._srcremaining );
-
-      MIL << "Commit (medianr " << medianr_r << ") returned: "
-          << res._result
-          << " (errors " << res._errors.size()
-          << ", remaining " << res._remaining.size()
-          << ", srcremaining " << res._srcremaining.size()
-          << ")" << endl;
+      ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
+
+      if (! policy_r.dryRun() )
+      {
+        if ( policy_r.syncPoolAfterCommit() )
+          {
+            // reload new status from target
+            DBG << "reloading " << sat::Pool::instance().systemRepoAlias() << " repo to pool" << endl;
+            _target->load();
+          }
+        else
+          {
+            DBG << "unloading " << sat::Pool::instance().systemRepoAlias() << " repo from pool" << endl;
+            _target->unload();
+          }
+      }
+
+      MIL << "Commit (" << policy_r << ") returned: "
+          << res << endl;
       return res;
     }
 
+    void ZYppImpl::installSrcPackage( const SrcPackage_constPtr & srcPackage_r )
+    {
+      if (! _target)
+        ZYPP_THROW( Exception("Target not initialized.") );
+      _target->_pimpl->installSrcPackage( srcPackage_r );
+    }
+
+    ManagedFile ZYppImpl::provideSrcPackage( const SrcPackage_constPtr & srcPackage_r )
+    {
+      if (! _target)
+        ZYPP_THROW( Exception("Target not initialized.") );
+      return _target->_pimpl->provideSrcPackage( srcPackage_r );
+    }
+
+    //------------------------------------------------------------------------
+    // target store path
+
+    Pathname ZYppImpl::homePath() const
+    { return _home_path.empty() ? Pathname("/var/lib/zypp") : _home_path; }
+
+    void ZYppImpl::setHomePath( const Pathname & path )
+    { _home_path = path; }
 
-    void ZYppImpl::setArchitecture( const Arch & arch )
+    Pathname ZYppImpl::tmpPath() const
     {
-       _architecture = arch;
-       if (_resolver) _resolver->setArchitecture( arch );
+      static TmpDir zypp_tmp_dir( TmpPath::defaultLocation(), "zypp." );
+      return zypp_tmp_dir.path();
     }
 
     /******************************************************************