acqprogress.cc 8.5 KB

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