acquire.cc 25 KB

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