You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1030 lines
35 KiB

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