packagemanager.cc 18 KB

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