acquire.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire.cc,v 1.6 1998/10/30 07:53:37 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. // Queue it into the named queue
  136. I->Enqueue(Itm,URI,Description);
  137. ToFetch++;
  138. // Some trace stuff
  139. if (Debug == true)
  140. {
  141. clog << "Fetching " << URI << endl;
  142. clog << " to " << Itm->DestFile << endl;
  143. clog << " Queue is: " << QueueName(URI) << endl;
  144. }
  145. }
  146. /*}}}*/
  147. // Acquire::Dequeue - Remove an item from all queues /*{{{*/
  148. // ---------------------------------------------------------------------
  149. /* This is called when an item is finished being fetched. It removes it
  150. from all the queues */
  151. void pkgAcquire::Dequeue(Item *Itm)
  152. {
  153. Queue *I = Queues;
  154. for (; I != 0; I = I->Next)
  155. I->Dequeue(Itm);
  156. if (Debug == true)
  157. clog << "Dequeuing " << Itm->DestFile << endl;
  158. ToFetch--;
  159. }
  160. /*}}}*/
  161. // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
  162. // ---------------------------------------------------------------------
  163. /* The string returned depends on the configuration settings and the
  164. method parameters. Given something like http://foo.org/bar it can
  165. return http://foo.org or http */
  166. string pkgAcquire::QueueName(string Uri)
  167. {
  168. URI U(Uri);
  169. const MethodConfig *Config = GetConfig(U.Access);
  170. if (Config == 0)
  171. return string();
  172. /* Single-Instance methods get exactly one queue per URI. This is
  173. also used for the Access queue method */
  174. if (Config->SingleInstance == true || QueueMode == QueueAccess)
  175. return U.Access;
  176. return U.Access + ':' + U.Host;
  177. }
  178. /*}}}*/
  179. // Acquire::GetConfig - Fetch the configuration information /*{{{*/
  180. // ---------------------------------------------------------------------
  181. /* This locates the configuration structure for an access method. If
  182. a config structure cannot be found a Worker will be created to
  183. retrieve it */
  184. pkgAcquire::MethodConfig *pkgAcquire::GetConfig(string Access)
  185. {
  186. // Search for an existing config
  187. MethodConfig *Conf;
  188. for (Conf = Configs; Conf != 0; Conf = Conf->Next)
  189. if (Conf->Access == Access)
  190. return Conf;
  191. // Create the new config class
  192. Conf = new MethodConfig;
  193. Conf->Access = Access;
  194. Conf->Next = Configs;
  195. Configs = Conf;
  196. // Create the worker to fetch the configuration
  197. Worker Work(Conf);
  198. if (Work.Start() == false)
  199. return 0;
  200. return Conf;
  201. }
  202. /*}}}*/
  203. // Acquire::SetFds - Deal with readable FDs /*{{{*/
  204. // ---------------------------------------------------------------------
  205. /* Collect FDs that have activity monitors into the fd sets */
  206. void pkgAcquire::SetFds(int &Fd,fd_set *RSet,fd_set *WSet)
  207. {
  208. for (Worker *I = Workers; I != 0; I = I->NextAcquire)
  209. {
  210. if (I->InReady == true && I->InFd >= 0)
  211. {
  212. if (Fd < I->InFd)
  213. Fd = I->InFd;
  214. FD_SET(I->InFd,RSet);
  215. }
  216. if (I->OutReady == true && I->OutFd >= 0)
  217. {
  218. if (Fd < I->OutFd)
  219. Fd = I->OutFd;
  220. FD_SET(I->OutFd,WSet);
  221. }
  222. }
  223. }
  224. /*}}}*/
  225. // Acquire::RunFds - Deal with active FDs /*{{{*/
  226. // ---------------------------------------------------------------------
  227. /* Dispatch active FDs over to the proper workers. It is very important
  228. that a worker never be erased while this is running! The queue class
  229. should never erase a worker except during shutdown processing. */
  230. void pkgAcquire::RunFds(fd_set *RSet,fd_set *WSet)
  231. {
  232. for (Worker *I = Workers; I != 0; I = I->NextAcquire)
  233. {
  234. if (I->InFd >= 0 && FD_ISSET(I->InFd,RSet) != 0)
  235. I->InFdReady();
  236. if (I->OutFd >= 0 && FD_ISSET(I->OutFd,WSet) != 0)
  237. I->OutFdReady();
  238. }
  239. }
  240. /*}}}*/
  241. // Acquire::Run - Run the fetch sequence /*{{{*/
  242. // ---------------------------------------------------------------------
  243. /* This runs the queues. It manages a select loop for all of the
  244. Worker tasks. The workers interact with the queues and items to
  245. manage the actual fetch. */
  246. bool pkgAcquire::Run()
  247. {
  248. Running = true;
  249. for (Queue *I = Queues; I != 0; I = I->Next)
  250. I->Startup();
  251. // Run till all things have been acquired
  252. while (ToFetch > 0)
  253. {
  254. fd_set RFds;
  255. fd_set WFds;
  256. int Highest = 0;
  257. FD_ZERO(&RFds);
  258. FD_ZERO(&WFds);
  259. SetFds(Highest,&RFds,&WFds);
  260. if (select(Highest+1,&RFds,&WFds,0,0) <= 0)
  261. {
  262. Running = false;
  263. return _error->Errno("select","Select has failed");
  264. }
  265. RunFds(&RFds,&WFds);
  266. if (_error->PendingError() == true)
  267. break;
  268. }
  269. for (Queue *I = Queues; I != 0; I = I->Next)
  270. I->Shutdown();
  271. Running = false;
  272. return _error->PendingError();
  273. }
  274. /*}}}*/
  275. // pkgAcquire::Bump - Called when an item is dequeued /*{{{*/
  276. // ---------------------------------------------------------------------
  277. /* This routine bumps idle queues in hopes that they will be able to fetch
  278. the dequeued item */
  279. void pkgAcquire::Bump()
  280. {
  281. }
  282. /*}}}*/
  283. // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
  284. // ---------------------------------------------------------------------
  285. /* */
  286. pkgAcquire::MethodConfig::MethodConfig()
  287. {
  288. SingleInstance = false;
  289. PreScan = false;
  290. Pipeline = false;
  291. SendConfig = false;
  292. Next = 0;
  293. }
  294. /*}}}*/
  295. // Queue::Queue - Constructor /*{{{*/
  296. // ---------------------------------------------------------------------
  297. /* */
  298. pkgAcquire::Queue::Queue(string Name,pkgAcquire *Owner) : Name(Name),
  299. Owner(Owner)
  300. {
  301. Items = 0;
  302. Next = 0;
  303. Workers = 0;
  304. }
  305. /*}}}*/
  306. // Queue::~Queue - Destructor /*{{{*/
  307. // ---------------------------------------------------------------------
  308. /* */
  309. pkgAcquire::Queue::~Queue()
  310. {
  311. Shutdown();
  312. while (Items != 0)
  313. {
  314. QItem *Jnk = Items;
  315. Items = Items->Next;
  316. delete Jnk;
  317. }
  318. }
  319. /*}}}*/
  320. // Queue::Enqueue - Queue an item to the queue /*{{{*/
  321. // ---------------------------------------------------------------------
  322. /* */
  323. void pkgAcquire::Queue::Enqueue(Item *Owner,string URI,string Description)
  324. {
  325. // Create a new item
  326. QItem *I = new QItem;
  327. I->Next = Items;
  328. Items = I;
  329. // Fill it in
  330. Items->Owner = Owner;
  331. Items->URI = URI;
  332. Items->Description = Description;
  333. Owner->QueueCounter++;
  334. if (Items->Next == 0)
  335. Cycle();
  336. }
  337. /*}}}*/
  338. // Queue::Dequeue - Remove an item from the queue /*{{{*/
  339. // ---------------------------------------------------------------------
  340. /* */
  341. void pkgAcquire::Queue::Dequeue(Item *Owner)
  342. {
  343. QItem **I = &Items;
  344. for (; *I != 0;)
  345. {
  346. if ((*I)->Owner == Owner)
  347. {
  348. QItem *Jnk= *I;
  349. *I = (*I)->Next;
  350. Owner->QueueCounter--;
  351. delete Jnk;
  352. }
  353. else
  354. I = &(*I)->Next;
  355. }
  356. }
  357. /*}}}*/
  358. // Queue::Startup - Start the worker processes /*{{{*/
  359. // ---------------------------------------------------------------------
  360. /* */
  361. bool pkgAcquire::Queue::Startup()
  362. {
  363. Shutdown();
  364. URI U(Name);
  365. pkgAcquire::MethodConfig *Cnf = Owner->GetConfig(U.Access);
  366. if (Cnf == 0)
  367. return false;
  368. Workers = new Worker(this,Cnf);
  369. Owner->Add(Workers);
  370. if (Workers->Start() == false)
  371. return false;
  372. return Cycle();
  373. }
  374. /*}}}*/
  375. // Queue::Shutdown - Shutdown the worker processes /*{{{*/
  376. // ---------------------------------------------------------------------
  377. /* */
  378. bool pkgAcquire::Queue::Shutdown()
  379. {
  380. // Delete all of the workers
  381. while (Workers != 0)
  382. {
  383. pkgAcquire::Worker *Jnk = Workers;
  384. Workers = Workers->NextQueue;
  385. Owner->Remove(Jnk);
  386. delete Jnk;
  387. }
  388. return true;
  389. }
  390. /*}}}*/
  391. // Queue::Finditem - Find a URI in the item list /*{{{*/
  392. // ---------------------------------------------------------------------
  393. /* */
  394. pkgAcquire::Queue::QItem *pkgAcquire::Queue::FindItem(string URI,pkgAcquire::Worker *Owner)
  395. {
  396. for (QItem *I = Items; I != 0; I = I->Next)
  397. if (I->URI == URI && I->Worker == Owner)
  398. return I;
  399. return 0;
  400. }
  401. /*}}}*/
  402. // Queue::ItemDone - Item has been completed /*{{{*/
  403. // ---------------------------------------------------------------------
  404. /* The worker signals this which causes the item to be removed from the
  405. queue. If this is the last queue instance then it is removed from the
  406. main queue too.*/
  407. bool pkgAcquire::Queue::ItemDone(QItem *Itm)
  408. {
  409. if (Itm->Owner->QueueCounter <= 1)
  410. Owner->Dequeue(Itm->Owner);
  411. else
  412. {
  413. Dequeue(Itm->Owner);
  414. Owner->Bump();
  415. }
  416. return Cycle();
  417. }
  418. /*}}}*/
  419. // Queue::Cycle - Queue new items into the method /*{{{*/
  420. // ---------------------------------------------------------------------
  421. /* This locates a new idle item and sends it to the worker */
  422. bool pkgAcquire::Queue::Cycle()
  423. {
  424. if (Items == 0 || Workers == 0)
  425. return true;
  426. // Look for a queable item
  427. QItem *I = Items;
  428. for (; I != 0; I = I->Next)
  429. if (I->Owner->Status == pkgAcquire::Item::StatIdle)
  430. break;
  431. // Nothing to do, queue is idle.
  432. if (I == 0)
  433. return true;
  434. I->Worker = Workers;
  435. I->Owner->Status = pkgAcquire::Item::StatFetching;
  436. return Workers->QueueItem(I);
  437. }
  438. /*}}}*/