acqprogress.cc 9.0 KB

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