libzypp  17.26.0
PurgeKernels.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #include <zypp/base/String.h>
14 #include <zypp/base/Logger.h>
15 #include <zypp/base/Regex.h>
16 #include <zypp/base/Iterator.h>
17 #include <zypp/PurgeKernels.h>
18 #include <zypp/PoolQuery.h>
19 #include <zypp/ResPool.h>
20 #include <zypp/Resolver.h>
21 #include <zypp/Filter.h>
22 #include <zypp/ZConfig.h>
23 
24 #include <iostream>
25 #include <fstream>
26 #include <map>
27 #include <unordered_map>
28 #include <sys/utsname.h>
29 #include <functional>
30 #include <array>
31 
32 #undef ZYPP_BASE_LOGGER_LOGGROUP
33 #define ZYPP_BASE_LOGGER_LOGGROUP "PurgeKernels"
34 
35 namespace zypp {
36 
37  using Flavour = std::string;
38  using SolvableList = std::list<sat::Solvable>;
39  using EditionToSolvableMap = std::map<Edition, SolvableList >;
40  using ArchToEditionMap = std::map<Arch, EditionToSolvableMap >;
41 
42  struct GroupInfo {
43 
44  enum GroupType {
45  None, //<< Just here to support default construction
46  Kernels, //<< Map contains kernel packages, so need to receive special handling and flavour matching
47  RelatedBinaries, //<< Map contains related binary packages, so need to receive special handling and flavour matching
48  Sources //<< Map contains source packages, so when matching those against running we ignore the flavour
49  } groupType;
50 
51  GroupInfo( const GroupType type = None, std::string flav = "") : groupType(type), groupFlavour( std::move(flav) ) { }
52 
53  ArchToEditionMap archToEdMap; //<< Map of actual packages
54  std::string groupFlavour; //<< This would contain a specific flavour if there is one calculated
55  };
56  using GroupMap = std::unordered_map<std::string, GroupInfo>;
57 
59 
60  Impl() {
61  struct utsname unameData;
62  if ( uname( &unameData) == 0 ) {
63 
64  const auto archStr = str::regex_substitute( unameData.machine, str::regex( "^i.86$", str::regex::match_extended ), "i586" );
65 
66  _kernelArch = Arch( archStr );
67  setUnameR( std::string( unameData.release ) );
68 
69  _detectedRunning = true;
70 
71  MIL << "Detected running kernel: " << _runningKernelEdition << " " << _runningKernelFlavour << " " << _kernelArch << std::endl;
72 
73  } else {
74  MIL << "Failed to detect running kernel: " << errno << std::endl;
75  }
76  }
77 
78  void setUnameR ( const std::string &uname ) {
79 
80  _uname_r = uname;
81 
82  MIL << "Set uname " << uname << std::endl;
83 
84  const std::string flavour = str::regex_substitute( _uname_r, str::regex( ".*-", str::regex::match_extended ), "", true );
85  std::string version = str::regex_substitute( _uname_r, str::regex( "-[^-]*$", str::regex::match_extended | str::regex::newline ), "", true );
86 
87  const std::string release = str::regex_substitute( version, str::regex( ".*-", str::regex::match_extended ), "", true );
88 
89  version = str::regex_substitute( version, str::regex( "-[^-]*$", str::regex::match_extended | str::regex::newline ), "", true );
90 
91  // from purge-kernels script, was copied from kernel-source/rpm/mkspec
92  version = str::regex_substitute( version, str::regex( "\\.0-rc", str::regex::match_extended ), ".rc", true );
93  version = str::regex_substitute( version, str::regex( "-rc\\d+", str::regex::match_extended ), "", true );
94  version = str::regex_substitute( version, str::regex( "-", str::regex::match_extended ), ".", true );
95 
96  _runningKernelEdition = Edition( version, release );
97  _runningKernelFlavour = flavour;
98 
99  MIL << "Parsed info from uname: " << std::endl;
100  MIL << "Kernel Flavour: " << _runningKernelFlavour << std::endl;
101  MIL << "Kernel Edition: " << _runningKernelEdition << std::endl;
102  }
103 
104  bool removePackageAndCheck( const sat::Solvable slv, const std::set<sat::Solvable> &keepList , const std::set<sat::Solvable> &removeList ) const;
105  static bool versionMatch ( const Edition &a, const Edition &b );
106  void parseKeepSpec();
107  void fillKeepList(const GroupMap &installedKernels, std::set<sat::Solvable> &keepList , std::set<sat::Solvable> &removeList ) const;
108 
109  std::set<size_t> _keepLatestOffsets = { 0 };
110  std::set<size_t> _keepOldestOffsets;
111  std::set<Edition> _keepSpecificEditions;
112  std::string _uname_r;
117  bool _keepRunning = true;
118  bool _detectedRunning = false;
119  };
120 
125  bool PurgeKernels::Impl::removePackageAndCheck( const sat::Solvable slv, const std::set<sat::Solvable> &keepList , const std::set<sat::Solvable> &removeList ) const
126  {
127  const filter::ByStatus toBeUninstalledFilter( &ResStatus::isToBeUninstalled );
128 
129  PoolItem pi ( slv );
130 
131  auto pool = ResPool::instance();
132 
133  // make sure the pool is clean
134  if ( !pool.resolver().resolvePool() ) {
135  MIL << "Pool failed to resolve, not doing anything" << std::endl;
136  return false;
137  }
138 
139  MIL << "Request to remove package: " << pi << std::endl;
140 
141  //list of packages that are allowed to be removed automatically.
142  const str::regex validRemovals("(kernel-syms(-.*)?|kgraft-patch(-.*)?|kernel-livepatch(-.*)?|.*-kmp(-.*)?)");
143 
144  if ( pi.status().isLocked() ) {
145  MIL << "Package " << pi << " is locked by the user, not removing." << std::endl;
146  return false;
147  }
148 
149  //remember which packages are already marked for removal, we do not need to check them again
150  std::set<sat::Solvable> currentSetOfRemovals;
151  for ( const PoolItem & p : pool.byStatus( toBeUninstalledFilter ) ) {
152  currentSetOfRemovals.insert( p.satSolvable() );
153  }
154 
156 
157  if ( !pool.resolver().resolvePool() ) {
158  MIL << "Failed to resolve pool, skipping " << pi << std::endl;
159  pool.resolver().problems();
160  pi.statusReset();
161 
162  return false;
163  }
164 
165  std::set<sat::Solvable> removedInThisRun;
166  removedInThisRun.insert( slv );
167 
168  for ( const PoolItem & p : pool.byStatus( toBeUninstalledFilter ) ) {
169 
170  //check if that package is removeable
171  if ( p.status().isByUser() //this was set by us, ignore it
172  || (currentSetOfRemovals.find( p.satSolvable() ) != currentSetOfRemovals.end()) //this was marked by a previous removal, ignore them
173  )
174  continue;
175 
176  // remember for later we need remove the debugsource and debuginfo packages as well
177  removedInThisRun.insert( p.satSolvable() );
178 
179  MIL << "Package " << p << " was marked by the solver for removal." << std::endl;
180 
181  // if we do not plan to remove that package anyway, we need to check if its allowed to be removed ( package in removelist can never be in keep list )
182  if ( removeList.find( p.satSolvable() ) != removeList.end() )
183  continue;
184 
185  if ( keepList.find( p.satSolvable() ) != keepList.end() ) {
186  MIL << "Package " << p << " is in keep spec, skipping" << pi << std::endl;
187  pi.statusReset();
188  return false;
189  }
190 
191  /*
192  * bsc#1185325 We can not solely rely on name matching to figure out
193  * which packages are kmod's, in SLES from Leap 15.3 forward we have the
194  * kernel-flavour-extra packages ( and others similarly named ) that are basically
195  * a collection of kmod's. So checking the name for .*-kmp(-.*)? is not enough.
196  * We first check if the package provides kmod(*) or ksym(*) and only fall back to name
197  * checking if that is not the case.
198  * Just to be safe I'll leave the regex in the fallback case as well, but it should be completely
199  * redundant now.
200  */
201  bool mostLikelyKmod = false;
202  StrMatcher matchMod( "kmod(*)", Match::GLOB );
203  StrMatcher matchSym( "ksym(*)", Match::GLOB );
204  for ( const auto &prov : p.provides() ) {
205  if ( matchMod.doMatch( prov.detail().name().c_str()) || matchSym.doMatch( prov.detail().name().c_str() ) ) {
206  mostLikelyKmod = true;
207  break;
208  }
209  }
210 
211  if ( mostLikelyKmod ) {
212  MIL << "Package " << p << " is most likely a kmod " << std::endl;
213  } else {
214  str::smatch what;
215  if ( !str::regex_match( p.name(), what, validRemovals) ) {
216  MIL << "Package " << p << " should not be removed, skipping " << pi << std::endl;
217  pi.statusReset();
218  return false;
219  }
220  }
221  }
222 
223  MIL << "Successfully marked package: " << pi << " for removal."<<std::endl;
224 
225  //now check and mark the -debugsource and -debuginfo packages for this package and all the packages that were removed. Maybe collect it before and just remove here
226  MIL << "Trying to remove debuginfo for: " << pi <<"."<<std::endl;
227  for ( sat::Solvable solvable : removedInThisRun ) {
228 
229  if ( solvable.arch() == Arch_noarch ||
230  solvable.arch() == Arch_empty )
231  continue;
232 
233  for ( const char * suffix : { "-debugsource", "-debuginfo" } ) {
234  PoolQuery q;
236  q.addDependency( sat::SolvAttr::provides, Capability( solvable.name()+suffix, Rel::EQ, solvable.edition() ) );
237  q.setInstalledOnly();
238  q.setMatchExact();
239 
240  for ( sat::Solvable debugPackage : q ) {
241 
242  if ( debugPackage.arch() != solvable.arch() )
243  continue;
244 
245  MIL << "Found debug package for " << solvable << " : " << debugPackage << std::endl;
246  //if removing the package fails it will not stop us from going on , so no need to check
247  removePackageAndCheck( debugPackage, keepList, removeList );
248  }
249  }
250  }
251  MIL << "Finished removing debuginfo for: " << pi <<"."<<std::endl;
252 
253  return true;
254  }
255 
260  {
261  if ( a == b )
262  return true;
263 
264  // the build counter should not be considered here, so if there is one we cut it off
265  const str::regex buildCntRegex( "\\.[0-9]+($|\\.g[0-9a-f]{7}$)", str::regex::match_extended );
266 
267  std::string versionStr = b.asString();
268  str::smatch matches;
269  if ( buildCntRegex.matches( versionStr.data(), matches ) ) {
270  if ( matches.size() >= 2 ) {
271  versionStr.replace( matches.begin(0), (matches.end(0) - matches.begin(0))+1, matches[1] );
272  return a == Edition(versionStr);
273  }
274  }
275  return false;
276  }
277 
282  {
283  //keep spec parse regex, make sure to edit the group offsets if changing this regex
284  const str::regex specRegex( "^(latest|oldest)([+-][0-9]+)?$", str::regex::match_extended );
285 
286  const unsigned tokenGrp = 1; //index of the group matching the token
287  const unsigned modifierGrp = 2; //index of the group matching the offset modifier
288 
289 
290  MIL << "Parsing keep spec: " << _keepSpec << std::endl;
291 
292  std::vector<std::string> words;
293  str::split( _keepSpec, std::back_inserter(words), ",", str::TRIM );
294  if ( words.empty() ) {
295  WAR << "Invalid keep spec: " << _keepSpec << " using default latest,running." << std::endl;
296  return;
297  }
298 
299  _keepRunning = false;
300  _keepLatestOffsets.clear();
301  _keepOldestOffsets.clear();
302 
303  for ( const std::string &word : words ) {
304  if ( word == "running" ) {
305  _keepRunning = true;
306  } else {
307  str::smatch what;
308  if ( !str::regex_match( word, what, specRegex ) ) {
309  _keepSpecificEditions.insert( Edition(word) );
310  continue;
311  }
312 
313  auto addKeepOff = []( const auto &off, auto &set, const auto &constraint ){
314  const off_t num = off.empty() ? 0 : str::strtonum<off_t>( off );
315  if ( !constraint(num) ) return false;
316  set.insert( static_cast<size_t>(std::abs(num)) );
317  return true;
318  };
319 
320  if ( what[tokenGrp] == "oldest" ) {
321  addKeepOff( what[modifierGrp], _keepOldestOffsets, [ &word ]( off_t num ) {
322  if ( num < 0 ) {
323  WAR << "Ignoring invalid modifier in keep spec: " << word << ", oldest supports only positive modifiers." << std::endl;
324  return false;
325  }
326  return true;
327  });
328  } else {
329  addKeepOff( what[modifierGrp], _keepLatestOffsets, [ &word ]( off_t num ) {
330  if ( num > 0 ) {
331  WAR << "Ignoring invalid modifier in keep spec: " << word << ", latest supports only negative modifiers." << std::endl;
332  return false;
333  }
334  return true;
335  });
336  }
337  }
338  }
339  }
340 
350  void PurgeKernels::Impl::fillKeepList( const GroupMap &installedKernels, std::set<sat::Solvable> &keepList, std::set<sat::Solvable> &removeList ) const
351  {
352 
353  const auto markAsKeep = [ &keepList, &removeList ]( sat::Solvable pck ) {
354  MIL << "Marking package " << pck << " as to keep." << std::endl;
355  keepList.insert( pck ) ;
356  removeList.erase( pck );
357  };
358 
359  const auto versionPredicate = []( const auto &edition ){
360  return [ &edition ]( const auto &elem ) {
361  return versionMatch( edition, elem.first );
362  };
363  };
364 
365  for ( const auto &groupInfo : installedKernels ) {
366 
367  MIL << "Starting with group " << groupInfo.first << std::endl;
368 
369  for ( const auto &archMap : groupInfo.second.archToEdMap ) {
370 
371  MIL << "Starting with arch " << archMap.first << std::endl;
372 
373  size_t currOff = 0; //the current "oldest" offset ( runs from map start to end )
374  size_t currROff = archMap.second.size() - 1; // the current "latest" offset ( runs from map end to start )
375 
376 
377  const EditionToSolvableMap &map = archMap.second;
378 
379  if ( _keepRunning
380  && ( ( archMap.first == _kernelArch && groupInfo.second.groupFlavour == _runningKernelFlavour )
381  || groupInfo.second.groupType == GroupInfo::Sources ) ) {
382 
383  MIL << "Matching packages against running kernel "<< _runningKernelEdition << "-" << _runningKernelFlavour << "-" <<_kernelArch << std::endl;
384 
385  auto it = std::find_if( map.begin(), map.end(), versionPredicate( _runningKernelEdition ) );
386  if ( it == map.end() ) {
387 
388  // If we look at Sources we cannot match the flavour but we still want to keep on checking the rest of the keep spec
389  if ( groupInfo.second.groupType != GroupInfo::Sources ) {
390  MIL << "Running kernel "<< _runningKernelEdition << "-" << _runningKernelFlavour << "-" <<_kernelArch << " not installed."<<std::endl;
391  MIL << "NOT removing any packages for flavor "<<_runningKernelFlavour<<"-"<<_kernelArch<<" ."<<std::endl;
392 
393  for ( const auto &kernelMap : map ) {
394  for( sat::Solvable pck : kernelMap.second )
395  markAsKeep(pck);
396  }
397  continue;
398  }
399 
400  } else {
401  // there could be multiple matches here because of rebuild counter, lets try to find the last one
402  MIL << "Found possible running candidate edition: " << it->first << std::endl;
403  auto nit = it;
404  for ( nit++ ; nit != map.end() && versionMatch( _runningKernelEdition, nit->first ) ; nit++ ) {
405  MIL << "Found possible more recent running candidate edition: " << nit->first << std::endl;
406  it = nit;
407  }
408  }
409 
410  // mark all packages of the running version as keep
411  if ( it != map.end() ) {
412  for( sat::Solvable pck : it->second ) {
413  markAsKeep(pck);
414  }
415  }
416  }
417 
418  for ( const auto &kernelMap : map ) {
419  //if we find one of the running offsets in the keepspec, we add the kernel id the the list of packages to keep
420  if ( _keepOldestOffsets.find( currOff ) != _keepOldestOffsets.end() || _keepLatestOffsets.find( currROff ) != _keepLatestOffsets.end() ) {
421  std::for_each( kernelMap.second.begin(), kernelMap.second.end(), markAsKeep );
422  }
423  currOff++;
424  currROff--;
425 
426  // a kernel package might be explicitely locked by version
427  // We need to go over all package name provides ( provides is named like the package ) and match
428  // them against the specified version to know which ones to keep. (bsc#1176740 bsc#1176192)
429  std::for_each( kernelMap.second.begin(), kernelMap.second.end(), [ & ]( sat::Solvable solv ){
430  for ( Capability prov : solv.provides() ) {
431  if ( prov.detail().name() == solv.name() && _keepSpecificEditions.count( prov.detail().ed() ) ) {
432  markAsKeep( solv );
433  }
434  }
435  });
436  }
437  }
438  }
439  }
440 
442  : _pimpl( new Impl() )
443  {
444 
445  }
446 
448  {
449  MIL << std::endl << "--------------------- Starting to mark obsolete kernels ---------------------"<<std::endl;
450 
451  if ( _pimpl->_keepSpec.empty() ) {
452  WAR << "Keep spec is empty, removing nothing." << std::endl;
453  return;
454  }
455 
457 
459  WAR << "Unable to detect running kernel, but keeping the running kernel was requested. Not removing any packages." << std::endl;
460  return;
461  }
462 
463  auto pool = ResPool::instance();
464  pool.resolver().setForceResolve( true ); // set allow uninstall flag
465 
466  const filter::ByStatus toBeUninstalledFilter( &ResStatus::isToBeUninstalled );
467 
468  // kernel flavour regex
469  const str::regex kernelFlavourRegex("^kernel-(.*)$");
470 
471  // the map of all installed kernel packages, grouped by Flavour -> Arch -> Version -> (List of all packages in that category)
472  // devel and source packages are grouped together
473  GroupMap installedKrnlPackages;
474 
475 
476  // packages that we plan to remove
477  std::set<sat::Solvable> packagesToRemove;
478 
479  const auto addPackageToMap = [&installedKrnlPackages, &packagesToRemove] ( const GroupInfo::GroupType type, const std::string &ident, const std::string &flavour, const sat::Solvable &installedKrnlPck ) {
480 
481  if ( !installedKrnlPackages.count( ident ) )
482  installedKrnlPackages.insert( std::make_pair( ident, GroupInfo(type, flavour) ) );
483 
484  auto &groupInfo = installedKrnlPackages[ ident ];
485  if ( groupInfo.groupType != type || groupInfo.groupFlavour != flavour ) {
486  ERR << "Got inconsistent type and flavour for ident this is a BUG: " << ident << std::endl
487  << "Original Flavour-Type: "<<groupInfo.groupFlavour<<"-"<<groupInfo.groupType << std::endl
488  << "Competing Flavour-Type: "<< flavour << "-" << type << std::endl;
489  }
490 
491  const auto currArch = installedKrnlPck.arch();
492  if ( !groupInfo.archToEdMap.count( currArch ) )
493  groupInfo.archToEdMap.insert( std::make_pair( currArch , EditionToSolvableMap {} ) );
494 
495  auto &editionToSolvableMap = groupInfo.archToEdMap[ currArch ];
496 
497  // calculate the "shortest" or most generic edition of all the package name provides
498  // ( the key of the provides is the package name ). This generic edition is used to
499  // group the packages together. This should get us around the issue that uname -r does
500  // not represent the actual rpm package version anymore. ( bsc#1176740 )
501  auto currCount = INT_MAX;
502  Edition edToUse;
503  for ( Capability prov : installedKrnlPck.provides() ) {
504  if ( prov.detail().name() == installedKrnlPck.name() ) {
505  if ( edToUse == Edition::noedition ) {
506  edToUse = installedKrnlPck.edition();
507  const auto &relStr = edToUse.release();
508  currCount = std::count( relStr.begin(), relStr.end(), '.');
509  } else {
510  const auto &pckEd = prov.detail().ed();
511  const auto &relStr = pckEd.release();
512  if ( const auto pntCnt = std::count( relStr.begin(), relStr.end(), '.'); pntCnt < currCount ) {
513  currCount = pntCnt;
514  edToUse = pckEd;
515  }
516  }
517  }
518  }
519 
520  if ( !editionToSolvableMap.count( edToUse ) )
521  editionToSolvableMap.insert( std::make_pair( edToUse, SolvableList{} ) );
522 
523  editionToSolvableMap[edToUse].push_back( installedKrnlPck );
524 
525  //in the first step we collect all packages in this list, then later we will remove the packages we want to explicitely keep
526  packagesToRemove.insert( installedKrnlPck );
527  };
528 
529  // the set of satSolvables that have to be kept always
530  std::set<sat::Solvable> packagesToKeep;
531 
532  //collect the list of installed kernel packages
533  PoolQuery q;
535  q.addAttribute( sat::SolvAttr::provides, "multiversion(kernel)" );
536  q.setInstalledOnly();
537  q.setMatchExact();
538 
539  MIL << "Searching for obsolete multiversion kernel packages." << std::endl;
540 
541  for ( sat::Solvable installedKrnlPck : q ) {
542 
543  MIL << "Found installed multiversion kernel package " << installedKrnlPck << std::endl;
544 
545  if ( installedKrnlPck.provides().matches(Capability("kernel-uname-r")) ) {
546  MIL << "Identified as a kernel package " << std::endl;
547 
548  // we group kernel packages by flavour
549  str::smatch what;
550  str::regex_match( installedKrnlPck.name(), what, kernelFlavourRegex );
551  if ( what[1].empty() ) {
552  WAR << "Could not detect flavour for: " << installedKrnlPck << " ...skipping" << std::endl;
553  continue;
554  }
555 
556  std::string flavour = what[1];
557 
558  // XXX: No dashes in flavor names
559  const auto dash = flavour.find_first_of('-');
560  if ( dash != std::string::npos ) {
561  flavour = flavour.substr( 0, dash );
562  }
563 
564  // the ident for kernels is the flavour, to also handle cases like kernel-base and kernel which should be in the same group handled together
565  addPackageToMap( GroupInfo::Kernels, flavour, flavour, installedKrnlPck );
566 
567  } else {
568 
569  // if adapting the groups do not forget to explicitely handle the group when querying the matches
570  const str::regex explicitelyHandled("kernel-syms(-.*)?|kernel(-.*)?-devel");
571 
572  MIL << "Not a kernel package, inspecting more closely " << std::endl;
573 
574  // we directly handle all noarch packages that export multiversion(kernel)
575  if ( installedKrnlPck.arch() == Arch_noarch ) {
576 
577  MIL << "Handling package explicitely due to architecture (noarch)."<< std::endl;
578  addPackageToMap( GroupInfo::Sources, installedKrnlPck.name(), "", installedKrnlPck );
579 
580  } else if ( str::smatch match; str::regex_match( installedKrnlPck.name(), match, explicitelyHandled ) ) {
581 
582  // try to get the flavour from the name
583  // if we have a kernel-syms getting no flavour means we have the "default" one, otherwise we use the flavour
584  // getting no flavour for a kernel(-*)?-devel means we have the kernel-devel package otherwise the flavour specific one
585  // ...yes this is horrible
586  std::string flav;
587 
588  // first group match is a kernel-syms
589  if ( match.size() > 1 && match[1].size() )
590  flav = match[1].substr(1);
591  // second group match is a kernel-flavour-devel
592  else if ( match.size() > 2 && match[2].size() )
593  flav = match[2].substr(1);
594  else if ( installedKrnlPck.name() == "kernel-syms" )
595  flav = "default";
596 
597  MIL << "Handling package explicitely due to name match."<< std::endl;
598  addPackageToMap ( GroupInfo::RelatedBinaries, installedKrnlPck.name(), flav, installedKrnlPck );
599  } else {
600  MIL << "Package not explicitely handled" << std::endl;
601  }
602  }
603 
604  }
605 
606  MIL << "Grouped packages: " << std::endl;
607  std::for_each( installedKrnlPackages.begin(), installedKrnlPackages.end(),[]( const auto &ident ){
608  MIL << "\tGroup ident: "<<ident.first<<std::endl;
609  MIL << "\t Group type: "<<ident.second.groupType<<std::endl;
610  MIL << "\t Group flav: "<<ident.second.groupFlavour<<std::endl;
611  std::for_each( ident.second.archToEdMap.begin(), ident.second.archToEdMap.end(), []( const auto &arch) {
612  MIL << "\t\tArch: "<<arch.first<<std::endl;
613  std::for_each( arch.second.begin(), arch.second.end(), []( const auto &edition) {
614  MIL << "\t\t\tEdition: "<<edition.first<<std::endl;
615  std::for_each( edition.second.begin(), edition.second.end(), []( const auto &packageId) {
616  MIL << "\t\t\t\t "<<sat::Solvable(packageId)<<std::endl;
617  });
618  });
619  });
620  });
621 
622  _pimpl->fillKeepList( installedKrnlPackages, packagesToKeep, packagesToRemove );
623 
624  for ( sat::Solvable slv : packagesToRemove )
625  _pimpl->removePackageAndCheck( slv, packagesToKeep, packagesToRemove );
626  }
627 
628  void PurgeKernels::setUnameR( const std::string &val )
629  {
630  _pimpl->setUnameR( val );
631  }
632 
633  std::string PurgeKernels::unameR() const
634  {
635  return _pimpl->_uname_r;
636  }
637 
639  {
640  _pimpl->_kernelArch = arch;
641  }
642 
644  {
645  return _pimpl->_kernelArch;
646  }
647 
648  void PurgeKernels::setKeepSpec( const std::string &val )
649  {
650  _pimpl->_keepSpec = val;
651  }
652 
653  std::string PurgeKernels::keepSpec() const
654  {
655  return _pimpl->_keepSpec;
656  }
657 
658 }
#define MIL
Definition: Logger.h:96
ArchToEditionMap archToEdMap
Definition: PurgeKernels.cc:53
A Solvable object within the sat Pool.
Definition: Solvable.h:53
unsigned size() const
Definition: Regex.cc:106
std::string regex_substitute(const std::string &s, const regex &regex, const std::string &replacement, bool global=true)
Replaces the matched regex with the string passed in replacement.
Definition: Regex.cc:120
void addAttribute(const sat::SolvAttr &attr, const std::string &value="")
Filter by the value of the specified attr attribute.
Definition: PoolQuery.cc:873
static const ResKind package
Definition: ResKind.h:40
Regular expression.
Definition: Regex.h:94
static ZConfig & instance()
Singleton ctor.
Definition: Resolver.cc:126
std::map< Edition, SolvableList > EditionToSolvableMap
Definition: PurgeKernels.cc:39
void fillKeepList(const GroupMap &installedKernels, std::set< sat::Solvable > &keepList, std::set< sat::Solvable > &removeList) const
std::set< size_t > _keepOldestOffsets
Architecture.
Definition: Arch.h:36
Filter solvables according to their status.
Definition: Filter.h:141
String matching (STRING|SUBSTRING|GLOB|REGEX).
Definition: StrMatcher.h:297
static const Rel EQ
Definition: Rel.h:50
std::map< Arch, EditionToSolvableMap > ArchToEditionMap
Definition: PurgeKernels.cc:40
ResStatus & status() const
Returns the current status.
Definition: PoolItem.cc:204
const Arch Arch_empty(IdString::Empty)
std::string unameR() const
bool doMatch(const char *string_r) const
Return whether string matches.
Definition: StrMatcher.cc:298
Definition: Arch.h:347
void addDependency(const sat::SolvAttr &attr, const std::string &name, const Rel &op, const Edition &edition)
Query "name|global op edition".
Definition: PoolQuery.cc:876
void addKind(const ResKind &kind)
Filter by selectable kind.
Definition: PoolQuery.cc:867
std::string::size_type end(unsigned i) const
End index of subexpression i in match_str (or std::string::npos)
Definition: Regex.cc:100
Edition represents [epoch:]version[-release]
Definition: Edition.h:60
std::string::size_type begin(unsigned i) const
Begin index of subexpression i in match_str (or std::string::npos)
Definition: Regex.cc:97
RW_pointer< Impl > _pimpl
Definition: PurgeKernels.h:66
Match newline.
Definition: Regex.h:102
#define ERR
Definition: Logger.h:98
std::set< Edition > _keepSpecificEditions
std::string Flavour
Definition: PurgeKernels.cc:37
void setUnameR(const std::string &val)
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \, const Trim trim_r=NO_TRIM)
Split line_r into words.
Definition: String.h:527
std::string asString() const
Definition: IdStringType.h:106
ResStatus & statusReset() const
Reset status.
Definition: PoolItem.cc:205
static bool versionMatch(const Edition &a, const Edition &b)
void setKernelArch(const zypp::Arch &arch)
std::set< size_t > _keepLatestOffsets
std::string release() const
Release.
Definition: Edition.cc:110
#define WAR
Definition: Logger.h:97
bool removePackageAndCheck(const sat::Solvable slv, const std::set< sat::Solvable > &keepList, const std::set< sat::Solvable > &removeList) const
void setUnameR(const std::string &uname)
Definition: PurgeKernels.cc:78
std::string keepSpec() const
bool matches(const char *s, str::smatch &matches, int flags=none) const
Definition: Regex.cc:57
void setInstalledOnly()
Return only repo packages.
Definition: PoolQuery.cc:963
bool setToBeUninstalled(TransactByValue causer)
Definition: ResStatus.h:545
std::unordered_map< std::string, GroupInfo > GroupMap
Definition: PurgeKernels.cc:56
void setKeepSpec(const std::string &val)
Regular expression match result.
Definition: Regex.h:162
Arch kernelArch() const
std::list< sat::Solvable > SolvableList
Definition: PurgeKernels.cc:38
Meta-data query API.
Definition: PoolQuery.h:90
Use POSIX Extended Regular Expression syntax when interpreting regex.
Definition: Regex.h:101
bool isToBeUninstalled() const
Definition: ResStatus.h:261
bool isLocked() const
Definition: ResStatus.h:264
void setMatchExact()
Set to match exact string instead of substring.
Definition: PoolQuery.cc:952
GroupInfo(const GroupType type=None, std::string flav="")
Definition: PurgeKernels.cc:51
A sat capability.
Definition: Capability.h:59
static const SolvAttr provides
Definition: SolvAttr.h:60
std::string multiversionKernels() const
Definition: ZConfig.cc:1202
enum zypp::GroupInfo::GroupType groupType
bool regex_match(const std::string &s, smatch &matches, const regex &regex)
regex ZYPP_STR_REGEX regex ZYPP_STR_REGEX
Definition: Regex.h:70
Combining sat::Solvable and ResStatus.
Definition: PoolItem.h:50
std::string groupFlavour
Definition: PurgeKernels.cc:54
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
static const Edition noedition
Value representing noedition ("") This is in fact a valid Edition.
Definition: Edition.h:73
static ResPool instance()
Singleton ctor.
Definition: ResPool.cc:37