Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / Locks.cc
index e276e6a..b4ff9d5 100644 (file)
@@ -29,9 +29,7 @@
 
 #include "zypp/Locks.h"
 
-using namespace std;
-using namespace zypp;
-using namespace zypp::str;
+using std::endl;
 
 namespace zypp
 {
@@ -48,8 +46,11 @@ public:
   LockList locks;
   LockList toAdd;
   LockList toRemove;
+  bool     locksDirty;
 
   bool mergeList(callback::SendReport<SavingLocksReport>& report);
+  
+  Impl():locksDirty(false){}
 };
 
 Locks::Locks() : _pimpl(new Impl){}
@@ -70,10 +71,10 @@ struct ApplyLock
 {
   void operator()(const PoolQuery& query) const
   {
-    for_( it,query.begin(),query.end() )
+    for ( const PoolItem & item : query.poolItem() )
     {
-      PoolItem item(*it);
       item.status().setLock(true,ResStatus::USER);
+      DBG << "lock "<< item.name();
     }
   }
 };
@@ -102,17 +103,26 @@ struct LockingOutputIterator
 void Locks::readAndApply( const Pathname& file )
 {
   MIL << "read and apply locks from "<<file << endl;
-  insert_iterator<LockList> ii( _pimpl->locks,
-      _pimpl->locks.end() );
-  LockingOutputIterator<insert_iterator<LockList> > lout(ii);
-  readPoolQueriesFromFile( file, boost::make_function_output_iterator(lout) );
+  PathInfo pinfo(file);
+  if ( pinfo.isExist() )
+  {
+    std::insert_iterator<LockList> ii( _pimpl->locks, _pimpl->locks.end() );
+    LockingOutputIterator<std::insert_iterator<LockList> > lout(ii);
+    readPoolQueriesFromFile( file, boost::make_function_output_iterator(lout) );
+  }
+  else
+    MIL << "file not exist(or cannot be stat), no lock added." << endl;
+
 }
 
 void Locks::read( const Pathname& file )
 {
   MIL << "read locks from "<<file << endl;
-  readPoolQueriesFromFile(
-    file, insert_iterator<LockList>(_pimpl->locks, _pimpl->locks.end()) );
+  PathInfo pinfo(file);
+  if ( pinfo.isExist() )
+    readPoolQueriesFromFile( file, std::insert_iterator<LockList>(_pimpl->locks, _pimpl->locks.end()) );
+  else 
+    MIL << "file not exist(or cannot be stat), no lock added." << endl;
 }
 
 
@@ -291,6 +301,9 @@ void Locks::removeEmpty()
     report->finish(CleanEmptyLocksReport::NO_ERROR);
 
   }
+
+  if ( sum != _pimpl->locks.size() ) //some locks has been removed
+    _pimpl->locksDirty = true;
 }
 
 class LocksRemovePredicate
@@ -339,12 +352,13 @@ public:
     switch( contains(q,solvs) )
     {
     case 0:
-      return false;
+      return false; //another lock
     case 1:
       cs = SavingLocksReport::SAME_RESULTS;
       break;
     case 2:
       cs = SavingLocksReport::INTERSECT;
+      break;
     default:
       return true;
     }
@@ -394,12 +408,11 @@ bool Locks::Impl::mergeList(callback::SendReport<SavingLocksReport>& report)
   return true;
 }
 
-void Locks::save( const Pathname& file )
+void Locks::merge()
 {
-  if( (_pimpl->toAdd.size() | _pimpl->toRemove.size())==0 )
+  if( (_pimpl->toAdd.size() | _pimpl->toRemove.size())==0)
   {
-    DBG << "nothing changed in locks - no write to file" << endl;
-    return;
+    return; //nothing to merge
   }
 
   callback::SendReport<SavingLocksReport> report;
@@ -409,9 +422,48 @@ void Locks::save( const Pathname& file )
     report->finish(SavingLocksReport::ABORTED);
     return;
   }
+  DBG << "locks merged" << endl;
+  report->finish(SavingLocksReport::NO_ERROR);
+  _pimpl->locksDirty = true;
+}
+
+void Locks::save( const Pathname& file )
+{
+  if( ((_pimpl->toAdd.size() | _pimpl->toRemove.size())==0)
+      && !_pimpl->locksDirty )
+  {
+    DBG << "nothing changed in locks - no write to file" << endl;
+    return;
+  }
+
+  callback::SendReport<SavingLocksReport> report;
+  report->start();
+
+  if ((_pimpl->toAdd.size() | _pimpl->toRemove.size())!=0)
+  {
+    if (!_pimpl->mergeList(report))
+    {
+      report->finish(SavingLocksReport::ABORTED);
+      return;
+    }
+  }
+
   DBG << "writed "<< _pimpl->locks.size() << "locks" << endl;
   writePoolQueriesToFile( file, _pimpl->locks.begin(), _pimpl->locks.end() );
   report->finish(SavingLocksReport::NO_ERROR);
 }
 
+void Locks::removeDuplicates()
+{
+  size_type sum = size();
+  for_(it,_pimpl->locks.begin(),_pimpl->locks.end())
+  {
+    if ( find(_pimpl->locks.begin(),it,*it) != it )
+      _pimpl->locks.erase(it--); //-- to avoid using break iterator
+  }
+  
+  if (sum!=size())
+    _pimpl->locksDirty = true;
+}
+
 } // ns zypp