cachefile.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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, pkgSourceList &List)
  102. {
  103. pkgAcquire Fetcher(&Stat);
  104. // Populate it with the source selection
  105. if (List.GetIndexes(&Fetcher) == false)
  106. return false;
  107. // Run scripts
  108. RunScripts("APT::Update::Pre-Invoke");
  109. // Run it
  110. if (Fetcher.Run() == pkgAcquire::Failed)
  111. return false;
  112. bool Failed = false;
  113. bool TransientNetworkFailure = false;
  114. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
  115. I != Fetcher.ItemsEnd(); I++)
  116. {
  117. if ((*I)->Status == pkgAcquire::Item::StatDone)
  118. continue;
  119. (*I)->Finished();
  120. fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
  121. (*I)->ErrorText.c_str());
  122. if ((*I)->Status == pkgAcquire::Item::StatTransientNetworkError)
  123. {
  124. TransientNetworkFailure = true;
  125. continue;
  126. }
  127. Failed = true;
  128. }
  129. // Clean out any old list files
  130. // Keep "APT::Get::List-Cleanup" name for compatibility, but
  131. // this is really a global option for the APT library now
  132. if (!TransientNetworkFailure && !Failed &&
  133. (_config->FindB("APT::Get::List-Cleanup",true) == true ||
  134. _config->FindB("APT::List-Cleanup",true) == true))
  135. {
  136. if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
  137. Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
  138. // something went wrong with the clean
  139. return false;
  140. }
  141. if (TransientNetworkFailure == true)
  142. _error->Warning(_("Some index files failed to download, they have been ignored, or old ones used instead."));
  143. else if (Failed == true)
  144. return _error->Error(_("Some index files failed to download, they have been ignored, or old ones used instead."));
  145. // Run the scripts if all was fine
  146. RunScripts("APT::Update::Post-Invoke");
  147. return true;
  148. }
  149. /*}}}*/
  150. // CacheFile::Close - close the cache files /*{{{*/
  151. // ---------------------------------------------------------------------
  152. /* */
  153. void pkgCacheFile::Close()
  154. {
  155. delete DCache;
  156. delete Policy;
  157. delete Cache;
  158. delete Map;
  159. _system->UnLock(true);
  160. Map = 0;
  161. DCache = 0;
  162. Policy = 0;
  163. Cache = 0;
  164. }
  165. /*}}}*/