packagemanager.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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 <apt-pkg/packagemanager.h>
  13. #include <apt-pkg/orderlist.h>
  14. #include <apt-pkg/depcache.h>
  15. #include <apt-pkg/error.h>
  16. #include <apt-pkg/version.h>
  17. #include <apt-pkg/acquire-item.h>
  18. #include <apt-pkg/algorithms.h>
  19. #include <apt-pkg/configuration.h>
  20. #include <apt-pkg/sptr.h>
  21. #include <apti18n.h>
  22. #include <iostream>
  23. #include <fcntl.h>
  24. /*}}}*/
  25. using namespace std;
  26. bool pkgPackageManager::SigINTStop = false;
  27. // PM::PackageManager - Constructor /*{{{*/
  28. // ---------------------------------------------------------------------
  29. /* */
  30. pkgPackageManager::pkgPackageManager(pkgDepCache *pCache) : Cache(*pCache)
  31. {
  32. FileNames = new string[Cache.Head().PackageCount];
  33. List = 0;
  34. Debug = _config->FindB("Debug::pkgPackageManager",false);
  35. }
  36. /*}}}*/
  37. // PM::PackageManager - Destructor /*{{{*/
  38. // ---------------------------------------------------------------------
  39. /* */
  40. pkgPackageManager::~pkgPackageManager()
  41. {
  42. delete List;
  43. delete [] FileNames;
  44. }
  45. /*}}}*/
  46. // PM::GetArchives - Queue the archives for download /*{{{*/
  47. // ---------------------------------------------------------------------
  48. /* */
  49. bool pkgPackageManager::GetArchives(pkgAcquire *Owner,pkgSourceList *Sources,
  50. pkgRecords *Recs)
  51. {
  52. if (CreateOrderList() == false)
  53. return false;
  54. bool const ordering =
  55. _config->FindB("PackageManager::UnpackAll",true) ?
  56. List->OrderUnpack() : List->OrderCritical();
  57. if (ordering == false)
  58. return _error->Error("Internal ordering error");
  59. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  60. {
  61. PkgIterator Pkg(Cache,*I);
  62. FileNames[Pkg->ID] = string();
  63. // Skip packages to erase
  64. if (Cache[Pkg].Delete() == true)
  65. continue;
  66. // Skip Packages that need configure only.
  67. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  68. Cache[Pkg].Keep() == true)
  69. continue;
  70. // Skip already processed packages
  71. if (List->IsNow(Pkg) == false)
  72. continue;
  73. new pkgAcqArchive(Owner,Sources,Recs,Cache[Pkg].InstVerIter(Cache),
  74. FileNames[Pkg->ID]);
  75. }
  76. return true;
  77. }
  78. /*}}}*/
  79. // PM::FixMissing - Keep all missing packages /*{{{*/
  80. // ---------------------------------------------------------------------
  81. /* This is called to correct the installation when packages could not
  82. be downloaded. */
  83. bool pkgPackageManager::FixMissing()
  84. {
  85. pkgDepCache::ActionGroup group(Cache);
  86. pkgProblemResolver Resolve(&Cache);
  87. List->SetFileList(FileNames);
  88. bool Bad = false;
  89. for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  90. {
  91. if (List->IsMissing(I) == false)
  92. continue;
  93. // Okay, this file is missing and we need it. Mark it for keep
  94. Bad = true;
  95. Cache.MarkKeep(I, false, false);
  96. }
  97. // We have to empty the list otherwise it will not have the new changes
  98. delete List;
  99. List = 0;
  100. if (Bad == false)
  101. return true;
  102. // Now downgrade everything that is broken
  103. return Resolve.ResolveByKeep() == true && Cache.BrokenCount() == 0;
  104. }
  105. /*}}}*/
  106. // PM::ImmediateAdd - Add the immediate flag recursivly /*{{{*/
  107. // ---------------------------------------------------------------------
  108. /* This adds the immediate flag to the pkg and recursively to the
  109. dependendies
  110. */
  111. void pkgPackageManager::ImmediateAdd(PkgIterator I, bool UseInstallVer, unsigned const int &Depth)
  112. {
  113. DepIterator D;
  114. if(UseInstallVer)
  115. {
  116. if(Cache[I].InstallVer == 0)
  117. return;
  118. D = Cache[I].InstVerIter(Cache).DependsList();
  119. } else {
  120. if (I->CurrentVer == 0)
  121. return;
  122. D = I.CurrentVer().DependsList();
  123. }
  124. for ( /* nothing */ ; D.end() == false; D++)
  125. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  126. {
  127. if(!List->IsFlag(D.TargetPkg(), pkgOrderList::Immediate))
  128. {
  129. if(Debug)
  130. clog << OutputInDepth(Depth) << "ImmediateAdd(): Adding Immediate flag to " << D.TargetPkg() << " cause of " << D.DepType() << " " << I.Name() << endl;
  131. List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
  132. ImmediateAdd(D.TargetPkg(), UseInstallVer, Depth + 1);
  133. }
  134. }
  135. return;
  136. }
  137. /*}}}*/
  138. // PM::CreateOrderList - Create the ordering class /*{{{*/
  139. // ---------------------------------------------------------------------
  140. /* This populates the ordering list with all the packages that are
  141. going to change. */
  142. bool pkgPackageManager::CreateOrderList()
  143. {
  144. if (List != 0)
  145. return true;
  146. delete List;
  147. List = new pkgOrderList(&Cache);
  148. NoImmConfigure = !_config->FindB("APT::Immediate-Configure",true);
  149. ImmConfigureAll = _config->FindB("APT::Immediate-Configure-All",false);
  150. if (Debug && ImmConfigureAll)
  151. clog << "CreateOrderList(): Adding Immediate flag for all packages because of APT::Immediate-Configure-All" << endl;
  152. // Generate the list of affected packages and sort it
  153. for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  154. {
  155. // Ignore no-version packages
  156. if (I->VersionList == 0)
  157. continue;
  158. // Mark the package and its dependends for immediate configuration
  159. if ((((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential ||
  160. (I->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important) &&
  161. NoImmConfigure == false) || ImmConfigureAll)
  162. {
  163. if(Debug && !ImmConfigureAll)
  164. clog << "CreateOrderList(): Adding Immediate flag for " << I.Name() << 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.ParentPkg() == 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.Name());
  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) == 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.Name(),1);
  256. else
  257. _error->Error("Internal error, packages left unconfigured. %s",Pkg.Name());
  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)
  277. {
  278. if (Debug)
  279. clog << "SmartConfigure " << Pkg.Name() << endl;
  280. VerIterator const instVer = Cache[Pkg].InstVerIter(Cache);
  281. /* Because of the ordered list, most dependancies should be unpacked,
  282. however if there is a loop (A depends on B, B depends on A) this will not
  283. be the case, so check for dependancies before configuring. */
  284. bool Bad = false;
  285. for (DepIterator D = instVer.DependsList();
  286. D.end() == false; )
  287. {
  288. // Compute a single dependency element (glob or)
  289. pkgCache::DepIterator Start;
  290. pkgCache::DepIterator End;
  291. D.GlobOr(Start,End);
  292. if (End->Type == pkgCache::Dep::Depends)
  293. Bad = true;
  294. // Check for dependanices that have not been unpacked, probably due to loops.
  295. while (End->Type == pkgCache::Dep::Depends) {
  296. PkgIterator DepPkg;
  297. VerIterator InstallVer;
  298. SPtrArray<Version *> VList = Start.AllTargets();
  299. // Check through each version of each package that could satisfy this dependancy
  300. for (Version **I = VList; *I != 0; I++) {
  301. VerIterator Ver(Cache,*I);
  302. DepPkg = Ver.ParentPkg();
  303. InstallVer = VerIterator(Cache,Cache[DepPkg].InstallVer);
  304. // Check if the current version of the package is avalible and will satisfy this dependancy
  305. if (DepPkg.CurrentVer() == Ver && List->IsNow(DepPkg) == true &&
  306. !List->IsFlag(DepPkg,pkgOrderList::Removed) && DepPkg.State() == PkgIterator::NeedsNothing)
  307. {
  308. Bad = false;
  309. break;
  310. }
  311. // Check if the version that is going to be installed will satisfy the dependancy
  312. if (Cache[DepPkg].InstallVer == *I) {
  313. if (List->IsFlag(DepPkg,pkgOrderList::UnPacked)) {
  314. /* Check for a loop to prevent one forming
  315. If A depends on B and B depends on A, SmartConfigure will
  316. just hop between them if this is not checked */
  317. if (!List->IsFlag(DepPkg,pkgOrderList::Loop)) {
  318. List->Flag(Pkg,pkgOrderList::Loop);
  319. // If SmartConfigure was succesfull, Bad is false, so break
  320. Bad = !SmartConfigure(DepPkg);
  321. List->RmFlag(Pkg,pkgOrderList::Loop);
  322. if (!Bad) break;
  323. }
  324. } else if (List->IsFlag(DepPkg,pkgOrderList::Configured)) {
  325. Bad = false;
  326. break;
  327. }
  328. }
  329. }
  330. /* If the dependany is still not satisfied, try, if possible, unpacking a package to satisfy it */
  331. if (InstallVer != 0 && Bad) {
  332. Bad = false;
  333. if (!List->IsFlag(DepPkg,pkgOrderList::Loop)) {
  334. List->Flag(Pkg,pkgOrderList::Loop);
  335. if (Debug)
  336. cout << " Unpacking " << DepPkg.Name() << " to avoid loop" << endl;
  337. SmartUnPack(DepPkg, true);
  338. //List->Flag(Pkg,~pkgOrderList::Loop);
  339. }
  340. }
  341. if (Start==End) {
  342. if (Bad && Debug) {
  343. if (!List->IsFlag(DepPkg,pkgOrderList::Loop)) {
  344. _error->Warning("Could not satisfy dependancies for %s",Pkg.Name());
  345. }
  346. }
  347. break;
  348. } else {
  349. Start++;
  350. }
  351. }
  352. }
  353. if (Bad) {
  354. if (Debug)
  355. _error->Warning(_("Could not configure '%s'. "),Pkg.Name());
  356. return false;
  357. }
  358. static std::string const conf = _config->Find("PackageManager::Configure","all");
  359. static bool const ConfigurePkgs = (conf == "all" || conf == "smart");
  360. if (List->IsFlag(Pkg,pkgOrderList::Configured))
  361. return _error->Error("Internal configure error on '%s'. ",Pkg.Name(),1);
  362. if (ConfigurePkgs == true && Configure(Pkg) == false)
  363. return false;
  364. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  365. if (Cache[Pkg].InstVerIter(Cache)->MultiArch == pkgCache::Version::Same)
  366. for (PkgIterator P = Pkg.Group().PackageList();
  367. P.end() == false; P = Pkg.Group().NextPkg(P))
  368. {
  369. if (Pkg == P || List->IsFlag(P,pkgOrderList::Configured) == true ||
  370. Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
  371. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
  372. continue;
  373. SmartConfigure(P);
  374. }
  375. // Sanity Check
  376. if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
  377. return _error->Error(_("Could not configure '%s'. "),Pkg.Name());
  378. return true;
  379. }
  380. /*}}}*/
  381. // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
  382. // ---------------------------------------------------------------------
  383. /* This is called to deal with conflicts arising from unpacking */
  384. bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
  385. {
  386. if (List->IsNow(Pkg) == false)
  387. return true;
  388. // Already removed it
  389. if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
  390. return true;
  391. // Woops, it will not be re-installed!
  392. if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
  393. return false;
  394. // Essential packages get special treatment
  395. bool IsEssential = false;
  396. if ((Pkg->Flags & pkgCache::Flag::Essential) != 0)
  397. IsEssential = true;
  398. /* Check for packages that are the dependents of essential packages and
  399. promote them too */
  400. if (Pkg->CurrentVer != 0)
  401. {
  402. for (DepIterator D = Pkg.RevDependsList(); D.end() == false &&
  403. IsEssential == false; D++)
  404. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  405. if ((D.ParentPkg()->Flags & pkgCache::Flag::Essential) != 0)
  406. IsEssential = true;
  407. }
  408. if (IsEssential == true)
  409. {
  410. if (_config->FindB("APT::Force-LoopBreak",false) == false)
  411. return _error->Error(_("This installation run will require temporarily "
  412. "removing the essential package %s due to a "
  413. "Conflicts/Pre-Depends loop. This is often bad, "
  414. "but if you really want to do it, activate the "
  415. "APT::Force-LoopBreak option."),Pkg.Name());
  416. }
  417. bool Res = SmartRemove(Pkg);
  418. if (Cache[Pkg].Delete() == false)
  419. List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
  420. return Res;
  421. }
  422. /*}}}*/
  423. // PM::SmartRemove - Removal Helper /*{{{*/
  424. // ---------------------------------------------------------------------
  425. /* */
  426. bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
  427. {
  428. if (List->IsNow(Pkg) == false)
  429. return true;
  430. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  431. return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
  432. return true;
  433. }
  434. /*}}}*/
  435. // PM::SmartUnPack - Install helper /*{{{*/
  436. // ---------------------------------------------------------------------
  437. /* This puts the system in a state where it can Unpack Pkg, if Pkg is allready
  438. unpacked, or when it has been unpacked, if Immediate==true it configures it. */
  439. bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
  440. {
  441. return SmartUnPack(Pkg, true);
  442. }
  443. bool pkgPackageManager::SmartUnPack(PkgIterator Pkg, bool const Immediate)
  444. {
  445. if (Debug)
  446. clog << "SmartUnPack " << Pkg.Name() << endl;
  447. // Check if it is already unpacked
  448. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  449. Cache[Pkg].Keep() == true)
  450. {
  451. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  452. if (Immediate == true &&
  453. List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
  454. if (SmartConfigure(Pkg) == false)
  455. _error->Warning(_("Could not perform immediate configuration on already unpacked '%s'. "
  456. "Please see man 5 apt.conf under APT::Immediate-Configure for details."),Pkg.Name());
  457. return true;
  458. }
  459. VerIterator const instVer = Cache[Pkg].InstVerIter(Cache);
  460. /* PreUnpack Checks: This loop checks and attempts to rectify and problems that would prevent the package being unpacked.
  461. It addresses: PreDepends, Conflicts, Obsoletes and Breaks (DpkgBreaks). Any resolutions that do not require it should
  462. avoid configuration (calling SmartUnpack with Immediate=true), this is because when unpacking some packages with
  463. complex dependancy structures, trying to configure some packages while breaking the loops can complicate things .
  464. This will be either dealt with if the package is configured as a dependency of Pkg (if and when Pkg is configured),
  465. or by the ConfigureAll call at the end of the for loop in OrderInstall. */
  466. for (DepIterator D = instVer.DependsList();
  467. D.end() == false; )
  468. {
  469. // Compute a single dependency element (glob or)
  470. pkgCache::DepIterator Start;
  471. pkgCache::DepIterator End;
  472. D.GlobOr(Start,End);
  473. while (End->Type == pkgCache::Dep::PreDepends)
  474. {
  475. if (Debug)
  476. clog << "PreDepends order for " << Pkg.Name() << std::endl;
  477. // Look for possible ok targets.
  478. SPtrArray<Version *> VList = Start.AllTargets();
  479. bool Bad = true;
  480. for (Version **I = VList; *I != 0 && Bad == true; I++)
  481. {
  482. VerIterator Ver(Cache,*I);
  483. PkgIterator Pkg = Ver.ParentPkg();
  484. // See if the current version is ok
  485. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  486. Pkg.State() == PkgIterator::NeedsNothing)
  487. {
  488. Bad = false;
  489. if (Debug)
  490. clog << "Found ok package " << Pkg.Name() << endl;
  491. continue;
  492. }
  493. }
  494. // Look for something that could be configured.
  495. for (Version **I = VList; *I != 0 && Bad == true; I++)
  496. {
  497. VerIterator Ver(Cache,*I);
  498. PkgIterator Pkg = Ver.ParentPkg();
  499. // Not the install version
  500. if (Cache[Pkg].InstallVer != *I ||
  501. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  502. continue;
  503. if (List->IsFlag(Pkg,pkgOrderList::Configured)) {
  504. Bad = false;
  505. continue;
  506. }
  507. if (Debug)
  508. clog << "Trying to SmartConfigure " << Pkg.Name() << endl;
  509. Bad = !SmartConfigure(Pkg);
  510. }
  511. /* If this or element did not match then continue on to the
  512. next or element until a matching element is found */
  513. if (Bad == true)
  514. {
  515. // This triggers if someone make a pre-depends/depend loop.
  516. if (Start == End)
  517. return _error->Error("Couldn't configure pre-depend %s for %s, "
  518. "probably a dependency cycle.",
  519. End.TargetPkg().Name(),Pkg.Name());
  520. Start++;
  521. }
  522. else
  523. break;
  524. }
  525. if (End->Type == pkgCache::Dep::Conflicts ||
  526. End->Type == pkgCache::Dep::Obsoletes)
  527. {
  528. /* Look for conflicts. Two packages that are both in the install
  529. state cannot conflict so we don't check.. */
  530. SPtrArray<Version *> VList = End.AllTargets();
  531. for (Version **I = VList; *I != 0; I++)
  532. {
  533. VerIterator Ver(Cache,*I);
  534. PkgIterator ConflictPkg = Ver.ParentPkg();
  535. VerIterator InstallVer(Cache,Cache[ConflictPkg].InstallVer);
  536. // See if the current version is conflicting
  537. if (ConflictPkg.CurrentVer() == Ver && List->IsNow(ConflictPkg))
  538. {
  539. cout << Pkg.Name() << " conflicts with " << ConflictPkg.Name() << endl;
  540. /* If a loop is not present or has not yet been detected, attempt to unpack packages
  541. to resolve this conflict. If there is a loop present, remove packages to resolve this conflict */
  542. if (!List->IsFlag(ConflictPkg,pkgOrderList::Loop)) {
  543. if (Cache[ConflictPkg].Keep() == 0 && Cache[ConflictPkg].InstallVer != 0) {
  544. if (Debug)
  545. cout << "Unpacking " << ConflictPkg.Name() << " to prevent conflict" << endl;
  546. List->Flag(Pkg,pkgOrderList::Loop);
  547. SmartUnPack(ConflictPkg,false);
  548. // Remove loop to allow it to be used later if needed
  549. List->RmFlag(Pkg,pkgOrderList::Loop);
  550. } else {
  551. if (EarlyRemove(ConflictPkg) == false)
  552. return _error->Error("Internal Error, Could not early remove %s",ConflictPkg.Name());
  553. }
  554. } else {
  555. if (!List->IsFlag(ConflictPkg,pkgOrderList::Removed)) {
  556. if (Debug)
  557. cout << "Because of conficts knot, removing " << ConflictPkg.Name() << " to conflict violation" << endl;
  558. if (EarlyRemove(ConflictPkg) == false)
  559. return _error->Error("Internal Error, Could not early remove %s",ConflictPkg.Name());
  560. }
  561. }
  562. }
  563. }
  564. }
  565. // Check for breaks
  566. if (End->Type == pkgCache::Dep::DpkgBreaks) {
  567. SPtrArray<Version *> VList = End.AllTargets();
  568. for (Version **I = VList; *I != 0; I++)
  569. {
  570. VerIterator Ver(Cache,*I);
  571. PkgIterator BrokenPkg = Ver.ParentPkg();
  572. VerIterator InstallVer(Cache,Cache[BrokenPkg].InstallVer);
  573. // Check if it needs to be unpacked
  574. if (List->IsFlag(BrokenPkg,pkgOrderList::InList) && Cache[BrokenPkg].Delete() == false &&
  575. !List->IsFlag(BrokenPkg,pkgOrderList::Loop) && List->IsNow(BrokenPkg)) {
  576. List->Flag(Pkg,pkgOrderList::Loop);
  577. // Found a break, so unpack the package
  578. if (Debug)
  579. cout << " Unpacking " << BrokenPkg.Name() << " to avoid break" << endl;
  580. SmartUnPack(BrokenPkg, false);
  581. List->RmFlag(Pkg,pkgOrderList::Loop);
  582. }
  583. // Check if a package needs to be removed
  584. if (Cache[BrokenPkg].Delete() == true && !List->IsFlag(BrokenPkg,pkgOrderList::Configured)) {
  585. if (Debug)
  586. cout << " Removing " << BrokenPkg.Name() << " to avoid break" << endl;
  587. SmartRemove(BrokenPkg);
  588. }
  589. }
  590. }
  591. }
  592. // Check for reverse conflicts.
  593. if (CheckRConflicts(Pkg,Pkg.RevDependsList(),
  594. instVer.VerStr()) == false)
  595. return false;
  596. for (PrvIterator P = instVer.ProvidesList();
  597. P.end() == false; P++)
  598. CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
  599. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  600. if (instVer->MultiArch == pkgCache::Version::Same)
  601. for (PkgIterator P = Pkg.Group().PackageList();
  602. P.end() == false; P = Pkg.Group().NextPkg(P))
  603. {
  604. if (Pkg == P || List->IsFlag(P,pkgOrderList::UnPacked) == true ||
  605. Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
  606. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
  607. continue;
  608. SmartUnPack(P, false);
  609. }
  610. if(Install(Pkg,FileNames[Pkg->ID]) == false)
  611. return false;
  612. if (Immediate == true && List->IsFlag(Pkg,pkgOrderList::Immediate) == true) {
  613. // Perform immedate configuration of the package.
  614. if (SmartConfigure(Pkg) == false)
  615. _error->Warning(_("Could not perform immediate configuration on '%s'. "
  616. "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg.Name(),2);
  617. }
  618. return true;
  619. }
  620. /*}}}*/
  621. // PM::OrderInstall - Installation ordering routine /*{{{*/
  622. // ---------------------------------------------------------------------
  623. /* */
  624. pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
  625. {
  626. if (CreateOrderList() == false)
  627. return Failed;
  628. Reset();
  629. if (Debug == true)
  630. clog << "Beginning to order" << endl;
  631. bool const ordering =
  632. _config->FindB("PackageManager::UnpackAll",true) ?
  633. List->OrderUnpack(FileNames) : List->OrderCritical();
  634. if (ordering == false)
  635. {
  636. _error->Error("Internal ordering error");
  637. return Failed;
  638. }
  639. if (Debug == true)
  640. clog << "Done ordering" << endl;
  641. bool DoneSomething = false;
  642. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  643. {
  644. PkgIterator Pkg(Cache,*I);
  645. if (List->IsNow(Pkg) == false)
  646. {
  647. if (!List->IsFlag(Pkg,pkgOrderList::Configured) && !NoImmConfigure) {
  648. if (SmartConfigure(Pkg) == false && Debug)
  649. _error->Warning("Internal Error, Could not configure %s",Pkg.Name());
  650. // FIXME: The above warning message might need changing
  651. } else {
  652. if (Debug == true)
  653. clog << "Skipping already done " << Pkg.Name() << endl;
  654. }
  655. continue;
  656. }
  657. if (List->IsMissing(Pkg) == true)
  658. {
  659. if (Debug == true)
  660. clog << "Sequence completed at " << Pkg.Name() << endl;
  661. if (DoneSomething == false)
  662. {
  663. _error->Error("Internal Error, ordering was unable to handle the media swap");
  664. return Failed;
  665. }
  666. return Incomplete;
  667. }
  668. // Sanity check
  669. if (Cache[Pkg].Keep() == true &&
  670. Pkg.State() == pkgCache::PkgIterator::NeedsNothing &&
  671. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall)
  672. {
  673. _error->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg.Name());
  674. return Failed;
  675. }
  676. // Perform a delete or an install
  677. if (Cache[Pkg].Delete() == true)
  678. {
  679. if (SmartRemove(Pkg) == false)
  680. return Failed;
  681. }
  682. else
  683. if (SmartUnPack(Pkg) == false)
  684. return Failed;
  685. DoneSomething = true;
  686. if (ImmConfigureAll) {
  687. /* ConfigureAll here to pick up and packages left unconfigured becuase they were unpacked in the
  688. "PreUnpack Checks" section */
  689. if (!ConfigureAll())
  690. return Failed;
  691. }
  692. }
  693. // Final run through the configure phase
  694. if (ConfigureAll() == false)
  695. return Failed;
  696. // Sanity check
  697. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  698. {
  699. if (List->IsFlag(*I,pkgOrderList::Configured) == false)
  700. {
  701. _error->Error("Internal error, packages left unconfigured. %s",
  702. PkgIterator(Cache,*I).Name());
  703. return Failed;
  704. }
  705. }
  706. return Completed;
  707. }
  708. /*}}}*/
  709. // PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
  710. // ---------------------------------------------------------------------
  711. pkgPackageManager::OrderResult
  712. pkgPackageManager::DoInstallPostFork(int statusFd)
  713. {
  714. if(statusFd > 0)
  715. // FIXME: use SetCloseExec here once it taught about throwing
  716. // exceptions instead of doing _exit(100) on failure
  717. fcntl(statusFd,F_SETFD,FD_CLOEXEC);
  718. bool goResult = Go(statusFd);
  719. if(goResult == false)
  720. return Failed;
  721. return Res;
  722. };
  723. // PM::DoInstall - Does the installation /*{{{*/
  724. // ---------------------------------------------------------------------
  725. /* This uses the filenames in FileNames and the information in the
  726. DepCache to perform the installation of packages.*/
  727. pkgPackageManager::OrderResult pkgPackageManager::DoInstall(int statusFd)
  728. {
  729. if(DoInstallPreFork() == Failed)
  730. return Failed;
  731. return DoInstallPostFork(statusFd);
  732. }
  733. /*}}}*/