acqprogress.cc 9.8 KB

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