acqprogress.cc 7.8 KB

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