acquire.cc 25 KB

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