cachefile.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 = pkgCacheGenerator::MakeStatusCache(List,Progress,&Map,!WithLock);
  61. if (Progress != NULL)
  62. Progress->Done();
  63. if (Res == false)
  64. return _error->Error(_("The package lists or status file could not be parsed or opened."));
  65. /* This sux, remove it someday */
  66. if (ErrorWasEmpty == true && _error->empty() == false)
  67. _error->Warning(_("You may want to run apt-get update to correct these problems"));
  68. Cache = new pkgCache(Map);
  69. if (_error->PendingError() == true)
  70. return false;
  71. return true;
  72. }
  73. /*}}}*/
  74. // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
  75. // ---------------------------------------------------------------------
  76. /* */
  77. bool pkgCacheFile::BuildPolicy(OpProgress *Progress)
  78. {
  79. Policy = new pkgPolicy(Cache);
  80. if (_error->PendingError() == true)
  81. return false;
  82. if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false)
  83. return false;
  84. return true;
  85. }
  86. /*}}}*/
  87. // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
  88. // ---------------------------------------------------------------------
  89. /* */
  90. bool pkgCacheFile::BuildDepCache(OpProgress *Progress)
  91. {
  92. DCache = new pkgDepCache(Cache,Policy);
  93. if (_error->PendingError() == true)
  94. return false;
  95. DCache->Init(Progress);
  96. return true;
  97. }
  98. /*}}}*/
  99. // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
  100. // ---------------------------------------------------------------------
  101. /* */
  102. bool pkgCacheFile::Open(OpProgress *Progress, bool WithLock)
  103. {
  104. if (BuildCaches(Progress,WithLock) == false)
  105. return false;
  106. if (BuildPolicy(Progress) == false)
  107. return false;
  108. if (BuildDepCache(Progress) == false)
  109. return false;
  110. if (Progress != NULL)
  111. Progress->Done();
  112. if (_error->PendingError() == true)
  113. return false;
  114. return true;
  115. }
  116. /*}}}*/
  117. // CacheFile::Close - close the cache files /*{{{*/
  118. // ---------------------------------------------------------------------
  119. /* */
  120. void pkgCacheFile::Close()
  121. {
  122. delete DCache;
  123. delete Policy;
  124. delete Cache;
  125. delete Map;
  126. _system->UnLock(true);
  127. Map = 0;
  128. DCache = 0;
  129. Policy = 0;
  130. Cache = 0;
  131. }
  132. /*}}}*/