cachefile.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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(NULL), Cache(NULL), DCache(NULL),
  27. SrcList(NULL), Policy(NULL)
  28. {
  29. }
  30. /*}}}*/
  31. // CacheFile::~CacheFile - Destructor /*{{{*/
  32. // ---------------------------------------------------------------------
  33. /* */
  34. pkgCacheFile::~pkgCacheFile()
  35. {
  36. delete DCache;
  37. delete Policy;
  38. delete SrcList;
  39. delete Cache;
  40. delete Map;
  41. _system->UnLock(true);
  42. }
  43. /*}}}*/
  44. // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
  45. // ---------------------------------------------------------------------
  46. /* */
  47. bool pkgCacheFile::BuildCaches(OpProgress *Progress, bool WithLock)
  48. {
  49. if (Cache != NULL)
  50. return true;
  51. if (_config->FindB("pkgCacheFile::Generate", true) == false)
  52. {
  53. Map = new MMap(*new FileFd(_config->FindFile("Dir::Cache::pkgcache"),
  54. FileFd::ReadOnly),MMap::Public|MMap::ReadOnly);
  55. Cache = new pkgCache(Map);
  56. if (_error->PendingError() == true)
  57. return false;
  58. return true;
  59. }
  60. const bool ErrorWasEmpty = _error->empty();
  61. if (WithLock == true)
  62. if (_system->Lock() == false)
  63. return false;
  64. if (_config->FindB("Debug::NoLocking",false) == true)
  65. WithLock = false;
  66. if (_error->PendingError() == true)
  67. return false;
  68. BuildSourceList(Progress);
  69. // Read the caches
  70. bool Res = pkgCacheGenerator::MakeStatusCache(*SrcList,Progress,&Map,!WithLock);
  71. if (Progress != NULL)
  72. Progress->Done();
  73. if (Res == false)
  74. return _error->Error(_("The package lists or status file could not be parsed or opened."));
  75. /* This sux, remove it someday */
  76. if (ErrorWasEmpty == true && _error->empty() == false)
  77. _error->Warning(_("You may want to run apt-get update to correct these problems"));
  78. Cache = new pkgCache(Map);
  79. if (_error->PendingError() == true)
  80. return false;
  81. return true;
  82. }
  83. /*}}}*/
  84. // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
  85. // ---------------------------------------------------------------------
  86. /* */
  87. bool pkgCacheFile::BuildSourceList(OpProgress *Progress)
  88. {
  89. if (SrcList != NULL)
  90. return true;
  91. SrcList = new pkgSourceList();
  92. if (SrcList->ReadMainList() == false)
  93. return _error->Error(_("The list of sources could not be read."));
  94. return true;
  95. }
  96. /*}}}*/
  97. // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
  98. // ---------------------------------------------------------------------
  99. /* */
  100. bool pkgCacheFile::BuildPolicy(OpProgress *Progress)
  101. {
  102. if (Policy != NULL)
  103. return true;
  104. Policy = new pkgPolicy(Cache);
  105. if (_error->PendingError() == true)
  106. return false;
  107. if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false)
  108. return false;
  109. return true;
  110. }
  111. /*}}}*/
  112. // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
  113. // ---------------------------------------------------------------------
  114. /* */
  115. bool pkgCacheFile::BuildDepCache(OpProgress *Progress)
  116. {
  117. if (DCache != NULL)
  118. return true;
  119. DCache = new pkgDepCache(Cache,Policy);
  120. if (_error->PendingError() == true)
  121. return false;
  122. DCache->Init(Progress);
  123. return true;
  124. }
  125. /*}}}*/
  126. // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
  127. // ---------------------------------------------------------------------
  128. /* */
  129. bool pkgCacheFile::Open(OpProgress *Progress, bool WithLock)
  130. {
  131. if (BuildCaches(Progress,WithLock) == false)
  132. return false;
  133. if (BuildPolicy(Progress) == false)
  134. return false;
  135. if (BuildDepCache(Progress) == false)
  136. return false;
  137. if (Progress != NULL)
  138. Progress->Done();
  139. if (_error->PendingError() == true)
  140. return false;
  141. return true;
  142. }
  143. /*}}}*/
  144. // CacheFile::Close - close the cache files /*{{{*/
  145. // ---------------------------------------------------------------------
  146. /* */
  147. void pkgCacheFile::Close()
  148. {
  149. delete DCache;
  150. delete Policy;
  151. delete Cache;
  152. delete SrcList;
  153. delete Map;
  154. _system->UnLock(true);
  155. Map = NULL;
  156. DCache = NULL;
  157. Policy = NULL;
  158. Cache = NULL;
  159. SrcList = NULL;
  160. }
  161. /*}}}*/