clean.cc 3.3 KB

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