clean.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. chdir(StartDir.c_str());
  49. closedir(D);
  50. return _error->Errno("stat",_("Unable to stat %s."),Dir->d_name);
  51. }
  52. // Grab the package name
  53. const char *I = Dir->d_name;
  54. for (; *I != 0 && *I != '_';I++);
  55. if (*I != '_')
  56. continue;
  57. std::string Pkg = DeQuoteString(std::string(Dir->d_name,I-Dir->d_name));
  58. // Grab the version
  59. const char *Start = I + 1;
  60. for (I = Start; *I != 0 && *I != '_';I++);
  61. if (*I != '_')
  62. continue;
  63. std::string Ver = DeQuoteString(std::string(Start,I-Start));
  64. // Grab the arch
  65. Start = I + 1;
  66. for (I = Start; *I != 0 && *I != '.' ;I++);
  67. if (*I != '.')
  68. continue;
  69. std::string const Arch = DeQuoteString(std::string(Start,I-Start));
  70. if (APT::Configuration::checkArchitecture(Arch) == false)
  71. continue;
  72. // Lookup the package
  73. pkgCache::PkgIterator P = Cache.FindPkg(Pkg);
  74. if (P.end() != true)
  75. {
  76. pkgCache::VerIterator V = P.VersionList();
  77. for (; V.end() == false; ++V)
  78. {
  79. // See if we can fetch this version at all
  80. bool IsFetchable = false;
  81. for (pkgCache::VerFileIterator J = V.FileList();
  82. J.end() == false; ++J)
  83. {
  84. if (CleanInstalled == true &&
  85. (J.File()->Flags & pkgCache::Flag::NotSource) != 0)
  86. continue;
  87. IsFetchable = true;
  88. break;
  89. }
  90. // See if this verison matches the file
  91. if (IsFetchable == true && Ver == V.VerStr())
  92. break;
  93. }
  94. // We found a match, keep the file
  95. if (V.end() == false)
  96. continue;
  97. }
  98. Erase(Dir->d_name,Pkg,Ver,St);
  99. };
  100. chdir(StartDir.c_str());
  101. closedir(D);
  102. return true;
  103. }
  104. /*}}}*/