clean.cc 3.3 KB

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