packagemanager.cc 14 KB

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