acqprogress.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acqprogress.cc,v 1.17 1999/08/04 05:37:18 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. if (Itm.Owner->Status == pkgAcquire::Item::StatIdle)
  86. return;
  87. if (Quiet <= 0)
  88. cout << '\r' << BlankLine << '\r';
  89. if (Itm.Owner->Status == pkgAcquire::Item::StatDone)
  90. {
  91. cout << "Ign " << Itm.Description << endl;
  92. }
  93. else
  94. {
  95. cout << "Err " << Itm.Description << endl;
  96. cout << " " << Itm.Owner->ErrorText << endl;
  97. }
  98. Update = true;
  99. };
  100. /*}}}*/
  101. // AcqTextStatus::Stop - Finished downloading /*{{{*/
  102. // ---------------------------------------------------------------------
  103. /* This prints out the bytes downloaded and the overall average line
  104. speed */
  105. void AcqTextStatus::Stop()
  106. {
  107. pkgAcquireStatus::Stop();
  108. if (Quiet > 1)
  109. return;
  110. if (Quiet <= 0)
  111. cout << '\r' << BlankLine << '\r';
  112. if (FetchedBytes != 0 && _error->PendingError() == false)
  113. cout << "Fetched " << SizeToStr(FetchedBytes) << "B in " <<
  114. TimeToStr(ElapsedTime) << " (" << SizeToStr(CurrentCPS) <<
  115. "B/s)" << endl;
  116. }
  117. /*}}}*/
  118. // AcqTextStatus::Pulse - Regular event pulse /*{{{*/
  119. // ---------------------------------------------------------------------
  120. /* This draws the current progress. Each line has an overall percent
  121. meter and a per active item status meter along with an overall
  122. bandwidth and ETA indicator. */
  123. bool AcqTextStatus::Pulse(pkgAcquire *Owner)
  124. {
  125. if (Quiet > 0)
  126. return true;
  127. pkgAcquireStatus::Pulse(Owner);
  128. enum {Long = 0,Medium,Short} Mode = Long;
  129. char Buffer[300];
  130. char *End = Buffer + sizeof(Buffer);
  131. char *S = Buffer;
  132. // Put in the percent done
  133. sprintf(S,"%ld%%",long(double((CurrentBytes + CurrentItems)*100.0)/double(TotalBytes+TotalItems)));
  134. bool Shown = false;
  135. for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
  136. I = Owner->WorkerStep(I))
  137. {
  138. S += strlen(S);
  139. // There is no item running
  140. if (I->CurrentItem == 0)
  141. {
  142. if (I->Status.empty() == false)
  143. {
  144. snprintf(S,End-S," [%s]",I->Status.c_str());
  145. Shown = true;
  146. }
  147. continue;
  148. }
  149. Shown = true;
  150. // Add in the short description
  151. if (I->CurrentItem->Owner->ID != 0)
  152. snprintf(S,End-S," [%lu %s",I->CurrentItem->Owner->ID,
  153. I->CurrentItem->ShortDesc.c_str());
  154. else
  155. snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
  156. S += strlen(S);
  157. // Show the short mode string
  158. if (I->CurrentItem->Owner->Mode != 0)
  159. {
  160. snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
  161. S += strlen(S);
  162. }
  163. // Add the current progress
  164. if (Mode == Long)
  165. snprintf(S,End-S," %lu",I->CurrentSize);
  166. else
  167. {
  168. if (Mode == Medium || I->TotalSize == 0)
  169. snprintf(S,End-S," %sB",SizeToStr(I->CurrentSize).c_str());
  170. }
  171. S += strlen(S);
  172. // Add the total size and percent
  173. if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
  174. {
  175. if (Mode == Short)
  176. snprintf(S,End-S," %lu%%",
  177. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  178. else
  179. snprintf(S,End-S,"/%sB %lu%%",SizeToStr(I->TotalSize).c_str(),
  180. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  181. }
  182. S += strlen(S);
  183. snprintf(S,End-S,"]");
  184. }
  185. // Show something..
  186. if (Shown == false)
  187. snprintf(S,End-S," [Working]");
  188. /* Put in the ETA and cps meter, block off signals to prevent strangeness
  189. during resizing */
  190. sigset_t Sigs,OldSigs;
  191. sigemptyset(&Sigs);
  192. sigaddset(&Sigs,SIGWINCH);
  193. sigprocmask(SIG_BLOCK,&Sigs,&OldSigs);
  194. if (CurrentCPS != 0)
  195. {
  196. char Tmp[300];
  197. unsigned long ETA = (unsigned long)((TotalBytes - CurrentBytes)/CurrentCPS);
  198. sprintf(Tmp," %sB/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
  199. unsigned int Len = strlen(Buffer);
  200. unsigned int LenT = strlen(Tmp);
  201. if (Len + LenT < ScreenWidth)
  202. {
  203. memset(Buffer + Len,' ',ScreenWidth - Len);
  204. strcpy(Buffer + ScreenWidth - LenT,Tmp);
  205. }
  206. }
  207. Buffer[ScreenWidth] = 0;
  208. BlankLine[ScreenWidth] = 0;
  209. sigprocmask(SIG_UNBLOCK,&OldSigs,0);
  210. // Draw the current status
  211. if (strlen(Buffer) == strlen(BlankLine))
  212. cout << '\r' << Buffer << flush;
  213. else
  214. cout << '\r' << BlankLine << '\r' << Buffer << flush;
  215. memset(BlankLine,' ',strlen(Buffer));
  216. BlankLine[strlen(Buffer)] = 0;
  217. Update = false;
  218. return true;
  219. }
  220. /*}}}*/
  221. // AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
  222. // ---------------------------------------------------------------------
  223. /* Prompt for a media swap */
  224. bool AcqTextStatus::MediaChange(string Media,string Drive)
  225. {
  226. if (Quiet <= 0)
  227. cout << '\r' << BlankLine << '\r';
  228. cout << "Media Change: Please insert the disc labeled '" << Media << "' in "\
  229. "the drive '" << Drive << "' and press enter" << endl;
  230. char C = 0;
  231. while (C != '\n' && C != '\r')
  232. read(STDIN_FILENO,&C,1);
  233. Update = true;
  234. return true;
  235. }
  236. /*}}}*/