acqprogress.cc 8.8 KB

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