packagemanager.cc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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) == false)
  345. return false;
  346. Bad = false;
  347. if (List->IsFlag(DepPkg,pkgOrderList::Loop) == false)
  348. Changed = true;
  349. if (PkgLoop == false)
  350. List->RmFlag(Pkg,pkgOrderList::Loop);
  351. if (Bad == false)
  352. break;
  353. }
  354. }
  355. if (Cur == End || Bad == false)
  356. break;
  357. }
  358. if (Bad == false)
  359. continue;
  360. needConfigure.push_back(Start);
  361. }
  362. if (i++ > max_loops)
  363. return _error->Error("Internal error: MaxLoopCount reached in SmartUnPack (1) for %s, aborting", Pkg.FullName().c_str());
  364. } while (Changed == true);
  365. Bad = false, Changed = false, i = 0;
  366. do
  367. {
  368. Changed = false;
  369. for (std::list<DepIterator>::const_iterator D = needConfigure.begin(); D != needConfigure.end(); ++D)
  370. {
  371. // Compute a single dependency element (glob or) without modifying D
  372. pkgCache::DepIterator Start, End;
  373. {
  374. pkgCache::DepIterator Discard = *D;
  375. Discard.GlobOr(Start,End);
  376. }
  377. if (End->Type != pkgCache::Dep::Depends)
  378. continue;
  379. Bad = true;
  380. // Search for dependencies which are unpacked but aren't configured yet (maybe loops)
  381. for (DepIterator Cur = Start; true; ++Cur)
  382. {
  383. SPtrArray<Version *> VList = Cur.AllTargets();
  384. for (Version **I = VList; *I != 0; ++I)
  385. {
  386. VerIterator Ver(Cache,*I);
  387. PkgIterator DepPkg = Ver.ParentPkg();
  388. // Check if the version that is going to be installed will satisfy the dependency
  389. if (Cache[DepPkg].InstallVer != *I)
  390. continue;
  391. if (List->IsFlag(DepPkg,pkgOrderList::UnPacked))
  392. {
  393. if (List->IsFlag(DepPkg,pkgOrderList::Loop) && PkgLoop)
  394. {
  395. // This dependency has already been dealt with by another SmartConfigure on Pkg
  396. Bad = false;
  397. break;
  398. }
  399. /* Check for a loop to prevent one forming
  400. If A depends on B and B depends on A, SmartConfigure will
  401. just hop between them if this is not checked. Dont remove the
  402. loop flag after finishing however as loop is already set.
  403. This means that there is another SmartConfigure call for this
  404. package and it will remove the loop flag */
  405. if (PkgLoop == false)
  406. List->Flag(Pkg,pkgOrderList::Loop);
  407. if (SmartConfigure(DepPkg, Depth + 1) == false)
  408. return false;
  409. Bad = false;
  410. if (List->IsFlag(DepPkg,pkgOrderList::Loop) == false)
  411. Changed = true;
  412. if (PkgLoop == false)
  413. List->RmFlag(Pkg,pkgOrderList::Loop);
  414. break;
  415. }
  416. else if (List->IsFlag(DepPkg,pkgOrderList::Configured))
  417. {
  418. Bad = false;
  419. break;
  420. }
  421. }
  422. if (Cur == End || Bad == false)
  423. break;
  424. }
  425. if (Bad == true && Changed == false && Debug == true)
  426. std::clog << OutputInDepth(Depth) << "Could not satisfy " << *D << std::endl;
  427. }
  428. if (i++ > max_loops)
  429. return _error->Error("Internal error: MaxLoopCount reached in SmartUnPack (2) for %s, aborting", Pkg.FullName().c_str());
  430. } while (Changed == true);
  431. if (Bad == true)
  432. return _error->Error(_("Could not configure '%s'. "),Pkg.FullName().c_str());
  433. if (PkgLoop) return true;
  434. static std::string const conf = _config->Find("PackageManager::Configure","all");
  435. static bool const ConfigurePkgs = (conf == "all" || conf == "smart");
  436. if (List->IsFlag(Pkg,pkgOrderList::Configured))
  437. return _error->Error("Internal configure error on '%s'.", Pkg.FullName().c_str());
  438. if (ConfigurePkgs == true && Configure(Pkg) == false)
  439. return false;
  440. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  441. if ((Cache[Pkg].InstVerIter(Cache)->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
  442. for (PkgIterator P = Pkg.Group().PackageList();
  443. P.end() == false; P = Pkg.Group().NextPkg(P))
  444. {
  445. if (Pkg == P || List->IsFlag(P,pkgOrderList::Configured) == true ||
  446. List->IsFlag(P,pkgOrderList::UnPacked) == false ||
  447. Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
  448. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
  449. continue;
  450. if (SmartConfigure(P, (Depth +1)) == false)
  451. return false;
  452. }
  453. // Sanity Check
  454. if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
  455. return _error->Error(_("Could not configure '%s'. "),Pkg.FullName().c_str());
  456. return true;
  457. }
  458. /*}}}*/
  459. // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
  460. // ---------------------------------------------------------------------
  461. /* This is called to deal with conflicts arising from unpacking */
  462. bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
  463. {
  464. if (List->IsNow(Pkg) == false)
  465. return true;
  466. // Already removed it
  467. if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
  468. return true;
  469. // Woops, it will not be re-installed!
  470. if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
  471. return false;
  472. // Essential packages get special treatment
  473. bool IsEssential = false;
  474. if ((Pkg->Flags & pkgCache::Flag::Essential) != 0 ||
  475. (Pkg->Flags & pkgCache::Flag::Important) != 0)
  476. IsEssential = true;
  477. /* Check for packages that are the dependents of essential packages and
  478. promote them too */
  479. if (Pkg->CurrentVer != 0)
  480. {
  481. for (DepIterator D = Pkg.RevDependsList(); D.end() == false &&
  482. IsEssential == false; ++D)
  483. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  484. if ((D.ParentPkg()->Flags & pkgCache::Flag::Essential) != 0 ||
  485. (D.ParentPkg()->Flags & pkgCache::Flag::Important) != 0)
  486. IsEssential = true;
  487. }
  488. if (IsEssential == true)
  489. {
  490. if (_config->FindB("APT::Force-LoopBreak",false) == false)
  491. return _error->Error(_("This installation run will require temporarily "
  492. "removing the essential package %s due to a "
  493. "Conflicts/Pre-Depends loop. This is often bad, "
  494. "but if you really want to do it, activate the "
  495. "APT::Force-LoopBreak option."),Pkg.FullName().c_str());
  496. }
  497. bool Res = SmartRemove(Pkg);
  498. if (Cache[Pkg].Delete() == false)
  499. List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
  500. return Res;
  501. }
  502. /*}}}*/
  503. // PM::SmartRemove - Removal Helper /*{{{*/
  504. // ---------------------------------------------------------------------
  505. /* */
  506. bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
  507. {
  508. if (List->IsNow(Pkg) == false)
  509. return true;
  510. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  511. return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
  512. }
  513. /*}}}*/
  514. // PM::SmartUnPack - Install helper /*{{{*/
  515. // ---------------------------------------------------------------------
  516. /* This puts the system in a state where it can Unpack Pkg, if Pkg is already
  517. unpacked, or when it has been unpacked, if Immediate==true it configures it. */
  518. bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
  519. {
  520. return SmartUnPack(Pkg, true, 0);
  521. }
  522. bool pkgPackageManager::SmartUnPack(PkgIterator Pkg, bool const Immediate, int const Depth)
  523. {
  524. bool PkgLoop = List->IsFlag(Pkg,pkgOrderList::Loop);
  525. if (Debug) {
  526. clog << OutputInDepth(Depth) << "SmartUnPack " << Pkg.FullName();
  527. VerIterator InstallVer = VerIterator(Cache,Cache[Pkg].InstallVer);
  528. if (Pkg.CurrentVer() == 0)
  529. clog << " (install version " << InstallVer.VerStr() << ")";
  530. else
  531. clog << " (replace version " << Pkg.CurrentVer().VerStr() << " with " << InstallVer.VerStr() << ")";
  532. if (PkgLoop)
  533. clog << " (Only Perform PreUnpack Checks)";
  534. if (Immediate)
  535. clog << " immediately";
  536. clog << endl;
  537. }
  538. VerIterator const instVer = Cache[Pkg].InstVerIter(Cache);
  539. /* PreUnpack Checks: This loop checks and attempts to rectify and problems that would prevent the package being unpacked.
  540. It addresses: PreDepends, Conflicts, Obsoletes and Breaks (DpkgBreaks). Any resolutions that do not require it should
  541. avoid configuration (calling SmartUnpack with Immediate=true), this is because when unpacking some packages with
  542. complex dependency structures, trying to configure some packages while breaking the loops can complicate things .
  543. This will be either dealt with if the package is configured as a dependency of Pkg (if and when Pkg is configured),
  544. or by the ConfigureAll call at the end of the for loop in OrderInstall. */
  545. bool Changed = false;
  546. const unsigned int max_loops = _config->FindI("APT::pkgPackageManager::MaxLoopCount", 5000);
  547. unsigned int i = 0;
  548. do
  549. {
  550. Changed = false;
  551. for (DepIterator D = instVer.DependsList(); D.end() == false; )
  552. {
  553. // Compute a single dependency element (glob or)
  554. pkgCache::DepIterator Start, End;
  555. D.GlobOr(Start,End);
  556. if (End->Type == pkgCache::Dep::PreDepends)
  557. {
  558. bool Bad = true;
  559. if (Debug)
  560. clog << OutputInDepth(Depth) << "PreDepends order for " << Pkg.FullName() << std::endl;
  561. // Look for easy targets: packages that are already okay
  562. for (DepIterator Cur = Start; Bad == true; ++Cur)
  563. {
  564. SPtrArray<Version *> VList = Cur.AllTargets();
  565. for (Version **I = VList; *I != 0; ++I)
  566. {
  567. VerIterator Ver(Cache,*I);
  568. PkgIterator Pkg = Ver.ParentPkg();
  569. // See if the current version is ok
  570. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  571. Pkg.State() == PkgIterator::NeedsNothing)
  572. {
  573. Bad = false;
  574. if (Debug)
  575. clog << OutputInDepth(Depth) << "Found ok package " << Pkg.FullName() << endl;
  576. break;
  577. }
  578. }
  579. if (Cur == End)
  580. break;
  581. }
  582. // Look for something that could be configured.
  583. for (DepIterator Cur = Start; Bad == true && Cur.end() == false; ++Cur)
  584. {
  585. SPtrArray<Version *> VList = Cur.AllTargets();
  586. for (Version **I = VList; *I != 0; ++I)
  587. {
  588. VerIterator Ver(Cache,*I);
  589. PkgIterator Pkg = Ver.ParentPkg();
  590. // Not the install version
  591. if (Cache[Pkg].InstallVer != *I ||
  592. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  593. continue;
  594. if (List->IsFlag(Pkg,pkgOrderList::Configured))
  595. {
  596. Bad = false;
  597. break;
  598. }
  599. // check if it needs unpack or if if configure is enough
  600. if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
  601. {
  602. if (Debug)
  603. clog << OutputInDepth(Depth) << "Trying to SmartUnpack " << Pkg.FullName() << endl;
  604. // SmartUnpack with the ImmediateFlag to ensure its really ready
  605. if (SmartUnPack(Pkg, true, Depth + 1) == false)
  606. return false;
  607. Bad = false;
  608. if (List->IsFlag(Pkg,pkgOrderList::Loop) == false)
  609. Changed = true;
  610. break;
  611. }
  612. else
  613. {
  614. if (Debug)
  615. clog << OutputInDepth(Depth) << "Trying to SmartConfigure " << Pkg.FullName() << endl;
  616. if (SmartConfigure(Pkg, Depth + 1) == false)
  617. return false;
  618. Bad = false;
  619. if (List->IsFlag(Pkg,pkgOrderList::Loop) == false)
  620. Changed = true;
  621. break;
  622. }
  623. }
  624. }
  625. if (Bad == true)
  626. {
  627. if (Start == End)
  628. return _error->Error("Couldn't configure pre-depend %s for %s, "
  629. "probably a dependency cycle.",
  630. End.TargetPkg().FullName().c_str(),Pkg.FullName().c_str());
  631. }
  632. else
  633. continue;
  634. }
  635. else if (End->Type == pkgCache::Dep::Conflicts ||
  636. End->Type == pkgCache::Dep::Obsoletes)
  637. {
  638. /* Look for conflicts. Two packages that are both in the install
  639. state cannot conflict so we don't check.. */
  640. SPtrArray<Version *> VList = End.AllTargets();
  641. for (Version **I = VList; *I != 0; I++)
  642. {
  643. VerIterator Ver(Cache,*I);
  644. PkgIterator ConflictPkg = Ver.ParentPkg();
  645. VerIterator InstallVer(Cache,Cache[ConflictPkg].InstallVer);
  646. // See if the current version is conflicting
  647. if (ConflictPkg.CurrentVer() == Ver && List->IsNow(ConflictPkg))
  648. {
  649. if (Debug)
  650. clog << OutputInDepth(Depth) << Pkg.FullName() << " conflicts with " << ConflictPkg.FullName() << endl;
  651. /* If a loop is not present or has not yet been detected, attempt to unpack packages
  652. to resolve this conflict. If there is a loop present, remove packages to resolve this conflict */
  653. if (List->IsFlag(ConflictPkg,pkgOrderList::Loop) == false)
  654. {
  655. if (Cache[ConflictPkg].Keep() == 0 && Cache[ConflictPkg].InstallVer != 0)
  656. {
  657. if (Debug)
  658. clog << OutputInDepth(Depth) << "Unpacking " << ConflictPkg.FullName() << " to prevent conflict" << endl;
  659. List->Flag(Pkg,pkgOrderList::Loop);
  660. if (SmartUnPack(ConflictPkg,false, Depth + 1) == false)
  661. return false;
  662. if (List->IsFlag(ConflictPkg,pkgOrderList::Loop) == false)
  663. Changed = true;
  664. // Remove loop to allow it to be used later if needed
  665. List->RmFlag(Pkg,pkgOrderList::Loop);
  666. }
  667. else if (EarlyRemove(ConflictPkg) == false)
  668. return _error->Error("Internal Error, Could not early remove %s (1)",ConflictPkg.FullName().c_str());
  669. }
  670. else if (List->IsFlag(ConflictPkg,pkgOrderList::Removed) == false)
  671. {
  672. if (Debug)
  673. clog << OutputInDepth(Depth) << "Because of conflict knot, removing " << ConflictPkg.FullName() << " temporarily" << endl;
  674. if (EarlyRemove(ConflictPkg) == false)
  675. return _error->Error("Internal Error, Could not early remove %s (2)",ConflictPkg.FullName().c_str());
  676. }
  677. }
  678. }
  679. }
  680. else if (End->Type == pkgCache::Dep::DpkgBreaks)
  681. {
  682. SPtrArray<Version *> VList = End.AllTargets();
  683. for (Version **I = VList; *I != 0; ++I)
  684. {
  685. VerIterator Ver(Cache,*I);
  686. PkgIterator BrokenPkg = Ver.ParentPkg();
  687. if (BrokenPkg.CurrentVer() != Ver)
  688. {
  689. if (Debug)
  690. std::clog << OutputInDepth(Depth) << " Ignore not-installed version " << Ver.VerStr() << " of " << Pkg.FullName() << " for " << End << std::endl;
  691. continue;
  692. }
  693. // Check if it needs to be unpacked
  694. if (List->IsFlag(BrokenPkg,pkgOrderList::InList) && Cache[BrokenPkg].Delete() == false &&
  695. List->IsNow(BrokenPkg))
  696. {
  697. if (List->IsFlag(BrokenPkg,pkgOrderList::Loop) && PkgLoop)
  698. {
  699. // This dependency has already been dealt with by another SmartUnPack on Pkg
  700. break;
  701. }
  702. else
  703. {
  704. // Found a break, so see if we can unpack the package to avoid it
  705. // but do not set loop if another SmartUnPack already deals with it
  706. // Also, avoid it if the package we would unpack pre-depends on this one
  707. VerIterator InstallVer(Cache,Cache[BrokenPkg].InstallVer);
  708. bool circle = false;
  709. for (pkgCache::DepIterator D = InstallVer.DependsList(); D.end() == false; ++D)
  710. {
  711. if (D->Type != pkgCache::Dep::PreDepends)
  712. continue;
  713. SPtrArray<Version *> VL = D.AllTargets();
  714. for (Version **I = VL; *I != 0; ++I)
  715. {
  716. VerIterator V(Cache,*I);
  717. PkgIterator P = V.ParentPkg();
  718. // we are checking for installation as an easy 'protection' against or-groups and (unchosen) providers
  719. if (P != Pkg || (P.CurrentVer() != V && Cache[P].InstallVer != V))
  720. continue;
  721. circle = true;
  722. break;
  723. }
  724. if (circle == true)
  725. break;
  726. }
  727. if (circle == true)
  728. {
  729. if (Debug)
  730. clog << OutputInDepth(Depth) << " Avoiding " << End << " avoided as " << BrokenPkg.FullName() << " has a pre-depends on " << Pkg.FullName() << std::endl;
  731. continue;
  732. }
  733. else
  734. {
  735. if (Debug)
  736. {
  737. clog << OutputInDepth(Depth) << " Unpacking " << BrokenPkg.FullName() << " to avoid " << End;
  738. if (PkgLoop == true)
  739. clog << " (Looping)";
  740. clog << std::endl;
  741. }
  742. if (PkgLoop == false)
  743. List->Flag(Pkg,pkgOrderList::Loop);
  744. if (SmartUnPack(BrokenPkg, false, Depth + 1) == false)
  745. return false;
  746. if (List->IsFlag(BrokenPkg,pkgOrderList::Loop) == false)
  747. Changed = true;
  748. if (PkgLoop == false)
  749. List->RmFlag(Pkg,pkgOrderList::Loop);
  750. }
  751. }
  752. }
  753. // Check if a package needs to be removed
  754. else if (Cache[BrokenPkg].Delete() == true && List->IsFlag(BrokenPkg,pkgOrderList::Configured) == false)
  755. {
  756. if (Debug)
  757. clog << OutputInDepth(Depth) << " Removing " << BrokenPkg.FullName() << " to avoid " << End << endl;
  758. if (SmartRemove(BrokenPkg) == false)
  759. return false;
  760. }
  761. }
  762. }
  763. }
  764. if (i++ > max_loops)
  765. return _error->Error("Internal error: APT::pkgPackageManager::MaxLoopCount reached in SmartConfigure for %s, aborting", Pkg.FullName().c_str());
  766. } while (Changed == true);
  767. // Check for reverse conflicts.
  768. if (CheckRConflicts(Pkg,Pkg.RevDependsList(),
  769. instVer.VerStr()) == false)
  770. return false;
  771. for (PrvIterator P = instVer.ProvidesList();
  772. P.end() == false; ++P)
  773. if (Pkg->Group != P.OwnerPkg()->Group)
  774. CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
  775. if (PkgLoop)
  776. return true;
  777. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  778. if (Immediate == true && (instVer->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
  779. {
  780. /* Do lockstep M-A:same unpacking in two phases:
  781. First unpack all installed architectures, then the not installed.
  782. This way we avoid that M-A: enabled packages are installed before
  783. their older non-M-A enabled packages are replaced by newer versions */
  784. bool const installed = Pkg->CurrentVer != 0;
  785. if (installed == true &&
  786. (instVer != Pkg.CurrentVer() ||
  787. ((Cache[Pkg].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)) &&
  788. Install(Pkg,FileNames[Pkg->ID]) == false)
  789. return false;
  790. for (PkgIterator P = Pkg.Group().PackageList();
  791. P.end() == false; P = Pkg.Group().NextPkg(P))
  792. {
  793. if (P->CurrentVer == 0 || P == Pkg || List->IsFlag(P,pkgOrderList::UnPacked) == true ||
  794. Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
  795. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
  796. continue;
  797. if (SmartUnPack(P, false, Depth + 1) == false)
  798. return false;
  799. }
  800. if (installed == false && Install(Pkg,FileNames[Pkg->ID]) == false)
  801. return false;
  802. for (PkgIterator P = Pkg.Group().PackageList();
  803. P.end() == false; P = Pkg.Group().NextPkg(P))
  804. {
  805. if (P->CurrentVer != 0 || P == Pkg || List->IsFlag(P,pkgOrderList::UnPacked) == true ||
  806. List->IsFlag(P,pkgOrderList::Configured) == true ||
  807. Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
  808. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
  809. continue;
  810. if (SmartUnPack(P, false, Depth + 1) == false)
  811. return false;
  812. }
  813. }
  814. // packages which are already unpacked don't need to be unpacked again
  815. else if ((instVer != Pkg.CurrentVer() ||
  816. ((Cache[Pkg].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)) &&
  817. Install(Pkg,FileNames[Pkg->ID]) == false)
  818. return false;
  819. if (Immediate == true) {
  820. // Perform immedate configuration of the package.
  821. if (SmartConfigure(Pkg, Depth + 1) == false)
  822. _error->Warning(_("Could not perform immediate configuration on '%s'. "
  823. "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg.FullName().c_str(),2);
  824. }
  825. return true;
  826. }
  827. /*}}}*/
  828. // PM::OrderInstall - Installation ordering routine /*{{{*/
  829. // ---------------------------------------------------------------------
  830. /* */
  831. pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
  832. {
  833. if (CreateOrderList() == false)
  834. return Failed;
  835. Reset();
  836. if (Debug == true)
  837. clog << "Beginning to order" << endl;
  838. bool const ordering =
  839. _config->FindB("PackageManager::UnpackAll",true) ?
  840. List->OrderUnpack(FileNames) : List->OrderCritical();
  841. if (ordering == false)
  842. {
  843. _error->Error("Internal ordering error");
  844. return Failed;
  845. }
  846. if (Debug == true)
  847. clog << "Done ordering" << endl;
  848. bool DoneSomething = false;
  849. for (pkgOrderList::iterator I = List->begin(); I != List->end(); ++I)
  850. {
  851. PkgIterator Pkg(Cache,*I);
  852. if (List->IsNow(Pkg) == false)
  853. {
  854. if (Debug == true)
  855. clog << "Skipping already done " << Pkg.FullName() << endl;
  856. continue;
  857. }
  858. if (List->IsMissing(Pkg) == true)
  859. {
  860. if (Debug == true)
  861. clog << "Sequence completed at " << Pkg.FullName() << endl;
  862. if (DoneSomething == false)
  863. {
  864. _error->Error("Internal Error, ordering was unable to handle the media swap");
  865. return Failed;
  866. }
  867. return Incomplete;
  868. }
  869. // Sanity check
  870. if (Cache[Pkg].Keep() == true &&
  871. Pkg.State() == pkgCache::PkgIterator::NeedsNothing &&
  872. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall)
  873. {
  874. _error->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg.FullName().c_str());
  875. return Failed;
  876. }
  877. // Perform a delete or an install
  878. if (Cache[Pkg].Delete() == true)
  879. {
  880. if (SmartRemove(Pkg) == false)
  881. return Failed;
  882. }
  883. else
  884. if (SmartUnPack(Pkg,List->IsFlag(Pkg,pkgOrderList::Immediate),0) == false)
  885. return Failed;
  886. DoneSomething = true;
  887. if (ImmConfigureAll) {
  888. /* ConfigureAll here to pick up and packages left unconfigured because they were unpacked in the
  889. "PreUnpack Checks" section */
  890. if (!ConfigureAll())
  891. return Failed;
  892. }
  893. }
  894. // Final run through the configure phase
  895. if (ConfigureAll() == false)
  896. return Failed;
  897. // Sanity check
  898. for (pkgOrderList::iterator I = List->begin(); I != List->end(); ++I)
  899. {
  900. if (List->IsFlag(*I,pkgOrderList::Configured) == false)
  901. {
  902. _error->Error("Internal error, packages left unconfigured. %s",
  903. PkgIterator(Cache,*I).FullName().c_str());
  904. return Failed;
  905. }
  906. }
  907. return Completed;
  908. }
  909. // PM::DoInstallPostFork - compat /*{{{*/
  910. // ---------------------------------------------------------------------
  911. /*}}}*/
  912. #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
  913. pkgPackageManager::OrderResult
  914. pkgPackageManager::DoInstallPostFork(int statusFd)
  915. {
  916. APT::Progress::PackageManager *progress = new
  917. APT::Progress::PackageManagerProgressFd(statusFd);
  918. pkgPackageManager::OrderResult res = DoInstallPostFork(progress);
  919. delete progress;
  920. return res;
  921. }
  922. /*}}}*/
  923. // PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
  924. // ---------------------------------------------------------------------
  925. pkgPackageManager::OrderResult
  926. pkgPackageManager::DoInstallPostFork(APT::Progress::PackageManager *progress)
  927. {
  928. bool goResult = Go(progress);
  929. if(goResult == false)
  930. return Failed;
  931. return Res;
  932. };
  933. #else
  934. pkgPackageManager::OrderResult
  935. pkgPackageManager::DoInstallPostFork(int statusFd)
  936. {
  937. bool goResult = Go(statusFd);
  938. if(goResult == false)
  939. return Failed;
  940. return Res;
  941. }
  942. #endif
  943. /*}}}*/
  944. // PM::DoInstall - Does the installation /*{{{*/
  945. // ---------------------------------------------------------------------
  946. /* compat */
  947. #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
  948. pkgPackageManager::OrderResult
  949. pkgPackageManager::DoInstall(int statusFd)
  950. {
  951. APT::Progress::PackageManager *progress = new
  952. APT::Progress::PackageManagerProgressFd(statusFd);
  953. OrderResult res = DoInstall(progress);
  954. delete progress;
  955. return res;
  956. }
  957. #else
  958. pkgPackageManager::OrderResult pkgPackageManager::DoInstall(int statusFd)
  959. {
  960. if(DoInstallPreFork() == Failed)
  961. return Failed;
  962. return DoInstallPostFork(statusFd);
  963. }
  964. #endif
  965. /*}}}*/
  966. // PM::DoInstall - Does the installation /*{{{*/
  967. // ---------------------------------------------------------------------
  968. /* This uses the filenames in FileNames and the information in the
  969. DepCache to perform the installation of packages.*/
  970. #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
  971. pkgPackageManager::OrderResult
  972. pkgPackageManager::DoInstall(APT::Progress::PackageManager *progress)
  973. {
  974. if(DoInstallPreFork() == Failed)
  975. return Failed;
  976. return DoInstallPostFork(progress);
  977. }
  978. #endif
  979. /*}}}*/