acqprogress.cc 8.9 KB

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