update.cc 3.5 KB

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