acqprogress.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acqprogress.cc,v 1.19 1999/11/16 03:16:34 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' << flush;
  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[1024];
  130. char *End = Buffer + sizeof(Buffer);
  131. char *S = Buffer;
  132. if (ScreenWidth >= sizeof(Buffer))
  133. ScreenWidth = sizeof(Buffer)-1;
  134. // Put in the percent done
  135. sprintf(S,"%ld%%",long(double((CurrentBytes + CurrentItems)*100.0)/double(TotalBytes+TotalItems)));
  136. bool Shown = false;
  137. for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
  138. I = Owner->WorkerStep(I))
  139. {
  140. S += strlen(S);
  141. // There is no item running
  142. if (I->CurrentItem == 0)
  143. {
  144. if (I->Status.empty() == false)
  145. {
  146. snprintf(S,End-S," [%s]",I->Status.c_str());
  147. Shown = true;
  148. }
  149. continue;
  150. }
  151. Shown = true;
  152. // Add in the short description
  153. if (I->CurrentItem->Owner->ID != 0)
  154. snprintf(S,End-S," [%lu %s",I->CurrentItem->Owner->ID,
  155. I->CurrentItem->ShortDesc.c_str());
  156. else
  157. snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
  158. S += strlen(S);
  159. // Show the short mode string
  160. if (I->CurrentItem->Owner->Mode != 0)
  161. {
  162. snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
  163. S += strlen(S);
  164. }
  165. // Add the current progress
  166. if (Mode == Long)
  167. snprintf(S,End-S," %lu",I->CurrentSize);
  168. else
  169. {
  170. if (Mode == Medium || I->TotalSize == 0)
  171. snprintf(S,End-S," %sB",SizeToStr(I->CurrentSize).c_str());
  172. }
  173. S += strlen(S);
  174. // Add the total size and percent
  175. if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
  176. {
  177. if (Mode == Short)
  178. snprintf(S,End-S," %lu%%",
  179. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  180. else
  181. snprintf(S,End-S,"/%sB %lu%%",SizeToStr(I->TotalSize).c_str(),
  182. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  183. }
  184. S += strlen(S);
  185. snprintf(S,End-S,"]");
  186. }
  187. // Show something..
  188. if (Shown == false)
  189. snprintf(S,End-S," [Working]");
  190. /* Put in the ETA and cps meter, block off signals to prevent strangeness
  191. during resizing */
  192. sigset_t Sigs,OldSigs;
  193. sigemptyset(&Sigs);
  194. sigaddset(&Sigs,SIGWINCH);
  195. sigprocmask(SIG_BLOCK,&Sigs,&OldSigs);
  196. if (CurrentCPS != 0)
  197. {
  198. char Tmp[300];
  199. unsigned long ETA = (unsigned long)((TotalBytes - CurrentBytes)/CurrentCPS);
  200. sprintf(Tmp," %sB/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
  201. unsigned int Len = strlen(Buffer);
  202. unsigned int LenT = strlen(Tmp);
  203. if (Len + LenT < ScreenWidth)
  204. {
  205. memset(Buffer + Len,' ',ScreenWidth - Len);
  206. strcpy(Buffer + ScreenWidth - LenT,Tmp);
  207. }
  208. }
  209. Buffer[ScreenWidth] = 0;
  210. BlankLine[ScreenWidth] = 0;
  211. sigprocmask(SIG_UNBLOCK,&OldSigs,0);
  212. // Draw the current status
  213. if (strlen(Buffer) == strlen(BlankLine))
  214. cout << '\r' << Buffer << flush;
  215. else
  216. cout << '\r' << BlankLine << '\r' << Buffer << flush;
  217. memset(BlankLine,' ',strlen(Buffer));
  218. BlankLine[strlen(Buffer)] = 0;
  219. Update = false;
  220. return true;
  221. }
  222. /*}}}*/
  223. // AcqTextStatus::MediaChange - Media need to be swapped /*{{{*/
  224. // ---------------------------------------------------------------------
  225. /* Prompt for a media swap */
  226. bool AcqTextStatus::MediaChange(string Media,string Drive)
  227. {
  228. if (Quiet <= 0)
  229. cout << '\r' << BlankLine << '\r';
  230. cout << "Media Change: Please insert the disc labeled '" << Media << "' in "\
  231. "the drive '" << Drive << "' and press enter" << endl;
  232. char C = 0;
  233. while (C != '\n' && C != '\r')
  234. read(STDIN_FILENO,&C,1);
  235. Update = true;
  236. return true;
  237. }
  238. /*}}}*/