packagemanager.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: packagemanager.cc,v 1.12 1999/01/31 06:24:46 jgg Exp $
  4. /* ######################################################################
  5. Package Manager - Abstacts the package manager
  6. More work is needed in the area of transitioning provides, ie exim
  7. replacing smail. This can cause interesing side effects.
  8. Other cases involving conflicts+replaces should be tested.
  9. ##################################################################### */
  10. /*}}}*/
  11. // Include Files /*{{{*/
  12. #ifdef __GNUG__
  13. #pragma implementation "apt-pkg/packagemanager.h"
  14. #endif
  15. #include <apt-pkg/packagemanager.h>
  16. #include <apt-pkg/orderlist.h>
  17. #include <apt-pkg/depcache.h>
  18. #include <apt-pkg/error.h>
  19. #include <apt-pkg/version.h>
  20. #include <apt-pkg/acquire-item.h>
  21. #include <apt-pkg/algorithms.h>
  22. #include <apt-pkg/configuration.h>
  23. /*}}}*/
  24. // PM::PackageManager - Constructor /*{{{*/
  25. // ---------------------------------------------------------------------
  26. /* */
  27. pkgPackageManager::pkgPackageManager(pkgDepCache &Cache) : Cache(Cache)
  28. {
  29. FileNames = new string[Cache.Head().PackageCount];
  30. List = 0;
  31. Debug = _config->FindB("Debug::pkgPackageManager",false);
  32. }
  33. /*}}}*/
  34. // PM::PackageManager - Destructor /*{{{*/
  35. // ---------------------------------------------------------------------
  36. /* */
  37. pkgPackageManager::~pkgPackageManager()
  38. {
  39. delete List;
  40. delete [] FileNames;
  41. }
  42. /*}}}*/
  43. // PM::GetArchives - Queue the archives for download /*{{{*/
  44. // ---------------------------------------------------------------------
  45. /* */
  46. bool pkgPackageManager::GetArchives(pkgAcquire *Owner,pkgSourceList *Sources,
  47. pkgRecords *Recs)
  48. {
  49. if (CreateOrderList() == false)
  50. return false;
  51. if (List->OrderUnpack() == false)
  52. return _error->Error("Internal ordering error");
  53. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  54. {
  55. PkgIterator Pkg(Cache,*I);
  56. // Skip packages to erase
  57. if (Cache[Pkg].Delete() == true)
  58. continue;
  59. // Skip Packages that need configure only.
  60. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure)
  61. continue;
  62. new pkgAcqArchive(Owner,Sources,Recs,Cache[Pkg].InstVerIter(Cache),
  63. FileNames[Pkg->ID]);
  64. }
  65. return true;
  66. }
  67. /*}}}*/
  68. // PM::FixMissing - Keep all missing packages /*{{{*/
  69. // ---------------------------------------------------------------------
  70. /* This is called to correct the installation when packages could not
  71. be downloaded. */
  72. bool pkgPackageManager::FixMissing()
  73. {
  74. pkgProblemResolver Resolve(Cache);
  75. for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  76. {
  77. if (Cache[I].Keep() == true)
  78. continue;
  79. if (FileNames[I->ID].empty() == false || Cache[I].Delete() == true)
  80. continue;
  81. Cache.MarkKeep(I);
  82. }
  83. // Now downgrade everything that is broken
  84. return Resolve.ResolveByKeep() == true && Cache.BrokenCount() == 0;
  85. }
  86. /*}}}*/
  87. // PM::CreateOrderList - Create the ordering class /*{{{*/
  88. // ---------------------------------------------------------------------
  89. /* This populates the ordering list with all the packages that are
  90. going to change. */
  91. bool pkgPackageManager::CreateOrderList()
  92. {
  93. delete List;
  94. List = new pkgOrderList(Cache);
  95. bool NoImmConfigure = _config->FindB("APT::Immediate-Configure",false);
  96. // Generate the list of affected packages and sort it
  97. for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  98. {
  99. // Mark the package for immediate configuration
  100. if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential &&
  101. NoImmConfigure == false)
  102. {
  103. List->Flag(I,pkgOrderList::Immediate);
  104. // Look for other packages to make immediate configurea
  105. if (Cache[I].InstallVer != 0)
  106. for (DepIterator D = Cache[I].InstVerIter(Cache).DependsList();
  107. D.end() == false; D++)
  108. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  109. List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
  110. // And again with the current version.
  111. if (I->CurrentVer != 0)
  112. for (DepIterator D = I.CurrentVer().DependsList();
  113. D.end() == false; D++)
  114. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  115. List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
  116. }
  117. // Not interesting
  118. if ((Cache[I].Keep() == true ||
  119. Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
  120. I.State() == pkgCache::PkgIterator::NeedsNothing)
  121. continue;
  122. // Append it to the list
  123. List->push_back(I);
  124. if ((I->Flags & pkgCache::Flag::ImmediateConf) == pkgCache::Flag::ImmediateConf)
  125. List->Flag(I,pkgOrderList::Immediate);
  126. }
  127. return true;
  128. }
  129. /*}}}*/
  130. // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
  131. // ---------------------------------------------------------------------
  132. /* The restriction on provides is to eliminate the case when provides
  133. are transitioning between valid states [ie exim to smail] */
  134. bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
  135. {
  136. if (D.TargetPkg()->ProvidesList != 0)
  137. return false;
  138. if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
  139. (Cache[D] & pkgDepCache::DepNow) != 0)
  140. return true;
  141. return false;
  142. }
  143. /*}}}*/
  144. // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
  145. // ---------------------------------------------------------------------
  146. /* This looks over the reverses for a conflicts line that needs early
  147. removal. */
  148. bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
  149. const char *Ver)
  150. {
  151. for (;D.end() == false; D++)
  152. {
  153. if (D->Type != pkgCache::Dep::Conflicts)
  154. continue;
  155. if (D.ParentPkg() == Pkg)
  156. continue;
  157. if (pkgCheckDep(D.TargetVer(),Ver,D->CompareOp) == false)
  158. continue;
  159. if (List->IsNow(Pkg) == false)
  160. continue;
  161. if (EarlyRemove(D.ParentPkg()) == false)
  162. return false;
  163. }
  164. return true;
  165. }
  166. /*}}}*/
  167. // PM::ConfigureAll - Run the all out configuration /*{{{*/
  168. // ---------------------------------------------------------------------
  169. /* This configures every package. It is assumed they are all unpacked and
  170. that the final configuration is valid. */
  171. bool pkgPackageManager::ConfigureAll()
  172. {
  173. pkgOrderList OList(Cache);
  174. // Populate the order list
  175. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  176. if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
  177. pkgOrderList::UnPacked) == true)
  178. OList.push_back(*I);
  179. if (OList.OrderConfigure() == false)
  180. return false;
  181. // Perform the configuring
  182. for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
  183. {
  184. PkgIterator Pkg(Cache,*I);
  185. if (Configure(Pkg) == false)
  186. return false;
  187. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  188. }
  189. return true;
  190. }
  191. /*}}}*/
  192. // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
  193. // ---------------------------------------------------------------------
  194. /* This routine scheduals the configuration of the given package and all
  195. of it's dependents. */
  196. bool pkgPackageManager::SmartConfigure(PkgIterator Pkg)
  197. {
  198. pkgOrderList OList(Cache);
  199. if (DepAdd(OList,Pkg) == false)
  200. return false;
  201. if (OList.OrderConfigure() == false)
  202. return false;
  203. // Perform the configuring
  204. for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
  205. {
  206. PkgIterator Pkg(Cache,*I);
  207. if (Configure(Pkg) == false)
  208. return false;
  209. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  210. }
  211. // Sanity Check
  212. if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
  213. return _error->Error("Internal error, could not immediate configure %s",Pkg.Name());
  214. return true;
  215. }
  216. /*}}}*/
  217. // PM::DepAdd - Add all dependents to the oder list /*{{{*/
  218. // ---------------------------------------------------------------------
  219. /* This recursively adds all dependents to the order list */
  220. bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
  221. {
  222. if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
  223. return true;
  224. if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
  225. return true;
  226. if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
  227. return false;
  228. // Put the package on the list
  229. OList.push_back(Pkg);
  230. OList.Flag(Pkg,pkgOrderList::Added);
  231. Depth++;
  232. // Check the dependencies to see if they are all satisfied.
  233. bool Bad = false;
  234. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
  235. {
  236. if (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)
  237. {
  238. D++;
  239. continue;
  240. }
  241. // Grok or groups
  242. Bad = true;
  243. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  244. {
  245. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  246. if (Bad == false)
  247. continue;
  248. Version **VList = D.AllTargets();
  249. for (Version **I = VList; *I != 0 && Bad == true; I++)
  250. {
  251. VerIterator Ver(Cache,*I);
  252. PkgIterator Pkg = Ver.ParentPkg();
  253. // See if the current version is ok
  254. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  255. Pkg.State() == PkgIterator::NeedsNothing)
  256. {
  257. Bad = false;
  258. continue;
  259. }
  260. // Not the install version
  261. if (Cache[Pkg].InstallVer != *I ||
  262. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  263. continue;
  264. if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
  265. Bad = !DepAdd(OList,Pkg,Depth);
  266. if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
  267. Bad = false;
  268. }
  269. delete [] VList;
  270. }
  271. if (Bad == true)
  272. {
  273. OList.Flag(Pkg,0,pkgOrderList::Added);
  274. OList.pop_back();
  275. Depth--;
  276. return false;
  277. }
  278. }
  279. Depth--;
  280. return true;
  281. }
  282. /*}}}*/
  283. // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
  284. // ---------------------------------------------------------------------
  285. /* This is called to deal with conflicts arising from unpacking */
  286. bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
  287. {
  288. if (List->IsNow(Pkg) == false)
  289. return true;
  290. // Already removed it
  291. if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
  292. return true;
  293. // Woops, it will not be re-installed!
  294. if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
  295. return false;
  296. bool Res = SmartRemove(Pkg);
  297. if (Cache[Pkg].Delete() == false)
  298. List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
  299. return Res;
  300. }
  301. /*}}}*/
  302. // PM::SmartRemove - Removal Helper /*{{{*/
  303. // ---------------------------------------------------------------------
  304. /* */
  305. bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
  306. {
  307. if (List->IsNow(Pkg) == false)
  308. return true;
  309. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  310. return Remove(Pkg);
  311. }
  312. /*}}}*/
  313. // PM::SmartUnPack - Install helper /*{{{*/
  314. // ---------------------------------------------------------------------
  315. /* This performs the task of handling pre-depends. */
  316. bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
  317. {
  318. // Check if it is already unpacked
  319. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  320. Cache[Pkg].Keep() == true)
  321. {
  322. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  323. if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
  324. if (SmartConfigure(Pkg) == false)
  325. return _error->Error("Internal Error, Could not perform immediate configuraton");
  326. return true;
  327. }
  328. /* See if this packages install version has any predependencies
  329. that are not met by 'now' packages. */
  330. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
  331. D.end() == false; D++)
  332. {
  333. if (D->Type == pkgCache::Dep::PreDepends)
  334. {
  335. // Look for possible ok targets.
  336. Version **VList = D.AllTargets();
  337. bool Bad = true;
  338. for (Version **I = VList; *I != 0 && Bad == true; I++)
  339. {
  340. VerIterator Ver(Cache,*I);
  341. PkgIterator Pkg = Ver.ParentPkg();
  342. // See if the current version is ok
  343. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  344. Pkg.State() == PkgIterator::NeedsNothing)
  345. {
  346. Bad = false;
  347. continue;
  348. }
  349. }
  350. // Look for something that could be configured.
  351. for (Version **I = VList; *I != 0 && Bad == true; I++)
  352. {
  353. VerIterator Ver(Cache,*I);
  354. PkgIterator Pkg = Ver.ParentPkg();
  355. // Not the install version
  356. if (Cache[Pkg].InstallVer != *I ||
  357. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  358. continue;
  359. Bad = !SmartConfigure(Pkg);
  360. }
  361. delete [] VList;
  362. if (Bad == true)
  363. return _error->Error("Internal Error, Couldn't configure a pre-depend");
  364. continue;
  365. }
  366. if (D->Type == pkgCache::Dep::Conflicts)
  367. {
  368. /* Look for conflicts. Two packages that are both in the install
  369. state cannot conflict so we don't check.. */
  370. Version **VList = D.AllTargets();
  371. for (Version **I = VList; *I != 0; I++)
  372. {
  373. VerIterator Ver(Cache,*I);
  374. PkgIterator Pkg = Ver.ParentPkg();
  375. // See if the current version is conflicting
  376. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
  377. {
  378. if (EarlyRemove(Pkg) == false)
  379. return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
  380. }
  381. }
  382. delete [] VList;
  383. }
  384. }
  385. // Check for reverse conflicts.
  386. CheckRConflicts(Pkg,Pkg.RevDependsList(),
  387. Cache[Pkg].InstVerIter(Cache).VerStr());
  388. for (PrvIterator P = Cache[Pkg].InstVerIter(Cache).ProvidesList();
  389. P.end() == false; P++)
  390. CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
  391. if (Install(Pkg,FileNames[Pkg->ID]) == false)
  392. return false;
  393. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  394. // Perform immedate configuration of the package.
  395. if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
  396. if (SmartConfigure(Pkg) == false)
  397. return _error->Error("Internal Error, Could not perform immediate configuraton");
  398. return true;
  399. }
  400. /*}}}*/
  401. // PM::OrderInstall - Installation ordering routine /*{{{*/
  402. // ---------------------------------------------------------------------
  403. /* */
  404. bool pkgPackageManager::OrderInstall()
  405. {
  406. if (CreateOrderList() == false)
  407. return false;
  408. if (Debug == true)
  409. clog << "Begining to order" << endl;
  410. if (List->OrderUnpack() == false)
  411. return _error->Error("Internal ordering error");
  412. if (Debug == true)
  413. clog << "Done ordering" << endl;
  414. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  415. {
  416. PkgIterator Pkg(Cache,*I);
  417. // Sanity check
  418. if (Cache[Pkg].Keep() == true && Pkg.State() == pkgCache::PkgIterator::NeedsNothing)
  419. return _error->Error("Internal Error, trying to manipulate a kept package");
  420. // Perform a delete or an install
  421. if (Cache[Pkg].Delete() == true)
  422. {
  423. if (SmartRemove(Pkg) == false)
  424. return false;
  425. }
  426. else
  427. if (SmartUnPack(Pkg) == false)
  428. return false;
  429. }
  430. // Final run through the configure phase
  431. if (ConfigureAll() == false)
  432. return false;
  433. // Sanity check
  434. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  435. if (List->IsFlag(*I,pkgOrderList::Configured) == false)
  436. return _error->Error("Internal error, packages left unconfigured. %s",
  437. PkgIterator(Cache,*I).Name());
  438. return true;
  439. }
  440. /*}}}*/
  441. // PM::DoInstall - Does the installation /*{{{*/
  442. // ---------------------------------------------------------------------
  443. /* This uses the filenames in FileNames and the information in the
  444. DepCache to perform the installation of packages.*/
  445. bool pkgPackageManager::DoInstall()
  446. {
  447. return OrderInstall() && Go();
  448. }
  449. /*}}}*/