Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / zypp_detail / ZYppImpl.cc
index c721c4e..8af290a 100644 (file)
  *
 */
 
-#include <sys/utsname.h>
-#include <unistd.h>
 #include <iostream>
-#include <fstream>
 #include "zypp/TmpPath.h"
 #include "zypp/base/Logger.h"
 #include "zypp/base/String.h"
 
 #include "zypp/zypp_detail/ZYppImpl.h"
-#include "zypp/detail/ResImplTraits.h"
-#include "zypp/solver/detail/Helper.h"
 #include "zypp/target/TargetImpl.h"
 #include "zypp/ZYpp.h"
-#include "zypp/NVRAD.h"
-#include "zypp/Language.h"
 #include "zypp/DiskUsageCounter.h"
-#include "zypp/NameKindProxy.h"
-#include "zypp/Locks.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()
-    {
-      Locale ret( "en" );
-      const char * envlist[] = { "LC_ALL", "LC_CTYPE", "LANG", NULL };
-      for ( const char ** envvar = envlist; *envvar; ++envvar )
-        {
-         const 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;
-    }
-
-    Arch defaultArchitecture()
+  ///////////////////////////////////////////////////////////////////
+  namespace media
+  {
+    ScopedDisableMediaChangeReport::ScopedDisableMediaChangeReport( bool condition_r )
     {
-      Arch architecture;
-
-      // detect the true architecture
-      struct utsname buf;
-      if ( uname( &buf ) < 0 )
-        {
-          ERR << "Can't determine system architecture" << endl;
-        }
-      else
-        {
-          architecture = Arch( buf.machine );
-          DBG << "uname architecture is '" << buf.machine << "'" << endl;
-
-          // some CPUs report i686 but dont implement cx8 and cmov
-          // check for both flags in /proc/cpuinfo and downgrade
-          // to i586 if either is missing (cf bug #18885)
-
-          if ( architecture == Arch_i686 )
-            {
-              std::ifstream cpuinfo( "/proc/cpuinfo" );
-              if ( !cpuinfo )
-                {
-                  ERR << "Cant open /proc/cpuinfo" << endl;
-                }
-              else
-                {
-                  char infoline[1024];
-                  while ( cpuinfo.good() )
-                    {
-                      if ( !cpuinfo.getline( infoline, 1024, '\n' ) )
-                        {
-                          if ( cpuinfo.eof() )
-                            break;
-                        }
-                      if ( strncmp( infoline, "flags", 5 ) == 0 )
-                        {
-                          std::string flagsline( infoline );
-                          if ( flagsline.find( "cx8" ) == std::string::npos
-                               || flagsline.find( "cmov" ) == std::string::npos )
-                            {
-                              architecture = Arch_i586;
-                              DBG << "CPU lacks 'cx8' or 'cmov': architecture downgraded to '" << architecture << "'" << endl;
-                            }
-                          break;
-                        } // flags found
-                    } // read proc/cpuinfo
-                } // proc/cpuinfo opened
-            } // i686 extra flags check
-        }
-
-      if ( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) )
+      static weak_ptr<callback::TempConnect<media::MediaChangeReport> > globalguard;
+      if ( condition_r && ! (_guard = globalguard.lock()) )
       {
-        architecture = Arch( getenv( "ZYPP_TESTSUITE_FAKE_ARCH" ) );
-        WAR << "ZYPP_TESTSUITE_FAKE_ARCH: Setting fake system architecture for test purpuses to: '" << architecture << "'" << endl;
+       // aquire a new one....
+       _guard.reset( new callback::TempConnect<media::MediaChangeReport>() );
+       globalguard = _guard;
       }
-
-      return architecture;
     }
+  } // namespace media
+  ///////////////////////////////////////////////////////////////////
+
+  callback::SendReport<JobReport> & JobReport::instance()
+  {
+    static callback::SendReport<JobReport> _report;
+    return _report;
+  }
+
+
+  ///////////////////////////////////////////////////////////////////
+  namespace zypp_detail
+  { /////////////////////////////////////////////////////////////////
+
     ///////////////////////////////////////////////////////////////////
     //
     // METHOD NAME : ZYppImpl::ZYppImpl
     // METHOD TYPE : Constructor
     //
     ZYppImpl::ZYppImpl()
-    : _textLocale( defaultTextLocale() )
-    , _pool()
-    , _target(0)
-    , _resolver( new Resolver(_pool.accessor()) )
-    , _architecture( defaultArchitecture() )
+    : _target(0)
+    , _resolver( new Resolver( ResPool::instance()) )
     {
-      MIL << "libzypp: " << VERSION << " built " << __DATE__ << " " <<  __TIME__ << endl;
-      MIL << "defaultTextLocale: '" << _textLocale << "'" << endl;
-      MIL << "System architecture is '" << _architecture << "'" << endl;
-
-      MIL << "initializing keyring..." << std::endl;
-      //_keyring = new KeyRing(homePath() + Pathname("/keyring/all"), homePath() + Pathname("/keyring/trusted"));
+      ZConfig::instance().about( MIL );
+      MIL << "Initializing keyring..." << std::endl;
       _keyring = new KeyRing(tmpPath());
     }
 
@@ -154,30 +83,6 @@ namespace zypp
     //------------------------------------------------------------------------
     // add/remove resolvables
 
-    void ZYppImpl::addResolvables (const ResStore& store, bool installed)
-    {
-       _pool.insert(store.begin(), store.end(), installed);
-    }
-
-    void ZYppImpl::removeResolvables (const ResStore& store)
-    {
-        for (ResStore::iterator it = store.begin(); it != store.end(); ++it)
-       {
-           _pool.erase(*it);
-       }
-    }
-
-    void ZYppImpl::removeInstalledResolvables ()
-    {
-        for (ResPool::const_iterator it = pool().begin(); it != pool().end();)
-       {
-           ResPool::const_iterator next = it; ++next;
-           if (it->status().isInstalled())
-               _pool.erase( *it );
-           it = next;
-       }
-    }
-
     DiskUsageCounter::MountPointSet ZYppImpl::diskUsage()
     {
       if ( ! _disk_usage )
@@ -211,43 +116,27 @@ namespace zypp
       return _target;
      }
 
-    void ZYppImpl::initializeTarget(const Pathname & root)
+    void ZYppImpl::initializeTarget( const Pathname & root, bool doRebuild_r )
     {
-      MIL << "initTarget( " << root << endl;
+      MIL << "initTarget( " << root << (doRebuild_r?", rebuilddb":"") << ")" << endl;
       if (_target) {
-       if (_target->root() == root) {
-           MIL << "Repeated call to initializeTarget()" << endl;
-           return;
-       }
-       removeInstalledResolvables( );
-      }
-      _target = new Target( root );
-      _target->enableStorage( root );
-    }
+          if (_target->root() == root) {
+              MIL << "Repeated call to initializeTarget()" << endl;
+              return;
+          }
+
+          _target->unload();
 
-    void ZYppImpl::initTarget(const Pathname & root, bool commit_only)
-    {
-      MIL << "initTarget( " << root << ", " << commit_only << ")" << endl;
-      if (_target) {
-        if (_target->root() == root) {
-          MIL << "Repeated call to initTarget()" << endl;
-          return;
-        }
-        removeInstalledResolvables( );
-      }
-      _target = new Target( root );
-      _target->enableStorage( root );
-      if (!commit_only)
-      {
-        addResolvables( _target->resolvables(), true );
       }
+      _target = new Target( root, doRebuild_r );
+      _target->buildCache();
     }
 
-
     void ZYppImpl::finishTarget()
     {
       if (_target)
-       removeInstalledResolvables();
+          _target->unload();
+
       _target = 0;
     }
 
@@ -258,6 +147,8 @@ namespace zypp
      * and target used for transact. */
     ZYppCommitResult ZYppImpl::commit( const ZYppCommitPolicy & policy_r )
     {
+      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.") );
@@ -269,16 +160,18 @@ namespace zypp
 
       ZYppCommitResult res = _target->_pimpl->commit( pool(), policy_r );
 
-      if (! policy_r.dryRun() ) {
-        // Tag target data invalid, so they are reloaded on the next call to
-        // target->resolvables(). Actually the target should do this without
-        // foreign help.
-        _target->reset();
-       removeInstalledResolvables();
+      if (! policy_r.dryRun() )
+      {
         if ( policy_r.syncPoolAfterCommit() )
           {
             // reload new status from target
-            addResolvables( _target->resolvables(), true );
+            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();
           }
       }
 
@@ -294,108 +187,11 @@ namespace zypp
       _target->_pimpl->installSrcPackage( srcPackage_r );
     }
 
-    //------------------------------------------------------------------------
-    // locales
-
-    /** */
-    void ZYppImpl::setRequestedLocales( const LocaleSet & locales_r )
-    {
-      ResPool mpool( pool() );
-      // assert all requested are available
-      for ( LocaleSet::const_iterator it = locales_r.begin();
-            it != locales_r.end(); ++it )
-        {
-          NameKindProxy select( nameKindProxy<Language>( mpool, it->code() ) );
-          if ( select.installedEmpty() && select.availableEmpty() )
-            _pool.insert( Language::availableInstance( *it ) );
-        }
-
-      // now adjust status
-      for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
-            it != mpool.byKindEnd<Language>(); ++it )
-        {
-          NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
-          if ( locales_r.find( Locale( (*it)->name() ) ) != locales_r.end() )
-            {
-              // Language is requested
-              if ( select.installedEmpty() )
-                {
-                  if ( select.availableEmpty() )
-                    {
-                      // no item ==> provide available to install
-                      _pool.insert( Language::availableInstance( Locale((*it)->name()) ) );
-                      select = nameKindProxy<Language>( mpool, (*it)->name() );
-                    }
-                  // available only ==> to install
-                  select.availableBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
-                }
-              else
-                {
-                  // installed ==> keep it
-                  select.installedBegin()->status().setTransactValue( ResStatus::KEEP_STATE, ResStatus::USER );
-                  if ( ! select.availableEmpty() )
-                    {
-                      // both items ==> keep
-                      select.availableBegin()->status().resetTransact( ResStatus::USER );
-                    }
-                }
-            }
-          else
-            {
-              // Language is NOT requested
-              if ( ! select.installedEmpty() )
-                select.installedBegin()->status().setTransactValue( ResStatus::TRANSACT, ResStatus::USER );
-              if ( ! select.availableEmpty() )
-                select.availableBegin()->status().resetTransact( ResStatus::USER );
-            }
-        }
-    }
-
-    /** */
-    ZYppImpl::LocaleSet ZYppImpl::getAvailableLocales() const
-    {
-      ZYpp::LocaleSet ret;
-      ResPool mpool( pool() );
-      for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
-            it != mpool.byKindEnd<Language>(); ++it )
-        {
-          if ( (*it).status().isUninstalled() ) // available!
-            ret.insert( Locale( (*it)->name() ) );
-        }
-      return ret;
-    }
-
-    /** */
-    ZYppImpl::LocaleSet ZYppImpl::getRequestedLocales() const
-    {
-      ZYpp::LocaleSet ret;
-      ResPool mpool( pool() );
-      for ( ResPool::byKind_iterator it = mpool.byKindBegin<Language>();
-            it != mpool.byKindEnd<Language>(); ++it )
-        {
-          NameKindProxy select( nameKindProxy<Language>( mpool, (*it)->name() ) );
-          if ( ! select.installedEmpty()
-               && select.installedBegin()->status().getTransactValue() != ResStatus::TRANSACT )
-            ret.insert( Locale( (*it)->name() ) );
-          else if ( ! select.availableEmpty()
-                    && select.availableBegin()->status().getTransactValue() == ResStatus::TRANSACT )
-            ret.insert( Locale( (*it)->name() ) );
-        }
-      return ret;
-    }
-
-    void ZYppImpl::availableLocale( const Locale & locale_r )
-    {
-      _pool.insert( Language::availableInstance( locale_r ) );
-    }
-
-    //------------------------------------------------------------------------
-    // architecture
-
-    void ZYppImpl::setArchitecture( const Arch & arch )
+    ManagedFile ZYppImpl::provideSrcPackage( const SrcPackage_constPtr & srcPackage_r )
     {
-       _architecture = arch;
-       if (_resolver) _resolver->setArchitecture( arch );
+      if (! _target)
+        ZYPP_THROW( Exception("Target not initialized.") );
+      return _target->_pimpl->provideSrcPackage( srcPackage_r );
     }
 
     //------------------------------------------------------------------------
@@ -413,34 +209,6 @@ namespace zypp
       return zypp_tmp_dir.path();
     }
 
-    int ZYppImpl::applyLocks()
-    {
-      Pathname locksrcPath( "/etc/zypp/locks" );
-      try
-      {
-        Target_Ptr trg( target() );
-        if ( trg )
-          locksrcPath = trg->root() / locksrcPath;
-      }
-      catch ( ... )
-      {
-        // noop: Someone decided to let target() throw if the ptr is NULL ;(
-      }
-
-      int num=0;
-      PathInfo locksrc( locksrcPath );
-      if ( locksrc.isFile() )
-      {
-        MIL << "Reading locks from '" << locksrcPath << "'" << endl;
-        num = zypp::locks::readLocks( pool(), locksrcPath );
-        MIL << num << " items locked." << endl;
-      }
-      else
-      {
-        MIL << "No file '" << locksrcPath << "' to read locks from" << endl;
-      }
-      return num;
-    }
     /******************************************************************
      **
      **        FUNCTION NAME : operator<<