clean.cc 3.7 KB

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