acquire.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire.cc,v 1.26 1999/01/30 08:08:54 jgg Exp $
  4. /* ######################################################################
  5. Acquire - File Acquiration
  6. The core element for the schedual system is the concept of a named
  7. queue. Each queue is unique and each queue has a name derived from the
  8. URI. The degree of paralization can be controled by how the queue
  9. name is derived from the URI.
  10. ##################################################################### */
  11. /*}}}*/
  12. // Include Files /*{{{*/
  13. #ifdef __GNUG__
  14. #pragma implementation "apt-pkg/acquire.h"
  15. #endif
  16. #include <apt-pkg/acquire.h>
  17. #include <apt-pkg/acquire-item.h>
  18. #include <apt-pkg/acquire-worker.h>
  19. #include <apt-pkg/configuration.h>
  20. #include <apt-pkg/error.h>
  21. #include <apt-pkg/strutl.h>
  22. #include <dirent.h>
  23. #include <sys/time.h>
  24. /*}}}*/
  25. // Acquire::pkgAcquire - Constructor /*{{{*/
  26. // ---------------------------------------------------------------------
  27. /* We grab some runtime state from the configuration space */
  28. pkgAcquire::pkgAcquire(pkgAcquireStatus *Log) : Log(Log)
  29. {
  30. Queues = 0;
  31. Configs = 0;
  32. Workers = 0;
  33. ToFetch = 0;
  34. Running = false;
  35. string Mode = _config->Find("Acquire::Queue-Mode","host");
  36. if (strcasecmp(Mode.c_str(),"host") == 0)
  37. QueueMode = QueueHost;
  38. if (strcasecmp(Mode.c_str(),"access") == 0)
  39. QueueMode = QueueAccess;
  40. Debug = _config->FindB("Debug::pkgAcquire",false);
  41. }
  42. /*}}}*/
  43. // Acquire::~pkgAcquire - Destructor /*{{{*/
  44. // ---------------------------------------------------------------------
  45. /* Free our memory, clean up the queues (destroy the workers) */
  46. pkgAcquire::~pkgAcquire()
  47. {
  48. while (Items.size() != 0)
  49. delete Items[0];
  50. while (Configs != 0)
  51. {
  52. MethodConfig *Jnk = Configs;
  53. Configs = Configs->Next;
  54. delete Jnk;
  55. }
  56. while (Queues != 0)
  57. {
  58. Queue *Jnk = Queues;
  59. Queues = Queues->Next;
  60. delete Jnk;
  61. }
  62. }
  63. /*}}}*/
  64. // Acquire::Add - Add a new item /*{{{*/
  65. // ---------------------------------------------------------------------
  66. /* This puts an item on the acquire list. This list is mainly for tracking
  67. item status */
  68. void pkgAcquire::Add(Item *Itm)
  69. {
  70. Items.push_back(Itm);
  71. }
  72. /*}}}*/
  73. // Acquire::Remove - Remove a item /*{{{*/
  74. // ---------------------------------------------------------------------
  75. /* Remove an item from the acquire list. This is usually not used.. */
  76. void pkgAcquire::Remove(Item *Itm)
  77. {
  78. for (vector<Item *>::iterator I = Items.begin(); I < Items.end(); I++)
  79. {
  80. if (*I == Itm)
  81. Items.erase(I);
  82. }
  83. }
  84. /*}}}*/
  85. // Acquire::Add - Add a worker /*{{{*/
  86. // ---------------------------------------------------------------------
  87. /* A list of workers is kept so that the select loop can direct their FD
  88. usage. */
  89. void pkgAcquire::Add(Worker *Work)
  90. {
  91. Work->NextAcquire = Workers;
  92. Workers = Work;
  93. }
  94. /*}}}*/
  95. // Acquire::Remove - Remove a worker /*{{{*/
  96. // ---------------------------------------------------------------------
  97. /* A worker has died. This can not be done while the select loop is running
  98. as it would require that RunFds could handling a changing list state and
  99. it cant.. */
  100. void pkgAcquire::Remove(Worker *Work)
  101. {
  102. if (Running == true)
  103. abort();
  104. Worker **I = &Workers;
  105. for (; *I != 0;)
  106. {
  107. if (*I == Work)
  108. *I = (*I)->NextAcquire;
  109. else
  110. I = &(*I)->NextAcquire;
  111. }
  112. }
  113. /*}}}*/
  114. // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
  115. // ---------------------------------------------------------------------
  116. /* This is the entry point for an item. An item calls this function when
  117. it is construction which creates a queue (based on the current queue
  118. mode) and puts the item in that queue. If the system is running then
  119. the queue might be started. */
  120. void pkgAcquire::Enqueue(ItemDesc &Item)
  121. {
  122. // Determine which queue to put the item in
  123. const MethodConfig *Config;
  124. string Name = QueueName(Item.URI,Config);
  125. if (Name.empty() == true)
  126. return;
  127. // Find the queue structure
  128. Queue *I = Queues;
  129. for (; I != 0 && I->Name != Name; I = I->Next);
  130. if (I == 0)
  131. {
  132. I = new Queue(Name,this);
  133. I->Next = Queues;
  134. Queues = I;
  135. if (Running == true)
  136. I->Startup();
  137. }
  138. // See if this is a local only URI
  139. if (Config->LocalOnly == true && Item.Owner->Complete == false)
  140. Item.Owner->Local = true;
  141. Item.Owner->Status = Item::StatIdle;
  142. // Queue it into the named queue
  143. I->Enqueue(Item);
  144. ToFetch++;
  145. // Some trace stuff
  146. if (Debug == true)
  147. {
  148. clog << "Fetching " << Item.URI << endl;
  149. clog << " to " << Item.Owner->DestFile << endl;
  150. clog << " Queue is: " << Name << endl;
  151. }
  152. }
  153. /*}}}*/
  154. // Acquire::Dequeue - Remove an item from all queues /*{{{*/
  155. // ---------------------------------------------------------------------
  156. /* This is called when an item is finished being fetched. It removes it
  157. from all the queues */
  158. void pkgAcquire::Dequeue(Item *Itm)
  159. {
  160. Queue *I = Queues;
  161. bool Res = false;
  162. for (; I != 0; I = I->Next)
  163. Res |= I->Dequeue(Itm);
  164. if (Debug == true)
  165. clog << "Dequeuing " << Itm->DestFile << endl;
  166. if (Res == true)
  167. ToFetch--;
  168. }
  169. /*}}}*/
  170. // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
  171. // ---------------------------------------------------------------------
  172. /* The string returned depends on the configuration settings and the
  173. method parameters. Given something like http://foo.org/bar it can
  174. return http://foo.org or http */
  175. string pkgAcquire::QueueName(string Uri,MethodConfig const *&Config)
  176. {
  177. URI U(Uri);
  178. Config = GetConfig(U.Access);
  179. if (Config == 0)
  180. return string();
  181. /* Single-Instance methods get exactly one queue per URI. This is
  182. also used for the Access queue method */
  183. if (Config->SingleInstance == true || QueueMode == QueueAccess)
  184. return U.Access;
  185. return U.Access + ':' + U.Host;
  186. }
  187. /*}}}*/
  188. // Acquire::GetConfig - Fetch the configuration information /*{{{*/
  189. // ---------------------------------------------------------------------
  190. /* This locates the configuration structure for an access method. If
  191. a config structure cannot be found a Worker will be created to
  192. retrieve it */
  193. pkgAcquire::MethodConfig *pkgAcquire::GetConfig(string Access)
  194. {
  195. // Search for an existing config
  196. MethodConfig *Conf;
  197. for (Conf = Configs; Conf != 0; Conf = Conf->Next)
  198. if (Conf->Access == Access)
  199. return Conf;
  200. // Create the new config class
  201. Conf = new MethodConfig;
  202. Conf->Access = Access;
  203. Conf->Next = Configs;
  204. Configs = Conf;
  205. // Create the worker to fetch the configuration
  206. Worker Work(Conf);
  207. if (Work.Start() == false)
  208. return 0;
  209. return Conf;
  210. }
  211. /*}}}*/
  212. // Acquire::SetFds - Deal with readable FDs /*{{{*/
  213. // ---------------------------------------------------------------------
  214. /* Collect FDs that have activity monitors into the fd sets */
  215. void pkgAcquire::SetFds(int &Fd,fd_set *RSet,fd_set *WSet)
  216. {
  217. for (Worker *I = Workers; I != 0; I = I->NextAcquire)
  218. {
  219. if (I->InReady == true && I->InFd >= 0)
  220. {
  221. if (Fd < I->InFd)
  222. Fd = I->InFd;
  223. FD_SET(I->InFd,RSet);
  224. }
  225. if (I->OutReady == true && I->OutFd >= 0)
  226. {
  227. if (Fd < I->OutFd)
  228. Fd = I->OutFd;
  229. FD_SET(I->OutFd,WSet);
  230. }
  231. }
  232. }
  233. /*}}}*/
  234. // Acquire::RunFds - Deal with active FDs /*{{{*/
  235. // ---------------------------------------------------------------------
  236. /* Dispatch active FDs over to the proper workers. It is very important
  237. that a worker never be erased while this is running! The queue class
  238. should never erase a worker except during shutdown processing. */
  239. void pkgAcquire::RunFds(fd_set *RSet,fd_set *WSet)
  240. {
  241. for (Worker *I = Workers; I != 0; I = I->NextAcquire)
  242. {
  243. if (I->InFd >= 0 && FD_ISSET(I->InFd,RSet) != 0)
  244. I->InFdReady();
  245. if (I->OutFd >= 0 && FD_ISSET(I->OutFd,WSet) != 0)
  246. I->OutFdReady();
  247. }
  248. }
  249. /*}}}*/
  250. // Acquire::Run - Run the fetch sequence /*{{{*/
  251. // ---------------------------------------------------------------------
  252. /* This runs the queues. It manages a select loop for all of the
  253. Worker tasks. The workers interact with the queues and items to
  254. manage the actual fetch. */
  255. bool pkgAcquire::Run()
  256. {
  257. Running = true;
  258. for (Queue *I = Queues; I != 0; I = I->Next)
  259. I->Startup();
  260. if (Log != 0)
  261. Log->Start();
  262. // Run till all things have been acquired
  263. struct timeval tv;
  264. tv.tv_sec = 0;
  265. tv.tv_usec = 500000;
  266. while (ToFetch > 0)
  267. {
  268. fd_set RFds;
  269. fd_set WFds;
  270. int Highest = 0;
  271. FD_ZERO(&RFds);
  272. FD_ZERO(&WFds);
  273. SetFds(Highest,&RFds,&WFds);
  274. int Res = select(Highest+1,&RFds,&WFds,0,&tv);
  275. if (Res < 0)
  276. {
  277. _error->Errno("select","Select has failed");
  278. break;
  279. }
  280. RunFds(&RFds,&WFds);
  281. if (_error->PendingError() == true)
  282. break;
  283. // Timeout, notify the log class
  284. if (Res == 0 || (Log != 0 && Log->Update == true))
  285. {
  286. tv.tv_usec = 500000;
  287. for (Worker *I = Workers; I != 0; I = I->NextAcquire)
  288. I->Pulse();
  289. if (Log != 0)
  290. Log->Pulse(this);
  291. }
  292. }
  293. if (Log != 0)
  294. Log->Stop();
  295. // Shut down the acquire bits
  296. Running = false;
  297. for (Queue *I = Queues; I != 0; I = I->Next)
  298. I->Shutdown();
  299. return !_error->PendingError();
  300. }
  301. /*}}}*/
  302. // Acquire::Bump - Called when an item is dequeued /*{{{*/
  303. // ---------------------------------------------------------------------
  304. /* This routine bumps idle queues in hopes that they will be able to fetch
  305. the dequeued item */
  306. void pkgAcquire::Bump()
  307. {
  308. for (Queue *I = Queues; I != 0; I = I->Next)
  309. I->Bump();
  310. }
  311. /*}}}*/
  312. // Acquire::WorkerStep - Step to the next worker /*{{{*/
  313. // ---------------------------------------------------------------------
  314. /* Not inlined to advoid including acquire-worker.h */
  315. pkgAcquire::Worker *pkgAcquire::WorkerStep(Worker *I)
  316. {
  317. return I->NextAcquire;
  318. };
  319. /*}}}*/
  320. // Acquire::Clean - Cleans a directory /*{{{*/
  321. // ---------------------------------------------------------------------
  322. /* This is a bit simplistic, it looks at every file in the dir and sees
  323. if it is part of the download set. */
  324. bool pkgAcquire::Clean(string Dir)
  325. {
  326. DIR *D = opendir(Dir.c_str());
  327. if (D == 0)
  328. return _error->Errno("opendir","Unable to read %s",Dir.c_str());
  329. string StartDir = SafeGetCWD();
  330. if (chdir(Dir.c_str()) != 0)
  331. {
  332. closedir(D);
  333. return _error->Errno("chdir","Unable to change to ",Dir.c_str());
  334. }
  335. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  336. {
  337. // Skip some files..
  338. if (strcmp(Dir->d_name,"lock") == 0 ||
  339. strcmp(Dir->d_name,"partial") == 0 ||
  340. strcmp(Dir->d_name,".") == 0 ||
  341. strcmp(Dir->d_name,"..") == 0)
  342. continue;
  343. // Look in the get list
  344. vector<Item *>::iterator I = Items.begin();
  345. for (; I != Items.end(); I++)
  346. if (flNotDir((*I)->DestFile) == Dir->d_name)
  347. break;
  348. // Nothing found, nuke it
  349. if (I == Items.end())
  350. unlink(Dir->d_name);
  351. };
  352. chdir(StartDir.c_str());
  353. closedir(D);
  354. return true;
  355. }
  356. /*}}}*/
  357. // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
  358. // ---------------------------------------------------------------------
  359. /* This is the total number of bytes needed */
  360. unsigned long pkgAcquire::TotalNeeded()
  361. {
  362. unsigned long Total = 0;
  363. for (pkgAcquire::Item **I = ItemsBegin(); I != ItemsEnd(); I++)
  364. Total += (*I)->FileSize;
  365. return Total;
  366. }
  367. /*}}}*/
  368. // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
  369. // ---------------------------------------------------------------------
  370. /* This is the number of bytes that is not local */
  371. unsigned long pkgAcquire::FetchNeeded()
  372. {
  373. unsigned long Total = 0;
  374. for (pkgAcquire::Item **I = ItemsBegin(); I != ItemsEnd(); I++)
  375. if ((*I)->Local == false)
  376. Total += (*I)->FileSize;
  377. return Total;
  378. }
  379. /*}}}*/
  380. // pkgAcquire::UriBegin - Start iterator for the uri list /*{{{*/
  381. // ---------------------------------------------------------------------
  382. /* */
  383. pkgAcquire::UriIterator pkgAcquire::UriBegin()
  384. {
  385. return UriIterator(Queues);
  386. }
  387. /*}}}*/
  388. // pkgAcquire::UriEnd - End iterator for the uri list /*{{{*/
  389. // ---------------------------------------------------------------------
  390. /* */
  391. pkgAcquire::UriIterator pkgAcquire::UriEnd()
  392. {
  393. return UriIterator(0);
  394. }
  395. /*}}}*/
  396. // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
  397. // ---------------------------------------------------------------------
  398. /* */
  399. pkgAcquire::MethodConfig::MethodConfig()
  400. {
  401. SingleInstance = false;
  402. Pipeline = false;
  403. SendConfig = false;
  404. LocalOnly = false;
  405. Next = 0;
  406. }
  407. /*}}}*/
  408. // Queue::Queue - Constructor /*{{{*/
  409. // ---------------------------------------------------------------------
  410. /* */
  411. pkgAcquire::Queue::Queue(string Name,pkgAcquire *Owner) : Name(Name),
  412. Owner(Owner)
  413. {
  414. Items = 0;
  415. Next = 0;
  416. Workers = 0;
  417. MaxPipeDepth = 1;
  418. PipeDepth = 0;
  419. }
  420. /*}}}*/
  421. // Queue::~Queue - Destructor /*{{{*/
  422. // ---------------------------------------------------------------------
  423. /* */
  424. pkgAcquire::Queue::~Queue()
  425. {
  426. Shutdown();
  427. while (Items != 0)
  428. {
  429. QItem *Jnk = Items;
  430. Items = Items->Next;
  431. delete Jnk;
  432. }
  433. }
  434. /*}}}*/
  435. // Queue::Enqueue - Queue an item to the queue /*{{{*/
  436. // ---------------------------------------------------------------------
  437. /* */
  438. void pkgAcquire::Queue::Enqueue(ItemDesc &Item)
  439. {
  440. QItem **I = &Items;
  441. for (; *I != 0; I = &(*I)->Next);
  442. // Create a new item
  443. QItem *Itm = new QItem;
  444. *Itm = Item;
  445. Itm->Next = 0;
  446. *I = Itm;
  447. Item.Owner->QueueCounter++;
  448. if (Items->Next == 0)
  449. Cycle();
  450. }
  451. /*}}}*/
  452. // Queue::Dequeue - Remove an item from the queue /*{{{*/
  453. // ---------------------------------------------------------------------
  454. /* We return true if we hit something */
  455. bool pkgAcquire::Queue::Dequeue(Item *Owner)
  456. {
  457. if (Owner->Status == pkgAcquire::Item::StatFetching)
  458. return _error->Error("Tried to dequeue a fetching object");
  459. bool Res = false;
  460. QItem **I = &Items;
  461. for (; *I != 0;)
  462. {
  463. if ((*I)->Owner == Owner)
  464. {
  465. QItem *Jnk= *I;
  466. *I = (*I)->Next;
  467. Owner->QueueCounter--;
  468. delete Jnk;
  469. Res = true;
  470. }
  471. else
  472. I = &(*I)->Next;
  473. }
  474. return Res;
  475. }
  476. /*}}}*/
  477. // Queue::Startup - Start the worker processes /*{{{*/
  478. // ---------------------------------------------------------------------
  479. /* */
  480. bool pkgAcquire::Queue::Startup()
  481. {
  482. Shutdown();
  483. URI U(Name);
  484. pkgAcquire::MethodConfig *Cnf = Owner->GetConfig(U.Access);
  485. if (Cnf == 0)
  486. return false;
  487. Workers = new Worker(this,Cnf,Owner->Log);
  488. Owner->Add(Workers);
  489. if (Workers->Start() == false)
  490. return false;
  491. /* When pipelining we commit 10 items. This needs to change when we
  492. added other source retry to have cycle maintain a pipeline depth
  493. on its own. */
  494. if (Cnf->Pipeline == true)
  495. MaxPipeDepth = 10;
  496. else
  497. MaxPipeDepth = 1;
  498. return Cycle();
  499. }
  500. /*}}}*/
  501. // Queue::Shutdown - Shutdown the worker processes /*{{{*/
  502. // ---------------------------------------------------------------------
  503. /* */
  504. bool pkgAcquire::Queue::Shutdown()
  505. {
  506. // Delete all of the workers
  507. while (Workers != 0)
  508. {
  509. pkgAcquire::Worker *Jnk = Workers;
  510. Workers = Workers->NextQueue;
  511. Owner->Remove(Jnk);
  512. delete Jnk;
  513. }
  514. return true;
  515. }
  516. /*}}}*/
  517. // Queue::FindItem - Find a URI in the item list /*{{{*/
  518. // ---------------------------------------------------------------------
  519. /* */
  520. pkgAcquire::Queue::QItem *pkgAcquire::Queue::FindItem(string URI,pkgAcquire::Worker *Owner)
  521. {
  522. for (QItem *I = Items; I != 0; I = I->Next)
  523. if (I->URI == URI && I->Worker == Owner)
  524. return I;
  525. return 0;
  526. }
  527. /*}}}*/
  528. // Queue::ItemDone - Item has been completed /*{{{*/
  529. // ---------------------------------------------------------------------
  530. /* The worker signals this which causes the item to be removed from the
  531. queue. If this is the last queue instance then it is removed from the
  532. main queue too.*/
  533. bool pkgAcquire::Queue::ItemDone(QItem *Itm)
  534. {
  535. PipeDepth--;
  536. if (Itm->Owner->Status == pkgAcquire::Item::StatFetching)
  537. Itm->Owner->Status = pkgAcquire::Item::StatDone;
  538. if (Itm->Owner->QueueCounter <= 1)
  539. Owner->Dequeue(Itm->Owner);
  540. else
  541. {
  542. Dequeue(Itm->Owner);
  543. Owner->Bump();
  544. }
  545. return Cycle();
  546. }
  547. /*}}}*/
  548. // Queue::Cycle - Queue new items into the method /*{{{*/
  549. // ---------------------------------------------------------------------
  550. /* This locates a new idle item and sends it to the worker. If pipelining
  551. is enabled then it keeps the pipe full. */
  552. bool pkgAcquire::Queue::Cycle()
  553. {
  554. if (Items == 0 || Workers == 0)
  555. return true;
  556. if (PipeDepth < 0)
  557. return _error->Error("Pipedepth failure");
  558. // Look for a queable item
  559. QItem *I = Items;
  560. while (PipeDepth < (signed)MaxPipeDepth)
  561. {
  562. for (; I != 0; I = I->Next)
  563. if (I->Owner->Status == pkgAcquire::Item::StatIdle)
  564. break;
  565. // Nothing to do, queue is idle.
  566. if (I == 0)
  567. return true;
  568. I->Worker = Workers;
  569. I->Owner->Status = pkgAcquire::Item::StatFetching;
  570. PipeDepth++;
  571. if (Workers->QueueItem(I) == false)
  572. return false;
  573. }
  574. return true;
  575. }
  576. /*}}}*/
  577. // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
  578. // ---------------------------------------------------------------------
  579. /* This is called when an item in multiple queues is dequeued */
  580. void pkgAcquire::Queue::Bump()
  581. {
  582. Cycle();
  583. }
  584. /*}}}*/
  585. // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
  586. // ---------------------------------------------------------------------
  587. /* */
  588. pkgAcquireStatus::pkgAcquireStatus()
  589. {
  590. Start();
  591. }
  592. /*}}}*/
  593. // AcquireStatus::Pulse - Called periodically /*{{{*/
  594. // ---------------------------------------------------------------------
  595. /* This computes some internal state variables for the derived classes to
  596. use. It generates the current downloaded bytes and total bytes to download
  597. as well as the current CPS estimate. */
  598. void pkgAcquireStatus::Pulse(pkgAcquire *Owner)
  599. {
  600. TotalBytes = 0;
  601. CurrentBytes = 0;
  602. TotalItems = 0;
  603. CurrentItems = 0;
  604. // Compute the total number of bytes to fetch
  605. unsigned int Unknown = 0;
  606. unsigned int Count = 0;
  607. for (pkgAcquire::Item **I = Owner->ItemsBegin(); I != Owner->ItemsEnd();
  608. I++, Count++)
  609. {
  610. TotalItems++;
  611. if ((*I)->Status == pkgAcquire::Item::StatDone)
  612. CurrentItems++;
  613. // Totally ignore local items
  614. if ((*I)->Local == true)
  615. continue;
  616. TotalBytes += (*I)->FileSize;
  617. if ((*I)->Complete == true)
  618. CurrentBytes += (*I)->FileSize;
  619. if ((*I)->FileSize == 0 && (*I)->Complete == false)
  620. Unknown++;
  621. }
  622. // Compute the current completion
  623. for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
  624. I = Owner->WorkerStep(I))
  625. if (I->CurrentItem != 0 && I->CurrentItem->Owner->Complete == false)
  626. CurrentBytes += I->CurrentSize;
  627. // Normalize the figures and account for unknown size downloads
  628. if (TotalBytes <= 0)
  629. TotalBytes = 1;
  630. if (Unknown == Count)
  631. TotalBytes = Unknown;
  632. else
  633. TotalBytes += TotalBytes/(Count - Unknown)*Unknown;
  634. // Compute the CPS
  635. struct timeval NewTime;
  636. gettimeofday(&NewTime,0);
  637. if (NewTime.tv_sec - Time.tv_sec == 6 && NewTime.tv_usec > Time.tv_usec ||
  638. NewTime.tv_sec - Time.tv_sec > 6)
  639. {
  640. // Compute the delta time with full accuracy
  641. long usdiff = NewTime.tv_usec - Time.tv_usec;
  642. long sdiff = NewTime.tv_sec - Time.tv_sec;
  643. // Borrow
  644. if (usdiff < 0)
  645. {
  646. usdiff += 1000000;
  647. sdiff--;
  648. }
  649. // Compute the CPS value
  650. if (sdiff == 0 && usdiff == 0)
  651. CurrentCPS = 0;
  652. else
  653. CurrentCPS = (CurrentBytes - LastBytes)/(sdiff + usdiff/1000000.0);
  654. LastBytes = CurrentBytes;
  655. ElapsedTime = NewTime.tv_sec - StartTime.tv_sec;
  656. Time = NewTime;
  657. }
  658. }
  659. /*}}}*/
  660. // AcquireStatus::Start - Called when the download is started /*{{{*/
  661. // ---------------------------------------------------------------------
  662. /* We just reset the counters */
  663. void pkgAcquireStatus::Start()
  664. {
  665. gettimeofday(&Time,0);
  666. gettimeofday(&StartTime,0);
  667. LastBytes = 0;
  668. CurrentCPS = 0;
  669. CurrentBytes = 0;
  670. TotalBytes = 0;
  671. FetchedBytes = 0;
  672. ElapsedTime = 0;
  673. TotalItems = 0;
  674. CurrentItems = 0;
  675. }
  676. /*}}}*/
  677. // AcquireStatus::Stop - Finished downloading /*{{{*/
  678. // ---------------------------------------------------------------------
  679. /* This accurately computes the elapsed time and the total overall CPS. */
  680. void pkgAcquireStatus::Stop()
  681. {
  682. // Compute the CPS and elapsed time
  683. struct timeval NewTime;
  684. gettimeofday(&NewTime,0);
  685. // Compute the delta time with full accuracy
  686. long usdiff = NewTime.tv_usec - StartTime.tv_usec;
  687. long sdiff = NewTime.tv_sec - StartTime.tv_sec;
  688. // Borrow
  689. if (usdiff < 0)
  690. {
  691. usdiff += 1000000;
  692. sdiff--;
  693. }
  694. // Compute the CPS value
  695. if (sdiff == 0 && usdiff == 0)
  696. CurrentCPS = 0;
  697. else
  698. CurrentCPS = FetchedBytes/(sdiff + usdiff/1000000.0);
  699. LastBytes = CurrentBytes;
  700. ElapsedTime = sdiff;
  701. }
  702. /*}}}*/
  703. // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
  704. // ---------------------------------------------------------------------
  705. /* This is used to get accurate final transfer rate reporting. */
  706. void pkgAcquireStatus::Fetched(unsigned long Size,unsigned long Resume)
  707. {
  708. FetchedBytes += Size - Resume;
  709. }
  710. /*}}}*/