PageRenderTime 21ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/plugins/in/kernels/kernels.cpp

#
C++ | 160 lines | 108 code | 28 blank | 24 comment | 16 complexity | 0c458a5cfca6b6bde67c40ec873a6061 MD5 | raw file
Possible License(s): GPL-3.0
  1. /**
  2. * This file is part of scleaner project.
  3. * Copyright (C) 2007, 2008 FROUIN Jean-Michel
  4. * Visit scleaner website : http://www.scleaner.org
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "kernels.h"
  17. #include <iostream>
  18. #include <sys/utsname.h>
  19. #include <string>
  20. #include <regex.h>
  21. #include <apt-pkg/pkgcache.h> //For pkgCache
  22. #include <apt-pkg/sourcelist.h> //For pkgSourceList
  23. #include <apt-pkg/pkgcachegen.h> //For pkgMakeStatusCache
  24. #include <apt-pkg/progress.h> //OpProgress
  25. #include <apt-pkg/init.h> //For configuration
  26. #include <apt-pkg/error.h> //_error
  27. #include <dirent.h>
  28. #include <plugins/in/in_plugin_initializer.h>
  29. #include <leak/leak_detector.h>
  30. Plugins::CInPluginInitializer<CkernelsPlugin> gKernels;
  31. CkernelsPlugin::CkernelsPlugin():
  32. fCache(0), fSrcList(0), fMap(0)
  33. {
  34. SetName("kernels");
  35. if (!pkgInitConfig(*_config) || !pkgInitSystem(*_config,_system))
  36. std::cerr << "[ERR] CkernelsPlugin() pkgInitConfig || pkgInitSystem\n";
  37. if (!(_config->FindB("APT::Cache::Generate",true)))
  38. fMap = new MMap(*new FileFd(_config->FindFile("Dir::Cache::pkgcache"), FileFd::ReadOnly),MMap::Public|MMap::ReadOnly);
  39. else
  40. {
  41. // Open the cache file
  42. fSrcList = new pkgSourceList;
  43. fSrcList->ReadMainList();
  44. // Generate it and map it
  45. OpProgress Prog;
  46. pkgMakeStatusCache(*fSrcList, Prog, &fMap, true);
  47. }
  48. if (!_error->PendingError())
  49. fCache = new pkgCache(fMap);
  50. else
  51. std::cerr << "[ERR] CkernelsPlugin(): Errors occured\n";
  52. }
  53. CkernelsPlugin::~CkernelsPlugin()
  54. {
  55. if(fMap)
  56. delete fMap;
  57. if(fCache)
  58. delete fCache;
  59. }
  60. bool CkernelsPlugin::Search(const std::string& Name, std::string& Result)
  61. {
  62. bool Ret = false;
  63. regex_t* Pattern = new regex_t;
  64. std::string Version = Name.substr(Name.find_first_of('-')+1, Name.length());
  65. std::string Filename = "linux-image-" + Version;
  66. if(regcomp(Pattern, Filename.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB))
  67. {
  68. std::cerr << "Regex error !\n";
  69. regfree(Pattern);
  70. }
  71. for (pkgCache::PkgIterator It = fCache->PkgBegin(); !(It.end()); ++It)
  72. {
  73. if (!regexec(Pattern,It.Name(),0,0,0))
  74. {
  75. Result = It.Name();
  76. Ret = true;
  77. break;
  78. }
  79. }
  80. //Did not add current kernel or computer won't works anymore ;)
  81. std::string CurrentKernelVersion;
  82. Engine::CEngine::GetKernelVersion(CurrentKernelVersion);
  83. if(CurrentKernelVersion == Version)
  84. Ret = false;
  85. regfree(Pattern);
  86. return Ret;
  87. }
  88. std::string CkernelsPlugin::Description()
  89. {
  90. return "Find unused installed kernels";
  91. }
  92. void CkernelsPlugin::GetFileList(std::list<std::string>& fl)
  93. {
  94. std::cout << "CkernelsPlugin::GetFileList " << fFL.size() << '\n';
  95. fl.merge(fFL);
  96. }
  97. void CkernelsPlugin::Run()
  98. {
  99. std::string Path("/boot/");
  100. while(fRunning)
  101. {
  102. struct dirent** NameList;
  103. int Nb = scandir(Path.c_str(), &NameList, 0, alphasort);
  104. if(Nb >= 0) //Fix Bug 4 : If no error append.
  105. {
  106. while (Nb-- > 0)
  107. {
  108. std::string Name(NameList[Nb]->d_name);
  109. if(Name.find("vmlinuz", 0) != std::string::npos)
  110. {
  111. std::string Res;
  112. if(Search(Name, Res))
  113. {
  114. fFL.push_back(Res);
  115. std::cout << CYAN << "CkernelsPlugin : I add : " << Res << STOP << '\n';
  116. }
  117. }
  118. free(NameList[Nb]);
  119. }
  120. free(NameList);
  121. }
  122. else
  123. std::cout << ROUGE << "CkernelsPlugin::Run() : Error\n" << STOP;
  124. fRunning=false;
  125. }
  126. }
  127. /* vi:set ts=4: */