You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

330 lines
9.7 KiB

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