cachefile.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #ifdef __GNUG__
  13. #pragma implementation "apt-pkg/cachefile.h"
  14. #endif
  15. #include <apt-pkg/cachefile.h>
  16. #include <apt-pkg/error.h>
  17. #include <apt-pkg/sourcelist.h>
  18. #include <apt-pkg/pkgcachegen.h>
  19. #include <apt-pkg/configuration.h>
  20. #include <apt-pkg/policy.h>
  21. #include <apt-pkg/pkgsystem.h>
  22. #include <apt-pkg/acquire-item.h>
  23. #include <apt-pkg/fileutl.h>
  24. #include <apti18n.h>
  25. /*}}}*/
  26. // CacheFile::CacheFile - Constructor /*{{{*/
  27. // ---------------------------------------------------------------------
  28. /* */
  29. pkgCacheFile::pkgCacheFile() : Map(0), Cache(0), DCache(0), Policy(0)
  30. {
  31. }
  32. /*}}}*/
  33. // CacheFile::~CacheFile - Destructor /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* */
  36. pkgCacheFile::~pkgCacheFile()
  37. {
  38. delete DCache;
  39. delete Policy;
  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 (WithLock == true)
  51. if (_system->Lock() == false)
  52. return false;
  53. if (_config->FindB("Debug::NoLocking",false) == true)
  54. WithLock = false;
  55. if (_error->PendingError() == true)
  56. return false;
  57. // Read the source list
  58. pkgSourceList List;
  59. if (List.ReadMainList() == false)
  60. return _error->Error(_("The list of sources could not be read."));
  61. // Read the caches
  62. bool Res = pkgMakeStatusCache(List,Progress,&Map,!WithLock);
  63. Progress.Done();
  64. if (Res == false)
  65. return _error->Error(_("The package lists or status file could not be parsed or opened."));
  66. /* This sux, remove it someday */
  67. if (_error->empty() == false)
  68. _error->Warning(_("You may want to run apt-get update to correct these problems"));
  69. Cache = new pkgCache(Map);
  70. if (_error->PendingError() == true)
  71. return false;
  72. return true;
  73. }
  74. /*}}}*/
  75. // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
  76. // ---------------------------------------------------------------------
  77. /* */
  78. bool pkgCacheFile::Open(OpProgress &Progress,bool WithLock)
  79. {
  80. if (BuildCaches(Progress,WithLock) == false)
  81. return false;
  82. // The policy engine
  83. Policy = new pkgPolicy(Cache);
  84. if (_error->PendingError() == true)
  85. return false;
  86. if (ReadPinFile(*Policy) == false)
  87. return false;
  88. // Create the dependency cache
  89. DCache = new pkgDepCache(Cache,Policy);
  90. if (_error->PendingError() == true)
  91. return false;
  92. DCache->Init(&Progress);
  93. Progress.Done();
  94. if (_error->PendingError() == true)
  95. return false;
  96. return true;
  97. }
  98. /*}}}*/
  99. // CacheFile::ListUpdate - update the cache files /*{{{*/
  100. // ---------------------------------------------------------------------
  101. /* */
  102. bool pkgCacheFile::ListUpdate(pkgAcquireStatus &Stat, pkgSourceList &List)
  103. {
  104. pkgAcquire Fetcher(&Stat);
  105. // Populate it with the source selection
  106. if (List.GetIndexes(&Fetcher) == false)
  107. return false;
  108. // Run scripts
  109. RunScripts("APT::Update::Pre-Invoke");
  110. // Run it
  111. if (Fetcher.Run() == pkgAcquire::Failed)
  112. return false;
  113. bool Failed = false;
  114. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
  115. {
  116. if ((*I)->Status == pkgAcquire::Item::StatDone)
  117. continue;
  118. (*I)->Finished();
  119. _error->Warning(_("Failed to fetch %s %s\n"),
  120. (*I)->DescURI().c_str(),
  121. (*I)->ErrorText.c_str());
  122. Failed = true;
  123. }
  124. // Clean out any old list files (if it was not a failure)
  125. // Keep "APT::Get::List-Cleanup" name for compatibility, but
  126. // this is really a global option for the APT library now
  127. if (!Failed && (_config->FindB("APT::Get::List-Cleanup",true) == true ||
  128. _config->FindB("APT::List-Cleanup",true) == true))
  129. {
  130. if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
  131. Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
  132. return false;
  133. }
  134. // Run the scripts
  135. RunScripts("APT::Update::Post-Invoke");
  136. return (Failed == false);
  137. }
  138. /*}}}*/
  139. // CacheFile::Close - close the cache files /*{{{*/
  140. // ---------------------------------------------------------------------
  141. /* */
  142. void pkgCacheFile::Close()
  143. {
  144. delete DCache;
  145. delete Policy;
  146. delete Cache;
  147. delete Map;
  148. _system->UnLock(true);
  149. Map = 0;
  150. DCache = 0;
  151. Policy = 0;
  152. Cache = 0;
  153. }
  154. /*}}}*/