- Load and maintain persistent hard locks stored in /etc/zypp/locks.
authorMichael Andres <ma@suse.de>
Wed, 30 Apr 2008 23:07:34 +0000 (23:07 +0000)
committerMichael Andres <ma@suse.de>
Wed, 30 Apr 2008 23:07:34 +0000 (23:07 +0000)
  Locks are loaded together with the target, and changes are writen
  back on commit. zypp.conf option locksfile.apply can be used to turn
  this feature on or off. (FATE #120352)

15 files changed:
VERSION.cmake
devel/devel.ma/NewPool.cc
package/libzypp.changes
zypp/CMakeLists.txt
zypp/ResPool.cc
zypp/ResPool.h
zypp/ZYpp.cc
zypp/ZYpp.h
zypp/target/HardLocksFile.cc [new file with mode: 0644]
zypp/target/HardLocksFile.h [new file with mode: 0644]
zypp/target/SoftLocksFile.h
zypp/target/TargetImpl.cc
zypp/target/TargetImpl.h
zypp/zypp_detail/ZYppImpl.cc
zypp/zypp_detail/ZYppImpl.h

index 1859706..50e637d 100644 (file)
@@ -45,6 +45,6 @@
 #
 
 SET(LIBZYPP_MAJOR "4")
-SET(LIBZYPP_MINOR "17")
-SET(LIBZYPP_COMPATMINOR "17")
-SET(LIBZYPP_PATCH "1")
+SET(LIBZYPP_MINOR "18")
+SET(LIBZYPP_COMPATMINOR "18")
+SET(LIBZYPP_PATCH "0")
index ec90add..c86bee4 100644 (file)
@@ -474,13 +474,17 @@ void sslk( const std::string & t = std::string() )
 {
   ResPool pool( ResPool::instance() );
   ostream & outs( SEC );
+  unsigned cnt = 0;
   outs << t << ": {" << endl;
   for_( it, pool.begin(), pool.end() )
   {
     if ( it->status().isLocked() )
+    {
       outs << "    " << *it << endl;
+      ++cnt;
+    }
   }
-  outs << '}' << endl;
+  outs << '}' << cnt << endl;
 }
 
 void ssup()
@@ -503,6 +507,15 @@ void ssup()
     dumpRange( DBG, q.begin(), q.end() ) << endl;
     newLocks.push_back( q );
   }
+  {
+    PoolQuery q;
+    q.addAttribute( sat::SolvAttr::name, "amarok" );
+    q.addKind( ResKind::package );
+    q.setMatchExact();
+    q.setCaseSensitive(true);
+    dumpRange( DBG, q.begin(), q.end() ) << endl;
+    newLocks.push_back( q );
+  }
 
   pool.setHardLockQueries( newLocks );
 
@@ -525,9 +538,6 @@ try {
   ResPool   pool( ResPool::instance() );
   sat::Pool satpool( sat::Pool::instance() );
 
-  ssup();
-  //sslk( "START" );
-
   if ( 1 )
   {
     RepoManager repoManager( makeRepoManager( sysRoot ) );
@@ -582,8 +592,6 @@ try {
         }
 
         USR << "pool: " << pool << endl;
-
-        sslk( nrepo.alias() );
       }
     }
   }
@@ -597,7 +605,6 @@ try {
         getZYpp()->initializeTarget( sysRoot );
       }
       getZYpp()->target()->load();
-      sslk( "TARGET" );
     }
   }
 
@@ -615,32 +622,14 @@ try {
   ///////////////////////////////////////////////////////////////////
   ///////////////////////////////////////////////////////////////////
 
-  ///////////////////////////////////////////////////////////////////
-  INT << "===[END]============================================" << endl << endl;
-  zypp::base::LogControl::instance().logNothing();
-  return 0;
-  //ResPool pool( ResPool::instance() );
-
-  //
-  PoolQueryResult result( pool.byKindBegin<Package>(), pool.byKindEnd<Package>() );
-  MIL << result.size() << endl;
-
-  {
-    PoolQuery q;
-    q.addAttribute( sat::SolvAttr::name, "[a-zA-Z]*" );
-    q.setMatchGlob();
-    result -= q;
-    MIL << result.size() << endl;
-  }
-  MIL << result << endl;
-
-  sat::WhatProvides poviders( Capability("3ddiag") );
-  result -= PoolQueryResult( poviders.begin(), poviders.end() );
-  MIL << result << endl;
+  //ssup();
+  //sslk("X");
 
+  ResPool::HardLockQueries newLocks;
+  pool.getHardLockQueries( newLocks );
+  SEC << newLocks << endl;
 
-  result -= result;
-  MIL << result << endl;
+  install();
 
   ///////////////////////////////////////////////////////////////////
   INT << "===[END]============================================" << endl << endl;
index a2d5771..a311e6e 100644 (file)
@@ -1,4 +1,14 @@
 -------------------------------------------------------------------
+Thu May  1 00:50:51 CEST 2008 - ma@suse.de
+
+- Load and maintain persistent hard locks stored in /etc/zypp/locks.
+  Locks are loaded together with the target, and changes are writen 
+  back on commit. zypp.conf option locksfile.apply can be used to turn 
+  this feature on or off. (FATE #120352)
+- version 4.18.0
+- revision 9927
+
+-------------------------------------------------------------------
 Wed Apr 30 16:27:49 CEST 2008 - ma@suse.de
 
 - Add zypp.conf option solvfilesdir: Path where the repo solv files 
index 13c1f44..ed68b96 100644 (file)
@@ -564,6 +564,7 @@ INSTALL(  FILES
 SET( zypp_target_SRCS
   target/RequestedLocalesFile.cc
   target/SoftLocksFile.cc
+  target/HardLocksFile.cc
   target/CommitLog.cc
   target/CommitPackageCache.cc
   target/CommitPackageCacheImpl.cc
@@ -576,6 +577,7 @@ SET( zypp_target_SRCS
 SET( zypp_target_HEADERS
   target/RequestedLocalesFile.h
   target/SoftLocksFile.h
+  target/HardLocksFile.h
   target/CommitLog.h
   target/CommitPackageCache.h
   target/CommitPackageCacheImpl.h
index d21fbf2..63e9f37 100644 (file)
@@ -111,8 +111,8 @@ namespace zypp
   void ResPool::setHardLockQueries( const HardLockQueries & newLocks_r )
   { _pimpl->setHardLockQueries( newLocks_r ); }
 
-  bool  ResPool::getHardLockQueries( HardLockQueries & activeLocks_r )
-  { return _pimpl->getHardLockQueries( activeLocks_r ); }
+  void ResPool::getHardLockQueries( HardLockQueries & activeLocks_r )
+  { _pimpl->getHardLockQueries( activeLocks_r ); }
 
 
   const pool::PoolTraits::ItemContainerT & ResPool::store() const
index a0be254..5e51288 100644 (file)
@@ -357,11 +357,9 @@ namespace zypp
       void setHardLockQueries( const HardLockQueries & newLocks_r );
 
       /** Suggest a new set of queries based on the current selection.
-       * (usually remembered on commit). Returns whether the sugested
-       * set differs from the one that was previously set via
-       * \ref setHardLockQueries.
+       * (usually remembered on commit).
        */
-      bool getHardLockQueries( HardLockQueries & activeLocks_r );
+      void getHardLockQueries( HardLockQueries & activeLocks_r );
       //@}
 
     private:
index 8c663cd..721f776 100644 (file)
@@ -118,8 +118,6 @@ namespace zypp
   void ZYpp::setHomePath( const Pathname & path )
   { _pimpl->setHomePath(path); }
 
-  int ZYpp::applyLocks()
-  { return _pimpl->applyLocks(); }
   /////////////////////////////////////////////////////////////////
 } // namespace zypp
 ///////////////////////////////////////////////////////////////////
index 14b1c17..80bc020 100644 (file)
@@ -180,12 +180,11 @@ namespace zypp
   public:
 
    /**
-    * \short Apply persistant locks to current pool.
-    * Call this before solving
-    *
-    * \returns Number of items locked
+    * \deprecated Persistent locks are automatically maintained, kept in the pool, loaded and saved together with the Target.
+    * \ref ZConfig::apply_locks_file tells whether locks are applied or not.
     */
-   int applyLocks();
+    ZYPP_DEPRECATED int applyLocks()
+    { return 0; }
 
   protected:
     /** Dtor */
diff --git a/zypp/target/HardLocksFile.cc b/zypp/target/HardLocksFile.cc
new file mode 100644 (file)
index 0000000..fbfd5a8
--- /dev/null
@@ -0,0 +1,87 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/target/HardLocksFile.cc
+ *
+*/
+#include <iostream>
+#include <fstream>
+
+#include "zypp/base/LogTools.h"
+#include "zypp/base/IOStream.h"
+#include "zypp/base/String.h"
+
+#include "zypp/PathInfo.h"
+#include "zypp/TmpPath.h"
+#include "zypp/Date.h"
+
+#include "zypp/target/HardLocksFile.h"
+#include "zypp/PoolQueryUtil.tcc"
+
+using std::endl;
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+  ///////////////////////////////////////////////////////////////////
+  namespace target
+  { /////////////////////////////////////////////////////////////////
+
+    void HardLocksFile::load( const Pathname & file_r, Data & data_r )
+    {
+      PathInfo pi( file_r );
+      if ( ! pi.isFile() )
+      {
+        WAR << "Can't read " << pi << endl;
+        return;
+      }
+
+      readPoolQueriesFromFile( file_r, std::back_inserter( data_r ) );
+
+      MIL << "Read " << pi << endl;
+    }
+
+    void HardLocksFile::store( const Pathname & file_r, const Data & data_r )
+    {
+      filesystem::TmpFile tmp( filesystem::TmpFile::makeSibling( file_r ) );
+      filesystem::chmod( tmp.path(), 0644 );
+
+      writePoolQueriesToFile( tmp.path(), data_r.begin(), data_r.end() );
+
+      if ( true ) // by now: no error info from writePoolQueriesToFile
+      {
+        filesystem::rename( tmp.path(), file_r );
+        MIL << "Wrote " << PathInfo(file_r) << endl;
+      }
+      else
+      {
+        ERR << "Can't write " << PathInfo(tmp.path()) << endl;
+      }
+    }
+
+    /******************************************************************
+    **
+    ** FUNCTION NAME : operator<<
+    ** FUNCTION TYPE : std::ostream &
+    */
+    std::ostream & operator<<( std::ostream & str, const HardLocksFile & obj )
+    {
+      str << obj.file() << ' ';
+      if ( obj._dataPtr )
+        str << obj.data();
+      else
+        str << "(unloaded)";
+      return str;
+    }
+
+    /////////////////////////////////////////////////////////////////
+  } // namespace target
+  ///////////////////////////////////////////////////////////////////
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
diff --git a/zypp/target/HardLocksFile.h b/zypp/target/HardLocksFile.h
new file mode 100644 (file)
index 0000000..70f9e9e
--- /dev/null
@@ -0,0 +1,122 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/target/HardLocksFile.h
+ *
+*/
+#ifndef ZYPP_TARGET_HARDLOCKSFILE_H
+#define ZYPP_TARGET_HARDLOCKSFILE_H
+
+#include <iosfwd>
+
+#include "zypp/base/PtrTypes.h"
+
+#include "zypp/Pathname.h"
+#include "zypp/pool/PoolTraits.h"
+#include "zypp/PoolQuery.h"
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+  ///////////////////////////////////////////////////////////////////
+  namespace target
+  { /////////////////////////////////////////////////////////////////
+
+    ///////////////////////////////////////////////////////////////////
+    //
+    // CLASS NAME : HardLocksFile
+    //
+    /** Save and restore hardlocks.
+     */
+    class HardLocksFile
+    {
+      friend std::ostream & operator<<( std::ostream & str, const HardLocksFile & obj );
+      public:
+
+        typedef pool::PoolTraits::HardLockQueries Data;
+
+      public:
+        /** Ctor taking the file to read/write. */
+        HardLocksFile( const Pathname & file_r )
+        : _file( file_r )
+        {}
+
+        /** Return the file path. */
+        const Pathname & file() const
+        { return _file; }
+
+        /** Return the data.
+         * The file is read once on demand. Returns empty \ref Data if
+         * the file does not exist or is not readable.
+        */
+        const Data & data() const
+        {
+          if ( !_dataPtr )
+          {
+            _dataPtr.reset( new Data );
+            Data & mydata( *_dataPtr );
+            load( _file, mydata );
+          }
+          return *_dataPtr;
+        }
+
+        /** Store new \ref Data.
+         * Write the new \ref Data to file, unless we know it
+         * did not change. The directory containing file must
+         * exist.
+        */
+        void setData( const Data & data_r )
+        {
+          if ( !_dataPtr )
+            _dataPtr.reset( new Data );
+
+          if ( differs( *_dataPtr, data_r ) )
+          {
+            store( _file, data_r );
+            *_dataPtr = data_r;
+          }
+        }
+
+      private:
+        /** Helper testing whether two \ref Data differ. */
+        bool differs( const Data & lhs, const Data & rhs ) const
+        {
+          if ( lhs.size() != rhs.size() )
+            return true;
+          // Complete diff is too expensive and not necessary here.
+          // Just check for the same sequence of items.
+          Data::const_iterator rit = rhs.begin();
+          for_( it, lhs.begin(), lhs.end() )
+          {
+            if ( *it != *rit )
+              return true;
+            ++rit;
+          }
+          return false;
+        }
+        /** Read \ref Data from \c file_r. */
+        static void load( const Pathname & file_r, Data & data_r );
+        /** Write \ref Data to \c file_r. */
+        static void store( const Pathname & file_r, const Data & data_r );
+
+      private:
+        Pathname                 _file;
+        mutable scoped_ptr<Data> _dataPtr;
+    };
+    ///////////////////////////////////////////////////////////////////
+
+    /** \relates HardLocksFile Stream output */
+    std::ostream & operator<<( std::ostream & str, const HardLocksFile & obj );
+
+    /////////////////////////////////////////////////////////////////
+  } // namespace target
+  ///////////////////////////////////////////////////////////////////
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+#endif // ZYPP_TARGET_HARDLOCKSFILE_H
index aed185c..bd91d6a 100644 (file)
@@ -16,8 +16,8 @@
 
 #include "zypp/base/PtrTypes.h"
 
+#include "zypp/IdString.h"
 #include "zypp/Pathname.h"
-#include "zypp/Locale.h"
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
@@ -30,7 +30,7 @@ namespace zypp
     //
     // CLASS NAME : SoftLocksFile
     //
-    /** Save and restore locale set from file.
+    /** Save and restore soft locks.
      */
     class SoftLocksFile
     {
index 0a2c6a8..c134de3 100644 (file)
@@ -282,6 +282,7 @@ namespace zypp
     : _root( root_r )
     , _requestedLocalesFile( home() / "RequestedLocales" )
     , _softLocksFile( home() / "SoftLocks" )
+    , _hardLocksFile( Pathname::assertprefix( _root, ZConfig::instance().locksFile() ) )
     {
       _rpm.initDatabase( root_r, Pathname(), doRebuild_r );
       MIL << "Initialized target on " << _root << endl;
@@ -442,16 +443,27 @@ namespace zypp
       // to avoid undoing changes the application applied. We expect this
       // to happen on a bare metal installation only. An already existing
       // target should be loaded before its settings are changed.
-      const LocaleSet & requestedLocales( _requestedLocalesFile.locales() );
-      if ( ! requestedLocales.empty() )
       {
-        satpool.setRequestedLocales( requestedLocales );
+        const LocaleSet & requestedLocales( _requestedLocalesFile.locales() );
+        if ( ! requestedLocales.empty() )
+        {
+          satpool.setRequestedLocales( requestedLocales );
+        }
       }
-
-      const SoftLocksFile::Data & softLocks( _softLocksFile.data() );
-      if ( ! softLocks.empty() )
       {
-        ResPool::instance().setAutoSoftLocks( softLocks );
+        const SoftLocksFile::Data & softLocks( _softLocksFile.data() );
+        if ( ! softLocks.empty() )
+        {
+          ResPool::instance().setAutoSoftLocks( softLocks );
+        }
+      }
+      if ( ZConfig::instance().apply_locks_file() )
+      {
+        const HardLocksFile::Data & hardLocks( _hardLocksFile.data() );
+        if ( ! hardLocks.empty() )
+        {
+          ResPool::instance().setHardLockQueries( hardLocks );
+        }
       }
 
 
@@ -478,6 +490,12 @@ namespace zypp
         pool_r.getActiveSoftLocks( newdata );
         _softLocksFile.setData( newdata );
       }
+      if ( ZConfig::instance().apply_locks_file() )
+      {
+        HardLocksFile::Data newdata;
+        pool_r.getHardLockQueries( newdata );
+        _hardLocksFile.setData( newdata );
+      }
 
       // Process packages:
       ZYppCommitResult result;
index a80eef0..62bf2a2 100644 (file)
@@ -30,6 +30,7 @@
 #include "zypp/target/TargetException.h"
 #include "zypp/target/RequestedLocalesFile.h"
 #include "zypp/target/SoftLocksFile.h"
+#include "zypp/target/HardLocksFile.h"
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
@@ -154,6 +155,8 @@ namespace zypp
       RequestedLocalesFile _requestedLocalesFile;
       /** Soft-locks database */
       SoftLocksFile _softLocksFile;
+      /** Hard-Locks database */
+      HardLocksFile _hardLocksFile;
     private:
       /** Null implementation */
       static TargetImpl_Ptr _nullimpl;
index 76d81de..d8231ff 100644 (file)
@@ -22,7 +22,6 @@
 #include "zypp/NVRAD.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"
@@ -177,27 +176,6 @@ namespace zypp
       return zypp_tmp_dir.path();
     }
 
-    int ZYppImpl::applyLocks()
-    {
-      if (!ZConfig::instance().apply_locks_file())
-        return 0;
-
-      //TODO catch posibble exceptions
-      Locks::instance().readAndApply();
-
-      //current locks api doesn't support counting locked solvables
-      //so count it after
-      int count = 0;
-      for_(it, sat::Pool::instance().solvablesBegin(),
-          sat::Pool::instance().solvablesEnd())
-      {
-        PoolItem i(*it);
-        if ( i.status().isLocked() )
-          count++;
-      }
-
-      return count;
-    }
     /******************************************************************
      **
      **        FUNCTION NAME : operator<<
index ea8a94a..f821d05 100644 (file)
@@ -101,10 +101,6 @@ namespace zypp
       void setPartitions(const DiskUsageCounter::MountPointSet &mp);
       DiskUsageCounter::MountPointSet getPartitions() const;
 
-    public:
-
-      int applyLocks();
-
     private:
       /** */
       Target_Ptr _target;