acqprogress.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Acquire Progress - Command line progress meter
  5. ##################################################################### */
  6. /*}}}*/
  7. // Include files /*{{{*/
  8. #include<config.h>
  9. #include <apt-pkg/acquire.h>
  10. #include <apt-pkg/acquire-item.h>
  11. #include <apt-pkg/acquire-worker.h>
  12. #include <apt-pkg/configuration.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <apt-pkg/error.h>
  15. #include <apt-private/acqprogress.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <signal.h>
  19. #include <iostream>
  20. #include <sstream>
  21. #include <unistd.h>
  22. #include <apti18n.h>
  23. /*}}}*/
  24. // AcqTextStatus::AcqTextStatus - Constructor /*{{{*/
  25. // ---------------------------------------------------------------------
  26. /* */
  27. AcqTextStatus::AcqTextStatus(std::ostream &out, unsigned int &ScreenWidth,unsigned int const Quiet) :
  28. pkgAcquireStatus(), out(out), ScreenWidth(ScreenWidth), LastLineLength(0), ID(0), Quiet(Quiet)
  29. {
  30. // testcases use it to disable pulses without disabling other user messages
  31. if (Quiet == 0 && _config->FindB("quiet::NoUpdate", false) == true)
  32. this->Quiet = 1;
  33. if (Quiet < 2 && _config->FindB("quiet::NoProgress", false) == true)
  34. this->Quiet = 2;
  35. }
  36. /*}}}*/
  37. // AcqTextStatus::Start - Downloading has started /*{{{*/
  38. // ---------------------------------------------------------------------
  39. /* */
  40. void AcqTextStatus::Start()
  41. {
  42. pkgAcquireStatus::Start();
  43. LastLineLength = 0;
  44. ID = 1;
  45. }
  46. /*}}}*/
  47. void AcqTextStatus::AssignItemID(pkgAcquire::ItemDesc &Itm) /*{{{*/
  48. {
  49. /* In theory calling it from Fetch() would be enough, but to be
  50. safe we call it from IMSHit and Fail as well.
  51. Also, an Item can pass through multiple stages, so ensure
  52. that it keeps the same number */
  53. if (Itm.Owner->ID == 0)
  54. Itm.Owner->ID = ID++;
  55. }
  56. /*}}}*/
  57. // AcqTextStatus::IMSHit - Called when an item got a HIT response /*{{{*/
  58. // ---------------------------------------------------------------------
  59. /* */
  60. void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc &Itm)
  61. {
  62. if (Quiet > 1)
  63. return;
  64. AssignItemID(Itm);
  65. clearLastLine();
  66. // TRANSLATOR: Very short word to be displayed before unchanged files in 'apt-get update'
  67. ioprintf(out, _("Hit:%lu %s"), Itm.Owner->ID, Itm.Description.c_str());
  68. out << std::endl;
  69. Update = true;
  70. }
  71. /*}}}*/
  72. // AcqTextStatus::Fetch - An item has started to download /*{{{*/
  73. // ---------------------------------------------------------------------
  74. /* This prints out the short description and the expected size */
  75. void AcqTextStatus::Fetch(pkgAcquire::ItemDesc &Itm)
  76. {
  77. Update = true;
  78. if (Itm.Owner->Complete == true)
  79. return;
  80. AssignItemID(Itm);
  81. if (Quiet > 1)
  82. return;
  83. clearLastLine();
  84. // TRANSLATOR: Very short word to be displayed for files processed in 'apt-get update'
  85. // Potentially replaced later by "Hit:", "Ign:" or "Err:" if something (bad) happens
  86. ioprintf(out, _("Get:%lu %s"), Itm.Owner->ID, Itm.Description.c_str());
  87. if (Itm.Owner->FileSize != 0)
  88. out << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
  89. out << std::endl;
  90. }
  91. /*}}}*/
  92. // AcqTextStatus::Done - Completed a download /*{{{*/
  93. // ---------------------------------------------------------------------
  94. /* We don't display anything... */
  95. void AcqTextStatus::Done(pkgAcquire::ItemDesc &Itm)
  96. {
  97. Update = true;
  98. AssignItemID(Itm);
  99. }
  100. /*}}}*/
  101. // AcqTextStatus::Fail - Called when an item fails to download /*{{{*/
  102. // ---------------------------------------------------------------------
  103. /* We print out the error text */
  104. void AcqTextStatus::Fail(pkgAcquire::ItemDesc &Itm)
  105. {
  106. if (Quiet > 1)
  107. return;
  108. AssignItemID(Itm);
  109. clearLastLine();
  110. if (Itm.Owner->Status == pkgAcquire::Item::StatDone || Itm.Owner->Status == pkgAcquire::Item::StatIdle)
  111. {
  112. // TRANSLATOR: Very short word to be displayed for files in 'apt-get update'
  113. // which failed to download, but the error is ignored (compare "Err:")
  114. ioprintf(out, _("Ign:%lu %s"), Itm.Owner->ID, Itm.Description.c_str());
  115. if (Itm.Owner->ErrorText.empty() == false &&
  116. _config->FindB("Acquire::Progress::Ignore::ShowErrorText", false) == true)
  117. out << std::endl << " " << Itm.Owner->ErrorText;
  118. out << std::endl;
  119. }
  120. else
  121. {
  122. // TRANSLATOR: Very short word to be displayed for files in 'apt-get update'
  123. // which failed to download and the error is critical (compare "Ign:")
  124. ioprintf(out, _("Err:%lu %s"), Itm.Owner->ID, Itm.Description.c_str());
  125. out << std::endl << " " << Itm.Owner->ErrorText << std::endl;
  126. }
  127. Update = true;
  128. }
  129. /*}}}*/
  130. // AcqTextStatus::Stop - Finished downloading /*{{{*/
  131. // ---------------------------------------------------------------------
  132. /* This prints out the bytes downloaded and the overall average line
  133. speed */
  134. void AcqTextStatus::Stop()
  135. {
  136. pkgAcquireStatus::Stop();
  137. if (Quiet > 1)
  138. return;
  139. clearLastLine();
  140. if (_config->FindB("quiet::NoStatistic", false) == true)
  141. return;
  142. if (FetchedBytes != 0 && _error->PendingError() == false)
  143. ioprintf(out,_("Fetched %sB in %s (%sB/s)\n"),
  144. SizeToStr(FetchedBytes).c_str(),
  145. TimeToStr(ElapsedTime).c_str(),
  146. SizeToStr(CurrentCPS).c_str());
  147. }
  148. /*}}}*/
  149. // AcqTextStatus::Pulse - Regular event pulse /*{{{*/
  150. // ---------------------------------------------------------------------
  151. /* This draws the current progress. Each line has an overall percent
  152. meter and a per active item status meter along with an overall
  153. bandwidth and ETA indicator. */
  154. bool AcqTextStatus::Pulse(pkgAcquire *Owner)
  155. {
  156. pkgAcquireStatus::Pulse(Owner);
  157. if (Quiet > 0)
  158. return true;
  159. std::string Line;
  160. {
  161. std::stringstream S;
  162. for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
  163. I = Owner->WorkerStep(I))
  164. {
  165. // There is no item running
  166. if (I->CurrentItem == 0)
  167. {
  168. if (I->Status.empty() == false)
  169. S << " [" << I->Status << "]";
  170. continue;
  171. }
  172. // Add in the short description
  173. S << " [";
  174. if (I->CurrentItem->Owner->ID != 0)
  175. S << I->CurrentItem->Owner->ID << " ";
  176. S << I->CurrentItem->ShortDesc;
  177. // Show the short mode string
  178. if (I->CurrentItem->Owner->ActiveSubprocess.empty() == false)
  179. S << " " << I->CurrentItem->Owner->ActiveSubprocess;
  180. enum {Long = 0,Medium,Short} Mode = Medium;
  181. // Add the current progress
  182. if (Mode == Long)
  183. S << " " << I->CurrentSize;
  184. else
  185. {
  186. if (Mode == Medium || I->TotalSize == 0)
  187. S << " " << SizeToStr(I->CurrentSize) << "B";
  188. }
  189. // Add the total size and percent
  190. if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
  191. {
  192. if (Mode == Short)
  193. ioprintf(S, " %.0f%%", (I->CurrentSize*100.0)/I->TotalSize);
  194. else
  195. ioprintf(S, "/%sB %.0f%%", SizeToStr(I->TotalSize).c_str(),
  196. (I->CurrentSize*100.0)/I->TotalSize);
  197. }
  198. S << "]";
  199. }
  200. // Show at least something
  201. Line = S.str();
  202. S.clear();
  203. if (Line.empty() == true)
  204. Line = _(" [Working]");
  205. }
  206. // Put in the percent done
  207. {
  208. std::stringstream S;
  209. ioprintf(S, "%.0f%%", Percent);
  210. S << Line;
  211. Line = S.str();
  212. S.clear();
  213. }
  214. /* Put in the ETA and cps meter, block off signals to prevent strangeness
  215. during resizing */
  216. sigset_t Sigs,OldSigs;
  217. sigemptyset(&Sigs);
  218. sigaddset(&Sigs,SIGWINCH);
  219. sigprocmask(SIG_BLOCK,&Sigs,&OldSigs);
  220. if (CurrentCPS != 0)
  221. {
  222. unsigned long long ETA = (TotalBytes - CurrentBytes)/CurrentCPS;
  223. std::string Tmp = " " + SizeToStr(CurrentCPS) + "B/s " + TimeToStr(ETA);
  224. size_t alignment = Line.length() + Tmp.length();
  225. if (alignment < ScreenWidth)
  226. {
  227. alignment = ScreenWidth - alignment;
  228. for (size_t i = 0; i < alignment; ++i)
  229. Line.append(" ");
  230. Line.append(Tmp);
  231. }
  232. }
  233. if (Line.length() > ScreenWidth)
  234. Line.erase(ScreenWidth);
  235. sigprocmask(SIG_SETMASK,&OldSigs,0);
  236. // Draw the current status
  237. if (_config->FindB("Apt::Color", false) == true)
  238. out << _config->Find("APT::Color::Yellow");
  239. if (LastLineLength > Line.length())
  240. clearLastLine();
  241. else
  242. out << '\r';
  243. out << Line << std::flush;
  244. if (_config->FindB("Apt::Color", false) == true)
  245. out << _config->Find("APT::Color::Neutral") << std::flush;
  246. LastLineLength = Line.length();
  247. Update = false;
  248. return true;
  249. }
  250. /*}}}*/
  251. // AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
  252. // ---------------------------------------------------------------------
  253. /* Prompt for a media swap */
  254. bool AcqTextStatus::MediaChange(std::string Media, std::string Drive)
  255. {
  256. // If we do not output on a terminal and one of the options to avoid user
  257. // interaction is given, we assume that no user is present who could react
  258. // on your media change request
  259. if (isatty(STDOUT_FILENO) != 1 && Quiet >= 2 &&
  260. (_config->FindB("APT::Get::Assume-Yes",false) == true ||
  261. _config->FindB("APT::Get::Force-Yes",false) == true ||
  262. _config->FindB("APT::Get::Trivial-Only",false) == true))
  263. return false;
  264. clearLastLine();
  265. ioprintf(out,_("Media change: please insert the disc labeled\n"
  266. " '%s'\n"
  267. "in the drive '%s' and press [Enter]\n"),
  268. Media.c_str(),Drive.c_str());
  269. char C = 0;
  270. bool bStatus = true;
  271. while (C != '\n' && C != '\r')
  272. {
  273. int len = read(STDIN_FILENO,&C,1);
  274. if(C == 'c' || len <= 0)
  275. bStatus = false;
  276. }
  277. if(bStatus)
  278. Update = true;
  279. return bStatus;
  280. }
  281. /*}}}*/
  282. void AcqTextStatus::clearLastLine() { /*{{{*/
  283. if (Quiet > 0 || LastLineLength == 0)
  284. return;
  285. // do not try to clear more than the (now smaller) screen
  286. if (LastLineLength > ScreenWidth)
  287. LastLineLength = ScreenWidth;
  288. out << '\r';
  289. for (size_t i = 0; i < LastLineLength; ++i)
  290. out << ' ';
  291. out << '\r' << std::flush;
  292. }
  293. /*}}}*/