acqprogress.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. }
  34. /*}}}*/
  35. // AcqTextStatus::Start - Downloading has started /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* */
  38. void AcqTextStatus::Start()
  39. {
  40. pkgAcquireStatus::Start();
  41. LastLineLength = 0;
  42. ID = 1;
  43. }
  44. /*}}}*/
  45. // AcqTextStatus::IMSHit - Called when an item got a HIT response /*{{{*/
  46. // ---------------------------------------------------------------------
  47. /* */
  48. void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc &Itm)
  49. {
  50. if (Quiet > 1)
  51. return;
  52. clearLastLine();
  53. out << _("Hit ") << Itm.Description;
  54. out << std::endl;
  55. Update = true;
  56. }
  57. /*}}}*/
  58. // AcqTextStatus::Fetch - An item has started to download /*{{{*/
  59. // ---------------------------------------------------------------------
  60. /* This prints out the short description and the expected size */
  61. void AcqTextStatus::Fetch(pkgAcquire::ItemDesc &Itm)
  62. {
  63. Update = true;
  64. if (Itm.Owner->Complete == true)
  65. return;
  66. Itm.Owner->ID = ID++;
  67. if (Quiet > 1)
  68. return;
  69. clearLastLine();
  70. out << _("Get:") << Itm.Owner->ID << ' ' << Itm.Description;
  71. if (Itm.Owner->FileSize != 0)
  72. out << " [" << SizeToStr(Itm.Owner->FileSize) << "B]";
  73. out << std::endl;
  74. }
  75. /*}}}*/
  76. // AcqTextStatus::Done - Completed a download /*{{{*/
  77. // ---------------------------------------------------------------------
  78. /* We don't display anything... */
  79. void AcqTextStatus::Done(pkgAcquire::ItemDesc &/*Itm*/)
  80. {
  81. Update = true;
  82. }
  83. /*}}}*/
  84. // AcqTextStatus::Fail - Called when an item fails to download /*{{{*/
  85. // ---------------------------------------------------------------------
  86. /* We print out the error text */
  87. void AcqTextStatus::Fail(pkgAcquire::ItemDesc &Itm)
  88. {
  89. if (Quiet > 1)
  90. return;
  91. // Ignore certain kinds of transient failures (bad code)
  92. if (Itm.Owner->Status == pkgAcquire::Item::StatIdle)
  93. return;
  94. clearLastLine();
  95. if (Itm.Owner->Status == pkgAcquire::Item::StatDone)
  96. {
  97. out << _("Ign ") << Itm.Description << std::endl;
  98. if (Itm.Owner->ErrorText.empty() == false &&
  99. _config->FindB("Acquire::Progress::Ignore::ShowErrorText", false) == true)
  100. out << " " << Itm.Owner->ErrorText << std::endl;
  101. }
  102. else
  103. {
  104. out << _("Err ") << Itm.Description << std::endl;
  105. out << " " << Itm.Owner->ErrorText << std::endl;
  106. }
  107. Update = true;
  108. }
  109. /*}}}*/
  110. // AcqTextStatus::Stop - Finished downloading /*{{{*/
  111. // ---------------------------------------------------------------------
  112. /* This prints out the bytes downloaded and the overall average line
  113. speed */
  114. void AcqTextStatus::Stop()
  115. {
  116. pkgAcquireStatus::Stop();
  117. if (Quiet > 1)
  118. return;
  119. clearLastLine();
  120. if (_config->FindB("quiet::NoStatistic", false) == true)
  121. return;
  122. if (FetchedBytes != 0 && _error->PendingError() == false)
  123. ioprintf(out,_("Fetched %sB in %s (%sB/s)\n"),
  124. SizeToStr(FetchedBytes).c_str(),
  125. TimeToStr(ElapsedTime).c_str(),
  126. SizeToStr(CurrentCPS).c_str());
  127. }
  128. /*}}}*/
  129. // AcqTextStatus::Pulse - Regular event pulse /*{{{*/
  130. // ---------------------------------------------------------------------
  131. /* This draws the current progress. Each line has an overall percent
  132. meter and a per active item status meter along with an overall
  133. bandwidth and ETA indicator. */
  134. bool AcqTextStatus::Pulse(pkgAcquire *Owner)
  135. {
  136. pkgAcquireStatus::Pulse(Owner);
  137. if (Quiet > 0)
  138. return true;
  139. enum {Long = 0,Medium,Short} Mode = Medium;
  140. std::string Line;
  141. {
  142. std::stringstream S;
  143. for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
  144. I = Owner->WorkerStep(I))
  145. {
  146. // There is no item running
  147. if (I->CurrentItem == 0)
  148. {
  149. if (I->Status.empty() == false)
  150. S << " [" << I->Status << "]";
  151. continue;
  152. }
  153. // Add in the short description
  154. S << " [";
  155. if (I->CurrentItem->Owner->ID != 0)
  156. S << I->CurrentItem->Owner->ID << " ";
  157. S << I->CurrentItem->ShortDesc;
  158. // Show the short mode string
  159. if (I->CurrentItem->Owner->ActiveSubprocess.empty() == false)
  160. S << " " << I->CurrentItem->Owner->ActiveSubprocess;
  161. // Add the current progress
  162. if (Mode == Long)
  163. S << " " << I->CurrentSize;
  164. else
  165. {
  166. if (Mode == Medium || I->TotalSize == 0)
  167. S << " " << SizeToStr(I->CurrentSize) << "B";
  168. }
  169. // Add the total size and percent
  170. if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
  171. {
  172. if (Mode == Short)
  173. ioprintf(S, " %.0f%%", (I->CurrentSize*100.0)/I->TotalSize);
  174. else
  175. ioprintf(S, "/%sB %.0f%%", SizeToStr(I->TotalSize).c_str(),
  176. (I->CurrentSize*100.0)/I->TotalSize);
  177. }
  178. S << "]";
  179. }
  180. // Show at least something
  181. Line = S.str();
  182. S.clear();
  183. if (Line.empty() == true)
  184. Line = _(" [Working]");
  185. }
  186. // Put in the percent done
  187. {
  188. std::stringstream S;
  189. ioprintf(S, "%.0f%%", Percent);
  190. S << Line;
  191. Line = S.str();
  192. S.clear();
  193. }
  194. /* Put in the ETA and cps meter, block off signals to prevent strangeness
  195. during resizing */
  196. sigset_t Sigs,OldSigs;
  197. sigemptyset(&Sigs);
  198. sigaddset(&Sigs,SIGWINCH);
  199. sigprocmask(SIG_BLOCK,&Sigs,&OldSigs);
  200. if (CurrentCPS != 0)
  201. {
  202. unsigned long long ETA = (TotalBytes - CurrentBytes)/CurrentCPS;
  203. std::string Tmp = " " + SizeToStr(CurrentCPS) + "B/s " + TimeToStr(ETA);
  204. size_t alignment = Line.length() + Tmp.length();
  205. if (alignment < ScreenWidth)
  206. {
  207. alignment = ScreenWidth - alignment;
  208. for (size_t i = 0; i < alignment; ++i)
  209. Line.append(" ");
  210. Line.append(Tmp);
  211. }
  212. }
  213. if (Line.length() > ScreenWidth)
  214. Line.erase(ScreenWidth);
  215. sigprocmask(SIG_SETMASK,&OldSigs,0);
  216. // Draw the current status
  217. if (_config->FindB("Apt::Color", false) == true)
  218. out << _config->Find("APT::Color::Yellow");
  219. if (LastLineLength > Line.length())
  220. clearLastLine();
  221. else
  222. out << '\r';
  223. out << Line << std::flush;
  224. if (_config->FindB("Apt::Color", false) == true)
  225. out << _config->Find("APT::Color::Neutral") << std::flush;
  226. LastLineLength = Line.length();
  227. Update = false;
  228. return true;
  229. }
  230. /*}}}*/
  231. // AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
  232. // ---------------------------------------------------------------------
  233. /* Prompt for a media swap */
  234. bool AcqTextStatus::MediaChange(std::string Media, std::string Drive)
  235. {
  236. // If we do not output on a terminal and one of the options to avoid user
  237. // interaction is given, we assume that no user is present who could react
  238. // on your media change request
  239. if (isatty(STDOUT_FILENO) != 1 && Quiet >= 2 &&
  240. (_config->FindB("APT::Get::Assume-Yes",false) == true ||
  241. _config->FindB("APT::Get::Force-Yes",false) == true ||
  242. _config->FindB("APT::Get::Trivial-Only",false) == true))
  243. return false;
  244. clearLastLine();
  245. ioprintf(out,_("Media change: please insert the disc labeled\n"
  246. " '%s'\n"
  247. "in the drive '%s' and press enter\n"),
  248. Media.c_str(),Drive.c_str());
  249. char C = 0;
  250. bool bStatus = true;
  251. while (C != '\n' && C != '\r')
  252. {
  253. int len = read(STDIN_FILENO,&C,1);
  254. if(C == 'c' || len <= 0)
  255. bStatus = false;
  256. }
  257. if(bStatus)
  258. Update = true;
  259. return bStatus;
  260. }
  261. /*}}}*/
  262. void AcqTextStatus::clearLastLine() { /*{{{*/
  263. if (Quiet > 0 || LastLineLength == 0)
  264. return;
  265. // do not try to clear more than the (now smaller) screen
  266. if (LastLineLength > ScreenWidth)
  267. LastLineLength = ScreenWidth;
  268. out << '\r';
  269. for (size_t i = 0; i < LastLineLength; ++i)
  270. out << ' ';
  271. out << '\r' << std::flush;
  272. }
  273. /*}}}*/