acquire.cc 24 KB

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