acquire.cc 23 KB

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