You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

136 lines
3.8 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: clean.cc,v 1.4 2001/02/20 07:03:17 jgg Exp $
  4. /* ######################################################################
  5. Clean - Clean out downloaded directories
  6. ##################################################################### */
  7. /*}}}*/
  8. // Includes /*{{{*/
  9. #include<config.h>
  10. #include <apt-pkg/clean.h>
  11. #include <apt-pkg/strutl.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/configuration.h>
  14. #include <apt-pkg/aptconfiguration.h>
  15. #include <apt-pkg/fileutl.h>
  16. #include <apt-pkg/pkgcache.h>
  17. #include <apt-pkg/cacheiterators.h>
  18. #include <string>
  19. #include <string.h>
  20. #include <dirent.h>
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. #include <apti18n.h>
  24. /*}}}*/
  25. // ArchiveCleaner::Go - Perform smart cleanup of the archive /*{{{*/
  26. // ---------------------------------------------------------------------
  27. /* Scan the directory for files to erase, we check the version information
  28. against our database to see if it is interesting */
  29. bool pkgArchiveCleaner::Go(std::string Dir,pkgCache &Cache)
  30. {
  31. bool CleanInstalled = _config->FindB("APT::Clean-Installed",true);
  32. if(Dir == "/")
  33. return _error->Error(_("Clean of %s is not supported"), Dir.c_str());
  34. DIR *D = opendir(Dir.c_str());
  35. if (D == 0)
  36. return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
  37. std::string StartDir = SafeGetCWD();
  38. if (chdir(Dir.c_str()) != 0)
  39. {
  40. closedir(D);
  41. return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
  42. }
  43. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  44. {
  45. // Skip some files..
  46. if (strcmp(Dir->d_name,"lock") == 0 ||
  47. strcmp(Dir->d_name,"partial") == 0 ||
  48. strcmp(Dir->d_name,".") == 0 ||
  49. strcmp(Dir->d_name,"..") == 0)
  50. continue;
  51. struct stat St;
  52. if (stat(Dir->d_name,&St) != 0)
  53. {
  54. _error->Errno("stat",_("Unable to stat %s."),Dir->d_name);
  55. closedir(D);
  56. if (chdir(StartDir.c_str()) != 0)
  57. return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
  58. return false;
  59. }
  60. // Grab the package name
  61. const char *I = Dir->d_name;
  62. for (; *I != 0 && *I != '_';I++);
  63. if (*I != '_')
  64. continue;
  65. std::string Pkg = DeQuoteString(std::string(Dir->d_name,I-Dir->d_name));
  66. // Grab the version
  67. const char *Start = I + 1;
  68. for (I = Start; *I != 0 && *I != '_';I++);
  69. if (*I != '_')
  70. continue;
  71. std::string Ver = DeQuoteString(std::string(Start,I-Start));
  72. // Grab the arch
  73. Start = I + 1;
  74. for (I = Start; *I != 0 && *I != '.' ;I++);
  75. if (*I != '.')
  76. continue;
  77. std::string const Arch = DeQuoteString(std::string(Start,I-Start));
  78. // ignore packages of unconfigured architectures
  79. if (APT::Configuration::checkArchitecture(Arch) == false)
  80. continue;
  81. // Lookup the package
  82. pkgCache::PkgIterator P = Cache.FindPkg(Pkg, Arch);
  83. if (P.end() != true)
  84. {
  85. pkgCache::VerIterator V = P.VersionList();
  86. for (; V.end() == false; ++V)
  87. {
  88. // See if we can fetch this version at all
  89. bool IsFetchable = false;
  90. for (pkgCache::VerFileIterator J = V.FileList();
  91. J.end() == false; ++J)
  92. {
  93. if (CleanInstalled == true &&
  94. J.File().Flagged(pkgCache::Flag::NotSource))
  95. continue;
  96. IsFetchable = true;
  97. break;
  98. }
  99. // See if this version matches the file
  100. if (IsFetchable == true && Ver == V.VerStr())
  101. break;
  102. }
  103. // We found a match, keep the file
  104. if (V.end() == false)
  105. continue;
  106. }
  107. Erase(Dir->d_name,Pkg,Ver,St);
  108. };
  109. closedir(D);
  110. if (chdir(StartDir.c_str()) != 0)
  111. return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
  112. return true;
  113. }
  114. /*}}}*/
  115. APT_CONST pkgArchiveCleaner::~pkgArchiveCleaner() {}