packagemanager.cc 38 KB

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