update.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
  55. I != Fetcher.ItemsEnd(); ++I)
  56. {
  57. if ((*I)->Status == pkgAcquire::Item::StatDone)
  58. continue;
  59. (*I)->Finished();
  60. ::URI uri((*I)->DescURI());
  61. uri.User.clear();
  62. uri.Password.clear();
  63. string descUri = string(uri);
  64. _error->Warning(_("Failed to fetch %s %s"), descUri.c_str(),
  65. (*I)->ErrorText.c_str());
  66. if ((*I)->Status == pkgAcquire::Item::StatTransientNetworkError)
  67. {
  68. TransientNetworkFailure = true;
  69. continue;
  70. }
  71. Failed = true;
  72. }
  73. // Clean out any old list files
  74. // Keep "APT::Get::List-Cleanup" name for compatibility, but
  75. // this is really a global option for the APT library now
  76. if (!TransientNetworkFailure && !Failed && ListCleanup == true &&
  77. (_config->FindB("APT::Get::List-Cleanup",true) == true &&
  78. _config->FindB("APT::List-Cleanup",true) == true))
  79. {
  80. if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
  81. Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
  82. // something went wrong with the clean
  83. return false;
  84. }
  85. if (TransientNetworkFailure == true)
  86. _error->Warning(_("Some index files failed to download. They have been ignored, or old ones used instead."));
  87. else if (Failed == true)
  88. return _error->Error(_("Some index files failed to download. They have been ignored, or old ones used instead."));
  89. // Run the success scripts if all was fine
  90. if (RunUpdateScripts == true)
  91. {
  92. if(!TransientNetworkFailure && !Failed)
  93. RunScripts("APT::Update::Post-Invoke-Success");
  94. // Run the other scripts
  95. RunScripts("APT::Update::Post-Invoke");
  96. }
  97. return true;
  98. }
  99. /*}}}*/