packagemanager.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 && Configure(Pkg) == 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 && Configure(Pkg) == false)
  270. return false;
  271. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  272. }
  273. // Sanity Check
  274. if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
  275. return _error->Error(_("Could not perform immediate configuration on '%s'. "
  276. "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg.Name(),1);
  277. return true;
  278. }
  279. /*}}}*/
  280. // PM::DepAdd - Add all dependents to the oder list /*{{{*/
  281. // ---------------------------------------------------------------------
  282. /* This recursively adds all dependents to the order list */
  283. bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
  284. {
  285. if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
  286. return true;
  287. if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
  288. return true;
  289. if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
  290. return false;
  291. if (Debug)
  292. std::clog << OutputInDepth(Depth) << "DepAdd: " << Pkg.Name() << std::endl;
  293. // Put the package on the list
  294. OList.push_back(Pkg);
  295. OList.Flag(Pkg,pkgOrderList::Added);
  296. Depth++;
  297. // Check the dependencies to see if they are all satisfied.
  298. bool Bad = false;
  299. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
  300. {
  301. if (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)
  302. {
  303. D++;
  304. continue;
  305. }
  306. // Grok or groups
  307. Bad = true;
  308. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  309. {
  310. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  311. if (Bad == false)
  312. continue;
  313. SPtrArray<Version *> VList = D.AllTargets();
  314. for (Version **I = VList; *I != 0 && Bad == true; I++)
  315. {
  316. VerIterator Ver(Cache,*I);
  317. PkgIterator Pkg = Ver.ParentPkg();
  318. // See if the current version is ok
  319. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  320. Pkg.State() == PkgIterator::NeedsNothing)
  321. {
  322. Bad = false;
  323. continue;
  324. }
  325. // Not the install version
  326. if (Cache[Pkg].InstallVer != *I ||
  327. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  328. continue;
  329. if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
  330. Bad = !DepAdd(OList,Pkg,Depth);
  331. if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
  332. Bad = false;
  333. }
  334. }
  335. if (Bad == true)
  336. {
  337. if (Debug)
  338. std::clog << OutputInDepth(Depth) << "DepAdd FAILS on: " << Pkg.Name() << std::endl;
  339. OList.Flag(Pkg,0,pkgOrderList::Added);
  340. OList.pop_back();
  341. Depth--;
  342. return false;
  343. }
  344. }
  345. Depth--;
  346. return true;
  347. }
  348. /*}}}*/
  349. // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
  350. // ---------------------------------------------------------------------
  351. /* This is called to deal with conflicts arising from unpacking */
  352. bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
  353. {
  354. if (List->IsNow(Pkg) == false)
  355. return true;
  356. // Already removed it
  357. if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
  358. return true;
  359. // Woops, it will not be re-installed!
  360. if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
  361. return false;
  362. // Essential packages get special treatment
  363. bool IsEssential = false;
  364. if ((Pkg->Flags & pkgCache::Flag::Essential) != 0)
  365. IsEssential = true;
  366. /* Check for packages that are the dependents of essential packages and
  367. promote them too */
  368. if (Pkg->CurrentVer != 0)
  369. {
  370. for (DepIterator D = Pkg.RevDependsList(); D.end() == false &&
  371. IsEssential == false; D++)
  372. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  373. if ((D.ParentPkg()->Flags & pkgCache::Flag::Essential) != 0)
  374. IsEssential = true;
  375. }
  376. if (IsEssential == true)
  377. {
  378. if (_config->FindB("APT::Force-LoopBreak",false) == false)
  379. return _error->Error(_("This installation run will require temporarily "
  380. "removing the essential package %s due to a "
  381. "Conflicts/Pre-Depends loop. This is often bad, "
  382. "but if you really want to do it, activate the "
  383. "APT::Force-LoopBreak option."),Pkg.Name());
  384. }
  385. bool Res = SmartRemove(Pkg);
  386. if (Cache[Pkg].Delete() == false)
  387. List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
  388. return Res;
  389. }
  390. /*}}}*/
  391. // PM::SmartRemove - Removal Helper /*{{{*/
  392. // ---------------------------------------------------------------------
  393. /* */
  394. bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
  395. {
  396. if (List->IsNow(Pkg) == false)
  397. return true;
  398. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  399. return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
  400. return true;
  401. }
  402. /*}}}*/
  403. // PM::SmartUnPack - Install helper /*{{{*/
  404. // ---------------------------------------------------------------------
  405. /* This performs the task of handling pre-depends. */
  406. bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
  407. {
  408. // Check if it is already unpacked
  409. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  410. Cache[Pkg].Keep() == true)
  411. {
  412. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  413. if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
  414. if (SmartConfigure(Pkg) == false)
  415. return _error->Error(_("Could not perform immediate configuration on already unpacked '%s'. "
  416. "Please see man 5 apt.conf under APT::Immediate-Configure for details."),Pkg.Name());
  417. return true;
  418. }
  419. /* See if this packages install version has any predependencies
  420. that are not met by 'now' packages. */
  421. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
  422. D.end() == false; )
  423. {
  424. // Compute a single dependency element (glob or)
  425. pkgCache::DepIterator Start;
  426. pkgCache::DepIterator End;
  427. D.GlobOr(Start,End);
  428. while (End->Type == pkgCache::Dep::PreDepends)
  429. {
  430. if (Debug == true)
  431. clog << "PreDepends order for " << Pkg.Name() << std::endl;
  432. // Look for possible ok targets.
  433. SPtrArray<Version *> VList = Start.AllTargets();
  434. bool Bad = true;
  435. for (Version **I = VList; *I != 0 && Bad == true; I++)
  436. {
  437. VerIterator Ver(Cache,*I);
  438. PkgIterator Pkg = Ver.ParentPkg();
  439. // See if the current version is ok
  440. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  441. Pkg.State() == PkgIterator::NeedsNothing)
  442. {
  443. Bad = false;
  444. if (Debug == true)
  445. clog << "Found ok package " << Pkg.Name() << endl;
  446. continue;
  447. }
  448. }
  449. // Look for something that could be configured.
  450. for (Version **I = VList; *I != 0 && Bad == true; I++)
  451. {
  452. VerIterator Ver(Cache,*I);
  453. PkgIterator Pkg = Ver.ParentPkg();
  454. // Not the install version
  455. if (Cache[Pkg].InstallVer != *I ||
  456. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  457. continue;
  458. if (Debug == true)
  459. clog << "Trying to SmartConfigure " << Pkg.Name() << endl;
  460. Bad = !SmartConfigure(Pkg);
  461. }
  462. /* If this or element did not match then continue on to the
  463. next or element until a matching element is found */
  464. if (Bad == true)
  465. {
  466. // This triggers if someone make a pre-depends/depend loop.
  467. if (Start == End)
  468. return _error->Error("Couldn't configure pre-depend %s for %s, "
  469. "probably a dependency cycle.",
  470. End.TargetPkg().Name(),Pkg.Name());
  471. Start++;
  472. }
  473. else
  474. break;
  475. }
  476. if (End->Type == pkgCache::Dep::Conflicts ||
  477. End->Type == pkgCache::Dep::Obsoletes)
  478. {
  479. /* Look for conflicts. Two packages that are both in the install
  480. state cannot conflict so we don't check.. */
  481. SPtrArray<Version *> VList = End.AllTargets();
  482. for (Version **I = VList; *I != 0; I++)
  483. {
  484. VerIterator Ver(Cache,*I);
  485. PkgIterator Pkg = Ver.ParentPkg();
  486. // See if the current version is conflicting
  487. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
  488. {
  489. if (EarlyRemove(Pkg) == false)
  490. return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
  491. }
  492. }
  493. }
  494. }
  495. // Check for reverse conflicts.
  496. if (CheckRConflicts(Pkg,Pkg.RevDependsList(),
  497. Cache[Pkg].InstVerIter(Cache).VerStr()) == false)
  498. return false;
  499. for (PrvIterator P = Cache[Pkg].InstVerIter(Cache).ProvidesList();
  500. P.end() == false; P++)
  501. CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
  502. if(Install(Pkg,FileNames[Pkg->ID]) == false)
  503. return false;
  504. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  505. // Perform immedate configuration of the package.
  506. if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
  507. if (SmartConfigure(Pkg) == false)
  508. return _error->Error(_("Could not perform immediate configuration on '%s'. "
  509. "Please see man 5 apt.conf under APT::Immediate-Configure for details. (%d)"),Pkg.Name(),2);
  510. return true;
  511. }
  512. /*}}}*/
  513. // PM::OrderInstall - Installation ordering routine /*{{{*/
  514. // ---------------------------------------------------------------------
  515. /* */
  516. pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
  517. {
  518. if (CreateOrderList() == false)
  519. return Failed;
  520. Reset();
  521. if (Debug == true)
  522. clog << "Beginning to order" << endl;
  523. bool const ordering =
  524. _config->FindB("PackageManager::UnpackAll",true) ?
  525. List->OrderUnpack(FileNames) : List->OrderCritical();
  526. if (ordering == false)
  527. {
  528. _error->Error("Internal ordering error");
  529. return Failed;
  530. }
  531. if (Debug == true)
  532. clog << "Done ordering" << endl;
  533. bool DoneSomething = false;
  534. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  535. {
  536. PkgIterator Pkg(Cache,*I);
  537. if (List->IsNow(Pkg) == false)
  538. {
  539. if (Debug == true)
  540. clog << "Skipping already done " << Pkg.Name() << endl;
  541. continue;
  542. }
  543. if (List->IsMissing(Pkg) == true)
  544. {
  545. if (Debug == true)
  546. clog << "Sequence completed at " << Pkg.Name() << endl;
  547. if (DoneSomething == false)
  548. {
  549. _error->Error("Internal Error, ordering was unable to handle the media swap");
  550. return Failed;
  551. }
  552. return Incomplete;
  553. }
  554. // Sanity check
  555. if (Cache[Pkg].Keep() == true &&
  556. Pkg.State() == pkgCache::PkgIterator::NeedsNothing &&
  557. (Cache[Pkg].iFlags & pkgDepCache::ReInstall) != pkgDepCache::ReInstall)
  558. {
  559. _error->Error("Internal Error, trying to manipulate a kept package (%s)",Pkg.Name());
  560. return Failed;
  561. }
  562. // Perform a delete or an install
  563. if (Cache[Pkg].Delete() == true)
  564. {
  565. if (SmartRemove(Pkg) == false)
  566. return Failed;
  567. }
  568. else
  569. if (SmartUnPack(Pkg) == false)
  570. return Failed;
  571. DoneSomething = true;
  572. }
  573. // Final run through the configure phase
  574. if (ConfigureAll() == false)
  575. return Failed;
  576. // Sanity check
  577. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  578. {
  579. if (List->IsFlag(*I,pkgOrderList::Configured) == false)
  580. {
  581. _error->Error("Internal error, packages left unconfigured. %s",
  582. PkgIterator(Cache,*I).Name());
  583. return Failed;
  584. }
  585. }
  586. return Completed;
  587. }
  588. /*}}}*/
  589. // PM::DoInstallPostFork - Does install part that happens after the fork /*{{{*/
  590. // ---------------------------------------------------------------------
  591. pkgPackageManager::OrderResult
  592. pkgPackageManager::DoInstallPostFork(int statusFd)
  593. {
  594. if(statusFd > 0)
  595. // FIXME: use SetCloseExec here once it taught about throwing
  596. // exceptions instead of doing _exit(100) on failure
  597. fcntl(statusFd,F_SETFD,FD_CLOEXEC);
  598. bool goResult = Go(statusFd);
  599. if(goResult == false)
  600. return Failed;
  601. return Res;
  602. };
  603. // PM::DoInstall - Does the installation /*{{{*/
  604. // ---------------------------------------------------------------------
  605. /* This uses the filenames in FileNames and the information in the
  606. DepCache to perform the installation of packages.*/
  607. pkgPackageManager::OrderResult pkgPackageManager::DoInstall(int statusFd)
  608. {
  609. if(DoInstallPreFork() == Failed)
  610. return Failed;
  611. return DoInstallPostFork(statusFd);
  612. }
  613. /*}}}*/