packagemanager.cc 37 KB

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