update.cc 3.7 KB

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