cachefile.cc 4.9 KB

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