clean.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: clean.cc,v 1.2 1999/06/24 04:06:30 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 <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. DIR *D = opendir(Dir.c_str());
  26. if (D == 0)
  27. return _error->Errno("opendir","Unable to read %s",Dir.c_str());
  28. string StartDir = SafeGetCWD();
  29. if (chdir(Dir.c_str()) != 0)
  30. {
  31. closedir(D);
  32. return _error->Errno("chdir","Unable to change to ",Dir.c_str());
  33. }
  34. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  35. {
  36. // Skip some files..
  37. if (strcmp(Dir->d_name,"lock") == 0 ||
  38. strcmp(Dir->d_name,"partial") == 0 ||
  39. strcmp(Dir->d_name,".") == 0 ||
  40. strcmp(Dir->d_name,"..") == 0)
  41. continue;
  42. struct stat St;
  43. if (stat(Dir->d_name,&St) != 0)
  44. return _error->Errno("stat","Unable to stat %s.",Dir->d_name);
  45. // Grab the package name
  46. const char *I = Dir->d_name;
  47. for (; *I != 0 && *I != '_';I++);
  48. if (*I != '_')
  49. continue;
  50. string Pkg = DeQuoteString(string(Dir->d_name,I-Dir->d_name));
  51. // Grab the version
  52. const char *Start = I + 1;
  53. for (I = Start; *I != 0 && *I != '_';I++);
  54. if (*I != '_')
  55. continue;
  56. string Ver = DeQuoteString(string(Start,I-Start));
  57. // Grab the arch
  58. Start = I + 1;
  59. for (I = Start; *I != 0 && *I != '.' ;I++);
  60. if (*I != '.')
  61. continue;
  62. string Arch = DeQuoteString(string(Start,I-Start));
  63. // Lookup the package
  64. pkgCache::PkgIterator P = Cache.FindPkg(Pkg);
  65. if (P.end() != true)
  66. {
  67. pkgCache::VerIterator V = P.VersionList();
  68. for (; V.end() == false; V++)
  69. {
  70. // See if we can fetch this version at all
  71. bool IsFetchable = false;
  72. for (pkgCache::VerFileIterator J = V.FileList();
  73. J.end() == false; J++)
  74. {
  75. if ((J.File()->Flags & pkgCache::Flag::NotSource) != 0)
  76. continue;
  77. IsFetchable = true;
  78. break;
  79. }
  80. // See if this verison matches the file
  81. if (IsFetchable == true && Ver == V.VerStr())
  82. break;
  83. }
  84. // We found a match, keep the file
  85. if (V.end() == false)
  86. continue;
  87. }
  88. Erase(Dir->d_name,Pkg,Ver,St);
  89. };
  90. chdir(StartDir.c_str());
  91. closedir(D);
  92. return true;
  93. }
  94. /*}}}*/