packagemanager.cc 15 KB

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