Use libsolv includes and adjust documentation
[platform/upstream/libzypp.git] / zypp / sat / Solvable.cc
index 4757488..a578d57 100644 (file)
@@ -35,63 +35,83 @@ namespace zypp
   namespace sat
   { /////////////////////////////////////////////////////////////////
 
-    Solvable::SplitIdent::SplitIdent( IdString ident_r )
-    : _ident( ident_r )
+    namespace
     {
-      if ( ! ident_r )
-        return;
-
-      const char * ident = ident_r.c_str();
-      const char * sep = ::strchr( ident, ':' );
-
-      // no ':' in package names (hopefully)
-      if ( ! sep )
+      void _doSplit( IdString & _ident, ResKind & _kind, IdString & _name )
       {
-        _kind = ResKind::package;
-        _name = ident_r;
-        return;
+        if ( ! _ident )
+          return;
+
+       ResKind explicitKind = Solvable::SplitIdent::explicitKind( _ident.c_str() );
+       // NOTE: kind package and srcpackage do not have namespaced ident!
+       if ( ! explicitKind  )
+       {
+          _name = _ident;
+         // No kind defaults to package
+         if ( !_kind )
+           _kind = ResKind::package;
+         if ( ! ( _kind == ResKind::package || _kind == ResKind::srcpackage ) )
+           _ident = IdString( str::form( "%s:%s", _kind.c_str(), _ident.c_str() ) );
+       }
+       else
+       {
+         // strip kind spec from name
+         _name = IdString( ::strchr( _ident.c_str(), ':' )+1 );
+         _kind = explicitKind;
+         if ( _kind == ResKind::package || _kind == ResKind::srcpackage )
+           _ident = _name;
+       }
+       return;
       }
+    }
 
-      // save name
-      _name = IdString( sep+1 );
-      // quick check for well known kinds
-      if ( sep-ident >= 4 )
-      {
-        switch ( ident[3] )
-        {
-#define OUTS(K,S) if ( !::strncmp( ident, ResKind::K.c_str(), S ) ) _kind = ResKind::K
-          //             ----v
-          case 'c': OUTS( patch, 5 );       return; break;
-          case 'd': OUTS( product, 7 );     return; break;
-          case 'k': OUTS( package, 7 );     return; break;
-          case 'p': OUTS( srcpackage, 10 ); return; break;
-          case 't': OUTS( pattern, 7 );     return; break;
-#undef OUTS
-        }
-      }
+    Solvable::SplitIdent::SplitIdent( IdString ident_r )
+    : _ident( ident_r )
+    { _doSplit( _ident, _kind, _name ); }
 
-      // an unknown kind
-      _kind = ResKind( std::string( ident, sep-ident ) );
-    }
+    Solvable::SplitIdent::SplitIdent( const char * ident_r )
+    : _ident( ident_r )
+    { _doSplit( _ident, _kind, _name ); }
+
+    Solvable::SplitIdent::SplitIdent( const std::string & ident_r )
+    : _ident( ident_r )
+    { _doSplit( _ident, _kind, _name ); }
 
     Solvable::SplitIdent::SplitIdent( ResKind kind_r, IdString name_r )
-    : _kind( kind_r )
-    , _name( name_r )
-    {
-      if ( kind_r == ResKind::package || kind_r == ResKind::srcpackage )
-        _ident = _name;
-      else
-        _ident = IdString( str::form( "%s:%s", kind_r.c_str(), name_r.c_str() ) );
-    }
+    : _ident( name_r )
+    , _kind( kind_r )
+    { _doSplit( _ident, _kind, _name ); }
 
     Solvable::SplitIdent::SplitIdent( ResKind kind_r, const C_Str & name_r )
-    : _kind( kind_r )
-    , _name( name_r )
+    : _ident( name_r )
+    , _kind( kind_r )
+    { _doSplit( _ident, _kind, _name ); }
+
+    ResKind Solvable::SplitIdent::explicitKind( const char * ident_r )
     {
-      if ( kind_r == ResKind::package || kind_r == ResKind::srcpackage )
-        _ident = _name;
-      else
-        _ident = IdString( str::form( "%s:%s", kind_r.c_str(), name_r.c_str() ) );
+      if ( ! ident_r )
+       return ResKind();
+
+      const char * sep = ::strchr( ident_r, ':' );
+      if ( ! sep )
+       return ResKind();
+
+      ResKind ret;
+      if ( sep-ident_r >= 4 )
+      {
+       switch ( ident_r[3] )
+       {
+         #define OUTS(K,S) if ( !::strncmp( ident_r, ResKind::K.c_str(), S ) && ident_r[S] == ':' ) ret = ResKind::K
+         //             ----v
+         case 'c': OUTS( patch, 5 );       break;
+         case 'd': OUTS( product, 7 );     break;
+         case 'k': OUTS( package, 7 );     break;
+         case 'p': OUTS( srcpackage, 10 ); break;
+         case 't': OUTS( pattern, 7 );     break;
+         #undef OUTS
+       }
+      }
+      return ret;
     }
 
     /////////////////////////////////////////////////////////////////
@@ -136,6 +156,11 @@ namespace zypp
       return myPool().isSystemRepo( _solvable->repo );
     }
 
+    bool Solvable::onSystemByUser() const
+    {
+      return isSystem() && myPool().isOnSystemByUser( ident() );
+    }
+
     IdString Solvable::ident() const
     {
       NO_SOLVABLE_RETURN( IdString() );
@@ -159,7 +184,11 @@ namespace zypp
       }
       else
       {
-        s = ::solvable_lookup_str_lang( _solvable, attr.id(), lang_r.code().c_str() );
+       for ( Locale l( lang_r ); l != Locale::noCode; l = l.fallback() )
+         if ( (s = ::solvable_lookup_str_lang( _solvable, attr.id(), l.code().c_str(), 0 )) )
+           return s;
+         // here: no matching locale, so use default
+         s = ::solvable_lookup_str_lang( _solvable, attr.id(), 0, 0 );
       }
       return s ? s : std::string();
    }
@@ -198,8 +227,30 @@ namespace zypp
       return CheckSum( std::string(), s ); // try to autodetect
     }
 
+    ///////////////////////////////////////////////////////////////////
+    namespace
+    {
+      inline Pathname lookupDatadirIn( Repository repor_r )
+      {
+        static const sat::SolvAttr susetagsDatadir( "susetags:datadir" );
+        Pathname ret;
+        // First look for repo attribute "susetags:datadir". If not found,
+        // look into the solvables as Code11 libsolv placed it there.
+        sat::LookupRepoAttr datadir( susetagsDatadir, repor_r );
+        if ( ! datadir.empty() )
+          ret = datadir.begin().asString();
+        else
+        {
+          sat::LookupAttr datadir( susetagsDatadir, repor_r );
+          if ( ! datadir.empty() )
+            ret = datadir.begin().asString();
+        }
+        return ret;
+      }
+    }
+    ///////////////////////////////////////////////////////////////////
+
     OnMediaLocation Solvable::lookupLocation() const
-    //std::string Solvable::lookupLocation( unsigned & medianr ) const
     {
       NO_SOLVABLE_RETURN( OnMediaLocation() );
       // medianumber and path
@@ -211,27 +262,23 @@ namespace zypp
       OnMediaLocation ret;
 
       Pathname path;
-      static const sat::SolvAttr susetagsDatadir( "susetags:datadir" );
       switch ( repository().info().type().toEnum() )
       {
         case repo::RepoType::NONE_e:
-          {
-            sat::LookupAttr datadir( susetagsDatadir, repository() );
-            if ( ! datadir.empty() )
-            {
-              repository().info().setProbedType( repo::RepoType::YAST2_e );
-              path = datadir.begin().asString();
-            }
-            path = datadir.empty() ? "suse" : datadir.begin().c_str();
-          }
-          break;
+        {
+          path = lookupDatadirIn( repository() );
+          if ( ! path.empty() )
+            repository().info().setProbedType( repo::RepoType::YAST2_e );
+        }
+        break;
 
         case repo::RepoType::YAST2_e:
-          {
-            sat::LookupAttr datadir( susetagsDatadir, repository() );
-            path = datadir.empty() ? "suse" : datadir.begin().c_str();
-          }
-          break;
+        {
+          path = lookupDatadirIn( repository() );
+          if ( path.empty() )
+            path = "suse";
+        }
+        break;
 
         default:
           break;
@@ -339,12 +386,9 @@ namespace zypp
       //return ArchId( _solvable->arch );
     }
 
-    bool Solvable::installOnly() const
+    bool Solvable::multiversionInstall() const
     {
-       std::set<IdString> multiversion = ZConfig::instance().multiversion();
-       if (multiversion.find(ident()) != multiversion.end())
-             return true;
-       return false;
+      return myPool().isMultiversion( ident() );
     }
 
     IdString Solvable::vendor() const
@@ -423,6 +467,39 @@ namespace zypp
                    : Capabilities();
     }
 
+    CapabilitySet Solvable::providesNamespace( const std::string & namespace_r ) const
+    {
+      NO_SOLVABLE_RETURN( CapabilitySet() );
+      CapabilitySet ret;
+      Capabilities caps( provides() );
+      for_( it, caps.begin(), caps.end() )
+      {
+        CapDetail caprep( it->detail() );
+        if ( str::hasPrefix( caprep.name().c_str(), namespace_r ) && *(caprep.name().c_str()+namespace_r.size()) == '(' )
+          ret.insert( *it );
+      }
+      return ret;
+   }
+
+    CapabilitySet Solvable::valuesOfNamespace( const std::string & namespace_r ) const
+    {
+      NO_SOLVABLE_RETURN( CapabilitySet() );
+      CapabilitySet ret;
+      Capabilities caps( provides() );
+      for_( it, caps.begin(), caps.end() )
+      {
+        CapDetail caprep( it->detail() );
+        if ( str::hasPrefix( caprep.name().c_str(), namespace_r ) && *(caprep.name().c_str()+namespace_r.size()) == '(' )
+        {
+          std::string value( caprep.name().c_str()+namespace_r.size()+1 );
+          value[value.size()-1] = '\0'; // erase the trailing ')'
+          ret.insert( Capability( value, caprep.op(), caprep.ed() ) );
+        }
+      }
+      return ret;
+    }
+
+
     std::string Solvable::asString() const
     {
       NO_SOLVABLE_RETURN( (_id == detail::systemSolvableId ? "systemSolvable" : "noSolvable") );
@@ -432,6 +509,12 @@ namespace zypp
                         IdString( _solvable->arch ).c_str() );
     }
 
+    bool Solvable::identical( Solvable rhs ) const
+    {
+      NO_SOLVABLE_RETURN( ! rhs.get() );
+      ::_Solvable * rhssolvable( rhs.get() );
+      return rhssolvable && ( _solvable == rhssolvable || ::solvable_identical( _solvable, rhssolvable ) );
+    }
 
     ///////////////////////////////////////////////////////////////////
     namespace