acquire.cc 24 KB

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