acqprogress.cc 8.8 KB

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