packagemanager.cc 37 KB

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