packagemanager.cc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: packagemanager.cc,v 1.30 2003/04/27 03:04:15 doogie 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. #include<config.h>
  13. #include <apt-pkg/packagemanager.h>
  14. #include <apt-pkg/orderlist.h>
  15. #include <apt-pkg/depcache.h>
  16. #include <apt-pkg/error.h>
  17. #include <apt-pkg/version.h>
  18. #include <apt-pkg/acquire-item.h>
  19. #include <apt-pkg/algorithms.h>
  20. #include <apt-pkg/configuration.h>
  21. #include <apt-pkg/sptr.h>
  22. #include <apti18n.h>
  23. #include <iostream>
  24. #include <fcntl.h>
  25. /*}}}*/
  26. using namespace std;
  27. bool pkgPackageManager::SigINTStop = false;
  28. // PM::PackageManager - Constructor /*{{{*/
  29. // ---------------------------------------------------------------------
  30. /* */
  31. pkgPackageManager::pkgPackageManager(pkgDepCache *pCache) : Cache(*pCache),
  32. List(NULL), Res(Incomplete)
  33. {
  34. FileNames = new string[Cache.Head().PackageCount];
  35. Debug = _config->FindB("Debug::pkgPackageManager",false);
  36. NoImmConfigure = !_config->FindB("APT::Immediate-Configure",true);
  37. ImmConfigureAll = _config->FindB("APT::Immediate-Configure-All",false);
  38. }
  39. /*}}}*/
  40. // PM::PackageManager - Destructor /*{{{*/
  41. // ---------------------------------------------------------------------
  42. /* */
  43. pkgPackageManager::~pkgPackageManager()
  44. {
  45. delete List;
  46. delete [] FileNames;
  47. }
  48. /*}}}*/
  49. // PM::GetArchives - Queue the archives for download /*{{{*/
  50. // ---------------------------------------------------------------------
  51. /* */
  52. bool pkgPackageManager::GetArchives(pkgAcquire *Owner,pkgSourceList *Sources,
  53. pkgRecords *Recs)
  54. {
  55. if (CreateOrderList() == false)
  56. return false;
  57. bool const ordering =
  58. _config->FindB("PackageManager::UnpackAll",true) ?
  59. List->OrderUnpack() : List->OrderCritical();
  60. if (ordering == false)
  61. return _error->Error("Internal ordering error");
  62. for (pkgOrderList::iterator I = List->begin(); I != List->end(); ++I)
  63. {
  64. PkgIterator Pkg(Cache,*I);
  65. FileNames[Pkg->ID] = string();
  66. // Skip packages to erase
  67. if (Cache[Pkg].Delete() == true)
  68. continue;
  69. // Skip Packages that need configure only.
  70. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  71. Cache[Pkg].Keep() == true)
  72. continue;
  73. // Skip already processed packages
  74. if (List->IsNow(Pkg) == false)
  75. continue;
  76. new pkgAcqArchive(Owner,Sources,Recs,Cache[Pkg].InstVerIter(Cache),
  77. FileNames[Pkg->ID]);
  78. }
  79. return true;
  80. }
  81. /*}}}*/
  82. // PM::FixMissing - Keep all missing packages /*{{{*/
  83. // ---------------------------------------------------------------------
  84. /* This is called to correct the installation when packages could not
  85. be downloaded. */
  86. bool pkgPackageManager::FixMissing()
  87. {
  88. pkgDepCache::ActionGroup group(Cache);
  89. pkgProblemResolver Resolve(&Cache);
  90. List->SetFileList(FileNames);
  91. bool Bad = false;
  92. for (PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  93. {
  94. if (List->IsMissing(I) == false)
  95. continue;
  96. // Okay, this file is missing and we need it. Mark it for keep
  97. Bad = true;
  98. Cache.MarkKeep(I, false, false);
  99. }
  100. // We have to empty the list otherwise it will not have the new changes
  101. delete List;
  102. List = 0;
  103. if (Bad == false)
  104. return true;
  105. // Now downgrade everything that is broken
  106. return Resolve.ResolveByKeep() == true && Cache.BrokenCount() == 0;
  107. }
  108. /*}}}*/
  109. // PM::ImmediateAdd - Add the immediate flag recursivly /*{{{*/
  110. // ---------------------------------------------------------------------
  111. /* This adds the immediate flag to the pkg and recursively to the
  112. dependendies
  113. */
  114. void pkgPackageManager::ImmediateAdd(PkgIterator I, bool UseInstallVer, unsigned const int &Depth)
  115. {
  116. DepIterator D;
  117. if(UseInstallVer)
  118. {
  119. if(Cache[I].InstallVer == 0)
  120. return;
  121. D = Cache[I].InstVerIter(Cache).DependsList();
  122. } else {
  123. if (I->CurrentVer == 0)
  124. return;
  125. D = I.CurrentVer().DependsList();
  126. }
  127. for ( /* nothing */ ; D.end() == false; ++D)
  128. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  129. {
  130. if(!List->IsFlag(D.TargetPkg(), pkgOrderList::Immediate))
  131. {
  132. if(Debug)
  133. clog << OutputInDepth(Depth) << "ImmediateAdd(): Adding Immediate flag to " << D.TargetPkg() << " cause of " << D.DepType() << " " << I.Name() << endl;
  134. List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
  135. ImmediateAdd(D.TargetPkg(), UseInstallVer, Depth + 1);
  136. }
  137. }
  138. return;
  139. }
  140. /*}}}*/
  141. // PM::CreateOrderList - Create the ordering class /*{{{*/
  142. // ---------------------------------------------------------------------
  143. /* This populates the ordering list with all the packages that are
  144. going to change. */
  145. bool pkgPackageManager::CreateOrderList()
  146. {
  147. if (List != 0)
  148. return true;
  149. delete List;
  150. List = new pkgOrderList(&Cache);
  151. if (Debug && ImmConfigureAll)
  152. clog << "CreateOrderList(): Adding Immediate flag for all packages because of APT::Immediate-Configure-All" << endl;
  153. // Generate the list of affected packages and sort it
  154. for (PkgIterator I = Cache.PkgBegin(); I.end() == false; ++I)
  155. {
  156. // Ignore no-version packages
  157. if (I->VersionList == 0)
  158. continue;
  159. // Mark the package and its dependends for immediate configuration
  160. if ((((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential) &&
  161. NoImmConfigure == false) || ImmConfigureAll)
  162. {
  163. if(Debug && !ImmConfigureAll)
  164. clog << "CreateOrderList(): Adding Immediate flag for " << I.Name() << endl;
  165. List->Flag(I,pkgOrderList::Immediate);
  166. if (!ImmConfigureAll) {
  167. // Look for other install packages to make immediate configurea
  168. ImmediateAdd(I, true);
  169. // And again with the current version.
  170. ImmediateAdd(I, false);
  171. }
  172. }
  173. // Not interesting
  174. if ((Cache[I].Keep() == true ||
  175. Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
  176. I.State() == pkgCache::PkgIterator::NeedsNothing &&
  177. (Cache[I].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall &&
  178. (I.Purge() != false || Cache[I].Mode != pkgDepCache::ModeDelete ||
  179. (Cache[I].iFlags & pkgDepCache::Purge) != pkgDepCache::Purge))
  180. continue;
  181. // Append it to the list
  182. List->push_back(I);
  183. }
  184. return true;
  185. }
  186. /*}}}*/
  187. // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
  188. // ---------------------------------------------------------------------
  189. /* The restriction on provides is to eliminate the case when provides
  190. are transitioning between valid states [ie exim to smail] */
  191. bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
  192. {
  193. if (D.TargetPkg()->ProvidesList != 0)
  194. return false;
  195. if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
  196. (Cache[D] & pkgDepCache::DepNow) != 0)
  197. return true;
  198. return false;
  199. }
  200. /*}}}*/
  201. // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
  202. // ---------------------------------------------------------------------
  203. /* This looks over the reverses for a conflicts line that needs early
  204. removal. */
  205. bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
  206. const char *Ver)
  207. {
  208. for (;D.end() == false; ++D)
  209. {
  210. if (D->Type != pkgCache::Dep::Conflicts &&
  211. D->Type != pkgCache::Dep::Obsoletes)
  212. continue;
  213. // The package hasnt been changed
  214. if (List->IsNow(Pkg) == false)
  215. continue;
  216. // Ignore self conflicts, ignore conflicts from irrelevent versions
  217. if (D.IsIgnorable(Pkg) || D.ParentVer() != D.ParentPkg().CurrentVer())
  218. continue;
  219. if (Cache.VS().CheckDep(Ver,D->CompareOp,D.TargetVer()) == false)
  220. continue;
  221. if (EarlyRemove(D.ParentPkg()) == false)
  222. return _error->Error("Reverse conflicts early remove for package '%s' failed",
  223. Pkg.Name());
  224. }
  225. return true;
  226. }
  227. /*}}}*/
  228. // PM::ConfigureAll - Run the all out configuration /*{{{*/
  229. // ---------------------------------------------------------------------
  230. /* This configures every package. It is assumed they are all unpacked and
  231. that the final configuration is valid. This is also used to catch packages
  232. that have not been configured when using ImmConfigureAll */
  233. bool pkgPackageManager::ConfigureAll()
  234. {
  235. pkgOrderList OList(&Cache);
  236. // Populate the order list
  237. for (pkgOrderList::iterator I = List->begin(); I != List->end(); ++I)
  238. if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
  239. pkgOrderList::UnPacked) == true)
  240. OList.push_back(*I);
  241. if (OList.OrderConfigure() == false)
  242. return false;
  243. std::string const conf = _config->Find("PackageManager::Configure","all");
  244. bool const ConfigurePkgs = (conf == "all");
  245. // Perform the configuring
  246. for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); ++I)
  247. {
  248. PkgIterator Pkg(Cache,*I);
  249. /* Check if the package has been configured, this can happen if SmartConfigure
  250. calls its self */
  251. if (List->IsFlag(Pkg,pkgOrderList::Configured)) continue;
  252. if (ConfigurePkgs == true && SmartConfigure(Pkg, 0) == false) {
  253. if (ImmConfigureAll)
  254. _error->Error(_("Could not perform immediate configuration on '%s'. "
  255. "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg.Name(),1);
  256. else
  257. _error->Error("Internal error, packages left unconfigured. %s",Pkg.Name());
  258. return false;
  259. }
  260. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  261. }
  262. return true;
  263. }
  264. /*}}}*/
  265. // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
  266. // ---------------------------------------------------------------------
  267. /* This function tries to put the system in a state where Pkg can be configured.
  268. This involves checking each of Pkg's dependanies and unpacking and
  269. configuring packages where needed.
  270. Note on failure: This method can fail, without causing any problems.
  271. This can happen when using Immediate-Configure-All, SmartUnPack may call
  272. SmartConfigure, it may fail because of a complex dependancy situation, but
  273. a error will only be reported if ConfigureAll fails. This is why some of the
  274. messages this function reports on failure (return false;) as just warnings
  275. only shown when debuging*/
  276. bool pkgPackageManager::SmartConfigure(PkgIterator Pkg, int const Depth)
  277. {
  278. // If this is true, only check and correct and dependencies without the Loop flag
  279. bool PkgLoop = List->IsFlag(Pkg,pkgOrderList::Loop);
  280. if (Debug) {
  281. VerIterator InstallVer = VerIterator(Cache,Cache[Pkg].InstallVer);
  282. clog << OutputInDepth(Depth) << "SmartConfigure " << Pkg.Name() << " (" << InstallVer.VerStr() << ")";
  283. if (PkgLoop)
  284. clog << " (Only Correct Dependencies)";
  285. clog << endl;
  286. }
  287. VerIterator const instVer = Cache[Pkg].InstVerIter(Cache);
  288. /* Because of the ordered list, most dependencies should be unpacked,
  289. however if there is a loop (A depends on B, B depends on A) this will not
  290. be the case, so check for dependencies before configuring. */
  291. bool Bad = false;
  292. for (DepIterator D = instVer.DependsList();
  293. D.end() == false; )
  294. {
  295. // Compute a single dependency element (glob or)
  296. pkgCache::DepIterator Start;
  297. pkgCache::DepIterator End;
  298. D.GlobOr(Start,End);
  299. if (End->Type == pkgCache::Dep::Depends)
  300. Bad = true;
  301. // Check for dependanices that have not been unpacked, probably due to loops.
  302. while (End->Type == pkgCache::Dep::Depends) {
  303. PkgIterator DepPkg;
  304. VerIterator InstallVer;
  305. SPtrArray<Version *> VList = Start.AllTargets();
  306. // Check through each version of each package that could satisfy this dependancy
  307. for (Version **I = VList; *I != 0; I++) {
  308. VerIterator Ver(Cache,*I);
  309. DepPkg = Ver.ParentPkg();
  310. InstallVer = VerIterator(Cache,Cache[DepPkg].InstallVer);
  311. // Check if the current version of the package is avalible and will satisfy this dependancy
  312. if (DepPkg.CurrentVer() == Ver && List->IsNow(DepPkg) == true &&
  313. !List->IsFlag(DepPkg,pkgOrderList::Removed) && DepPkg.State() == PkgIterator::NeedsNothing)
  314. {
  315. Bad = false;
  316. break;
  317. }
  318. // Check if the version that is going to be installed will satisfy the dependancy
  319. if (Cache[DepPkg].InstallVer == *I) {
  320. if (List->IsFlag(DepPkg,pkgOrderList::UnPacked)) {
  321. if (List->IsFlag(DepPkg,pkgOrderList::Loop) && PkgLoop) {
  322. // This dependancy has already been dealt with by another SmartConfigure on Pkg
  323. Bad = false;
  324. break;
  325. } else if (List->IsFlag(Pkg,pkgOrderList::Loop)) {
  326. /* Check for a loop to prevent one forming
  327. If A depends on B and B depends on A, SmartConfigure will
  328. just hop between them if this is not checked. Dont remove the
  329. loop flag after finishing however as loop is already set.
  330. This means that there is another SmartConfigure call for this
  331. package and it will remove the loop flag */
  332. Bad = !SmartConfigure(DepPkg, Depth + 1);
  333. } else {
  334. /* Check for a loop to prevent one forming
  335. If A depends on B and B depends on A, SmartConfigure will
  336. just hop between them if this is not checked */
  337. List->Flag(Pkg,pkgOrderList::Loop);
  338. Bad = !SmartConfigure(DepPkg, Depth + 1);
  339. List->RmFlag(Pkg,pkgOrderList::Loop);
  340. }
  341. // If SmartConfigure was succesfull, Bad is false, so break
  342. if (!Bad) break;
  343. } else if (List->IsFlag(DepPkg,pkgOrderList::Configured)) {
  344. Bad = false;
  345. break;
  346. }
  347. }
  348. }
  349. /* If the dependany is still not satisfied, try, if possible, unpacking a package to satisfy it */
  350. if (InstallVer != 0 && Bad) {
  351. if (List->IsNow(DepPkg)) {
  352. Bad = false;
  353. if (List->IsFlag(Pkg,pkgOrderList::Loop))
  354. {
  355. if (Debug)
  356. std::clog << OutputInDepth(Depth) << "Package " << Pkg << " loops in SmartConfigure" << std::endl;
  357. }
  358. else
  359. {
  360. List->Flag(Pkg,pkgOrderList::Loop);
  361. if (Debug)
  362. cout << OutputInDepth(Depth) << "Unpacking " << DepPkg.Name() << " to avoid loop" << endl;
  363. SmartUnPack(DepPkg, true, Depth + 1);
  364. List->RmFlag(Pkg,pkgOrderList::Loop);
  365. }
  366. }
  367. }
  368. if (Start==End) {
  369. if (Bad && Debug && List->IsFlag(DepPkg,pkgOrderList::Loop) == false)
  370. std::clog << OutputInDepth(Depth) << "Could not satisfy " << Start << std::endl;
  371. break;
  372. } else {
  373. Start++;
  374. }
  375. }
  376. }
  377. if (Bad) {
  378. if (Debug)
  379. _error->Warning(_("Could not configure '%s'. "),Pkg.Name());
  380. return false;
  381. }
  382. if (PkgLoop) return true;
  383. static std::string const conf = _config->Find("PackageManager::Configure","all");
  384. static bool const ConfigurePkgs = (conf == "all" || conf == "smart");
  385. if (List->IsFlag(Pkg,pkgOrderList::Configured))
  386. return _error->Error("Internal configure error on '%s'.", Pkg.Name());
  387. if (ConfigurePkgs == true && Configure(Pkg) == false)
  388. return false;
  389. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  390. if ((Cache[Pkg].InstVerIter(Cache)->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
  391. for (PkgIterator P = Pkg.Group().PackageList();
  392. P.end() == false; P = Pkg.Group().NextPkg(P))
  393. {
  394. if (Pkg == P || List->IsFlag(P,pkgOrderList::Configured) == true ||
  395. Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
  396. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
  397. continue;
  398. SmartConfigure(P, (Depth +1));
  399. }
  400. // Sanity Check
  401. if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
  402. return _error->Error(_("Could not configure '%s'. "),Pkg.Name());
  403. return true;
  404. }
  405. /*}}}*/
  406. // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
  407. // ---------------------------------------------------------------------
  408. /* This is called to deal with conflicts arising from unpacking */
  409. bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
  410. {
  411. if (List->IsNow(Pkg) == false)
  412. return true;
  413. // Already removed it
  414. if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
  415. return true;
  416. // Woops, it will not be re-installed!
  417. if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
  418. return false;
  419. // Essential packages get special treatment
  420. bool IsEssential = false;
  421. if ((Pkg->Flags & pkgCache::Flag::Essential) != 0 ||
  422. (Pkg->Flags & pkgCache::Flag::Important) != 0)
  423. IsEssential = true;
  424. /* Check for packages that are the dependents of essential packages and
  425. promote them too */
  426. if (Pkg->CurrentVer != 0)
  427. {
  428. for (DepIterator D = Pkg.RevDependsList(); D.end() == false &&
  429. IsEssential == false; ++D)
  430. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  431. if ((D.ParentPkg()->Flags & pkgCache::Flag::Essential) != 0 ||
  432. (D.ParentPkg()->Flags & pkgCache::Flag::Important) != 0)
  433. IsEssential = true;
  434. }
  435. if (IsEssential == true)
  436. {
  437. if (_config->FindB("APT::Force-LoopBreak",false) == false)
  438. return _error->Error(_("This installation run will require temporarily "
  439. "removing the essential package %s due to a "
  440. "Conflicts/Pre-Depends loop. This is often bad, "
  441. "but if you really want to do it, activate the "
  442. "APT::Force-LoopBreak option."),Pkg.Name());
  443. }
  444. bool Res = SmartRemove(Pkg);
  445. if (Cache[Pkg].Delete() == false)
  446. List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
  447. return Res;
  448. }
  449. /*}}}*/
  450. // PM::SmartRemove - Removal Helper /*{{{*/
  451. // ---------------------------------------------------------------------
  452. /* */
  453. bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
  454. {
  455. if (List->IsNow(Pkg) == false)
  456. return true;
  457. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  458. return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
  459. }
  460. /*}}}*/
  461. // PM::SmartUnPack - Install helper /*{{{*/
  462. // ---------------------------------------------------------------------
  463. /* This puts the system in a state where it can Unpack Pkg, if Pkg is allready
  464. unpacked, or when it has been unpacked, if Immediate==true it configures it. */
  465. bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
  466. {
  467. return SmartUnPack(Pkg, true, 0);
  468. }
  469. bool pkgPackageManager::SmartUnPack(PkgIterator Pkg, bool const Immediate, int const Depth)
  470. {
  471. bool PkgLoop = List->IsFlag(Pkg,pkgOrderList::Loop);
  472. if (Debug) {
  473. clog << OutputInDepth(Depth) << "SmartUnPack " << Pkg.Name();
  474. VerIterator InstallVer = VerIterator(Cache,Cache[Pkg].InstallVer);
  475. if (Pkg.CurrentVer() == 0)
  476. cout << " (install version " << InstallVer.VerStr() << ")";
  477. else
  478. cout << " (replace version " << Pkg.CurrentVer().VerStr() << " with " << InstallVer.VerStr() << ")";
  479. if (PkgLoop)
  480. cout << " (Only Perform PreUnpack Checks)";
  481. cout << endl;
  482. }
  483. VerIterator const instVer = Cache[Pkg].InstVerIter(Cache);
  484. /* PreUnpack Checks: This loop checks and attempts to rectify and problems that would prevent the package being unpacked.
  485. It addresses: PreDepends, Conflicts, Obsoletes and Breaks (DpkgBreaks). Any resolutions that do not require it should
  486. avoid configuration (calling SmartUnpack with Immediate=true), this is because when unpacking some packages with
  487. complex dependancy structures, trying to configure some packages while breaking the loops can complicate things .
  488. This will be either dealt with if the package is configured as a dependency of Pkg (if and when Pkg is configured),
  489. or by the ConfigureAll call at the end of the for loop in OrderInstall. */
  490. for (DepIterator D = instVer.DependsList();
  491. D.end() == false; )
  492. {
  493. // Compute a single dependency element (glob or)
  494. pkgCache::DepIterator Start;
  495. pkgCache::DepIterator End;
  496. D.GlobOr(Start,End);
  497. while (End->Type == pkgCache::Dep::PreDepends)
  498. {
  499. if (Debug)
  500. clog << OutputInDepth(Depth) << "PreDepends order for " << Pkg.Name() << std::endl;
  501. // Look for possible ok targets.
  502. SPtrArray<Version *> VList = Start.AllTargets();
  503. bool Bad = true;
  504. for (Version **I = VList; *I != 0 && Bad == true; I++)
  505. {
  506. VerIterator Ver(Cache,*I);
  507. PkgIterator Pkg = Ver.ParentPkg();
  508. // See if the current version is ok
  509. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  510. Pkg.State() == PkgIterator::NeedsNothing)
  511. {
  512. Bad = false;
  513. if (Debug)
  514. clog << OutputInDepth(Depth) << "Found ok package " << Pkg.Name() << endl;
  515. continue;
  516. }
  517. }
  518. // Look for something that could be configured.
  519. for (Version **I = VList; *I != 0 && Bad == true; I++)
  520. {
  521. VerIterator Ver(Cache,*I);
  522. PkgIterator Pkg = Ver.ParentPkg();
  523. // Not the install version
  524. if (Cache[Pkg].InstallVer != *I ||
  525. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  526. continue;
  527. if (List->IsFlag(Pkg,pkgOrderList::Configured)) {
  528. Bad = false;
  529. continue;
  530. }
  531. // check if it needs unpack or if if configure is enough
  532. if (!List->IsFlag(Pkg,pkgOrderList::UnPacked))
  533. {
  534. if (Debug)
  535. clog << OutputInDepth(Depth) << "Trying to SmartUnpack " << Pkg.Name() << endl;
  536. // SmartUnpack with the ImmediateFlag to ensure its really ready
  537. Bad = !SmartUnPack(Pkg, true, Depth + 1);
  538. } else {
  539. if (Debug)
  540. clog << OutputInDepth(Depth) << "Trying to SmartConfigure " << Pkg.Name() << endl;
  541. Bad = !SmartConfigure(Pkg, Depth + 1);
  542. }
  543. }
  544. /* If this or element did not match then continue on to the
  545. next or element until a matching element is found */
  546. if (Bad == true)
  547. {
  548. // This triggers if someone make a pre-depends/depend loop.
  549. if (Start == End)
  550. return _error->Error("Couldn't configure pre-depend %s for %s, "
  551. "probably a dependency cycle.",
  552. End.TargetPkg().Name(),Pkg.Name());
  553. ++Start;
  554. }
  555. else
  556. break;
  557. }
  558. if (End->Type == pkgCache::Dep::Conflicts ||
  559. End->Type == pkgCache::Dep::Obsoletes)
  560. {
  561. /* Look for conflicts. Two packages that are both in the install
  562. state cannot conflict so we don't check.. */
  563. SPtrArray<Version *> VList = End.AllTargets();
  564. for (Version **I = VList; *I != 0; I++)
  565. {
  566. VerIterator Ver(Cache,*I);
  567. PkgIterator ConflictPkg = Ver.ParentPkg();
  568. VerIterator InstallVer(Cache,Cache[ConflictPkg].InstallVer);
  569. // See if the current version is conflicting
  570. if (ConflictPkg.CurrentVer() == Ver && List->IsNow(ConflictPkg))
  571. {
  572. cout << OutputInDepth(Depth) << Pkg.Name() << " conflicts with " << ConflictPkg.Name() << endl;
  573. /* If a loop is not present or has not yet been detected, attempt to unpack packages
  574. to resolve this conflict. If there is a loop present, remove packages to resolve this conflict */
  575. if (!List->IsFlag(ConflictPkg,pkgOrderList::Loop)) {
  576. if (Cache[ConflictPkg].Keep() == 0 && Cache[ConflictPkg].InstallVer != 0) {
  577. if (Debug)
  578. cout << OutputInDepth(Depth) << OutputInDepth(Depth) << "Unpacking " << ConflictPkg.Name() << " to prevent conflict" << endl;
  579. List->Flag(Pkg,pkgOrderList::Loop);
  580. SmartUnPack(ConflictPkg,false, Depth + 1);
  581. // Remove loop to allow it to be used later if needed
  582. List->RmFlag(Pkg,pkgOrderList::Loop);
  583. } else {
  584. if (EarlyRemove(ConflictPkg) == false)
  585. return _error->Error("Internal Error, Could not early remove %s",ConflictPkg.Name());
  586. }
  587. } else {
  588. if (!List->IsFlag(ConflictPkg,pkgOrderList::Removed)) {
  589. if (Debug)
  590. cout << OutputInDepth(Depth) << "Because of conficts knot, removing " << ConflictPkg.Name() << " to conflict violation" << endl;
  591. if (EarlyRemove(ConflictPkg) == false)
  592. return _error->Error("Internal Error, Could not early remove %s",ConflictPkg.Name());
  593. }
  594. }
  595. }
  596. }
  597. }
  598. // Check for breaks
  599. if (End->Type == pkgCache::Dep::DpkgBreaks) {
  600. SPtrArray<Version *> VList = End.AllTargets();
  601. for (Version **I = VList; *I != 0; I++)
  602. {
  603. VerIterator Ver(Cache,*I);
  604. PkgIterator BrokenPkg = Ver.ParentPkg();
  605. if (BrokenPkg.CurrentVer() != Ver)
  606. {
  607. if (Debug)
  608. std::clog << OutputInDepth(Depth) << " Ignore not-installed version " << Ver.VerStr() << " of " << Pkg.FullName() << " for " << End << std::endl;
  609. continue;
  610. }
  611. // Check if it needs to be unpacked
  612. if (List->IsFlag(BrokenPkg,pkgOrderList::InList) && Cache[BrokenPkg].Delete() == false &&
  613. List->IsNow(BrokenPkg)) {
  614. if (List->IsFlag(BrokenPkg,pkgOrderList::Loop) && PkgLoop) {
  615. // This dependancy has already been dealt with by another SmartUnPack on Pkg
  616. break;
  617. } else {
  618. // Found a break, so see if we can unpack the package to avoid it
  619. // but do not set loop if another SmartUnPack already deals with it
  620. VerIterator InstallVer(Cache,Cache[BrokenPkg].InstallVer);
  621. bool circle = false;
  622. for (pkgCache::DepIterator D = InstallVer.DependsList(); D.end() == false; ++D)
  623. {
  624. if (D->Type != pkgCache::Dep::PreDepends)
  625. continue;
  626. SPtrArray<Version *> VL = D.AllTargets();
  627. for (Version **I = VL; *I != 0; ++I)
  628. {
  629. VerIterator V(Cache,*I);
  630. PkgIterator P = V.ParentPkg();
  631. // we are checking for installation as an easy 'protection' against or-groups and (unchosen) providers
  632. if (P->CurrentVer == 0 || P != Pkg || (P.CurrentVer() != V && Cache[P].InstallVer != V))
  633. continue;
  634. circle = true;
  635. break;
  636. }
  637. if (circle == true)
  638. break;
  639. }
  640. if (circle == true)
  641. {
  642. if (Debug)
  643. cout << OutputInDepth(Depth) << " Avoiding " << End << " avoided as " << BrokenPkg.FullName() << " has a pre-depends on " << Pkg.FullName() << std::endl;
  644. continue;
  645. }
  646. else
  647. {
  648. if (Debug)
  649. {
  650. cout << OutputInDepth(Depth) << " Unpacking " << BrokenPkg.FullName() << " to avoid " << End;
  651. if (PkgLoop == true)
  652. cout << " (Looping)";
  653. cout << std::endl;
  654. }
  655. if (PkgLoop == false)
  656. List->Flag(Pkg,pkgOrderList::Loop);
  657. SmartUnPack(BrokenPkg, false, Depth + 1);
  658. if (PkgLoop == false)
  659. List->RmFlag(Pkg,pkgOrderList::Loop);
  660. }
  661. }
  662. } else {
  663. // Check if a package needs to be removed
  664. if (Cache[BrokenPkg].Delete() == true && !List->IsFlag(BrokenPkg,pkgOrderList::Configured))
  665. {
  666. if (Debug)
  667. cout << OutputInDepth(Depth) << " Removing " << BrokenPkg.Name() << " to avoid " << End << endl;
  668. SmartRemove(BrokenPkg);
  669. }
  670. }
  671. }
  672. }
  673. }
  674. // Check for reverse conflicts.
  675. if (CheckRConflicts(Pkg,Pkg.RevDependsList(),
  676. instVer.VerStr()) == false)
  677. return false;
  678. for (PrvIterator P = instVer.ProvidesList();
  679. P.end() == false; ++P)
  680. if (Pkg->Group != P.OwnerPkg()->Group)
  681. CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
  682. if (PkgLoop)
  683. return true;
  684. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  685. if (Immediate == true && (instVer->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
  686. {
  687. /* Do lockstep M-A:same unpacking in two phases:
  688. First unpack all installed architectures, then the not installed.
  689. This way we avoid that M-A: enabled packages are installed before
  690. their older non-M-A enabled packages are replaced by newer versions */
  691. bool const installed = Pkg->CurrentVer != 0;
  692. if (installed == true && Install(Pkg,FileNames[Pkg->ID]) == false)
  693. return false;
  694. for (PkgIterator P = Pkg.Group().PackageList();
  695. P.end() == false; P = Pkg.Group().NextPkg(P))
  696. {
  697. if (P->CurrentVer == 0 || P == Pkg || List->IsFlag(P,pkgOrderList::UnPacked) == true ||
  698. Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
  699. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
  700. continue;
  701. if (SmartUnPack(P, false, Depth + 1) == false)
  702. return false;
  703. }
  704. if (installed == false && Install(Pkg,FileNames[Pkg->ID]) == false)
  705. return false;
  706. for (PkgIterator P = Pkg.Group().PackageList();
  707. P.end() == false; P = Pkg.Group().NextPkg(P))
  708. {
  709. if (P->CurrentVer != 0 || P == Pkg || List->IsFlag(P,pkgOrderList::UnPacked) == true ||
  710. Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
  711. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
  712. continue;
  713. if (SmartUnPack(P, false, Depth + 1) == false)
  714. return false;
  715. }
  716. }
  717. // packages which are already unpacked don't need to be unpacked again
  718. else if (Pkg.State() != pkgCache::PkgIterator::NeedsConfigure && Install(Pkg,FileNames[Pkg->ID]) == false)
  719. return false;
  720. if (Immediate == true) {
  721. // Perform immedate configuration of the package.
  722. if (SmartConfigure(Pkg, Depth + 1) == false)
  723. _error->Warning(_("Could not perform immediate configuration on '%s'. "
  724. "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg.Name(),2);
  725. }
  726. return true;
  727. }
  728. /*}}}*/
  729. // PM::OrderInstall - Installation ordering routine /*{{{*/
  730. // ---------------------------------------------------------------------
  731. /* */
  732. pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
  733. {
  734. if (CreateOrderList() == false)
  735. return Failed;
  736. Reset();
  737. if (Debug == true)
  738. clog << "Beginning to order" << endl;
  739. bool const ordering =
  740. _config->FindB("PackageManager::UnpackAll",true) ?
  741. List->OrderUnpack(FileNames) : List->OrderCritical();
  742. if (ordering == false)
  743. {
  744. _error->Error("Internal ordering error");
  745. return Failed;
  746. }
  747. if (Debug == true)
  748. clog << "Done ordering" << endl;
  749. bool DoneSomething = false;
  750. for (pkgOrderList::iterator I = List->begin(); I != List->end(); ++I)
  751. {
  752. PkgIterator Pkg(Cache,*I);
  753. if (List->IsNow(Pkg) == false)
  754. {
  755. if (!List->IsFlag(Pkg,pkgOrderList::Configured) && !NoImmConfigure) {
  756. if (SmartConfigure(Pkg, 0) == false && Debug)
  757. _error->Warning("Internal Error, Could not configure %s",Pkg.Name());
  758. // FIXME: The above warning message might need changing
  759. } else {
  760. if (Debug == true)
  761. clog << "Skipping already done " << Pkg.Name() << endl;
  762. }
  763. continue;
  764. }
  765. if (List->IsMissing(Pkg) == true)
  766. {
  767. if (Debug == true)
  768. clog << "Sequence completed at " << Pkg.Name() << endl;
  769. if (DoneSomething == false)
  770. {
  771. _error->Error("Internal Error, ordering was unable to handle the media swap");
  772. return Failed;
  773. }
  774. return Incomplete;
  775. }
  776. // Sanity check
  777. if (Cache[Pkg].Keep() == true &&
  778. Pkg.State() == pkgCache::PkgIterator::NeedsNothing &&
  779. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall)
  780. {
  781. _error->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg.Name());
  782. return Failed;
  783. }
  784. // Perform a delete or an install
  785. if (Cache[Pkg].Delete() == true)
  786. {
  787. if (SmartRemove(Pkg) == false)
  788. return Failed;
  789. }
  790. else
  791. if (SmartUnPack(Pkg,List->IsFlag(Pkg,pkgOrderList::Immediate),0) == false)
  792. return Failed;
  793. DoneSomething = true;
  794. if (ImmConfigureAll) {
  795. /* ConfigureAll here to pick up and packages left unconfigured becuase they were unpacked in the
  796. "PreUnpack Checks" section */
  797. if (!ConfigureAll())
  798. return Failed;
  799. }
  800. }
  801. // Final run through the configure phase
  802. if (ConfigureAll() == false)
  803. return Failed;
  804. // Sanity check
  805. for (pkgOrderList::iterator I = List->begin(); I != List->end(); ++I)
  806. {
  807. if (List->IsFlag(*I,pkgOrderList::Configured) == false)
  808. {
  809. _error->Error("Internal error, packages left unconfigured. %s",
  810. PkgIterator(Cache,*I).Name());
  811. return Failed;
  812. }
  813. }
  814. return Completed;
  815. }
  816. /*}}}*/
  817. // PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
  818. // ---------------------------------------------------------------------
  819. pkgPackageManager::OrderResult
  820. pkgPackageManager::DoInstallPostFork(int statusFd)
  821. {
  822. if(statusFd > 0)
  823. // FIXME: use SetCloseExec here once it taught about throwing
  824. // exceptions instead of doing _exit(100) on failure
  825. fcntl(statusFd,F_SETFD,FD_CLOEXEC);
  826. bool goResult = Go(statusFd);
  827. if(goResult == false)
  828. return Failed;
  829. return Res;
  830. };
  831. // PM::DoInstall - Does the installation /*{{{*/
  832. // ---------------------------------------------------------------------
  833. /* This uses the filenames in FileNames and the information in the
  834. DepCache to perform the installation of packages.*/
  835. pkgPackageManager::OrderResult pkgPackageManager::DoInstall(int statusFd)
  836. {
  837. if(DoInstallPreFork() == Failed)
  838. return Failed;
  839. return DoInstallPostFork(statusFd);
  840. }
  841. /*}}}*/