Imported Upstream version 15.19.0
[platform/upstream/libzypp.git] / zypp / ZConfig.cc
index a68973e..e66235c 100644 (file)
 */
 extern "C"
 {
+#include <features.h>
 #include <sys/utsname.h>
+#if __GLIBC_PREREQ (2,16)
+#include <sys/auxv.h>  // getauxval for PPC64P7 detection
+#endif
 #include <unistd.h>
 #include <solv/solvversion.h>
 }
 #include <iostream>
 #include <fstream>
-#include "zypp/base/Logger.h"
+#include "zypp/base/LogTools.h"
 #include "zypp/base/IOStream.h"
 #include "zypp/base/InputStream.h"
 #include "zypp/base/String.h"
+#include "zypp/base/Regex.h"
 
 #include "zypp/ZConfig.h"
 #include "zypp/ZYppFactory.h"
@@ -28,6 +33,7 @@ extern "C"
 #include "zypp/parser/IniDict.h"
 
 #include "zypp/sat/Pool.h"
+#include "zypp/sat/detail/PoolImpl.h"
 
 using namespace std;
 using namespace zypp::filesystem;
@@ -127,6 +133,37 @@ namespace zypp
           ERR << "Cant open " << PathInfo("/proc/cpuinfo") << endl;
         }
       }
+      else if ( architecture == Arch_armv7l || architecture == Arch_armv6l )
+      {
+       std::ifstream platform( "/etc/rpm/platform" );
+       if (platform)
+       {
+         for( iostr::EachLine in( platform ); in; in.next() )
+         {
+           if ( str::hasPrefix( *in, "armv7hl-" ) )
+           {
+             architecture = Arch_armv7hl;
+             WAR << "/etc/rpm/platform contains armv7hl-: architecture upgraded to '" << architecture << "'" << endl;
+             break;
+           }
+           if ( str::hasPrefix( *in, "armv6hl-" ) )
+           {
+             architecture = Arch_armv6hl;
+             WAR << "/etc/rpm/platform contains armv6hl-: architecture upgraded to '" << architecture << "'" << endl;
+             break;
+           }
+         }
+       }
+      }
+#if __GLIBC_PREREQ (2,16)
+      else if ( architecture == Arch_ppc64 )
+      {
+       const char * platform = (const char *)getauxval( AT_PLATFORM );
+       int powerlvl;
+       if ( platform && sscanf( platform, "power%d", &powerlvl ) == 1 && powerlvl > 6 )
+         architecture = Arch_ppc64p7;
+      }
+#endif
       return architecture;
     }
 
@@ -149,7 +186,7 @@ namespace zypp
      */
     Locale _autodetectTextLocale()
     {
-      Locale ret( "en" );
+      Locale ret( Locale::enCode );
       const char * envlist[] = { "LC_ALL", "LC_MESSAGES", "LANG", NULL };
       for ( const char ** envvar = envlist; *envvar; ++envvar )
       {
@@ -160,7 +197,7 @@ namespace zypp
           if ( envstr != "POSIX" && envstr != "C" )
           {
             Locale lang( envstr );
-            if ( ! lang.code().empty() )
+            if ( lang )
             {
               MIL << "Found " << *envvar << "=" << envstr << endl;
               ret = lang;
@@ -175,15 +212,28 @@ namespace zypp
       return ret;
     }
 
+
+    inline Pathname _autodetectSystemRoot()
+    {
+      Target_Ptr target( getZYpp()->getTarget() );
+      return target ? target->root() : Pathname();
+    }
+
+    inline Pathname _autodetectZyppConfPath()
+    {
+      const char *env_confpath = getenv( "ZYPP_CONF" );
+      return env_confpath ? env_confpath : "/etc/zypp/zypp.conf";
+    }
+
    /////////////////////////////////////////////////////////////////
   } // namespace zypp
   ///////////////////////////////////////////////////////////////////
 
   /** Mutable option. */
-  template<class _Tp>
+  template<class Tp>
       struct Option
       {
-       typedef _Tp value_type;
+       typedef Tp value_type;
 
        /** No default ctor, explicit initialisation! */
        Option( const value_type & initial_r )
@@ -211,14 +261,14 @@ namespace zypp
       };
 
   /** Mutable option with initial value also remembering a config value. */
-  template<class _Tp>
-      struct DefaultOption : public Option<_Tp>
+  template<class Tp>
+      struct DefaultOption : public Option<Tp>
       {
-       typedef _Tp         value_type;
-       typedef Option<_Tp> option_type;
+       typedef Tp         value_type;
+       typedef Option<Tp> option_type;
 
         DefaultOption( const value_type & initial_r )
-          : Option<_Tp>( initial_r ), _default( initial_r )
+          : Option<Tp>( initial_r ), _default( initial_r )
         {}
 
        /** Reset value to the current default. */
@@ -252,6 +302,8 @@ namespace zypp
   */
   class ZConfig::Impl
   {
+    typedef std::set<std::string> MultiversionSpec;
+
     public:
       Impl( const Pathname & override_r = Pathname() )
         : _parsedZyppConf              ( override_r )
@@ -268,9 +320,17 @@ namespace zypp
         , download_min_download_speed  ( 0 )
         , download_max_download_speed  ( 0 )
         , download_max_silent_tries    ( 5 )
+        , download_transfer_timeout    ( 180 )
         , commit_downloadMode          ( DownloadDefault )
+       , gpgCheck                      ( true )
+       , repoGpgCheck                  ( indeterminate )
+       , pkgGpgCheck                   ( indeterminate )
         , solver_onlyRequires          ( false )
         , solver_allowVendorChange     ( false )
+       , solver_dupAllowDowngrade      ( true )
+       , solver_dupAllowNameChange     ( true )
+       , solver_dupAllowArchChange     ( true )
+       , solver_dupAllowVendorChange   ( true )
         , solver_cleandepsOnRemove     ( false )
         , solver_upgradeTestcasesToKeep        ( 2 )
         , solverUpgradeRemoveDroppedPackages( true )
@@ -282,8 +342,7 @@ namespace zypp
         // ZYPP_CONF might override /etc/zypp/zypp.conf
         if ( _parsedZyppConf.empty() )
         {
-          const char *env_confpath = getenv( "ZYPP_CONF" );
-          _parsedZyppConf = env_confpath ? env_confpath : "/etc/zypp/zypp.conf";
+          _parsedZyppConf = _autodetectZyppConfPath();
         }
         else
         {
@@ -394,10 +453,28 @@ namespace zypp
                 {
                   str::strtonum(value, download_max_silent_tries);
                 }
+                else if ( entry == "download.transfer_timeout" )
+                {
+                  str::strtonum(value, download_transfer_timeout);
+                 if ( download_transfer_timeout < 0 )          download_transfer_timeout = 0;
+                 else if ( download_transfer_timeout > 3600 )  download_transfer_timeout = 3600;
+                }
                 else if ( entry == "commit.downloadMode" )
                 {
                   commit_downloadMode.set( deserializeDownloadMode( value ) );
                 }
+                else if ( entry == "gpgcheck" )
+               {
+                 gpgCheck.set( str::strToBool( value, gpgCheck ) );
+               }
+               else if ( entry == "repo_gpgcheck" )
+               {
+                 repoGpgCheck.set( str::strToTriBool( value ) );
+               }
+               else if ( entry == "pkg_gpgcheck" )
+               {
+                 pkgGpgCheck.set( str::strToTriBool( value ) );
+               }
                 else if ( entry == "vendordir" )
                 {
                   cfg_vendor_path = Pathname(value);
@@ -414,6 +491,22 @@ namespace zypp
                 {
                   solver_allowVendorChange.set( str::strToBool( value, solver_allowVendorChange ) );
                 }
+                else if ( entry == "solver.dupAllowDowngrade" )
+               {
+                 solver_dupAllowDowngrade.set( str::strToBool( value, solver_dupAllowDowngrade ) );
+               }
+               else if ( entry == "solver.dupAllowNameChange" )
+               {
+                 solver_dupAllowNameChange.set( str::strToBool( value, solver_dupAllowNameChange ) );
+               }
+               else if ( entry == "solver.dupAllowArchChange" )
+               {
+                 solver_dupAllowArchChange.set( str::strToBool( value, solver_dupAllowArchChange ) );
+               }
+               else if ( entry == "solver.dupAllowVendorChange" )
+               {
+                 solver_dupAllowVendorChange.set( str::strToBool( value, solver_dupAllowVendorChange ) );
+               }
                 else if ( entry == "solver.cleandepsOnRemove" )
                 {
                   solver_cleandepsOnRemove.set( str::strToBool( value, solver_cleandepsOnRemove ) );
@@ -430,9 +523,14 @@ namespace zypp
                 {
                   solver_checkSystemFile = Pathname(value);
                 }
+                else if ( entry == "solver.checkSystemFileDir" )
+                {
+                  solver_checkSystemFileDir = Pathname(value);
+                }
                 else if ( entry == "multiversion" )
                 {
-                  str::split( value, inserter( _multiversion, _multiversion.end() ), ", \t" );
+                 MultiversionSpec & defSpec( _multiversionMap.getDefaultSpec() );
+                  str::splitEscaped( value, std::inserter( defSpec, defSpec.end() ), ", \t" );
                 }
                 else if ( entry == "locksfile.path" )
                 {
@@ -478,6 +576,8 @@ namespace zypp
               }
             }
           }
+          //
+
         }
         else
         {
@@ -539,19 +639,29 @@ namespace zypp
     int download_min_download_speed;
     int download_max_download_speed;
     int download_max_silent_tries;
+    int download_transfer_timeout;
 
     Option<DownloadMode> commit_downloadMode;
 
+    Option<bool>       gpgCheck;
+    Option<TriBool>    repoGpgCheck;
+    Option<TriBool>    pkgGpgCheck;
+
     Option<bool>       solver_onlyRequires;
     Option<bool>       solver_allowVendorChange;
+    Option<bool>       solver_dupAllowDowngrade;
+    Option<bool>       solver_dupAllowNameChange;
+    Option<bool>       solver_dupAllowArchChange;
+    Option<bool>       solver_dupAllowVendorChange;
     Option<bool>       solver_cleandepsOnRemove;
     Option<unsigned>   solver_upgradeTestcasesToKeep;
     DefaultOption<bool> solverUpgradeRemoveDroppedPackages;
 
     Pathname solver_checkSystemFile;
+    Pathname solver_checkSystemFileDir;
 
-    std::set<std::string> &            multiversion()          { return getMultiversion(); }
-    const std::set<std::string> &      multiversion() const    { return getMultiversion(); }
+    MultiversionSpec &         multiversion()          { return getMultiversion(); }
+    const MultiversionSpec &   multiversion() const    { return getMultiversion(); }
 
     bool apply_locks_file;
 
@@ -561,36 +671,96 @@ namespace zypp
     Pathname credentials_global_dir_path;
     Pathname credentials_global_file_path;
 
+    std::string userData;
+
     Option<Pathname> pluginsPath;
 
   private:
-    std::set<std::string> & getMultiversion() const
+    // HACK for bnc#906096: let pool re-evaluate multiversion spec
+    // if target root changes. ZConfig returns data sensitive to
+    // current target root.
+    // TODO Actually we'd need to scan the target systems zypp.conf and
+    // overlay all system specific values.
+    struct MultiversionMap
     {
-      if ( ! _multiversionInitialized )
+      typedef std::map<Pathname,MultiversionSpec> SpecMap;
+
+      MultiversionSpec & getSpec( Pathname root_r, const Impl & zConfImpl_r )  // from system at root
+      {
+       // _specMap[]     - the plain zypp.conf value
+       // _specMap[/]    - combine [] and multiversion.d scan
+       // _specMap[root] - scan root/zypp.conf and root/multiversion.d
+
+       if ( root_r.empty() )
+         root_r == "/";
+       bool cacheHit = _specMap.count( root_r );
+       MultiversionSpec & ret( _specMap[root_r] );     // creates new entry on the fly
+
+       if ( ! cacheHit )
+       {
+         if ( root_r == "/" )
+           ret.swap( _specMap[Pathname()] );           // original zypp.conf
+         else
+           scanConfAt( root_r, ret, zConfImpl_r );     // scan zypp.conf at root_r
+         scanDirAt( root_r, ret, zConfImpl_r );        // add multiversion.d at root_r
+         using zypp::operator<<;
+         MIL << "MultiversionSpec '" << root_r << "' = " << ret << endl;
+       }
+       return ret;
+      }
+
+      MultiversionSpec & getDefaultSpec()      // Spec from zypp.conf parsing; called before any getSpec
+      {        return _specMap[Pathname()]; }
+
+    private:
+      void scanConfAt( const Pathname root_r, MultiversionSpec & spec_r, const Impl & zConfImpl_r )
+      {
+       static const str::regex rx( "^multiversion *= *(.*)" );
+       str::smatch what;
+       iostr::simpleParseFile( InputStream( Pathname::assertprefix( root_r, _autodetectZyppConfPath() ) ),
+                               [&]( int num_r, std::string line_r )->bool
+                               {
+                                 if ( line_r[0] == 'm' && str::regex_match( line_r, what, rx ) )
+                                 {
+                                   str::splitEscaped( what[1], std::inserter( spec_r, spec_r.end() ), ", \t" );
+                                   return false;       // stop after match
+                                 }
+                                 return true;
+                               } );
+      }
+
+      void scanDirAt( const Pathname root_r, MultiversionSpec & spec_r, const Impl & zConfImpl_r )
       {
-       Pathname multiversionDir( cfg_multiversion_path );
+       // NOTE:  Actually we'd need to scan and use the root_r! zypp.conf values.
+       Pathname multiversionDir( zConfImpl_r.cfg_multiversion_path );
        if ( multiversionDir.empty() )
-         multiversionDir = ( cfg_config_path.empty() ? Pathname("/etc/zypp") : cfg_config_path ) / "multiversion.d";
+         multiversionDir = ( zConfImpl_r.cfg_config_path.empty()
+                           ? Pathname("/etc/zypp")
+                           : zConfImpl_r.cfg_config_path ) / "multiversion.d";
 
-       filesystem::dirForEach( multiversionDir,
-                               [this]( const Pathname & dir_r, const char *const & name_r )->bool
+       filesystem::dirForEach( Pathname::assertprefix( root_r, multiversionDir ),
+                               [&spec_r]( const Pathname & dir_r, const char *const & name_r )->bool
                                {
                                  MIL << "Parsing " << dir_r/name_r << endl;
                                  iostr::simpleParseFile( InputStream( dir_r/name_r ),
-                                                         [this]( int num_r, std::string line_r )->bool
+                                                         [&spec_r]( int num_r, std::string line_r )->bool
                                                          {
                                                            DBG << "  found " << line_r << endl;
-                                                          _multiversion.insert( line_r );
+                                                           spec_r.insert( std::move(line_r) );
                                                            return true;
                                                          } );
                                  return true;
                                } );
-       _multiversionInitialized = true;
       }
-      return _multiversion;
-    }
-    mutable std::set<std::string>      _multiversion;
-    mutable DefaultIntegral<bool,false>        _multiversionInitialized;
+
+    private:
+      SpecMap _specMap;
+    };
+
+    MultiversionSpec & getMultiversion() const
+    { return _multiversionMap.getSpec( _autodetectSystemRoot(), *this ); }
+
+    mutable MultiversionMap _multiversionMap;
   };
   ///////////////////////////////////////////////////////////////////
 
@@ -633,10 +803,7 @@ namespace zypp
   {}
 
   Pathname ZConfig::systemRoot() const
-  {
-    Target_Ptr target( getZYpp()->getTarget() );
-    return target ? target->root() : Pathname();
-  }
+  { return _autodetectSystemRoot(); }
 
   ///////////////////////////////////////////////////////////////////
   //
@@ -689,6 +856,31 @@ namespace zypp
   }
 
   ///////////////////////////////////////////////////////////////////
+  // user data
+  ///////////////////////////////////////////////////////////////////
+
+  bool ZConfig::hasUserData() const
+  { return !_pimpl->userData.empty(); }
+
+  std::string ZConfig::userData() const
+  { return _pimpl->userData; }
+
+  bool ZConfig::setUserData( const std::string & str_r )
+  {
+    for_( ch, str_r.begin(), str_r.end() )
+    {
+      if ( *ch < ' ' && *ch != '\t' )
+      {
+       ERR << "New user data string rejectded: char " << (int)*ch << " at position " <<  (ch - str_r.begin()) << endl;
+       return false;
+      }
+    }
+    MIL << "Set user data string to '" << str_r << "'" << endl;
+    _pimpl->userData = str_r;
+    return true;
+  }
+
+  ///////////////////////////////////////////////////////////////////
 
   Pathname ZConfig::repoCachePath() const
   {
@@ -731,7 +923,7 @@ namespace zypp
   Pathname ZConfig::knownServicesPath() const
   {
     return ( _pimpl->cfg_known_services_path.empty()
-        ? (configPath()/"services.d") : _pimpl->cfg_known_repos_path );
+        ? (configPath()/"services.d") : _pimpl->cfg_known_services_path );
   }
 
   Pathname ZConfig::vendorPath() const
@@ -790,15 +982,32 @@ namespace zypp
   long ZConfig::download_max_silent_tries() const
   { return _pimpl->download_max_silent_tries; }
 
+  long ZConfig::download_transfer_timeout() const
+  { return _pimpl->download_transfer_timeout; }
+
   DownloadMode ZConfig::commit_downloadMode() const
   { return _pimpl->commit_downloadMode; }
 
+  bool ZConfig::gpgCheck() const
+  { return _pimpl->gpgCheck; }
+
+  TriBool ZConfig::repoGpgCheck() const
+  { return _pimpl->repoGpgCheck; }
+
+  TriBool ZConfig::pkgGpgCheck() const
+  { return _pimpl->pkgGpgCheck; }
+
   bool ZConfig::solver_onlyRequires() const
   { return _pimpl->solver_onlyRequires; }
 
   bool ZConfig::solver_allowVendorChange() const
   { return _pimpl->solver_allowVendorChange; }
 
+  bool ZConfig::solver_dupAllowDowngrade() const       { return _pimpl->solver_dupAllowDowngrade; }
+  bool ZConfig::solver_dupAllowNameChange() const      { return _pimpl->solver_dupAllowNameChange; }
+  bool ZConfig::solver_dupAllowArchChange() const      { return _pimpl->solver_dupAllowArchChange; }
+  bool ZConfig::solver_dupAllowVendorChange() const    { return _pimpl->solver_dupAllowVendorChange; }
+
   bool ZConfig::solver_cleandepsOnRemove() const
   { return _pimpl->solver_cleandepsOnRemove; }
 
@@ -806,6 +1015,10 @@ namespace zypp
   { return ( _pimpl->solver_checkSystemFile.empty()
       ? (configPath()/"systemCheck") : _pimpl->solver_checkSystemFile ); }
 
+  Pathname ZConfig::solver_checkSystemFileDir() const
+  { return ( _pimpl->solver_checkSystemFileDir.empty()
+      ? (configPath()/"systemCheck.d") : _pimpl->solver_checkSystemFileDir ); }
+
   unsigned ZConfig::solver_upgradeTestcasesToKeep() const
   { return _pimpl->solver_upgradeTestcasesToKeep; }
 
@@ -813,9 +1026,19 @@ namespace zypp
   void ZConfig::setSolverUpgradeRemoveDroppedPackages( bool val_r )    { _pimpl->solverUpgradeRemoveDroppedPackages.set( val_r ); }
   void ZConfig::resetSolverUpgradeRemoveDroppedPackages()              { _pimpl->solverUpgradeRemoveDroppedPackages.restoreToDefault(); }
 
+  namespace
+  {
+    inline void sigMultiversionSpecChanged()
+    {
+      sat::detail::PoolMember::myPool().multiversionSpecChanged();
+    }
+  }
+
   const std::set<std::string> & ZConfig::multiversionSpec() const      { return _pimpl->multiversion(); }
-  void ZConfig::addMultiversionSpec( const std::string & name_r )      { _pimpl->multiversion().insert( name_r ); }
-  void ZConfig::removeMultiversionSpec( const std::string & name_r )   { _pimpl->multiversion().erase( name_r ); }
+  void ZConfig::multiversionSpec( std::set<std::string> new_r )                { _pimpl->multiversion().swap( new_r );         sigMultiversionSpecChanged(); }
+  void ZConfig::clearMultiversionSpec()                                        { _pimpl->multiversion().clear();               sigMultiversionSpecChanged(); }
+  void ZConfig::addMultiversionSpec( const std::string & name_r )      { _pimpl->multiversion().insert( name_r );      sigMultiversionSpecChanged(); }
+  void ZConfig::removeMultiversionSpec( const std::string & name_r )   { _pimpl->multiversion().erase( name_r );       sigMultiversionSpecChanged(); }
 
   bool ZConfig::apply_locks_file() const
   { return _pimpl->apply_locks_file; }