packagemanager.cc 37 KB

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