packagemanager.cc 15 KB

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