acqprogress.cc 8.5 KB

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