cachefile.cc 5.7 KB

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