clean.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <dirent.h>
  17. #include <sys/stat.h>
  18. #include <unistd.h>
  19. #include <apti18n.h>
  20. /*}}}*/
  21. // ArchiveCleaner::Go - Perform smart cleanup of the archive /*{{{*/
  22. // ---------------------------------------------------------------------
  23. /* Scan the directory for files to erase, we check the version information
  24. against our database to see if it is interesting */
  25. bool pkgArchiveCleaner::Go(std::string Dir,pkgCache &Cache)
  26. {
  27. bool CleanInstalled = _config->FindB("APT::Clean-Installed",true);
  28. DIR *D = opendir(Dir.c_str());
  29. if (D == 0)
  30. return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
  31. std::string StartDir = SafeGetCWD();
  32. if (chdir(Dir.c_str()) != 0)
  33. {
  34. closedir(D);
  35. return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
  36. }
  37. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  38. {
  39. // Skip some files..
  40. if (strcmp(Dir->d_name,"lock") == 0 ||
  41. strcmp(Dir->d_name,"partial") == 0 ||
  42. strcmp(Dir->d_name,".") == 0 ||
  43. strcmp(Dir->d_name,"..") == 0)
  44. continue;
  45. struct stat St;
  46. if (stat(Dir->d_name,&St) != 0)
  47. {
  48. _error->Errno("stat",_("Unable to stat %s."),Dir->d_name);
  49. closedir(D);
  50. if (chdir(StartDir.c_str()) != 0)
  51. return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
  52. return false;
  53. }
  54. // Grab the package name
  55. const char *I = Dir->d_name;
  56. for (; *I != 0 && *I != '_';I++);
  57. if (*I != '_')
  58. continue;
  59. std::string Pkg = DeQuoteString(std::string(Dir->d_name,I-Dir->d_name));
  60. // Grab the version
  61. const char *Start = I + 1;
  62. for (I = Start; *I != 0 && *I != '_';I++);
  63. if (*I != '_')
  64. continue;
  65. std::string Ver = DeQuoteString(std::string(Start,I-Start));
  66. // Grab the arch
  67. Start = I + 1;
  68. for (I = Start; *I != 0 && *I != '.' ;I++);
  69. if (*I != '.')
  70. continue;
  71. std::string const Arch = DeQuoteString(std::string(Start,I-Start));
  72. // ignore packages of unconfigured architectures
  73. if (APT::Configuration::checkArchitecture(Arch) == false)
  74. continue;
  75. // Lookup the package
  76. pkgCache::PkgIterator P = Cache.FindPkg(Pkg, Arch);
  77. if (P.end() != true)
  78. {
  79. pkgCache::VerIterator V = P.VersionList();
  80. for (; V.end() == false; ++V)
  81. {
  82. // See if we can fetch this version at all
  83. bool IsFetchable = false;
  84. for (pkgCache::VerFileIterator J = V.FileList();
  85. J.end() == false; ++J)
  86. {
  87. if (CleanInstalled == true &&
  88. (J.File()->Flags & pkgCache::Flag::NotSource) != 0)
  89. continue;
  90. IsFetchable = true;
  91. break;
  92. }
  93. // See if this version matches the file
  94. if (IsFetchable == true && Ver == V.VerStr())
  95. break;
  96. }
  97. // We found a match, keep the file
  98. if (V.end() == false)
  99. continue;
  100. }
  101. Erase(Dir->d_name,Pkg,Ver,St);
  102. };
  103. closedir(D);
  104. if (chdir(StartDir.c_str()) != 0)
  105. return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
  106. return true;
  107. }
  108. /*}}}*/