acquire.cc 24 KB

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