update.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. bool const errorsWereReported = (res == pkgAcquire::Failed);
  51. bool Failed = errorsWereReported;
  52. bool TransientNetworkFailure = false;
  53. bool AllFailed = true;
  54. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
  55. I != Fetcher.ItemsEnd(); ++I)
  56. {
  57. switch ((*I)->Status)
  58. {
  59. case pkgAcquire::Item::StatDone:
  60. AllFailed = false;
  61. continue;
  62. case pkgAcquire::Item::StatTransientNetworkError:
  63. TransientNetworkFailure = true;
  64. break;
  65. case pkgAcquire::Item::StatIdle:
  66. case pkgAcquire::Item::StatFetching:
  67. case pkgAcquire::Item::StatError:
  68. case pkgAcquire::Item::StatAuthError:
  69. Failed = true;
  70. break;
  71. }
  72. (*I)->Finished();
  73. if (errorsWereReported)
  74. continue;
  75. ::URI uri((*I)->DescURI());
  76. uri.User.clear();
  77. uri.Password.clear();
  78. std::string const descUri = std::string(uri);
  79. // Show an error for non-transient failures, otherwise only warn
  80. if ((*I)->Status == pkgAcquire::Item::StatTransientNetworkError)
  81. _error->Warning(_("Failed to fetch %s %s"), descUri.c_str(),
  82. (*I)->ErrorText.c_str());
  83. else
  84. _error->Error(_("Failed to fetch %s %s"), descUri.c_str(),
  85. (*I)->ErrorText.c_str());
  86. }
  87. // Clean out any old list files
  88. // Keep "APT::Get::List-Cleanup" name for compatibility, but
  89. // this is really a global option for the APT library now
  90. if (!TransientNetworkFailure && !Failed && ListCleanup == true &&
  91. (_config->FindB("APT::Get::List-Cleanup",true) == true &&
  92. _config->FindB("APT::List-Cleanup",true) == true))
  93. {
  94. if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
  95. Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
  96. // something went wrong with the clean
  97. return false;
  98. }
  99. bool Res = true;
  100. if (errorsWereReported == true)
  101. Res = false;
  102. else if (TransientNetworkFailure == true)
  103. Res = _error->Warning(_("Some index files failed to download. They have been ignored, or old ones used instead."));
  104. else if (Failed == true)
  105. Res = _error->Error(_("Some index files failed to download. They have been ignored, or old ones used instead."));
  106. // Run the success scripts if all was fine
  107. if (RunUpdateScripts == true)
  108. {
  109. if(AllFailed == false)
  110. RunScripts("APT::Update::Post-Invoke-Success");
  111. // Run the other scripts
  112. RunScripts("APT::Update::Post-Invoke");
  113. }
  114. return Res;
  115. }
  116. /*}}}*/