Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / ResStatus.h
index bc25d96..a7e7dd3 100644 (file)
@@ -12,6 +12,7 @@
 #ifndef ZYPP_RESSTATUS_H
 #define ZYPP_RESSTATUS_H
 
+#include <inttypes.h>
 #include <iosfwd>
 #include "zypp/Bit.h"
 
@@ -22,6 +23,7 @@ namespace zypp
   namespace resstatus
   {
     class UserLockQueryManip;
+    class StatusBackup;
   }
 
   ///////////////////////////////////////////////////////////////////
@@ -35,7 +37,7 @@ namespace zypp
    *        nonrelevant: it is unimportant for the user
    *       satisfied: it important nothing has to be done
    *       broken: it is incomplete. So e.g. an update is needed
-   * \li \c TransactField Wheter to transact this resolvable
+   * \li \c TransactField Whether to transact this resolvable
    *        (delete if installed install if uninstalled).
    *        In case the resolvable is locked, only USER may modify the
    *        transact bit.
@@ -68,9 +70,8 @@ namespace zypp
     typedef bit::Range<FieldType,ValidateField::end,         2> TransactField;
     typedef bit::Range<FieldType,TransactField::end,         2> TransactByField;
     typedef bit::Range<FieldType,TransactByField::end,       2> TransactDetailField;
-    typedef bit::Range<FieldType,TransactDetailField::end,   1> SolverStateField;
-    typedef bit::Range<FieldType,SolverStateField::end,      1> LicenceConfirmedField;
-    typedef bit::Range<FieldType,LicenceConfirmedField::end, 2> WeakField;
+    typedef bit::Range<FieldType,TransactDetailField::end,   1> LicenceConfirmedField;
+    typedef bit::Range<FieldType,LicenceConfirmedField::end, 4> WeakField;
     typedef bit::Range<FieldType,WeakField::end,             1> UserLockQueryField; // internal
     // enlarge FieldType if more bit's needed. It's not yet
     // checked by the compiler.
@@ -115,7 +116,6 @@ namespace zypp
         /** Detail for no transact, i.e. reset any Install/RemoveDetailValue. */
         NO_DETAIL = bit::RangeValue<TransactDetailField,0>::value,
       };
-
     enum InstallDetailValue
       {
         EXPLICIT_INSTALL = bit::RangeValue<TransactDetailField,0>::value,
@@ -128,11 +128,6 @@ namespace zypp
         DUE_TO_OBSOLETE = bit::RangeValue<TransactDetailField,2>::value,
         DUE_TO_UPGRADE  = bit::RangeValue<TransactDetailField,3>::value
       };
-    enum SolverStateValue
-      {
-       NORMAL     = bit::RangeValue<SolverStateField,0>::value, // default, notthing special
-       SEEN       = bit::RangeValue<SolverStateField,1>::value, // already seen during ResolverUpgrade
-      };
 
     enum LicenceConfirmedValue
       {
@@ -140,12 +135,13 @@ namespace zypp
         LICENCE_CONFIRMED   = bit::RangeValue<LicenceConfirmedField,1>::value
       };
 
-    enum WeakValue
+    enum WeakValue     // Unlike the other fields those are BITS that may be or'ed!
       {
         NO_WEAK                = bit::RangeValue<WeakField,0>::value,
-        SUGGESTED              = bit::RangeValue<WeakField,1>::value,
-       RECOMMENDED             = bit::RangeValue<WeakField,2>::value,
-       SUGGESTED_AND_RECOMMENDED = bit::RangeValue<WeakField,3>::value
+        SUGGESTED              = bit::RangeValue<WeakField,1<<0>::value,
+       RECOMMENDED             = bit::RangeValue<WeakField,1<<1>::value,
+       ORPHANED                = bit::RangeValue<WeakField,1<<2>::value,
+       UNNEEDED                = bit::RangeValue<WeakField,1<<3>::value
       };
 
     enum UserLockQuery // internal
@@ -183,30 +179,37 @@ namespace zypp
     { fieldValueAssign<LicenceConfirmedField>( toVal_r ? LICENCE_CONFIRMED : LICENCE_UNCONFIRMED ); }
 
   public:
-
     bool isRecommended() const
-    { return fieldValueIs<WeakField>( RECOMMENDED ); }
+    { return _bitfield.test( RECOMMENDED ); }
 
     bool isSuggested() const
-    { return fieldValueIs<WeakField>( SUGGESTED ); }
+    { return _bitfield.test( SUGGESTED ); }
+
+    bool isOrphaned() const
+    { return _bitfield.test( ORPHANED ); }
+
+    bool isUnneeded() const
+    { return _bitfield.test( UNNEEDED ); }
 
-    bool resetWeak() const
-    { return fieldValueIs<WeakField>( NO_WEAK ); }
+    bool hasWeak() const
+    { return ! fieldValueIs<WeakField>( NO_WEAK ); }
+
+    void resetWeak()
+    { return fieldValueAssign<WeakField>( NO_WEAK ); }
 
     void setRecommended( bool toVal_r = true )
-    { if (isSuggested())
-       fieldValueAssign<WeakField>( toVal_r ? SUGGESTED_AND_RECOMMENDED : SUGGESTED );
-    else
-       fieldValueAssign<WeakField>( toVal_r ? RECOMMENDED : NO_WEAK );
-    }
+    { _bitfield.set( RECOMMENDED, toVal_r ); }
 
     void setSuggested( bool toVal_r = true )
-    { if (isRecommended())
-       fieldValueAssign<WeakField>( toVal_r ? SUGGESTED_AND_RECOMMENDED : RECOMMENDED );
-    else
-       fieldValueAssign<WeakField>( toVal_r ? SUGGESTED : NO_WEAK );
-    }
+    { _bitfield.set( SUGGESTED, toVal_r ); }
+
+    void setOrphaned( bool toVal_r = true )
+    { _bitfield.set( ORPHANED, toVal_r ); }
+
+    void setUnneeded( bool toVal_r = true )
+    { _bitfield.set( UNNEEDED, toVal_r ); }
 
+  public:
     ValidateValue validate() const
     { return (ValidateValue)_bitfield.value<ValidateField>(); }
 
@@ -252,8 +255,11 @@ namespace zypp
     bool isLocked() const
     { return fieldValueIs<TransactField>( LOCKED ); }
 
-    bool isSoftLocked( TransactByValue causer_r = USER ) const
-    { return isKept() && fieldValueIs<TransactByField>( causer_r ); }
+    bool isUserLocked() const
+    { return isLocked() && isByUser(); }
+
+    bool isSoftLocked() const
+    { return isKept() && ( isByApplLow() || isByUser() ); }
 
     bool isKept() const
     { return fieldValueIs<TransactField>( KEEP_STATE ); }
@@ -264,6 +270,14 @@ namespace zypp
     TransactValue getTransactValue() const
     { return (TransactValue)_bitfield.value<TransactField>(); }
 
+    /** True if would be on system after commit. */
+    bool onSystem() const
+    { return( isInstalled() != transacts() ); }
+
+    /** True if would be off system after commit. */
+    bool offSystem() const
+    { return ! onSystem(); }
+
     bool isBySolver() const
     { return fieldValueIs<TransactByField>( SOLVER ); }
 
@@ -279,6 +293,15 @@ namespace zypp
     TransactByValue getTransactByValue() const
     { return (TransactByValue)_bitfield.value<TransactByField>(); }
 
+    bool setTransactByValue(TransactByValue causer)
+    {
+       if ( isLessThan<TransactByField>( causer ) ) {
+           fieldValueAssign<TransactByField>( causer );
+           return true;
+       } else {
+           return false;
+       }
+    }
 
     bool isToBeUninstalledDueToObsolete () const
     { return isToBeUninstalled() && fieldValueIs<TransactDetailField>( DUE_TO_OBSOLETE ); }
@@ -349,9 +372,9 @@ namespace zypp
     }
 
     /** Apply a lock (prevent transaction).
-     * Currently by USER only, but who knows... Set LOCKED
-     * from KEEP_STATE to be shure all transaction details
-     * were reset properly.
+     * Currently by USER or APPL_HIGH only, but who knows...
+     * Set LOCKED from KEEP_STATE to be shure all transaction
+     * details were reset properly.
     */
     bool setLock( bool toLock_r, TransactByValue causer_r )
     {
@@ -364,19 +387,23 @@ namespace zypp
            return true;
         }
       // Here: Lock status is to be changed:
-      if ( causer_r != USER )
-        return false;
-      // Setting no transact removes an existing lock,
-      // or brings this into KEEP_STATE, and we apply the lock.
-      if ( ! setTransact( false, causer_r ) )
+      if ( causer_r != USER && causer_r != APPL_HIGH )
         return false;
       if ( toLock_r ) {
-         fieldValueAssign<TransactField>( LOCKED );
-         fieldValueAssign<TransactByField>( causer_r );
+        // We're in unlocked state, which includes TRANSACT.
+        // Causer must be allowed to reset this. But from
+        // KEEP_STATE every causer is allowed to set the lock.
+        if ( ! setTransact( false, causer_r ) )
+          return false;
+        fieldValueAssign<TransactField>( LOCKED );
+        fieldValueAssign<TransactByField>( causer_r );
       } else {
-         fieldValueAssign<TransactField>( KEEP_STATE );
-         fieldValueAssign<TransactByField>( SOLVER ); // reset to lowest causer
-                                                      // in order to distinguish from keep_state_by_user
+        // To leave Locked state it needs a superior causer.
+        if ( isGreaterThan<TransactByField>( causer_r ) )
+          return false;
+        fieldValueAssign<TransactField>( KEEP_STATE );
+        fieldValueAssign<TransactByField>( SOLVER ); // reset to lowest causer
+                                                    // in order to distinguish from keep_state_by_user
       }
       return true;
     }
@@ -398,17 +425,17 @@ namespace zypp
     {
       if ( toTansact_r == transacts() )
         {
-          // we're already in the desired state, but
-          // remember a superior causer.
-          if ( isLessThan<TransactByField>( causer_r ) )
+          // we're already in the desired state, but in case of
+          // TRANSACT, remember a superior causer.
+          if ( transacts() && isLessThan<TransactByField>( causer_r ) )
               fieldValueAssign<TransactByField>( causer_r );
 
          fieldValueAssign<TransactDetailField>( NO_DETAIL ); // Details has to be set again
           return true;
         }
       // Here: transact status is to be changed:
-      if ( ! fieldValueIs<TransactField>( KEEP_STATE )
-          && isGreaterThan<TransactByField>( causer_r ) ) {
+      if (    ! fieldValueIs<TransactField>( KEEP_STATE )
+             && isGreaterThan<TransactByField>( causer_r ) ) {
         return false;
       }
 
@@ -420,7 +447,7 @@ namespace zypp
       {
           fieldValueAssign<TransactField>( KEEP_STATE );
       }
-      fieldValueAssign<TransactDetailField>( NO_DETAIL ); // Details has to be set again     
+      fieldValueAssign<TransactDetailField>( NO_DETAIL ); // Details has to be set again
       fieldValueAssign<TransactByField>( causer_r );
       return true;
     }
@@ -607,15 +634,6 @@ namespace zypp
       return true;
     }
 
-    bool isSeen () const
-    { return fieldValueIs<SolverStateField>( SEEN ); }
-
-    bool setSeen (bool value)
-    {
-      fieldValueAssign<SolverStateField>( value ? SEEN : NORMAL );
-      return true;
-    }
-
     bool setStatus( ResStatus newStatus_r )
     {
       // State field is immutable!
@@ -633,21 +651,9 @@ namespace zypp
     /** \name Builtin ResStatus constants. */
     //@{
     static const ResStatus toBeInstalled;
-    static const ResStatus toBeInstalledSoft;
     static const ResStatus toBeUninstalled;
-    static const ResStatus toBeUninstalledSoft;
-    static const ResStatus toBeUninstalledDueToUnlink;
-    static const ResStatus toBeUninstalledDueToObsolete;
     static const ResStatus toBeUninstalledDueToUpgrade;
-    static const ResStatus installed;  // installed, status after successful target 'install' commit
-    static const ResStatus uninstalled;        // uninstalled, status after successful target 'uninstall' commit
-    static const ResStatus satisfied;  // uninstalled, satisfied
-    static const ResStatus complete;   // installed, satisfied
-    static const ResStatus unneeded;   // uninstalled, unneeded
-    static const ResStatus needed;     // uninstalled, incomplete
-    static const ResStatus incomplete; // installed, incomplete
-    static const ResStatus recommended;        // recommended
-    static const ResStatus suggested;  // suggested
+    static const ResStatus toBeUninstalledDueToObsolete;
     //@}
 
   private:
@@ -656,32 +662,32 @@ namespace zypp
                ValidateValue v      = UNDETERMINED,
                TransactValue t      = KEEP_STATE,
                InstallDetailValue i = EXPLICIT_INSTALL,
-               RemoveDetailValue r  = EXPLICIT_REMOVE,
-              SolverStateValue ssv = NORMAL );
+               RemoveDetailValue r  = EXPLICIT_REMOVE );
 
     /** Return whether the corresponding Field has value \a val_r.
     */
-    template<class _Field>
+    template<class TField>
       bool fieldValueIs( FieldType val_r ) const
-      { return _bitfield.isEqual<_Field>( val_r ); }
+    { return _bitfield.isEqual<TField>( val_r ); }
 
     /** Set the corresponding Field to value \a val_r.
     */
-    template<class _Field>
+    template<class TField>
       void fieldValueAssign( FieldType val_r )
-      { _bitfield.assign<_Field>( val_r ); }
+    { _bitfield.assign<TField>( val_r ); }
 
     /** compare two values.
     */
-    template<class _Field>
+    template<class TField>
       bool isGreaterThan( FieldType val_r )
-         { return _bitfield.value<_Field>() > val_r; }
+    { return _bitfield.value<TField>() > val_r; }
 
-    template<class _Field>
+    template<class TField>
       bool isLessThan( FieldType val_r )
-         { return _bitfield.value<_Field>() < val_r; }
+    { return _bitfield.value<TField>() < val_r; }
 
   private:
+    friend class resstatus::StatusBackup;
     BitFieldType _bitfield;
   };
   ///////////////////////////////////////////////////////////////////
@@ -689,6 +695,12 @@ namespace zypp
   /** \relates ResStatus Stream output */
   std::ostream & operator<<( std::ostream & str, const ResStatus & obj );
 
+  /** \relates ResStatus Stream output */
+  std::ostream & operator<<( std::ostream & str, ResStatus::TransactValue obj );
+
+  /** \relates ResStatus Stream output */
+  std::ostream & operator<<( std::ostream & str, ResStatus::TransactByValue obj );
+
   /** \relates ResStatus */
   inline bool operator==( const ResStatus & lhs, const ResStatus & rhs )
   { return lhs._bitfield == rhs._bitfield; }
@@ -697,7 +709,32 @@ namespace zypp
   inline bool operator!=( const ResStatus & lhs, const ResStatus & rhs )
   { return ! (lhs == rhs); }
 
-  /////////////////////////////////////////////////////////////////
+  ///////////////////////////////////////////////////////////////////
+
+  namespace resstatus
+  {
+    class StatusBackup
+    {
+      public:
+        StatusBackup()
+        : _status( 0 )
+        {}
+
+        StatusBackup( ResStatus & status_r )
+        : _status( &status_r )
+        , _bitfield( _status->_bitfield )
+        {}
+
+        void replay()
+        { if ( _status ) _status->_bitfield = _bitfield; }
+
+      private:
+        ResStatus *             _status;
+        ResStatus::BitFieldType _bitfield;
+    };
+  }
+
+ /////////////////////////////////////////////////////////////////
 } // namespace zypp
 ///////////////////////////////////////////////////////////////////
 #endif // ZYPP_RESSTATUS_H