clean.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // non-existing directories are always clean
  35. // we do not check for a directory explicitly to support symlinks
  36. if (FileExists(Dir) == false)
  37. return true;
  38. DIR *D = opendir(Dir.c_str());
  39. if (D == 0)
  40. return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
  41. std::string StartDir = SafeGetCWD();
  42. if (chdir(Dir.c_str()) != 0)
  43. {
  44. closedir(D);
  45. return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
  46. }
  47. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  48. {
  49. // Skip some files..
  50. if (strcmp(Dir->d_name,"lock") == 0 ||
  51. strcmp(Dir->d_name,"partial") == 0 ||
  52. strcmp(Dir->d_name,"lost+found") == 0 ||
  53. strcmp(Dir->d_name,".") == 0 ||
  54. strcmp(Dir->d_name,"..") == 0)
  55. continue;
  56. struct stat St;
  57. if (stat(Dir->d_name,&St) != 0)
  58. {
  59. _error->Errno("stat",_("Unable to stat %s."),Dir->d_name);
  60. closedir(D);
  61. if (chdir(StartDir.c_str()) != 0)
  62. return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
  63. return false;
  64. }
  65. // Grab the package name
  66. const char *I = Dir->d_name;
  67. for (; *I != 0 && *I != '_';I++);
  68. if (*I != '_')
  69. continue;
  70. std::string Pkg = DeQuoteString(std::string(Dir->d_name,I-Dir->d_name));
  71. // Grab the version
  72. const char *Start = I + 1;
  73. for (I = Start; *I != 0 && *I != '_';I++);
  74. if (*I != '_')
  75. continue;
  76. std::string Ver = DeQuoteString(std::string(Start,I-Start));
  77. // Grab the arch
  78. Start = I + 1;
  79. for (I = Start; *I != 0 && *I != '.' ;I++);
  80. if (*I != '.')
  81. continue;
  82. std::string const Arch = DeQuoteString(std::string(Start,I-Start));
  83. // ignore packages of unconfigured architectures
  84. if (APT::Configuration::checkArchitecture(Arch) == false)
  85. continue;
  86. // Lookup the package
  87. pkgCache::PkgIterator P = Cache.FindPkg(Pkg, Arch);
  88. if (P.end() != true)
  89. {
  90. pkgCache::VerIterator V = P.VersionList();
  91. for (; V.end() == false; ++V)
  92. {
  93. // See if we can fetch this version at all
  94. bool IsFetchable = false;
  95. for (pkgCache::VerFileIterator J = V.FileList();
  96. J.end() == false; ++J)
  97. {
  98. if (CleanInstalled == true &&
  99. J.File().Flagged(pkgCache::Flag::NotSource))
  100. continue;
  101. IsFetchable = true;
  102. break;
  103. }
  104. // See if this version matches the file
  105. if (IsFetchable == true && Ver == V.VerStr())
  106. break;
  107. }
  108. // We found a match, keep the file
  109. if (V.end() == false)
  110. continue;
  111. }
  112. Erase(Dir->d_name,Pkg,Ver,St);
  113. };
  114. closedir(D);
  115. if (chdir(StartDir.c_str()) != 0)
  116. return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
  117. return true;
  118. }
  119. /*}}}*/
  120. pkgArchiveCleaner::pkgArchiveCleaner() : d(NULL) {}
  121. APT_CONST pkgArchiveCleaner::~pkgArchiveCleaner() {}