Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / zypp / Patch.cc
index 9954773..a7d81dc 100644 (file)
@@ -9,7 +9,14 @@
 /** \file zypp/Patch.cc
  *
 */
+#include <iostream>
+
+#include "zypp/base/LogTools.h"
+#include "zypp/base/String.h"
 #include "zypp/Patch.h"
+#include "zypp/sat/WhatProvides.h"
+
+using std::endl;
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
@@ -35,96 +42,272 @@ namespace zypp
   {}
 
   ///////////////////////////////////////////////////////////////////
-  //
-  //   Patch interface forwarded to implementation
-  //
+
+  std::string Patch::category() const
+  { return lookupStrAttribute( sat::SolvAttr::patchcategory ); }
+
+  Patch::Category Patch::categoryEnum() const
+  { return categoryEnum( category() ); }
+
+  bool Patch::isCategory( const std::string & category_r ) const
+  { return( str::compareCI( category_r, category() ) == 0 ); }
+
+  Patch::Category Patch::categoryEnum( const std::string & category_r )
+  {
+    switch ( category_r[0] )
+    {
+      //       CAT_YAST
+      case 'y':
+      case 'Y':
+       if ( str::compareCI( category_r, "yast" ) == 0 )
+         return CAT_YAST;
+       break;
+
+      //       CAT_SECURITY
+      case 's':
+      case 'S':
+       if ( str::compareCI( category_r, "security" ) == 0 )
+         return CAT_SECURITY;
+       break;
+
+      //       CAT_RECOMMENDED
+      case 'r':
+      case 'R':
+       if ( str::compareCI( category_r, "recommended" ) == 0 )
+         return CAT_RECOMMENDED;
+       break;
+      case 'b':
+      case 'B':
+       if ( str::compareCI( category_r, "bugfix" ) == 0 )      // rhn
+         return CAT_RECOMMENDED;
+       break;
+
+      //       CAT_OPTIONAL
+      case 'o':
+      case 'O':
+       if ( str::compareCI( category_r, "optional" ) == 0 )
+         return CAT_OPTIONAL;
+       break;
+      case 'f':
+      case 'F':
+       if ( str::compareCI( category_r, "feature" ) == 0 )
+         return CAT_OPTIONAL;
+       break;
+      case 'e':
+      case 'E':
+       if ( str::compareCI( category_r, "enhancement" ) == 0 ) // rhn
+         return CAT_OPTIONAL;
+       break;
+
+      //       CAT_DOCUMENT
+      case 'd':
+      case 'D':
+       if ( str::compareCI( category_r, "document" ) == 0 )
+         return CAT_DOCUMENT;
+       break;
+    }
+    // default:
+    return CAT_OTHER;
+  }
+
   ///////////////////////////////////////////////////////////////////
 
-  std::string Patch::id() const
-  { return std::string(); }
+  std::string Patch::severity() const
+  { return lookupStrAttribute( sat::SolvAttr::severity ); }
 
-  Date Patch::timestamp() const
-  { return Date(); }
+  Patch::SeverityFlag Patch::severityFlag() const
+  { return severityFlag( severity() ); }
 
-  std::string Patch::category() const
-  { return std::string(); }
+  bool Patch::isSeverity( const std::string & severity_r ) const
+  { return( str::compareCI( severity_r, severity() ) == 0 ); }
+
+  Patch::SeverityFlag Patch::severityFlag( const std::string & severity_r )
+  {
+    switch ( severity_r[0] )
+    {
+      case 'l':
+      case 'L':
+       if ( str::compareCI( severity_r, "low" ) == 0 )
+         return SEV_LOW;
+       break;
+
+      case 'm':
+      case 'M':
+       if ( str::compareCI( severity_r, "moderate" ) == 0 )
+         return SEV_MODERATE;
+       break;
 
-  bool Patch::reboot_needed() const
-  { return false; }
+      case 'i':
+      case 'I':
+       if ( str::compareCI( severity_r, "important" ) == 0 )
+         return SEV_IMPORTANT;
+       break;
 
-  bool Patch::affects_pkg_manager() const
-  { return false; }
+      case 'c':
+      case 'C':
+       if ( str::compareCI( severity_r, "critical" ) == 0 )
+         return SEV_CRITICAL;
+       break;
 
-  Patch::AtomList Patch::atoms() const
+      case 'u':
+      case 'U':
+       if ( str::compareCI( severity_r, "unspecified" ) == 0 )
+         return SEV_NONE;
+       break;
+
+      case '\0':
+       return SEV_NONE;
+       break;
+    }
+    // default:
+    return SEV_OTHER;
+  }
+
+  std::string asString( const Patch::SeverityFlag & obj )
   {
-#warning Implement PATCH::ATOMS
-#if 0
-      if ( ! _atomlist )
-      {
-        if ( ! hasBackRef() )
-        {
-          // We are not jet connected to the Resolvable that
-          // contains our dependencies.
-          return AtomList();
-        }
+    switch ( obj )
+    {
+      case Patch::SEV_NONE:    return std::string( "unspecified" );    break;
+      case Patch::SEV_OTHER:   return std::string( "unknown" );        break;
+      case Patch::SEV_LOW:     return std::string( "low" );            break;
+      case Patch::SEV_MODERATE:        return std::string( "moderate" );       break;
+      case Patch::SEV_IMPORTANT:return std::string( "important" );     break;
+      case Patch::SEV_CRITICAL:        return std::string( "critical" );       break;
+    }
+    // make gcc happy:
+    return std::string( "unknown" );
+  }
+
+  ///////////////////////////////////////////////////////////////////
 
-        // lazy init
-        _atomlist.reset( new AtomList );
+  std::string Patch::message( const Locale & lang_r ) const
+  { return lookupStrAttribute( sat::SolvAttr::message, lang_r ); }
 
-        // Build the list using the repositories resolvables.
-        // Installed Patches (no repository) have this method overloaded.
-        if ( repository() )
-        {
-          const CapSet &   requires( self()->dep( Dep::REQUIRES ) );
-          const ResStore & store( repository().resolvables() );
-
-          for_( req, requires.begin(), requires.end() )
-          {
-            // lookup Patch requirements that refer to an Atom, Script or Message.
-            if ( refersTo<Atom>( *req ) || refersTo<Script>( *req ) || refersTo<Message>( *req ) )
-            {
-              for_( res, store.begin(), store.end() )
-              {
-                // Collect ALL matches in the store.
-                if ( hasMatches( (*res)->dep( Dep::PROVIDES ), (*req) ) )
-                {
-                  _atomlist->push_back( *res );
-                }
-              }
-            }
-          }
-        }
+  bool Patch::rebootSuggested() const
+  { return lookupBoolAttribute( sat::SolvAttr::rebootSuggested ); }
+
+  bool Patch::restartSuggested() const
+  { return lookupBoolAttribute( sat::SolvAttr::restartSuggested ); }
+
+  bool Patch::reloginSuggested() const
+  { return lookupBoolAttribute( sat::SolvAttr::reloginSuggested ); }
+
+  Patch::InteractiveFlags Patch::interactiveFlags() const
+  {
+    InteractiveFlags patchFlags (NoFlags);
+    if ( rebootSuggested() )
+      patchFlags |= Reboot;
+
+    if ( ! message().empty() )
+      patchFlags |= Message;
+
+    if ( ! licenseToConfirm().empty() )
+      patchFlags |= License;
+
+    Patch::Contents c( contents() );
+    for_( it, c.begin(), c.end() )
+    {
+      if ( ! makeResObject(*it)->licenseToConfirm().empty() )
+      {
+        patchFlags |= License;
+        break;
       }
-      return *_atomlist;
-#endif
-    return AtomList();
+    }
+    return patchFlags;
+  }
+
+  bool Patch::interactiveWhenIgnoring( InteractiveFlags flags_r ) const
+  {
+    if ( interactiveFlags() & ( ~flags_r ) )
+    {
+      return true;
+    }
+    else
+    {
+      return false;
+    }
   }
 
   bool Patch::interactive() const
   {
-#warning Implement PATCH::INTERACTIVE
-#if 0
-      if ( reboot_needed()
-           || ! licenseToConfirm().empty() )
+    return interactiveWhenIgnoring();
+  }
+
+  Patch::Contents Patch::contents() const
+  {
+    Contents result;
+    // DBG << *this << endl;
+    sat::LookupAttr updateCollection( sat::SolvAttr::updateCollection, satSolvable() );
+    for_( entry, updateCollection.begin(), updateCollection.end() )
+    {
+      IdString name    ( entry.subFind( sat::SolvAttr::updateCollectionName ).idStr() );
+      Edition  edition ( entry.subFind( sat::SolvAttr::updateCollectionEvr ).idStr() );
+      Arch     arch    ( entry.subFind( sat::SolvAttr::updateCollectionArch ).idStr() );
+      if ( name.empty() )
+      {
+        WAR << "Ignore malformed updateCollection entry: " << name << "-" << edition << "." << arch << endl;
+        continue;
+      }
+
+      // The entry is relevant if there is an installed
+      // package with the same name and arch.
+      bool relevant = false;
+      sat::WhatProvides providers( (Capability( name.id() )) );
+      for_( it, providers.begin(), providers.end() )
+      {
+        if ( it->isSystem() && it->ident() == name && it->arch() == arch )
         {
-          return true;
+          relevant = true;
+          break;
         }
+      }
+      if ( ! relevant )
+      {
+        // DBG << "Not relevant: " << name << "-" << edition << "." << arch << endl;
+        continue;
+      }
 
-      AtomList atoms = all_atoms();
-      for ( AtomList::const_iterator it = atoms.begin(); it != atoms.end(); it++)
+      /* find exact providers first (this matches the _real_ 'collection content' of the patch */
+      providers = sat::WhatProvides( Capability( arch, name.c_str(), Rel::EQ, edition, ResKind::package ) );
+      if ( providers.empty() )
+      {
+        /* no exact providers: find 'best' providers: those with a larger evr */
+        providers = sat::WhatProvides( Capability( arch, name.c_str(), Rel::GT, edition, ResKind::package ) );
+        if ( providers.empty() )
         {
-          if (    isKind<Message>( *it )
-               || ! licenseToConfirm().empty() )
-            {
-              return true;
-            }
+          // Hmm, this patch is not installable, no one is providing the package in the collection
+          // FIXME: raise execption ? fake a solvable ?
+          WAR << "Missing provider: " << name << "-" << edition << "." << arch << endl;
+          continue;
         }
+      }
 
-      return false;
-#endif
-    return false;
+      // FIXME ?! loop over providers and try to find installed ones ?
+      // DBG << "Found " << name << "-" << edition << "." << arch << ": " << *(providers.begin()) << endl;
+      result.get().insert( *(providers.begin()) );
+    }
+
+    return result;
   }
 
+  ///////////////////////////////////////////////////////////////////
+  //
+  //   CLASS NAME : Patch::ReferenceIterator
+  //
+  ///////////////////////////////////////////////////////////////////
+
+  Patch::ReferenceIterator::ReferenceIterator( const sat::Solvable & val_r )
+  { base_reference() = sat::LookupAttr( sat::SolvAttr::updateReference, val_r ).begin(); }
+
+  std::string Patch::ReferenceIterator::id() const
+  { return base_reference().subFind( sat::SolvAttr::updateReferenceId ).asString(); }
+  std::string Patch::ReferenceIterator::href() const
+  { return base_reference().subFind( sat::SolvAttr::updateReferenceHref ).asString(); }
+  std::string Patch::ReferenceIterator::title() const
+  { return base_reference().subFind( sat::SolvAttr::updateReferenceTitle ).asString(); }
+  std::string Patch::ReferenceIterator::type() const
+  { return base_reference().subFind( sat::SolvAttr::updateReferenceType ).asString(); }
+
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
 ///////////////////////////////////////////////////////////////////