packagemanager.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: packagemanager.cc,v 1.10 1998/12/10 04:22: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. // Generate the list of affected packages and sort it
  96. for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  97. {
  98. // Mark the package for immediate configuration
  99. if ((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  100. {
  101. List->Flag(I,pkgOrderList::Immediate);
  102. // Look for other packages to make immediate configurea
  103. if (Cache[I].InstallVer != 0)
  104. for (DepIterator D = Cache[I].InstVerIter(Cache).DependsList();
  105. D.end() == false; D++)
  106. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  107. List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
  108. // And again with the current version.
  109. if (I->CurrentVer != 0)
  110. for (DepIterator D = I.CurrentVer().DependsList();
  111. D.end() == false; D++)
  112. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  113. List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
  114. }
  115. // Not interesting
  116. if ((Cache[I].Keep() == true ||
  117. Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
  118. I.State() == pkgCache::PkgIterator::NeedsNothing)
  119. continue;
  120. // Append it to the list
  121. List->push_back(I);
  122. if ((I->Flags & pkgCache::Flag::ImmediateConf) == pkgCache::Flag::ImmediateConf)
  123. List->Flag(I,pkgOrderList::Immediate);
  124. }
  125. return true;
  126. }
  127. /*}}}*/
  128. // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
  129. // ---------------------------------------------------------------------
  130. /* The restriction on provides is to eliminate the case when provides
  131. are transitioning between valid states [ie exim to smail] */
  132. bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
  133. {
  134. if (D.TargetPkg()->ProvidesList != 0)
  135. return false;
  136. if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
  137. (Cache[D] & pkgDepCache::DepNow) != 0)
  138. return true;
  139. return false;
  140. }
  141. /*}}}*/
  142. // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
  143. // ---------------------------------------------------------------------
  144. /* This looks over the reverses for a conflicts line that needs early
  145. removal. */
  146. bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
  147. const char *Ver)
  148. {
  149. for (;D.end() == false; D++)
  150. {
  151. if (D->Type != pkgCache::Dep::Conflicts)
  152. continue;
  153. if (D.ParentPkg() == Pkg)
  154. continue;
  155. if (pkgCheckDep(D.TargetVer(),Ver,D->CompareOp) == false)
  156. continue;
  157. if (List->IsNow(Pkg) == false)
  158. continue;
  159. if (EarlyRemove(D.ParentPkg()) == false)
  160. return false;
  161. }
  162. return true;
  163. }
  164. /*}}}*/
  165. // PM::ConfigureAll - Run the all out configuration /*{{{*/
  166. // ---------------------------------------------------------------------
  167. /* This configures every package. It is assumed they are all unpacked and
  168. that the final configuration is valid. */
  169. bool pkgPackageManager::ConfigureAll()
  170. {
  171. pkgOrderList OList(Cache);
  172. // Populate the order list
  173. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  174. if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
  175. pkgOrderList::UnPacked) == true)
  176. OList.push_back(*I);
  177. if (OList.OrderConfigure() == false)
  178. return false;
  179. // Perform the configuring
  180. for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
  181. {
  182. PkgIterator Pkg(Cache,*I);
  183. if (Configure(Pkg) == false)
  184. return false;
  185. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  186. }
  187. return true;
  188. }
  189. /*}}}*/
  190. // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
  191. // ---------------------------------------------------------------------
  192. /* This routine scheduals the configuration of the given package and all
  193. of it's dependents. */
  194. bool pkgPackageManager::SmartConfigure(PkgIterator Pkg)
  195. {
  196. pkgOrderList OList(Cache);
  197. if (DepAdd(OList,Pkg) == false)
  198. return false;
  199. if (OList.OrderConfigure() == false)
  200. return false;
  201. // Perform the configuring
  202. for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
  203. {
  204. PkgIterator Pkg(Cache,*I);
  205. if (Configure(Pkg) == false)
  206. return false;
  207. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  208. }
  209. // Sanity Check
  210. if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
  211. return _error->Error("Internal error, could not immediate configure %s",Pkg.Name());
  212. return true;
  213. }
  214. /*}}}*/
  215. // PM::DepAdd - Add all dependents to the oder list /*{{{*/
  216. // ---------------------------------------------------------------------
  217. /* This recursively adds all dependents to the order list */
  218. bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
  219. {
  220. if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
  221. return true;
  222. if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
  223. return true;
  224. if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
  225. return false;
  226. // Put the package on the list
  227. OList.push_back(Pkg);
  228. OList.Flag(Pkg,pkgOrderList::Added);
  229. Depth++;
  230. // Check the dependencies to see if they are all satisfied.
  231. bool Bad = false;
  232. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
  233. {
  234. if (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)
  235. {
  236. D++;
  237. continue;
  238. }
  239. // Grok or groups
  240. Bad = true;
  241. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  242. {
  243. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  244. if (Bad == false)
  245. continue;
  246. Version **VList = D.AllTargets();
  247. for (Version **I = VList; *I != 0 && Bad == true; I++)
  248. {
  249. VerIterator Ver(Cache,*I);
  250. PkgIterator Pkg = Ver.ParentPkg();
  251. // See if the current version is ok
  252. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  253. Pkg.State() == PkgIterator::NeedsNothing)
  254. {
  255. Bad = false;
  256. continue;
  257. }
  258. // Not the install version
  259. if (Cache[Pkg].InstallVer != *I ||
  260. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  261. continue;
  262. if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
  263. Bad = !DepAdd(OList,Pkg,Depth);
  264. if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
  265. Bad = false;
  266. }
  267. delete [] VList;
  268. }
  269. if (Bad == true)
  270. {
  271. OList.Flag(Pkg,0,pkgOrderList::Added);
  272. OList.pop_back();
  273. Depth--;
  274. return false;
  275. }
  276. }
  277. Depth--;
  278. return true;
  279. }
  280. /*}}}*/
  281. // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
  282. // ---------------------------------------------------------------------
  283. /* This is called to deal with conflicts arising from unpacking */
  284. bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
  285. {
  286. if (List->IsNow(Pkg) == false)
  287. return true;
  288. // Already removed it
  289. if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
  290. return true;
  291. // Woops, it will not be re-installed!
  292. if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
  293. return false;
  294. bool Res = SmartRemove(Pkg);
  295. if (Cache[Pkg].Delete() == false)
  296. List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
  297. return Res;
  298. }
  299. /*}}}*/
  300. // PM::SmartRemove - Removal Helper /*{{{*/
  301. // ---------------------------------------------------------------------
  302. /* */
  303. bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
  304. {
  305. if (List->IsNow(Pkg) == false)
  306. return true;
  307. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  308. return Remove(Pkg);
  309. }
  310. /*}}}*/
  311. // PM::SmartUnPack - Install helper /*{{{*/
  312. // ---------------------------------------------------------------------
  313. /* This performs the task of handling pre-depends. */
  314. bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
  315. {
  316. // Check if it is already unpacked
  317. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  318. Cache[Pkg].Keep() == true)
  319. {
  320. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  321. if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
  322. if (SmartConfigure(Pkg) == false)
  323. return _error->Error("Internal Error, Could not perform immediate configuraton");
  324. return true;
  325. }
  326. /* See if this packages install version has any predependencies
  327. that are not met by 'now' packages. */
  328. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
  329. D.end() == false; D++)
  330. {
  331. if (D->Type == pkgCache::Dep::PreDepends)
  332. {
  333. // Look for possible ok targets.
  334. Version **VList = D.AllTargets();
  335. bool Bad = true;
  336. for (Version **I = VList; *I != 0 && Bad == true; I++)
  337. {
  338. VerIterator Ver(Cache,*I);
  339. PkgIterator Pkg = Ver.ParentPkg();
  340. // See if the current version is ok
  341. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  342. Pkg.State() == PkgIterator::NeedsNothing)
  343. {
  344. Bad = false;
  345. continue;
  346. }
  347. }
  348. // Look for something that could be configured.
  349. for (Version **I = VList; *I != 0 && Bad == true; I++)
  350. {
  351. VerIterator Ver(Cache,*I);
  352. PkgIterator Pkg = Ver.ParentPkg();
  353. // Not the install version
  354. if (Cache[Pkg].InstallVer != *I ||
  355. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  356. continue;
  357. Bad = !SmartConfigure(Pkg);
  358. }
  359. delete [] VList;
  360. if (Bad == true)
  361. return _error->Error("Internal Error, Couldn't configure a pre-depend");
  362. continue;
  363. }
  364. if (D->Type == pkgCache::Dep::Conflicts)
  365. {
  366. /* Look for conflicts. Two packages that are both in the install
  367. state cannot conflict so we don't check.. */
  368. Version **VList = D.AllTargets();
  369. for (Version **I = VList; *I != 0; I++)
  370. {
  371. VerIterator Ver(Cache,*I);
  372. PkgIterator Pkg = Ver.ParentPkg();
  373. // See if the current version is conflicting
  374. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
  375. {
  376. if (EarlyRemove(Pkg) == false)
  377. return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
  378. }
  379. }
  380. delete [] VList;
  381. }
  382. }
  383. // Check for reverse conflicts.
  384. CheckRConflicts(Pkg,Pkg.RevDependsList(),
  385. Cache[Pkg].InstVerIter(Cache).VerStr());
  386. for (PrvIterator P = Cache[Pkg].InstVerIter(Cache).ProvidesList();
  387. P.end() == false; P++)
  388. CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
  389. if (Install(Pkg,FileNames[Pkg->ID]) == false)
  390. return false;
  391. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  392. // Perform immedate configuration of the package.
  393. if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
  394. if (SmartConfigure(Pkg) == false)
  395. return _error->Error("Internal Error, Could not perform immediate configuraton");
  396. return true;
  397. }
  398. /*}}}*/
  399. // PM::OrderInstall - Installation ordering routine /*{{{*/
  400. // ---------------------------------------------------------------------
  401. /* */
  402. bool pkgPackageManager::OrderInstall()
  403. {
  404. if (CreateOrderList() == false)
  405. return false;
  406. if (Debug == true)
  407. clog << "Begining to order" << endl;
  408. if (List->OrderUnpack() == false)
  409. return _error->Error("Internal ordering error");
  410. if (Debug == true)
  411. clog << "Done ordering" << endl;
  412. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  413. {
  414. PkgIterator Pkg(Cache,*I);
  415. // Sanity check
  416. if (Cache[Pkg].Keep() == true && Pkg.State() == pkgCache::PkgIterator::NeedsNothing)
  417. return _error->Error("Internal Error, trying to manipulate a kept package");
  418. // Perform a delete or an install
  419. if (Cache[Pkg].Delete() == true)
  420. {
  421. if (SmartRemove(Pkg) == false)
  422. return false;
  423. }
  424. else
  425. if (SmartUnPack(Pkg) == false)
  426. return false;
  427. }
  428. // Final run through the configure phase
  429. if (ConfigureAll() == false)
  430. return false;
  431. // Sanity check
  432. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  433. if (List->IsFlag(*I,pkgOrderList::Configured) == false)
  434. return _error->Error("Internal error, packages left unconfigured. %s",
  435. PkgIterator(Cache,*I).Name());
  436. return true;
  437. }
  438. /*}}}*/
  439. // PM::DoInstall - Does the installation /*{{{*/
  440. // ---------------------------------------------------------------------
  441. /* This uses the filenames in FileNames and the information in the
  442. DepCache to perform the installation of packages.*/
  443. bool pkgPackageManager::DoInstall()
  444. {
  445. return OrderInstall() && Go();
  446. }
  447. /*}}}*/