cachefile.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cachefile.cc,v 1.8 2002/04/27 04:28:04 jgg Exp $
  4. /* ######################################################################
  5. CacheFile - Simple wrapper class for opening, generating and whatnot
  6. This class implements a simple 2 line mechanism to open various sorts
  7. of caches. It can operate as root, as not root, show progress and so on,
  8. it transparently handles everything necessary.
  9. ##################################################################### */
  10. /*}}}*/
  11. // Include Files /*{{{*/
  12. #include <apt-pkg/cachefile.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/sourcelist.h>
  15. #include <apt-pkg/pkgcachegen.h>
  16. #include <apt-pkg/configuration.h>
  17. #include <apt-pkg/policy.h>
  18. #include <apt-pkg/pkgsystem.h>
  19. #include <apt-pkg/acquire-item.h>
  20. #include <apt-pkg/fileutl.h>
  21. #include <apti18n.h>
  22. /*}}}*/
  23. // CacheFile::CacheFile - Constructor /*{{{*/
  24. // ---------------------------------------------------------------------
  25. /* */
  26. pkgCacheFile::pkgCacheFile() : Map(0), Cache(0), DCache(0), Policy(0)
  27. {
  28. }
  29. /*}}}*/
  30. // CacheFile::~CacheFile - Destructor /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* */
  33. pkgCacheFile::~pkgCacheFile()
  34. {
  35. delete DCache;
  36. delete Policy;
  37. delete Cache;
  38. delete Map;
  39. _system->UnLock(true);
  40. }
  41. /*}}}*/
  42. // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
  43. // ---------------------------------------------------------------------
  44. /* */
  45. bool pkgCacheFile::BuildCaches(OpProgress &Progress,bool WithLock)
  46. {
  47. const bool ErrorWasEmpty = _error->empty();
  48. if (WithLock == true)
  49. if (_system->Lock() == false)
  50. return false;
  51. if (_config->FindB("Debug::NoLocking",false) == true)
  52. WithLock = false;
  53. if (_error->PendingError() == true)
  54. return false;
  55. // Read the source list
  56. pkgSourceList List;
  57. if (List.ReadMainList() == false)
  58. return _error->Error(_("The list of sources could not be read."));
  59. // Read the caches
  60. bool Res = pkgMakeStatusCache(List,Progress,&Map,!WithLock);
  61. Progress.Done();
  62. if (Res == false)
  63. return _error->Error(_("The package lists or status file could not be parsed or opened."));
  64. /* This sux, remove it someday */
  65. if (ErrorWasEmpty == true && _error->empty() == false)
  66. _error->Warning(_("You may want to run apt-get update to correct these problems"));
  67. Cache = new pkgCache(Map);
  68. if (_error->PendingError() == true)
  69. return false;
  70. return true;
  71. }
  72. /*}}}*/
  73. // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
  74. // ---------------------------------------------------------------------
  75. /* */
  76. bool pkgCacheFile::Open(OpProgress &Progress,bool WithLock)
  77. {
  78. if (BuildCaches(Progress,WithLock) == false)
  79. return false;
  80. // The policy engine
  81. Policy = new pkgPolicy(Cache);
  82. if (_error->PendingError() == true)
  83. return false;
  84. if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false)
  85. return false;
  86. // Create the dependency cache
  87. DCache = new pkgDepCache(Cache,Policy);
  88. if (_error->PendingError() == true)
  89. return false;
  90. DCache->Init(&Progress);
  91. Progress.Done();
  92. if (_error->PendingError() == true)
  93. return false;
  94. return true;
  95. }
  96. /*}}}*/
  97. // CacheFile::Close - close the cache files /*{{{*/
  98. // ---------------------------------------------------------------------
  99. /* */
  100. void pkgCacheFile::Close()
  101. {
  102. delete DCache;
  103. delete Policy;
  104. delete Cache;
  105. delete Map;
  106. _system->UnLock(true);
  107. Map = 0;
  108. DCache = 0;
  109. Policy = 0;
  110. Cache = 0;
  111. }
  112. /*}}}*/