acqprogress.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. bool ShowErrorText = true;
  111. if (Itm.Owner->Status == pkgAcquire::Item::StatDone || Itm.Owner->Status == pkgAcquire::Item::StatIdle)
  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() ||
  117. _config->FindB("Acquire::Progress::Ignore::ShowErrorText", false) == false)
  118. ShowErrorText = false;
  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. }
  126. if (ShowErrorText)
  127. {
  128. std::string::size_type line_start = 0;
  129. std::string::size_type line_end;
  130. while ((line_end = Itm.Owner->ErrorText.find_first_of("\n\r", line_start)) != std::string::npos) {
  131. out << std::endl << " " << Itm.Owner->ErrorText.substr(line_start, line_end - line_start);
  132. line_start = Itm.Owner->ErrorText.find_first_not_of("\n\r", line_end + 1);
  133. if (line_start == std::string::npos)
  134. break;
  135. }
  136. if (line_start == 0)
  137. out << std::endl << " " << Itm.Owner->ErrorText;
  138. else if (line_start != std::string::npos)
  139. out << std::endl << " " << Itm.Owner->ErrorText.substr(line_start);
  140. }
  141. out << std::endl;
  142. Update = true;
  143. }
  144. /*}}}*/
  145. // AcqTextStatus::Stop - Finished downloading /*{{{*/
  146. // ---------------------------------------------------------------------
  147. /* This prints out the bytes downloaded and the overall average line
  148. speed */
  149. void AcqTextStatus::Stop()
  150. {
  151. pkgAcquireStatus::Stop();
  152. if (Quiet > 1)
  153. return;
  154. clearLastLine();
  155. if (_config->FindB("quiet::NoStatistic", false) == true)
  156. return;
  157. if (FetchedBytes != 0 && _error->PendingError() == false)
  158. ioprintf(out,_("Fetched %sB in %s (%sB/s)\n"),
  159. SizeToStr(FetchedBytes).c_str(),
  160. TimeToStr(ElapsedTime).c_str(),
  161. SizeToStr(CurrentCPS).c_str());
  162. }
  163. /*}}}*/
  164. // AcqTextStatus::Pulse - Regular event pulse /*{{{*/
  165. // ---------------------------------------------------------------------
  166. /* This draws the current progress. Each line has an overall percent
  167. meter and a per active item status meter along with an overall
  168. bandwidth and ETA indicator. */
  169. bool AcqTextStatus::Pulse(pkgAcquire *Owner)
  170. {
  171. pkgAcquireStatus::Pulse(Owner);
  172. if (Quiet > 0)
  173. return true;
  174. std::string Line;
  175. {
  176. std::stringstream S;
  177. for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
  178. I = Owner->WorkerStep(I))
  179. {
  180. // There is no item running
  181. if (I->CurrentItem == 0)
  182. {
  183. if (I->Status.empty() == false)
  184. S << " [" << I->Status << "]";
  185. continue;
  186. }
  187. // Add in the short description
  188. S << " [";
  189. if (I->CurrentItem->Owner->ID != 0)
  190. S << I->CurrentItem->Owner->ID << " ";
  191. S << I->CurrentItem->ShortDesc;
  192. // Show the short mode string
  193. if (I->CurrentItem->Owner->ActiveSubprocess.empty() == false)
  194. S << " " << I->CurrentItem->Owner->ActiveSubprocess;
  195. enum {Long = 0,Medium,Short} Mode = Medium;
  196. // Add the current progress
  197. if (Mode == Long)
  198. S << " " << I->CurrentSize;
  199. else
  200. {
  201. if (Mode == Medium || I->TotalSize == 0)
  202. S << " " << SizeToStr(I->CurrentSize) << "B";
  203. }
  204. // Add the total size and percent
  205. if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
  206. {
  207. if (Mode == Short)
  208. ioprintf(S, " %.0f%%", (I->CurrentSize*100.0)/I->TotalSize);
  209. else
  210. ioprintf(S, "/%sB %.0f%%", SizeToStr(I->TotalSize).c_str(),
  211. (I->CurrentSize*100.0)/I->TotalSize);
  212. }
  213. S << "]";
  214. }
  215. // Show at least something
  216. Line = S.str();
  217. S.clear();
  218. if (Line.empty() == true)
  219. Line = _(" [Working]");
  220. }
  221. // Put in the percent done
  222. {
  223. std::stringstream S;
  224. ioprintf(S, "%.0f%%", Percent);
  225. S << Line;
  226. Line = S.str();
  227. S.clear();
  228. }
  229. /* Put in the ETA and cps meter, block off signals to prevent strangeness
  230. during resizing */
  231. sigset_t Sigs,OldSigs;
  232. sigemptyset(&Sigs);
  233. sigaddset(&Sigs,SIGWINCH);
  234. sigprocmask(SIG_BLOCK,&Sigs,&OldSigs);
  235. if (CurrentCPS != 0)
  236. {
  237. unsigned long long ETA = (TotalBytes - CurrentBytes)/CurrentCPS;
  238. std::string Tmp = " " + SizeToStr(CurrentCPS) + "B/s " + TimeToStr(ETA);
  239. size_t alignment = Line.length() + Tmp.length();
  240. if (alignment < ScreenWidth)
  241. {
  242. alignment = ScreenWidth - alignment;
  243. for (size_t i = 0; i < alignment; ++i)
  244. Line.append(" ");
  245. Line.append(Tmp);
  246. }
  247. }
  248. if (Line.length() > ScreenWidth)
  249. Line.erase(ScreenWidth);
  250. sigprocmask(SIG_SETMASK,&OldSigs,0);
  251. // Draw the current status
  252. if (_config->FindB("Apt::Color", false) == true)
  253. out << _config->Find("APT::Color::Yellow");
  254. if (LastLineLength > Line.length())
  255. clearLastLine();
  256. else
  257. out << '\r';
  258. out << Line << std::flush;
  259. if (_config->FindB("Apt::Color", false) == true)
  260. out << _config->Find("APT::Color::Neutral") << std::flush;
  261. LastLineLength = Line.length();
  262. Update = false;
  263. return true;
  264. }
  265. /*}}}*/
  266. // AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
  267. // ---------------------------------------------------------------------
  268. /* Prompt for a media swap */
  269. bool AcqTextStatus::MediaChange(std::string Media, std::string Drive)
  270. {
  271. // If we do not output on a terminal and one of the options to avoid user
  272. // interaction is given, we assume that no user is present who could react
  273. // on your media change request
  274. if (isatty(STDOUT_FILENO) != 1 && Quiet >= 2 &&
  275. (_config->FindB("APT::Get::Assume-Yes",false) == true ||
  276. _config->FindB("APT::Get::Force-Yes",false) == true ||
  277. _config->FindB("APT::Get::Trivial-Only",false) == true))
  278. return false;
  279. clearLastLine();
  280. ioprintf(out,_("Media change: please insert the disc labeled\n"
  281. " '%s'\n"
  282. "in the drive '%s' and press [Enter]\n"),
  283. Media.c_str(),Drive.c_str());
  284. char C = 0;
  285. bool bStatus = true;
  286. while (C != '\n' && C != '\r')
  287. {
  288. int len = read(STDIN_FILENO,&C,1);
  289. if(C == 'c' || len <= 0)
  290. bStatus = false;
  291. }
  292. if(bStatus)
  293. Update = true;
  294. return bStatus;
  295. }
  296. /*}}}*/
  297. void AcqTextStatus::clearLastLine() { /*{{{*/
  298. if (Quiet > 0 || LastLineLength == 0)
  299. return;
  300. // do not try to clear more than the (now smaller) screen
  301. if (LastLineLength > ScreenWidth)
  302. LastLineLength = ScreenWidth;
  303. out << '\r';
  304. for (size_t i = 0; i < LastLineLength; ++i)
  305. out << ' ';
  306. out << '\r' << std::flush;
  307. }
  308. /*}}}*/