update.cc 3.5 KB

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