update.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Include Files /*{{{*/
  2. #include <config.h>
  3. #include <apt-pkg/acquire-item.h>
  4. #include <apt-pkg/configuration.h>
  5. #include <apt-pkg/error.h>
  6. #include <apt-pkg/fileutl.h>
  7. #include <apt-pkg/sourcelist.h>
  8. #include <apt-pkg/acquire.h>
  9. #include <apt-pkg/strutl.h>
  10. #include <apt-pkg/update.h>
  11. #include <string>
  12. #include <apti18n.h>
  13. /*}}}*/
  14. using namespace std;
  15. // ListUpdate - construct Fetcher and update the cache files /*{{{*/
  16. // ---------------------------------------------------------------------
  17. /* This is a simple wrapper to update the cache. it will fetch stuff
  18. * from the network (or any other sources defined in sources.list)
  19. */
  20. bool ListUpdate(pkgAcquireStatus &Stat,
  21. pkgSourceList &List,
  22. int PulseInterval)
  23. {
  24. pkgAcquire Fetcher(&Stat);
  25. if (Fetcher.GetLock(_config->FindDir("Dir::State::Lists")) == false)
  26. return false;
  27. // Populate it with the source selection
  28. if (List.GetIndexes(&Fetcher) == false)
  29. return false;
  30. return AcquireUpdate(Fetcher, PulseInterval, true);
  31. }
  32. /*}}}*/
  33. // AcquireUpdate - take Fetcher and update the cache files /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* This is a simple wrapper to update the cache with a provided acquire
  36. * If you only need control over Status and the used SourcesList use
  37. * ListUpdate method instead.
  38. */
  39. bool AcquireUpdate(pkgAcquire &Fetcher, int const PulseInterval,
  40. bool const RunUpdateScripts, bool const ListCleanup)
  41. {
  42. // Run scripts
  43. if (RunUpdateScripts == true)
  44. RunScripts("APT::Update::Pre-Invoke");
  45. pkgAcquire::RunResult res;
  46. if(PulseInterval > 0)
  47. res = Fetcher.Run(PulseInterval);
  48. else
  49. res = Fetcher.Run();
  50. if (res == pkgAcquire::Failed)
  51. return false;
  52. bool Failed = false;
  53. bool TransientNetworkFailure = false;
  54. bool AllFailed = true;
  55. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
  56. I != Fetcher.ItemsEnd(); ++I)
  57. {
  58. if ((*I)->Status == pkgAcquire::Item::StatDone) {
  59. AllFailed = false;
  60. continue;
  61. }
  62. (*I)->Finished();
  63. ::URI uri((*I)->DescURI());
  64. uri.User.clear();
  65. uri.Password.clear();
  66. string descUri = string(uri);
  67. // Show an error for non-transient failures, otherwise only warn
  68. if ((*I)->Status == pkgAcquire::Item::StatTransientNetworkError)
  69. _error->Warning(_("Failed to fetch %s %s"), descUri.c_str(),
  70. (*I)->ErrorText.c_str());
  71. else
  72. _error->Error(_("Failed to fetch %s %s"), descUri.c_str(),
  73. (*I)->ErrorText.c_str());
  74. if ((*I)->Status == pkgAcquire::Item::StatTransientNetworkError)
  75. {
  76. TransientNetworkFailure = true;
  77. continue;
  78. }
  79. Failed = true;
  80. }
  81. // Clean out any old list files
  82. // Keep "APT::Get::List-Cleanup" name for compatibility, but
  83. // this is really a global option for the APT library now
  84. if (!TransientNetworkFailure && !Failed && ListCleanup == true &&
  85. (_config->FindB("APT::Get::List-Cleanup",true) == true &&
  86. _config->FindB("APT::List-Cleanup",true) == true))
  87. {
  88. if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
  89. Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
  90. // something went wrong with the clean
  91. return false;
  92. }
  93. bool Res = true;
  94. if (TransientNetworkFailure == true)
  95. Res = _error->Warning(_("Some index files failed to download. They have been ignored, or old ones used instead."));
  96. else if (Failed == true)
  97. Res = _error->Error(_("Some index files failed to download. They have been ignored, or old ones used instead."));
  98. // Run the success scripts if all was fine
  99. if (RunUpdateScripts == true)
  100. {
  101. if(AllFailed == false)
  102. RunScripts("APT::Update::Post-Invoke-Success");
  103. // Run the other scripts
  104. RunScripts("APT::Update::Post-Invoke");
  105. }
  106. return Res;
  107. }
  108. /*}}}*/