scratch.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include <apt-pkg/acquire-item.h>
  2. #include <apt-pkg/acquire-worker.h>
  3. #include <apt-pkg/init.h>
  4. #include <apt-pkg/error.h>
  5. #include <strutl.h>
  6. #include <signal.h>
  7. #include <stdio.h>
  8. class AcqTextStatus : public pkgAcquireStatus
  9. {
  10. unsigned int ScreenWidth;
  11. char BlankLine[300];
  12. unsigned long ID;
  13. public:
  14. virtual void IMSHit(pkgAcquire::ItemDesc &Itm);
  15. virtual void Fetch(pkgAcquire::ItemDesc &Itm);
  16. virtual void Done(pkgAcquire::ItemDesc &Itm);
  17. virtual void Fail(pkgAcquire::ItemDesc &Itm);
  18. virtual void Start() {pkgAcquireStatus::Start(); BlankLine[0] = 0; ID = 1;};
  19. virtual void Stop();
  20. void Pulse(pkgAcquire *Owner);
  21. };
  22. // AcqTextStatus::IMSHit - Called when an item got a HIT response /*{{{*/
  23. // ---------------------------------------------------------------------
  24. /* */
  25. void AcqTextStatus::IMSHit(pkgAcquire::ItemDesc &Itm)
  26. {
  27. cout << '\r' << BlankLine << '\r';
  28. cout << "Hit " << Itm.Description;
  29. if (Itm.Owner->FileSize != 0)
  30. cout << " [" << SizeToStr(Itm.Owner->FileSize) << ']';
  31. cout << endl;
  32. Update = true;
  33. };
  34. /*}}}*/
  35. // AcqTextStatus::Fetch - An item has started to download /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* This prints out the short description and the expected size */
  38. void AcqTextStatus::Fetch(pkgAcquire::ItemDesc &Itm)
  39. {
  40. Update = true;
  41. if (Itm.Owner->Complete == true)
  42. return;
  43. Itm.Owner->ID = ID++;
  44. cout << '\r' << BlankLine << '\r';
  45. cout << hex << Itm.Owner->ID << dec << " Get " << Itm.Description;
  46. if (Itm.Owner->FileSize != 0)
  47. cout << " [" << SizeToStr(Itm.Owner->FileSize) << ']';
  48. cout << endl;
  49. };
  50. /*}}}*/
  51. // AcqTextStatus::Done - Completed a download /*{{{*/
  52. // ---------------------------------------------------------------------
  53. /* We don't display anything... */
  54. void AcqTextStatus::Done(pkgAcquire::ItemDesc &Itm)
  55. {
  56. Update = true;
  57. };
  58. /*}}}*/
  59. // AcqTextStatus::Fail - Called when an item fails to download /*{{{*/
  60. // ---------------------------------------------------------------------
  61. /* We print out the error text */
  62. void AcqTextStatus::Fail(pkgAcquire::ItemDesc &Itm)
  63. {
  64. cout << '\r' << BlankLine << '\r';
  65. cout << "Err " << Itm.Description << endl;
  66. cout << " " << Itm.Owner->ErrorText << endl;
  67. Update = true;
  68. };
  69. /*}}}*/
  70. // AcqTextStatus::Stop - Finished downloading /*{{{*/
  71. // ---------------------------------------------------------------------
  72. /* This prints out the bytes downloaded and the overall average line
  73. speed */
  74. void AcqTextStatus::Stop()
  75. {
  76. pkgAcquireStatus::Stop();
  77. cout << '\r' << BlankLine << '\r';
  78. if (FetchedBytes == 0)
  79. cout << flush;
  80. else
  81. cout << "Fetched " << SizeToStr(FetchedBytes) << " in " <<
  82. TimeToStr(ElapsedTime) << " (" << SizeToStr(CurrentCPS) <<
  83. "/s)" << endl;
  84. }
  85. /*}}}*/
  86. // AcqTextStatus::Pulse - Regular event pulse /*{{{*/
  87. // ---------------------------------------------------------------------
  88. /* This draws the current progress. Each line has an overall percent
  89. meter and a per active item status meter along with an overall
  90. bandwidth and ETA indicator. */
  91. void AcqTextStatus::Pulse(pkgAcquire *Owner)
  92. {
  93. pkgAcquireStatus::Pulse(Owner);
  94. enum {Long = 0,Medium,Short} Mode = Long;
  95. ScreenWidth = 78;
  96. char Buffer[300];
  97. char *End = Buffer + sizeof(Buffer);
  98. char *S = Buffer;
  99. // Put in the percent done
  100. sprintf(S,"%ld%%",long(double(CurrentBytes*100.0)/double(TotalBytes)));
  101. for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
  102. I = Owner->WorkerStep(I))
  103. {
  104. S += strlen(S);
  105. // There is no item running
  106. if (I->CurrentItem == 0)
  107. {
  108. if (I->Status.empty() == false)
  109. snprintf(S,End-S," [%s]",I->Status.c_str());
  110. continue;
  111. }
  112. // Add in the short description
  113. if (I->CurrentItem->Owner->ID != 0)
  114. snprintf(S,End-S," [%x %s",I->CurrentItem->Owner->ID,
  115. I->CurrentItem->ShortDesc.c_str());
  116. else
  117. snprintf(S,End-S," [%s",I->CurrentItem->ShortDesc.c_str());
  118. S += strlen(S);
  119. // Show the short mode string
  120. if (I->CurrentItem->Owner->Mode != 0)
  121. {
  122. snprintf(S,End-S," %s",I->CurrentItem->Owner->Mode);
  123. S += strlen(S);
  124. }
  125. // Add the current progress
  126. if (Mode == Long)
  127. snprintf(S,End-S," %u",I->CurrentSize);
  128. else
  129. {
  130. if (Mode == Medium || I->TotalSize == 0)
  131. snprintf(S,End-S," %s",SizeToStr(I->CurrentSize).c_str());
  132. }
  133. S += strlen(S);
  134. // Add the total size and percent
  135. if (I->TotalSize > 0 && I->CurrentItem->Owner->Complete == false)
  136. {
  137. if (Mode == Long)
  138. snprintf(S,End-S,"/%u %u%%",I->TotalSize,
  139. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  140. else
  141. {
  142. if (Mode == Medium)
  143. snprintf(S,End-S,"/%s %u%%",SizeToStr(I->TotalSize).c_str(),
  144. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  145. else
  146. snprintf(S,End-S," %u%%",
  147. long(double(I->CurrentSize*100.0)/double(I->TotalSize)));
  148. }
  149. }
  150. S += strlen(S);
  151. snprintf(S,End-S,"]");
  152. }
  153. // Put in the ETA and cps meter
  154. if (CurrentCPS != 0)
  155. {
  156. char Tmp[300];
  157. unsigned long ETA = (unsigned long)((TotalBytes - CurrentBytes)/CurrentCPS);
  158. sprintf(Tmp," %s/s %s",SizeToStr(CurrentCPS).c_str(),TimeToStr(ETA).c_str());
  159. unsigned int Len = strlen(Buffer);
  160. unsigned int LenT = strlen(Tmp);
  161. if (Len + LenT < ScreenWidth)
  162. {
  163. memset(Buffer + Len,' ',ScreenWidth - Len);
  164. strcpy(Buffer + ScreenWidth - LenT,Tmp);
  165. }
  166. }
  167. Buffer[ScreenWidth] = 0;
  168. // Draw the current status
  169. if (strlen(Buffer) == strlen(BlankLine))
  170. cout << '\r' << Buffer << flush;
  171. else
  172. cout << '\r' << BlankLine << '\r' << Buffer << flush;
  173. memset(BlankLine,' ',strlen(Buffer));
  174. BlankLine[strlen(Buffer)] = 0;
  175. Update = false;
  176. }
  177. /*}}}*/
  178. int main(int argc,char *argv[])
  179. {
  180. signal(SIGPIPE,SIG_IGN);
  181. /* URI Foo(argv[1]);
  182. cout << Foo.Access << '\'' << endl;
  183. cout << Foo.Host << '\'' << endl;
  184. cout << Foo.Path << '\'' << endl;
  185. cout << Foo.User << '\'' << endl;
  186. cout << Foo.Password << '\'' << endl;
  187. cout << Foo.Port << endl;
  188. return 0;*/
  189. pkgInitialize(*_config);
  190. pkgSourceList List;
  191. AcqTextStatus Stat;
  192. pkgAcquire Fetcher(&Stat);
  193. List.ReadMainList();
  194. pkgSourceList::const_iterator I;
  195. for (I = List.begin(); I != List.end(); I++)
  196. {
  197. new pkgAcqIndex(&Fetcher,I);
  198. if (_error->PendingError() == true)
  199. break;
  200. }
  201. Fetcher.Run();
  202. _error->DumpErrors();
  203. }