clean.cc 3.3 KB

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