packagemanager.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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. static bool const NoImmConfigure = !_config->FindB("APT::Immediate-Configure",true);
  148. // Generate the list of affected packages and sort it
  149. for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  150. {
  151. // Ignore no-version packages
  152. if (I->VersionList == 0)
  153. continue;
  154. // Mark the package and its dependends for immediate configuration
  155. if (((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential ||
  156. (I->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important) &&
  157. NoImmConfigure == false)
  158. {
  159. if(Debug)
  160. clog << "CreateOrderList(): Adding Immediate flag for " << I.Name() << endl;
  161. List->Flag(I,pkgOrderList::Immediate);
  162. // Look for other install packages to make immediate configurea
  163. ImmediateAdd(I, true);
  164. // And again with the current version.
  165. ImmediateAdd(I, false);
  166. }
  167. // Not interesting
  168. if ((Cache[I].Keep() == true ||
  169. Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
  170. I.State() == pkgCache::PkgIterator::NeedsNothing &&
  171. (Cache[I].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall &&
  172. (I.Purge() != false || Cache[I].Mode != pkgDepCache::ModeDelete ||
  173. (Cache[I].iFlags & pkgDepCache::Purge) != pkgDepCache::Purge))
  174. continue;
  175. // Append it to the list
  176. List->push_back(I);
  177. }
  178. return true;
  179. }
  180. /*}}}*/
  181. // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
  182. // ---------------------------------------------------------------------
  183. /* The restriction on provides is to eliminate the case when provides
  184. are transitioning between valid states [ie exim to smail] */
  185. bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
  186. {
  187. if (D.TargetPkg()->ProvidesList != 0)
  188. return false;
  189. if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
  190. (Cache[D] & pkgDepCache::DepNow) != 0)
  191. return true;
  192. return false;
  193. }
  194. /*}}}*/
  195. // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
  196. // ---------------------------------------------------------------------
  197. /* This looks over the reverses for a conflicts line that needs early
  198. removal. */
  199. bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
  200. const char *Ver)
  201. {
  202. for (;D.end() == false; D++)
  203. {
  204. if (D->Type != pkgCache::Dep::Conflicts &&
  205. D->Type != pkgCache::Dep::Obsoletes)
  206. continue;
  207. // The package hasnt been changed
  208. if (List->IsNow(Pkg) == false)
  209. continue;
  210. // Ignore self conflicts, ignore conflicts from irrelevent versions
  211. if (D.ParentPkg() == Pkg || D.ParentVer() != D.ParentPkg().CurrentVer())
  212. continue;
  213. if (Cache.VS().CheckDep(Ver,D->CompareOp,D.TargetVer()) == false)
  214. continue;
  215. if (EarlyRemove(D.ParentPkg()) == false)
  216. return _error->Error("Reverse conflicts early remove for package '%s' failed",
  217. Pkg.Name());
  218. }
  219. return true;
  220. }
  221. /*}}}*/
  222. // PM::ConfigureAll - Run the all out configuration /*{{{*/
  223. // ---------------------------------------------------------------------
  224. /* This configures every package. It is assumed they are all unpacked and
  225. that the final configuration is valid. */
  226. bool pkgPackageManager::ConfigureAll()
  227. {
  228. pkgOrderList OList(&Cache);
  229. // Populate the order list
  230. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  231. if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
  232. pkgOrderList::UnPacked) == true)
  233. OList.push_back(*I);
  234. if (OList.OrderConfigure() == false)
  235. return false;
  236. std::string const conf = _config->Find("PackageManager::Configure","all");
  237. bool const ConfigurePkgs = (conf == "all");
  238. // Perform the configuring
  239. for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
  240. {
  241. PkgIterator Pkg(Cache,*I);
  242. if (ConfigurePkgs == true && VerifyAndConfigure(Pkg,OList) == false)
  243. return false;
  244. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  245. }
  246. return true;
  247. }
  248. /*}}}*/
  249. // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
  250. // ---------------------------------------------------------------------
  251. /* This routine scheduals the configuration of the given package and all
  252. of it's dependents. */
  253. bool pkgPackageManager::SmartConfigure(PkgIterator Pkg)
  254. {
  255. if (Debug == true)
  256. clog << "SmartConfigure " << Pkg.Name() << endl;
  257. pkgOrderList OList(&Cache);
  258. if (DepAdd(OList,Pkg) == false)
  259. return false;
  260. static std::string const conf = _config->Find("PackageManager::Configure","all");
  261. static bool const ConfigurePkgs = (conf == "all" || conf == "smart");
  262. if (ConfigurePkgs == true)
  263. if (OList.OrderConfigure() == false)
  264. return false;
  265. // Perform the configuring
  266. for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
  267. {
  268. PkgIterator Pkg(Cache,*I);
  269. if (ConfigurePkgs == true && VerifyAndConfigure(Pkg,OList) == false)
  270. return false;
  271. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  272. }
  273. if (Cache[Pkg].InstVerIter(Cache)->MultiArch == pkgCache::Version::Same)
  274. for (PkgIterator P = Pkg.Group().PackageList();
  275. P.end() == false; P = Pkg.Group().NextPkg(P))
  276. {
  277. if (Pkg == P || List->IsFlag(P,pkgOrderList::Configured) == true ||
  278. Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
  279. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
  280. continue;
  281. SmartConfigure(P);
  282. }
  283. // Sanity Check
  284. if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
  285. return _error->Error(_("Could not perform immediate configuration on '%s'. "
  286. "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg.Name(),1);
  287. return true;
  288. }
  289. // PM::VerifyConfigure - Check configuration of dependancies /*{{{*/
  290. // ---------------------------------------------------------------------
  291. /* This routine checks that all a packages dependancies have been
  292. configured, before it is going to be configured. If this gives a warning
  293. on a virtual package, it means that the package thats providing it is not
  294. configured*/
  295. bool pkgPackageManager::VerifyConfigure(PkgIterator Pkg, pkgOrderList &OList)
  296. {
  297. // If this is true at the end, then the package should not be configured
  298. bool error=true;
  299. // This holds the the OR status of the previous dependancy
  300. bool previousOr=false;
  301. // First iterate through the dependancies of Pkg
  302. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false; D++)
  303. {
  304. /* If the dependancy is of type Depends or PreDepends, we need to check it, but only if it is going to be
  305. configured at some point */
  306. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends) {
  307. /* If the previous package and this package are OR dependancies, and the previous package satisfied the dependancy
  308. then skip this dependancy as it is not relevent, this will repeat for the next package if the situation is the
  309. same */
  310. if (previousOr && !error) { // As error has not been reset, this refers to the previous dependancy
  311. previousOr = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  312. continue;
  313. }
  314. // Reset error
  315. error = true;
  316. // Check thorugh all possible versions of this dependancy (D)
  317. SPtrArray<Version *> VList = D.AllTargets();
  318. for (Version **I = VList; *I != 0; I++)
  319. {
  320. VerIterator DepVer(Cache,*I);
  321. PkgIterator DepPkg = DepVer.ParentPkg();
  322. VerIterator DepInstallVer(Cache,Cache[DepPkg].InstallVer);
  323. if (DepPkg.CurrentVer() == DepVer && !List->IsFlag(DepPkg,pkgOrderList::UnPacked)) {
  324. error=false;
  325. break;
  326. }
  327. if (Cache[DepPkg].InstallVer == DepVer &&
  328. (List->IsFlag(DepPkg,pkgOrderList::Configured) || OList.IsFlag(DepPkg,pkgOrderList::InList))) {
  329. error=false;
  330. break;
  331. }
  332. }
  333. /* Only worry here if this package is a OR with the next, as even though this package does not satisfy the OR
  334. the next one might */
  335. if (error && !((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)) {
  336. _error->Error("Package %s should not be configured because package %s is not configured",Pkg.Name(),D.TargetPkg().Name());
  337. return false;
  338. /* If the previous package is a OR but not this package, but there is still an error then fail as it will not
  339. be satisfied */
  340. } else if (error && previousOr && !((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)) {
  341. _error->Error("Package %s should not be configured because package %s (or any alternatives) are not configured",Pkg.Name(),D.TargetPkg().Name());
  342. return false;
  343. }
  344. previousOr = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  345. } else {
  346. previousOr=false;
  347. }
  348. }
  349. return true;
  350. }
  351. // PM::VerifyAndConfigure - Check configuration of dependancies /*{{{*/
  352. // ---------------------------------------------------------------------
  353. /* This routine verifies if a package can be configured and if so
  354. configures it */
  355. bool pkgPackageManager::VerifyAndConfigure(PkgIterator Pkg, pkgOrderList &OList)
  356. {
  357. if (VerifyConfigure(Pkg, OList))
  358. return Configure(Pkg);
  359. else
  360. return false;
  361. }
  362. /*}}}*/
  363. // PM::DepAdd - Add all dependents to the oder list /*{{{*/
  364. // ---------------------------------------------------------------------
  365. /* This recursively adds all dependents to the order list */
  366. bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
  367. {
  368. if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
  369. return true;
  370. if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
  371. return true;
  372. if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
  373. return false;
  374. if (Debug)
  375. std::clog << OutputInDepth(Depth) << "DepAdd: " << Pkg.Name() << std::endl;
  376. // Put the package on the list
  377. OList.push_back(Pkg);
  378. OList.Flag(Pkg,pkgOrderList::Added);
  379. Depth++;
  380. // Check the dependencies to see if they are all satisfied.
  381. bool Bad = false;
  382. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
  383. {
  384. if (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)
  385. {
  386. D++;
  387. continue;
  388. }
  389. // Grok or groups
  390. Bad = true;
  391. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  392. {
  393. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  394. if (Bad == false)
  395. continue;
  396. SPtrArray<Version *> VList = D.AllTargets();
  397. for (Version **I = VList; *I != 0 && Bad == true; I++)
  398. {
  399. VerIterator Ver(Cache,*I);
  400. PkgIterator Pkg = Ver.ParentPkg();
  401. // See if the current version is ok
  402. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  403. Pkg.State() == PkgIterator::NeedsNothing)
  404. {
  405. Bad = false;
  406. continue;
  407. }
  408. // Check if this package is being re-installed
  409. if ((Cache[Pkg].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall && Cache[Pkg].InstallVer != *I &&
  410. List->IsNow(Pkg) == true && Pkg.State() == PkgIterator::NeedsNothing) {
  411. Bad = false;
  412. continue;
  413. }
  414. // Not the install version
  415. if (Cache[Pkg].InstallVer != *I ||
  416. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  417. continue;
  418. if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
  419. Bad = !DepAdd(OList,Pkg,Depth);
  420. if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
  421. Bad = false;
  422. }
  423. }
  424. if (Bad == true)
  425. {
  426. if (Debug)
  427. std::clog << OutputInDepth(Depth) << "DepAdd FAILS on: " << Pkg.Name() << std::endl;
  428. OList.Flag(Pkg,0,pkgOrderList::Added);
  429. OList.pop_back();
  430. Depth--;
  431. return false;
  432. }
  433. }
  434. Depth--;
  435. return true;
  436. }
  437. /*}}}*/
  438. // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
  439. // ---------------------------------------------------------------------
  440. /* This is called to deal with conflicts arising from unpacking */
  441. bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
  442. {
  443. if (List->IsNow(Pkg) == false)
  444. return true;
  445. // Already removed it
  446. if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
  447. return true;
  448. // Woops, it will not be re-installed!
  449. if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
  450. return false;
  451. // Essential packages get special treatment
  452. bool IsEssential = false;
  453. if ((Pkg->Flags & pkgCache::Flag::Essential) != 0)
  454. IsEssential = true;
  455. /* Check for packages that are the dependents of essential packages and
  456. promote them too */
  457. if (Pkg->CurrentVer != 0)
  458. {
  459. for (DepIterator D = Pkg.RevDependsList(); D.end() == false &&
  460. IsEssential == false; D++)
  461. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  462. if ((D.ParentPkg()->Flags & pkgCache::Flag::Essential) != 0)
  463. IsEssential = true;
  464. }
  465. if (IsEssential == true)
  466. {
  467. if (_config->FindB("APT::Force-LoopBreak",false) == false)
  468. return _error->Error(_("This installation run will require temporarily "
  469. "removing the essential package %s due to a "
  470. "Conflicts/Pre-Depends loop. This is often bad, "
  471. "but if you really want to do it, activate the "
  472. "APT::Force-LoopBreak option."),Pkg.Name());
  473. }
  474. bool Res = SmartRemove(Pkg);
  475. if (Cache[Pkg].Delete() == false)
  476. List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
  477. return Res;
  478. }
  479. /*}}}*/
  480. // PM::SmartRemove - Removal Helper /*{{{*/
  481. // ---------------------------------------------------------------------
  482. /* */
  483. bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
  484. {
  485. if (List->IsNow(Pkg) == false)
  486. return true;
  487. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  488. return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
  489. return true;
  490. }
  491. /*}}}*/
  492. // PM::SmartUnPack - Install helper /*{{{*/
  493. // ---------------------------------------------------------------------
  494. /* This performs the task of handling pre-depends. */
  495. bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
  496. {
  497. return SmartUnPack(Pkg, true);
  498. }
  499. bool pkgPackageManager::SmartUnPack(PkgIterator Pkg, bool const Immediate)
  500. {
  501. if (Debug == true)
  502. clog << "SmartUnPack " << Pkg.Name() << endl;
  503. // Check if it is already unpacked
  504. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  505. Cache[Pkg].Keep() == true)
  506. {
  507. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  508. if (Immediate == true &&
  509. List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
  510. if (SmartConfigure(Pkg) == false)
  511. return _error->Error(_("Could not perform immediate configuration on already unpacked '%s'. "
  512. "Please see man 5 apt.conf under APT::Immediate-Configure for details."),Pkg.Name());
  513. return true;
  514. }
  515. VerIterator const instVer = Cache[Pkg].InstVerIter(Cache);
  516. /* See if this packages install version has any predependencies
  517. that are not met by 'now' packages. */
  518. for (DepIterator D = instVer.DependsList();
  519. D.end() == false; )
  520. {
  521. // Compute a single dependency element (glob or)
  522. pkgCache::DepIterator Start;
  523. pkgCache::DepIterator End;
  524. D.GlobOr(Start,End);
  525. while (End->Type == pkgCache::Dep::PreDepends)
  526. {
  527. if (Debug == true)
  528. clog << "PreDepends order for " << Pkg.Name() << std::endl;
  529. // Look for possible ok targets.
  530. SPtrArray<Version *> VList = Start.AllTargets();
  531. bool Bad = true;
  532. for (Version **I = VList; *I != 0 && Bad == true; I++)
  533. {
  534. VerIterator Ver(Cache,*I);
  535. PkgIterator Pkg = Ver.ParentPkg();
  536. // See if the current version is ok
  537. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  538. Pkg.State() == PkgIterator::NeedsNothing)
  539. {
  540. Bad = false;
  541. if (Debug == true)
  542. clog << "Found ok package " << Pkg.Name() << endl;
  543. continue;
  544. }
  545. }
  546. // Look for something that could be configured.
  547. for (Version **I = VList; *I != 0 && Bad == true; I++)
  548. {
  549. VerIterator Ver(Cache,*I);
  550. PkgIterator Pkg = Ver.ParentPkg();
  551. // Not the install version
  552. if (Cache[Pkg].InstallVer != *I ||
  553. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  554. continue;
  555. if (Debug == true)
  556. clog << "Trying to SmartConfigure " << Pkg.Name() << endl;
  557. Bad = !SmartConfigure(Pkg);
  558. }
  559. /* If this or element did not match then continue on to the
  560. next or element until a matching element is found */
  561. if (Bad == true)
  562. {
  563. // This triggers if someone make a pre-depends/depend loop.
  564. if (Start == End)
  565. return _error->Error("Couldn't configure pre-depend %s for %s, "
  566. "probably a dependency cycle.",
  567. End.TargetPkg().Name(),Pkg.Name());
  568. Start++;
  569. }
  570. else
  571. break;
  572. }
  573. if (End->Type == pkgCache::Dep::Conflicts ||
  574. End->Type == pkgCache::Dep::Obsoletes)
  575. {
  576. /* Look for conflicts. Two packages that are both in the install
  577. state cannot conflict so we don't check.. */
  578. SPtrArray<Version *> VList = End.AllTargets();
  579. for (Version **I = VList; *I != 0; I++)
  580. {
  581. VerIterator Ver(Cache,*I);
  582. PkgIterator Pkg = Ver.ParentPkg();
  583. // See if the current version is conflicting
  584. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
  585. {
  586. if (EarlyRemove(Pkg) == false)
  587. return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
  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 Pkg = Ver.ParentPkg();
  598. // Check if it needs to be unpacked
  599. if (List->IsFlag(Pkg,pkgOrderList::InList) && Cache[Pkg].Delete() == false) {
  600. // Found a break, so unpack the package
  601. SmartUnPack(Pkg, false);
  602. }
  603. }
  604. }
  605. }
  606. // Check for reverse conflicts.
  607. if (CheckRConflicts(Pkg,Pkg.RevDependsList(),
  608. instVer.VerStr()) == false)
  609. return false;
  610. for (PrvIterator P = instVer.ProvidesList();
  611. P.end() == false; P++)
  612. CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
  613. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  614. if (instVer->MultiArch == pkgCache::Version::Same)
  615. for (PkgIterator P = Pkg.Group().PackageList();
  616. P.end() == false; P = Pkg.Group().NextPkg(P))
  617. {
  618. if (Pkg == P || List->IsFlag(P,pkgOrderList::UnPacked) == true ||
  619. Cache[P].InstallVer == 0 || (P.CurrentVer() == Cache[P].InstallVer &&
  620. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall))
  621. continue;
  622. SmartUnPack(P, false);
  623. }
  624. if(Install(Pkg,FileNames[Pkg->ID]) == false)
  625. return false;
  626. // Perform immedate configuration of the package.
  627. if (Immediate == true &&
  628. List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
  629. if (SmartConfigure(Pkg) == false)
  630. return _error->Error(_("Could not perform immediate configuration on '%s'. "
  631. "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg.Name(),2);
  632. return true;
  633. }
  634. /*}}}*/
  635. // PM::OrderInstall - Installation ordering routine /*{{{*/
  636. // ---------------------------------------------------------------------
  637. /* */
  638. pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
  639. {
  640. if (CreateOrderList() == false)
  641. return Failed;
  642. Reset();
  643. if (Debug == true)
  644. clog << "Beginning to order" << endl;
  645. bool const ordering =
  646. _config->FindB("PackageManager::UnpackAll",true) ?
  647. List->OrderUnpack(FileNames) : List->OrderCritical();
  648. if (ordering == false)
  649. {
  650. _error->Error("Internal ordering error");
  651. return Failed;
  652. }
  653. if (Debug == true)
  654. clog << "Done ordering" << endl;
  655. bool DoneSomething = false;
  656. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  657. {
  658. PkgIterator Pkg(Cache,*I);
  659. if (List->IsNow(Pkg) == false)
  660. {
  661. if (Debug == true)
  662. clog << "Skipping already done " << Pkg.Name() << endl;
  663. continue;
  664. }
  665. if (List->IsMissing(Pkg) == true)
  666. {
  667. if (Debug == true)
  668. clog << "Sequence completed at " << Pkg.Name() << endl;
  669. if (DoneSomething == false)
  670. {
  671. _error->Error("Internal Error, ordering was unable to handle the media swap");
  672. return Failed;
  673. }
  674. return Incomplete;
  675. }
  676. // Sanity check
  677. if (Cache[Pkg].Keep() == true &&
  678. Pkg.State() == pkgCache::PkgIterator::NeedsNothing &&
  679. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall)
  680. {
  681. _error->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg.Name());
  682. return Failed;
  683. }
  684. // Perform a delete or an install
  685. if (Cache[Pkg].Delete() == true)
  686. {
  687. if (SmartRemove(Pkg) == false)
  688. return Failed;
  689. }
  690. else
  691. if (SmartUnPack(Pkg) == false)
  692. return Failed;
  693. DoneSomething = true;
  694. }
  695. // Final run through the configure phase
  696. if (ConfigureAll() == false)
  697. return Failed;
  698. // Sanity check
  699. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  700. {
  701. if (List->IsFlag(*I,pkgOrderList::Configured) == false)
  702. {
  703. _error->Error("Internal error, packages left unconfigured. %s",
  704. PkgIterator(Cache,*I).Name());
  705. return Failed;
  706. }
  707. }
  708. return Completed;
  709. }
  710. /*}}}*/
  711. // PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
  712. // ---------------------------------------------------------------------
  713. pkgPackageManager::OrderResult
  714. pkgPackageManager::DoInstallPostFork(int statusFd)
  715. {
  716. if(statusFd > 0)
  717. // FIXME: use SetCloseExec here once it taught about throwing
  718. // exceptions instead of doing _exit(100) on failure
  719. fcntl(statusFd,F_SETFD,FD_CLOEXEC);
  720. bool goResult = Go(statusFd);
  721. if(goResult == false)
  722. return Failed;
  723. return Res;
  724. };
  725. // PM::DoInstall - Does the installation /*{{{*/
  726. // ---------------------------------------------------------------------
  727. /* This uses the filenames in FileNames and the information in the
  728. DepCache to perform the installation of packages.*/
  729. pkgPackageManager::OrderResult pkgPackageManager::DoInstall(int statusFd)
  730. {
  731. if(DoInstallPreFork() == Failed)
  732. return Failed;
  733. return DoInstallPostFork(statusFd);
  734. }
  735. /*}}}*/