You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

820 lines
23 KiB

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