packagemanager.cc 32 KB

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