acqprogress.cc 8.7 KB

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