acquire.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire.cc,v 1.9 1998/11/06 02:52:20 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 <strutl.h>
  22. /*}}}*/
  23. // Acquire::pkgAcquire - Constructor /*{{{*/
  24. // ---------------------------------------------------------------------
  25. /* We grab some runtime state from the configuration space */
  26. pkgAcquire::pkgAcquire()
  27. {
  28. Queues = 0;
  29. Configs = 0;
  30. Workers = 0;
  31. ToFetch = 0;
  32. Running = false;
  33. string Mode = _config->Find("Acquire::Queue-Mode","host");
  34. if (strcasecmp(Mode.c_str(),"host") == 0)
  35. QueueMode = QueueHost;
  36. if (strcasecmp(Mode.c_str(),"access") == 0)
  37. QueueMode = QueueAccess;
  38. Debug = _config->FindB("Debug::pkgAcquire",false);
  39. }
  40. /*}}}*/
  41. // Acquire::~pkgAcquire - Destructor /*{{{*/
  42. // ---------------------------------------------------------------------
  43. /* Free our memory, clean up the queues (destroy the workers) */
  44. pkgAcquire::~pkgAcquire()
  45. {
  46. while (Items.size() != 0)
  47. delete Items[0];
  48. while (Configs != 0)
  49. {
  50. MethodConfig *Jnk = Configs;
  51. Configs = Configs->Next;
  52. delete Jnk;
  53. }
  54. while (Queues != 0)
  55. {
  56. Queue *Jnk = Queues;
  57. Queues = Queues->Next;
  58. delete Jnk;
  59. }
  60. }
  61. /*}}}*/
  62. // Acquire::Add - Add a new item /*{{{*/
  63. // ---------------------------------------------------------------------
  64. /* This puts an item on the acquire list. This list is mainly for tracking
  65. item status */
  66. void pkgAcquire::Add(Item *Itm)
  67. {
  68. Items.push_back(Itm);
  69. }
  70. /*}}}*/
  71. // Acquire::Remove - Remove a item /*{{{*/
  72. // ---------------------------------------------------------------------
  73. /* Remove an item from the acquire list. This is usually not used.. */
  74. void pkgAcquire::Remove(Item *Itm)
  75. {
  76. for (vector<Item *>::iterator I = Items.begin(); I < Items.end(); I++)
  77. {
  78. if (*I == Itm)
  79. Items.erase(I);
  80. }
  81. }
  82. /*}}}*/
  83. // Acquire::Add - Add a worker /*{{{*/
  84. // ---------------------------------------------------------------------
  85. /* A list of workers is kept so that the select loop can direct their FD
  86. usage. */
  87. void pkgAcquire::Add(Worker *Work)
  88. {
  89. Work->NextAcquire = Workers;
  90. Workers = Work;
  91. }
  92. /*}}}*/
  93. // Acquire::Remove - Remove a worker /*{{{*/
  94. // ---------------------------------------------------------------------
  95. /* A worker has died. This can not be done while the select loop is running
  96. as it would require that RunFds could handling a changing list state and
  97. it cant.. */
  98. void pkgAcquire::Remove(Worker *Work)
  99. {
  100. if (Running == true)
  101. abort();
  102. Worker **I = &Workers;
  103. for (; *I != 0;)
  104. {
  105. if (*I == Work)
  106. *I = (*I)->NextAcquire;
  107. else
  108. I = &(*I)->NextAcquire;
  109. }
  110. }
  111. /*}}}*/
  112. // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
  113. // ---------------------------------------------------------------------
  114. /* This is the entry point for an item. An item calls this function when
  115. it is construction which creates a queue (based on the current queue
  116. mode) and puts the item in that queue. If the system is running then
  117. the queue might be started. */
  118. void pkgAcquire::Enqueue(Item *Itm,string URI,string Description)
  119. {
  120. // Determine which queue to put the item in
  121. string Name = QueueName(URI);
  122. if (Name.empty() == true)
  123. return;
  124. // Find the queue structure
  125. Queue *I = Queues;
  126. for (; I != 0 && I->Name != Name; I = I->Next);
  127. if (I == 0)
  128. {
  129. I = new Queue(Name,this);
  130. I->Next = Queues;
  131. Queues = I;
  132. if (Running == true)
  133. I->Startup();
  134. }
  135. Itm->Status = Item::StatIdle;
  136. // Queue it into the named queue
  137. I->Enqueue(Itm,URI,Description);
  138. ToFetch++;
  139. // Some trace stuff
  140. if (Debug == true)
  141. {
  142. clog << "Fetching " << URI << endl;
  143. clog << " to " << Itm->DestFile << endl;
  144. clog << " Queue is: " << QueueName(URI) << endl;
  145. }
  146. }
  147. /*}}}*/
  148. // Acquire::Dequeue - Remove an item from all queues /*{{{*/
  149. // ---------------------------------------------------------------------
  150. /* This is called when an item is finished being fetched. It removes it
  151. from all the queues */
  152. void pkgAcquire::Dequeue(Item *Itm)
  153. {
  154. Queue *I = Queues;
  155. bool Res = false;
  156. for (; I != 0; I = I->Next)
  157. Res |= I->Dequeue(Itm);
  158. if (Debug == true)
  159. clog << "Dequeuing " << Itm->DestFile << endl;
  160. if (Res == true)
  161. ToFetch--;
  162. }
  163. /*}}}*/
  164. // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
  165. // ---------------------------------------------------------------------
  166. /* The string returned depends on the configuration settings and the
  167. method parameters. Given something like http://foo.org/bar it can
  168. return http://foo.org or http */
  169. string pkgAcquire::QueueName(string Uri)
  170. {
  171. URI U(Uri);
  172. const MethodConfig *Config = GetConfig(U.Access);
  173. if (Config == 0)
  174. return string();
  175. /* Single-Instance methods get exactly one queue per URI. This is
  176. also used for the Access queue method */
  177. if (Config->SingleInstance == true || QueueMode == QueueAccess)
  178. return U.Access;
  179. return U.Access + ':' + U.Host;
  180. }
  181. /*}}}*/
  182. // Acquire::GetConfig - Fetch the configuration information /*{{{*/
  183. // ---------------------------------------------------------------------
  184. /* This locates the configuration structure for an access method. If
  185. a config structure cannot be found a Worker will be created to
  186. retrieve it */
  187. pkgAcquire::MethodConfig *pkgAcquire::GetConfig(string Access)
  188. {
  189. // Search for an existing config
  190. MethodConfig *Conf;
  191. for (Conf = Configs; Conf != 0; Conf = Conf->Next)
  192. if (Conf->Access == Access)
  193. return Conf;
  194. // Create the new config class
  195. Conf = new MethodConfig;
  196. Conf->Access = Access;
  197. Conf->Next = Configs;
  198. Configs = Conf;
  199. // Create the worker to fetch the configuration
  200. Worker Work(Conf);
  201. if (Work.Start() == false)
  202. return 0;
  203. return Conf;
  204. }
  205. /*}}}*/
  206. // Acquire::SetFds - Deal with readable FDs /*{{{*/
  207. // ---------------------------------------------------------------------
  208. /* Collect FDs that have activity monitors into the fd sets */
  209. void pkgAcquire::SetFds(int &Fd,fd_set *RSet,fd_set *WSet)
  210. {
  211. for (Worker *I = Workers; I != 0; I = I->NextAcquire)
  212. {
  213. if (I->InReady == true && I->InFd >= 0)
  214. {
  215. if (Fd < I->InFd)
  216. Fd = I->InFd;
  217. FD_SET(I->InFd,RSet);
  218. }
  219. if (I->OutReady == true && I->OutFd >= 0)
  220. {
  221. if (Fd < I->OutFd)
  222. Fd = I->OutFd;
  223. FD_SET(I->OutFd,WSet);
  224. }
  225. }
  226. }
  227. /*}}}*/
  228. // Acquire::RunFds - Deal with active FDs /*{{{*/
  229. // ---------------------------------------------------------------------
  230. /* Dispatch active FDs over to the proper workers. It is very important
  231. that a worker never be erased while this is running! The queue class
  232. should never erase a worker except during shutdown processing. */
  233. void pkgAcquire::RunFds(fd_set *RSet,fd_set *WSet)
  234. {
  235. for (Worker *I = Workers; I != 0; I = I->NextAcquire)
  236. {
  237. if (I->InFd >= 0 && FD_ISSET(I->InFd,RSet) != 0)
  238. I->InFdReady();
  239. if (I->OutFd >= 0 && FD_ISSET(I->OutFd,WSet) != 0)
  240. I->OutFdReady();
  241. }
  242. }
  243. /*}}}*/
  244. // Acquire::Run - Run the fetch sequence /*{{{*/
  245. // ---------------------------------------------------------------------
  246. /* This runs the queues. It manages a select loop for all of the
  247. Worker tasks. The workers interact with the queues and items to
  248. manage the actual fetch. */
  249. bool pkgAcquire::Run()
  250. {
  251. Running = true;
  252. for (Queue *I = Queues; I != 0; I = I->Next)
  253. I->Startup();
  254. // Run till all things have been acquired
  255. while (ToFetch > 0)
  256. {
  257. fd_set RFds;
  258. fd_set WFds;
  259. int Highest = 0;
  260. FD_ZERO(&RFds);
  261. FD_ZERO(&WFds);
  262. SetFds(Highest,&RFds,&WFds);
  263. if (select(Highest+1,&RFds,&WFds,0,0) <= 0)
  264. {
  265. Running = false;
  266. return _error->Errno("select","Select has failed");
  267. }
  268. RunFds(&RFds,&WFds);
  269. if (_error->PendingError() == true)
  270. break;
  271. }
  272. // Shut down the acquire bits
  273. Running = false;
  274. for (Queue *I = Queues; I != 0; I = I->Next)
  275. I->Shutdown();
  276. return _error->PendingError();
  277. }
  278. /*}}}*/
  279. // Acquire::Bump - Called when an item is dequeued /*{{{*/
  280. // ---------------------------------------------------------------------
  281. /* This routine bumps idle queues in hopes that they will be able to fetch
  282. the dequeued item */
  283. void pkgAcquire::Bump()
  284. {
  285. for (Queue *I = Queues; I != 0; I = I->Next)
  286. I->Bump();
  287. }
  288. /*}}}*/
  289. // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
  290. // ---------------------------------------------------------------------
  291. /* */
  292. pkgAcquire::MethodConfig::MethodConfig()
  293. {
  294. SingleInstance = false;
  295. PreScan = false;
  296. Pipeline = false;
  297. SendConfig = false;
  298. Next = 0;
  299. }
  300. /*}}}*/
  301. // Queue::Queue - Constructor /*{{{*/
  302. // ---------------------------------------------------------------------
  303. /* */
  304. pkgAcquire::Queue::Queue(string Name,pkgAcquire *Owner) : Name(Name),
  305. Owner(Owner)
  306. {
  307. Items = 0;
  308. Next = 0;
  309. Workers = 0;
  310. }
  311. /*}}}*/
  312. // Queue::~Queue - Destructor /*{{{*/
  313. // ---------------------------------------------------------------------
  314. /* */
  315. pkgAcquire::Queue::~Queue()
  316. {
  317. Shutdown();
  318. while (Items != 0)
  319. {
  320. QItem *Jnk = Items;
  321. Items = Items->Next;
  322. delete Jnk;
  323. }
  324. }
  325. /*}}}*/
  326. // Queue::Enqueue - Queue an item to the queue /*{{{*/
  327. // ---------------------------------------------------------------------
  328. /* */
  329. void pkgAcquire::Queue::Enqueue(Item *Owner,string URI,string Description)
  330. {
  331. // Create a new item
  332. QItem *I = new QItem;
  333. I->Next = Items;
  334. Items = I;
  335. // Fill it in
  336. Items->Owner = Owner;
  337. Items->URI = URI;
  338. Items->Description = Description;
  339. Owner->QueueCounter++;
  340. if (Items->Next == 0)
  341. Cycle();
  342. }
  343. /*}}}*/
  344. // Queue::Dequeue - Remove an item from the queue /*{{{*/
  345. // ---------------------------------------------------------------------
  346. /* We return true if we hit something*/
  347. bool pkgAcquire::Queue::Dequeue(Item *Owner)
  348. {
  349. bool Res = false;
  350. QItem **I = &Items;
  351. for (; *I != 0;)
  352. {
  353. if ((*I)->Owner == Owner)
  354. {
  355. QItem *Jnk= *I;
  356. *I = (*I)->Next;
  357. Owner->QueueCounter--;
  358. delete Jnk;
  359. Res = true;
  360. }
  361. else
  362. I = &(*I)->Next;
  363. }
  364. return Res;
  365. }
  366. /*}}}*/
  367. // Queue::Startup - Start the worker processes /*{{{*/
  368. // ---------------------------------------------------------------------
  369. /* */
  370. bool pkgAcquire::Queue::Startup()
  371. {
  372. Shutdown();
  373. URI U(Name);
  374. pkgAcquire::MethodConfig *Cnf = Owner->GetConfig(U.Access);
  375. if (Cnf == 0)
  376. return false;
  377. Workers = new Worker(this,Cnf);
  378. Owner->Add(Workers);
  379. if (Workers->Start() == false)
  380. return false;
  381. return Cycle();
  382. }
  383. /*}}}*/
  384. // Queue::Shutdown - Shutdown the worker processes /*{{{*/
  385. // ---------------------------------------------------------------------
  386. /* */
  387. bool pkgAcquire::Queue::Shutdown()
  388. {
  389. // Delete all of the workers
  390. while (Workers != 0)
  391. {
  392. pkgAcquire::Worker *Jnk = Workers;
  393. Workers = Workers->NextQueue;
  394. Owner->Remove(Jnk);
  395. delete Jnk;
  396. }
  397. return true;
  398. }
  399. /*}}}*/
  400. // Queue::Finditem - Find a URI in the item list /*{{{*/
  401. // ---------------------------------------------------------------------
  402. /* */
  403. pkgAcquire::Queue::QItem *pkgAcquire::Queue::FindItem(string URI,pkgAcquire::Worker *Owner)
  404. {
  405. for (QItem *I = Items; I != 0; I = I->Next)
  406. if (I->URI == URI && I->Worker == Owner)
  407. return I;
  408. return 0;
  409. }
  410. /*}}}*/
  411. // Queue::ItemDone - Item has been completed /*{{{*/
  412. // ---------------------------------------------------------------------
  413. /* The worker signals this which causes the item to be removed from the
  414. queue. If this is the last queue instance then it is removed from the
  415. main queue too.*/
  416. bool pkgAcquire::Queue::ItemDone(QItem *Itm)
  417. {
  418. if (Itm->Owner->QueueCounter <= 1)
  419. Owner->Dequeue(Itm->Owner);
  420. else
  421. {
  422. Dequeue(Itm->Owner);
  423. Owner->Bump();
  424. }
  425. return Cycle();
  426. }
  427. /*}}}*/
  428. // Queue::Cycle - Queue new items into the method /*{{{*/
  429. // ---------------------------------------------------------------------
  430. /* This locates a new idle item and sends it to the worker */
  431. bool pkgAcquire::Queue::Cycle()
  432. {
  433. if (Items == 0 || Workers == 0)
  434. return true;
  435. // Look for a queable item
  436. QItem *I = Items;
  437. for (; I != 0; I = I->Next)
  438. if (I->Owner->Status == pkgAcquire::Item::StatIdle)
  439. break;
  440. // Nothing to do, queue is idle.
  441. if (I == 0)
  442. return true;
  443. I->Worker = Workers;
  444. I->Owner->Status = pkgAcquire::Item::StatFetching;
  445. return Workers->QueueItem(I);
  446. }
  447. /*}}}*/
  448. // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
  449. // ---------------------------------------------------------------------
  450. /* */
  451. void pkgAcquire::Queue::Bump()
  452. {
  453. }
  454. /*}}}*/