acquire.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire.cc,v 1.17 1998/11/22 23:37:03 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 <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. // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
  381. // ---------------------------------------------------------------------
  382. /* */
  383. pkgAcquire::MethodConfig::MethodConfig()
  384. {
  385. SingleInstance = false;
  386. PreScan = false;
  387. Pipeline = false;
  388. SendConfig = false;
  389. LocalOnly = false;
  390. Next = 0;
  391. }
  392. /*}}}*/
  393. // Queue::Queue - Constructor /*{{{*/
  394. // ---------------------------------------------------------------------
  395. /* */
  396. pkgAcquire::Queue::Queue(string Name,pkgAcquire *Owner) : Name(Name),
  397. Owner(Owner)
  398. {
  399. Items = 0;
  400. Next = 0;
  401. Workers = 0;
  402. }
  403. /*}}}*/
  404. // Queue::~Queue - Destructor /*{{{*/
  405. // ---------------------------------------------------------------------
  406. /* */
  407. pkgAcquire::Queue::~Queue()
  408. {
  409. Shutdown();
  410. while (Items != 0)
  411. {
  412. QItem *Jnk = Items;
  413. Items = Items->Next;
  414. delete Jnk;
  415. }
  416. }
  417. /*}}}*/
  418. // Queue::Enqueue - Queue an item to the queue /*{{{*/
  419. // ---------------------------------------------------------------------
  420. /* */
  421. void pkgAcquire::Queue::Enqueue(ItemDesc &Item)
  422. {
  423. QItem **I = &Items;
  424. for (; *I != 0; I = &(*I)->Next);
  425. // Create a new item
  426. QItem *Itm = new QItem;
  427. *Itm = Item;
  428. Itm->Next = 0;
  429. *I = Itm;
  430. Item.Owner->QueueCounter++;
  431. if (Items->Next == 0)
  432. Cycle();
  433. }
  434. /*}}}*/
  435. // Queue::Dequeue - Remove an item from the queue /*{{{*/
  436. // ---------------------------------------------------------------------
  437. /* We return true if we hit something*/
  438. bool pkgAcquire::Queue::Dequeue(Item *Owner)
  439. {
  440. bool Res = false;
  441. QItem **I = &Items;
  442. for (; *I != 0;)
  443. {
  444. if ((*I)->Owner == Owner)
  445. {
  446. QItem *Jnk= *I;
  447. *I = (*I)->Next;
  448. Owner->QueueCounter--;
  449. delete Jnk;
  450. Res = true;
  451. }
  452. else
  453. I = &(*I)->Next;
  454. }
  455. return Res;
  456. }
  457. /*}}}*/
  458. // Queue::Startup - Start the worker processes /*{{{*/
  459. // ---------------------------------------------------------------------
  460. /* */
  461. bool pkgAcquire::Queue::Startup()
  462. {
  463. Shutdown();
  464. URI U(Name);
  465. pkgAcquire::MethodConfig *Cnf = Owner->GetConfig(U.Access);
  466. if (Cnf == 0)
  467. return false;
  468. Workers = new Worker(this,Cnf,Owner->Log);
  469. Owner->Add(Workers);
  470. if (Workers->Start() == false)
  471. return false;
  472. return Cycle();
  473. }
  474. /*}}}*/
  475. // Queue::Shutdown - Shutdown the worker processes /*{{{*/
  476. // ---------------------------------------------------------------------
  477. /* */
  478. bool pkgAcquire::Queue::Shutdown()
  479. {
  480. // Delete all of the workers
  481. while (Workers != 0)
  482. {
  483. pkgAcquire::Worker *Jnk = Workers;
  484. Workers = Workers->NextQueue;
  485. Owner->Remove(Jnk);
  486. delete Jnk;
  487. }
  488. return true;
  489. }
  490. /*}}}*/
  491. // Queue::Finditem - Find a URI in the item list /*{{{*/
  492. // ---------------------------------------------------------------------
  493. /* */
  494. pkgAcquire::Queue::QItem *pkgAcquire::Queue::FindItem(string URI,pkgAcquire::Worker *Owner)
  495. {
  496. for (QItem *I = Items; I != 0; I = I->Next)
  497. if (I->URI == URI && I->Worker == Owner)
  498. return I;
  499. return 0;
  500. }
  501. /*}}}*/
  502. // Queue::ItemDone - Item has been completed /*{{{*/
  503. // ---------------------------------------------------------------------
  504. /* The worker signals this which causes the item to be removed from the
  505. queue. If this is the last queue instance then it is removed from the
  506. main queue too.*/
  507. bool pkgAcquire::Queue::ItemDone(QItem *Itm)
  508. {
  509. if (Itm->Owner->QueueCounter <= 1)
  510. Owner->Dequeue(Itm->Owner);
  511. else
  512. {
  513. Dequeue(Itm->Owner);
  514. Owner->Bump();
  515. }
  516. return Cycle();
  517. }
  518. /*}}}*/
  519. // Queue::Cycle - Queue new items into the method /*{{{*/
  520. // ---------------------------------------------------------------------
  521. /* This locates a new idle item and sends it to the worker */
  522. bool pkgAcquire::Queue::Cycle()
  523. {
  524. if (Items == 0 || Workers == 0)
  525. return true;
  526. // Look for a queable item
  527. QItem *I = Items;
  528. for (; I != 0; I = I->Next)
  529. if (I->Owner->Status == pkgAcquire::Item::StatIdle)
  530. break;
  531. // Nothing to do, queue is idle.
  532. if (I == 0)
  533. return true;
  534. I->Worker = Workers;
  535. I->Owner->Status = pkgAcquire::Item::StatFetching;
  536. return Workers->QueueItem(I);
  537. }
  538. /*}}}*/
  539. // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
  540. // ---------------------------------------------------------------------
  541. /* */
  542. void pkgAcquire::Queue::Bump()
  543. {
  544. }
  545. /*}}}*/
  546. // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
  547. // ---------------------------------------------------------------------
  548. /* */
  549. pkgAcquireStatus::pkgAcquireStatus()
  550. {
  551. Start();
  552. }
  553. /*}}}*/
  554. // AcquireStatus::Pulse - Called periodically /*{{{*/
  555. // ---------------------------------------------------------------------
  556. /* This computes some internal state variables for the derived classes to
  557. use. It generates the current downloaded bytes and total bytes to download
  558. as well as the current CPS estimate. */
  559. void pkgAcquireStatus::Pulse(pkgAcquire *Owner)
  560. {
  561. TotalBytes = 0;
  562. CurrentBytes = 0;
  563. // Compute the total number of bytes to fetch
  564. unsigned int Unknown = 0;
  565. unsigned int Count = 0;
  566. for (pkgAcquire::Item **I = Owner->ItemsBegin(); I != Owner->ItemsEnd();
  567. I++, Count++)
  568. {
  569. // Totally ignore local items
  570. if ((*I)->Local == true)
  571. continue;
  572. TotalBytes += (*I)->FileSize;
  573. if ((*I)->Complete == true)
  574. CurrentBytes += (*I)->FileSize;
  575. if ((*I)->FileSize == 0 && (*I)->Complete == false)
  576. Unknown++;
  577. }
  578. // Compute the current completion
  579. for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
  580. I = Owner->WorkerStep(I))
  581. if (I->CurrentItem != 0 && I->CurrentItem->Owner->Complete == false)
  582. CurrentBytes += I->CurrentSize;
  583. // Normalize the figures and account for unknown size downloads
  584. if (TotalBytes <= 0)
  585. TotalBytes = 1;
  586. if (Unknown == Count)
  587. TotalBytes = Unknown;
  588. else
  589. TotalBytes += TotalBytes/(Count - Unknown)*Unknown;
  590. // Compute the CPS
  591. struct timeval NewTime;
  592. gettimeofday(&NewTime,0);
  593. if (NewTime.tv_sec - Time.tv_sec == 6 && NewTime.tv_usec > Time.tv_usec ||
  594. NewTime.tv_sec - Time.tv_sec > 6)
  595. {
  596. // Compute the delta time with full accuracy
  597. long usdiff = NewTime.tv_usec - Time.tv_usec;
  598. long sdiff = NewTime.tv_sec - Time.tv_sec;
  599. // Borrow
  600. if (usdiff < 0)
  601. {
  602. usdiff += 1000000;
  603. sdiff--;
  604. }
  605. // Compute the CPS value
  606. if (sdiff == 0 && usdiff == 0)
  607. CurrentCPS = 0;
  608. else
  609. CurrentCPS = (CurrentBytes - LastBytes)/(sdiff + usdiff/1000000.0);
  610. LastBytes = CurrentBytes;
  611. ElapsedTime = NewTime.tv_sec - StartTime.tv_sec;
  612. Time = NewTime;
  613. }
  614. }
  615. /*}}}*/
  616. // AcquireStatus::Start - Called when the download is started /*{{{*/
  617. // ---------------------------------------------------------------------
  618. /* We just reset the counters */
  619. void pkgAcquireStatus::Start()
  620. {
  621. gettimeofday(&Time,0);
  622. gettimeofday(&StartTime,0);
  623. LastBytes = 0;
  624. CurrentCPS = 0;
  625. CurrentBytes = 0;
  626. TotalBytes = 0;
  627. FetchedBytes = 0;
  628. ElapsedTime = 0;
  629. }
  630. /*}}}*/
  631. // AcquireStatus::Stop - Finished downloading /*{{{*/
  632. // ---------------------------------------------------------------------
  633. /* This accurately computes the elapsed time and the total overall CPS. */
  634. void pkgAcquireStatus::Stop()
  635. {
  636. // Compute the CPS and elapsed time
  637. struct timeval NewTime;
  638. gettimeofday(&NewTime,0);
  639. // Compute the delta time with full accuracy
  640. long usdiff = NewTime.tv_usec - StartTime.tv_usec;
  641. long sdiff = NewTime.tv_sec - StartTime.tv_sec;
  642. // Borrow
  643. if (usdiff < 0)
  644. {
  645. usdiff += 1000000;
  646. sdiff--;
  647. }
  648. // Compute the CPS value
  649. if (sdiff == 0 && usdiff == 0)
  650. CurrentCPS = 0;
  651. else
  652. CurrentCPS = FetchedBytes/(sdiff + usdiff/1000000.0);
  653. LastBytes = CurrentBytes;
  654. ElapsedTime = sdiff;
  655. }
  656. /*}}}*/
  657. // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
  658. // ---------------------------------------------------------------------
  659. /* This is used to get accurate final transfer rate reporting. */
  660. void pkgAcquireStatus::Fetched(unsigned long Size,unsigned long Resume)
  661. {
  662. FetchedBytes += Size - Resume;
  663. }
  664. /*}}}*/