#include "zypp/Locks.h"
-using namespace std;
-using namespace zypp;
-using namespace zypp::str;
+using std::endl;
namespace zypp
{
LockList locks;
LockList toAdd;
LockList toRemove;
+ bool locksDirty;
bool mergeList(callback::SendReport<SavingLocksReport>& report);
+
+ Impl():locksDirty(false){}
};
Locks::Locks() : _pimpl(new Impl){}
{
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();
}
}
};
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;
}
report->finish(CleanEmptyLocksReport::NO_ERROR);
}
+
+ if ( sum != _pimpl->locks.size() ) //some locks has been removed
+ _pimpl->locksDirty = true;
}
class LocksRemovePredicate
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;
}
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;
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