cachefile.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. if (WithLock == true)
  48. if (_system->Lock() == false)
  49. return false;
  50. if (_config->FindB("Debug::NoLocking",false) == true)
  51. WithLock = false;
  52. if (_error->PendingError() == true)
  53. return false;
  54. // Read the source list
  55. pkgSourceList List;
  56. if (List.ReadMainList() == false)
  57. return _error->Error(_("The list of sources could not be read."));
  58. // Read the caches
  59. bool Res = pkgMakeStatusCache(List,Progress,&Map,!WithLock);
  60. Progress.Done();
  61. if (Res == false)
  62. return _error->Error(_("The package lists or status file could not be parsed or opened."));
  63. /* This sux, remove it someday */
  64. if (_error->empty() == false)
  65. _error->Warning(_("You may want to run apt-get update to correct these problems"));
  66. Cache = new pkgCache(Map);
  67. if (_error->PendingError() == true)
  68. return false;
  69. return true;
  70. }
  71. /*}}}*/
  72. // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
  73. // ---------------------------------------------------------------------
  74. /* */
  75. bool pkgCacheFile::Open(OpProgress &Progress,bool WithLock)
  76. {
  77. if (BuildCaches(Progress,WithLock) == false)
  78. return false;
  79. // The policy engine
  80. Policy = new pkgPolicy(Cache);
  81. if (_error->PendingError() == true)
  82. return false;
  83. if (ReadPinFile(*Policy) == false)
  84. return false;
  85. // Create the dependency cache
  86. DCache = new pkgDepCache(Cache,Policy);
  87. if (_error->PendingError() == true)
  88. return false;
  89. DCache->Init(&Progress);
  90. Progress.Done();
  91. if (_error->PendingError() == true)
  92. return false;
  93. return true;
  94. }
  95. /*}}}*/
  96. // CacheFile::ListUpdate - update the cache files /*{{{*/
  97. // ---------------------------------------------------------------------
  98. /* This is a simple wrapper to update the cache. it will fetch stuff
  99. * from the network (or any other sources defined in sources.list)
  100. */
  101. bool pkgCacheFile::ListUpdate(pkgAcquireStatus &Stat,
  102. pkgSourceList &List,
  103. int PulseInterval)
  104. {
  105. pkgAcquire::RunResult res;
  106. pkgAcquire Fetcher(&Stat);
  107. // Populate it with the source selection
  108. if (List.GetIndexes(&Fetcher) == false)
  109. return false;
  110. // Run scripts
  111. RunScripts("APT::Update::Pre-Invoke");
  112. // check arguments
  113. if(PulseInterval>0)
  114. res = Fetcher.Run(PulseInterval);
  115. else
  116. res = Fetcher.Run();
  117. if (res == pkgAcquire::Failed)
  118. return false;
  119. bool Failed = false;
  120. bool TransientNetworkFailure = false;
  121. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
  122. I != Fetcher.ItemsEnd(); I++)
  123. {
  124. if ((*I)->Status == pkgAcquire::Item::StatDone)
  125. continue;
  126. (*I)->Finished();
  127. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  128. (*I)->ErrorText.c_str());
  129. if ((*I)->Status == pkgAcquire::Item::StatTransientNetworkError)
  130. {
  131. TransientNetworkFailure = true;
  132. continue;
  133. }
  134. Failed = true;
  135. }
  136. // Clean out any old list files
  137. // Keep "APT::Get::List-Cleanup" name for compatibility, but
  138. // this is really a global option for the APT library now
  139. if (!TransientNetworkFailure && !Failed &&
  140. (_config->FindB("APT::Get::List-Cleanup",true) == true ||
  141. _config->FindB("APT::List-Cleanup",true) == true))
  142. {
  143. if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
  144. Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
  145. // something went wrong with the clean
  146. return false;
  147. }
  148. if (TransientNetworkFailure == true)
  149. _error->Warning(_("Some index files failed to download, they have been ignored, or old ones used instead."));
  150. else if (Failed == true)
  151. return _error->Error(_("Some index files failed to download, they have been ignored, or old ones used instead."));
  152. // Run the scripts if all was fine
  153. RunScripts("APT::Update::Post-Invoke");
  154. return true;
  155. }
  156. /*}}}*/
  157. // CacheFile::Close - close the cache files /*{{{*/
  158. // ---------------------------------------------------------------------
  159. /* */
  160. void pkgCacheFile::Close()
  161. {
  162. delete DCache;
  163. delete Policy;
  164. delete Cache;
  165. delete Map;
  166. _system->UnLock(true);
  167. Map = 0;
  168. DCache = 0;
  169. Policy = 0;
  170. Cache = 0;
  171. }
  172. /*}}}*/