packagemanager.cc 16 KB

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