packagemanager.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: packagemanager.cc,v 1.18 1999/07/10 04:58:42 jgg 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. #ifdef __GNUG__
  13. #pragma implementation "apt-pkg/packagemanager.h"
  14. #endif
  15. #include <apt-pkg/packagemanager.h>
  16. #include <apt-pkg/orderlist.h>
  17. #include <apt-pkg/depcache.h>
  18. #include <apt-pkg/error.h>
  19. #include <apt-pkg/version.h>
  20. #include <apt-pkg/acquire-item.h>
  21. #include <apt-pkg/algorithms.h>
  22. #include <apt-pkg/configuration.h>
  23. /*}}}*/
  24. // PM::PackageManager - Constructor /*{{{*/
  25. // ---------------------------------------------------------------------
  26. /* */
  27. pkgPackageManager::pkgPackageManager(pkgDepCache &Cache) : Cache(Cache)
  28. {
  29. FileNames = new string[Cache.Head().PackageCount];
  30. List = 0;
  31. Debug = _config->FindB("Debug::pkgPackageManager",false);
  32. }
  33. /*}}}*/
  34. // PM::PackageManager - Destructor /*{{{*/
  35. // ---------------------------------------------------------------------
  36. /* */
  37. pkgPackageManager::~pkgPackageManager()
  38. {
  39. delete List;
  40. delete [] FileNames;
  41. }
  42. /*}}}*/
  43. // PM::GetArchives - Queue the archives for download /*{{{*/
  44. // ---------------------------------------------------------------------
  45. /* */
  46. bool pkgPackageManager::GetArchives(pkgAcquire *Owner,pkgSourceList *Sources,
  47. pkgRecords *Recs)
  48. {
  49. if (CreateOrderList() == false)
  50. return false;
  51. if (List->OrderUnpack() == false)
  52. return _error->Error("Internal ordering error");
  53. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  54. {
  55. PkgIterator Pkg(Cache,*I);
  56. FileNames[Pkg->ID] = string();
  57. // Skip packages to erase
  58. if (Cache[Pkg].Delete() == true)
  59. continue;
  60. // Skip Packages that need configure only.
  61. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  62. Cache[Pkg].Keep() == true)
  63. continue;
  64. // Skip already processed packages
  65. if (List->IsNow(Pkg) == false)
  66. continue;
  67. new pkgAcqArchive(Owner,Sources,Recs,Cache[Pkg].InstVerIter(Cache),
  68. FileNames[Pkg->ID]);
  69. }
  70. return true;
  71. }
  72. /*}}}*/
  73. // PM::FixMissing - Keep all missing packages /*{{{*/
  74. // ---------------------------------------------------------------------
  75. /* This is called to correct the installation when packages could not
  76. be downloaded. */
  77. bool pkgPackageManager::FixMissing()
  78. {
  79. pkgProblemResolver Resolve(Cache);
  80. List->SetFileList(FileNames);
  81. bool Bad = false;
  82. for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  83. {
  84. if (List->IsMissing(I) == false)
  85. continue;
  86. // Okay, this file is missing and we need it. Mark it for keep
  87. Bad = true;
  88. Cache.MarkKeep(I);
  89. }
  90. if (Bad == false)
  91. return true;
  92. // Now downgrade everything that is broken
  93. return Resolve.ResolveByKeep() == true && Cache.BrokenCount() == 0;
  94. }
  95. /*}}}*/
  96. // PM::CreateOrderList - Create the ordering class /*{{{*/
  97. // ---------------------------------------------------------------------
  98. /* This populates the ordering list with all the packages that are
  99. going to change. */
  100. bool pkgPackageManager::CreateOrderList()
  101. {
  102. if (List != 0)
  103. return true;
  104. delete List;
  105. List = new pkgOrderList(Cache);
  106. bool NoImmConfigure = _config->FindB("APT::Immediate-Configure",false);
  107. // Generate the list of affected packages and sort it
  108. for (PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
  109. {
  110. // Mark the package and its dependends for immediate configuration
  111. if (((I->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential ||
  112. (I->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important) &&
  113. NoImmConfigure == false)
  114. {
  115. List->Flag(I,pkgOrderList::Immediate);
  116. // Look for other packages to make immediate configurea
  117. if (Cache[I].InstallVer != 0)
  118. for (DepIterator D = Cache[I].InstVerIter(Cache).DependsList();
  119. D.end() == false; D++)
  120. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  121. List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
  122. // And again with the current version.
  123. if (I->CurrentVer != 0)
  124. for (DepIterator D = I.CurrentVer().DependsList();
  125. D.end() == false; D++)
  126. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  127. List->Flag(D.TargetPkg(),pkgOrderList::Immediate);
  128. }
  129. // Not interesting
  130. if ((Cache[I].Keep() == true ||
  131. Cache[I].InstVerIter(Cache) == I.CurrentVer()) &&
  132. I.State() == pkgCache::PkgIterator::NeedsNothing &&
  133. (I.Purge() != false || Cache[I].Mode != pkgDepCache::ModeDelete ||
  134. (Cache[I].iFlags & pkgDepCache::Purge) != pkgDepCache::Purge))
  135. continue;
  136. // Append it to the list
  137. List->push_back(I);
  138. }
  139. return true;
  140. }
  141. /*}}}*/
  142. // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
  143. // ---------------------------------------------------------------------
  144. /* The restriction on provides is to eliminate the case when provides
  145. are transitioning between valid states [ie exim to smail] */
  146. bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
  147. {
  148. if (D.TargetPkg()->ProvidesList != 0)
  149. return false;
  150. if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
  151. (Cache[D] & pkgDepCache::DepNow) != 0)
  152. return true;
  153. return false;
  154. }
  155. /*}}}*/
  156. // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
  157. // ---------------------------------------------------------------------
  158. /* This looks over the reverses for a conflicts line that needs early
  159. removal. */
  160. bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
  161. const char *Ver)
  162. {
  163. for (;D.end() == false; D++)
  164. {
  165. if (D->Type != pkgCache::Dep::Conflicts)
  166. continue;
  167. if (D.ParentPkg() == Pkg)
  168. continue;
  169. if (pkgCheckDep(D.TargetVer(),Ver,D->CompareOp) == false)
  170. continue;
  171. if (List->IsNow(Pkg) == false)
  172. continue;
  173. if (EarlyRemove(D.ParentPkg()) == false)
  174. return false;
  175. }
  176. return true;
  177. }
  178. /*}}}*/
  179. // PM::ConfigureAll - Run the all out configuration /*{{{*/
  180. // ---------------------------------------------------------------------
  181. /* This configures every package. It is assumed they are all unpacked and
  182. that the final configuration is valid. */
  183. bool pkgPackageManager::ConfigureAll()
  184. {
  185. pkgOrderList OList(Cache);
  186. // Populate the order list
  187. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  188. if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
  189. pkgOrderList::UnPacked) == true)
  190. OList.push_back(*I);
  191. if (OList.OrderConfigure() == false)
  192. return false;
  193. // Perform the configuring
  194. for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
  195. {
  196. PkgIterator Pkg(Cache,*I);
  197. if (Configure(Pkg) == false)
  198. return false;
  199. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  200. }
  201. return true;
  202. }
  203. /*}}}*/
  204. // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
  205. // ---------------------------------------------------------------------
  206. /* This routine scheduals the configuration of the given package and all
  207. of it's dependents. */
  208. bool pkgPackageManager::SmartConfigure(PkgIterator Pkg)
  209. {
  210. pkgOrderList OList(Cache);
  211. if (DepAdd(OList,Pkg) == false)
  212. return false;
  213. if (OList.OrderConfigure() == false)
  214. return false;
  215. // Perform the configuring
  216. for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
  217. {
  218. PkgIterator Pkg(Cache,*I);
  219. if (Configure(Pkg) == false)
  220. return false;
  221. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  222. }
  223. // Sanity Check
  224. if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
  225. return _error->Error("Internal error, could not immediate configure %s",Pkg.Name());
  226. return true;
  227. }
  228. /*}}}*/
  229. // PM::DepAdd - Add all dependents to the oder list /*{{{*/
  230. // ---------------------------------------------------------------------
  231. /* This recursively adds all dependents to the order list */
  232. bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
  233. {
  234. if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
  235. return true;
  236. if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
  237. return true;
  238. if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
  239. return false;
  240. // Put the package on the list
  241. OList.push_back(Pkg);
  242. OList.Flag(Pkg,pkgOrderList::Added);
  243. Depth++;
  244. // Check the dependencies to see if they are all satisfied.
  245. bool Bad = false;
  246. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
  247. {
  248. if (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)
  249. {
  250. D++;
  251. continue;
  252. }
  253. // Grok or groups
  254. Bad = true;
  255. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  256. {
  257. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  258. if (Bad == false)
  259. continue;
  260. Version **VList = D.AllTargets();
  261. for (Version **I = VList; *I != 0 && Bad == true; I++)
  262. {
  263. VerIterator Ver(Cache,*I);
  264. PkgIterator Pkg = Ver.ParentPkg();
  265. // See if the current version is ok
  266. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  267. Pkg.State() == PkgIterator::NeedsNothing)
  268. {
  269. Bad = false;
  270. continue;
  271. }
  272. // Not the install version
  273. if (Cache[Pkg].InstallVer != *I ||
  274. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  275. continue;
  276. if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
  277. Bad = !DepAdd(OList,Pkg,Depth);
  278. if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
  279. Bad = false;
  280. }
  281. delete [] VList;
  282. }
  283. if (Bad == true)
  284. {
  285. OList.Flag(Pkg,0,pkgOrderList::Added);
  286. OList.pop_back();
  287. Depth--;
  288. return false;
  289. }
  290. }
  291. Depth--;
  292. return true;
  293. }
  294. /*}}}*/
  295. // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
  296. // ---------------------------------------------------------------------
  297. /* This is called to deal with conflicts arising from unpacking */
  298. bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
  299. {
  300. if (List->IsNow(Pkg) == false)
  301. return true;
  302. // Already removed it
  303. if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
  304. return true;
  305. // Woops, it will not be re-installed!
  306. if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
  307. return false;
  308. bool Res = SmartRemove(Pkg);
  309. if (Cache[Pkg].Delete() == false)
  310. List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
  311. return Res;
  312. }
  313. /*}}}*/
  314. // PM::SmartRemove - Removal Helper /*{{{*/
  315. // ---------------------------------------------------------------------
  316. /* */
  317. bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
  318. {
  319. if (List->IsNow(Pkg) == false)
  320. return true;
  321. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  322. return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
  323. }
  324. /*}}}*/
  325. // PM::SmartUnPack - Install helper /*{{{*/
  326. // ---------------------------------------------------------------------
  327. /* This performs the task of handling pre-depends. */
  328. bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
  329. {
  330. // Check if it is already unpacked
  331. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  332. Cache[Pkg].Keep() == true)
  333. {
  334. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  335. if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
  336. if (SmartConfigure(Pkg) == false)
  337. return _error->Error("Internal Error, Could not perform immediate configuraton");
  338. return true;
  339. }
  340. /* See if this packages install version has any predependencies
  341. that are not met by 'now' packages. */
  342. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
  343. D.end() == false; D++)
  344. {
  345. if (D->Type == pkgCache::Dep::PreDepends)
  346. {
  347. // Look for possible ok targets.
  348. Version **VList = D.AllTargets();
  349. bool Bad = true;
  350. for (Version **I = VList; *I != 0 && Bad == true; I++)
  351. {
  352. VerIterator Ver(Cache,*I);
  353. PkgIterator Pkg = Ver.ParentPkg();
  354. // See if the current version is ok
  355. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  356. Pkg.State() == PkgIterator::NeedsNothing)
  357. {
  358. Bad = false;
  359. continue;
  360. }
  361. }
  362. // Look for something that could be configured.
  363. for (Version **I = VList; *I != 0 && Bad == true; I++)
  364. {
  365. VerIterator Ver(Cache,*I);
  366. PkgIterator Pkg = Ver.ParentPkg();
  367. // Not the install version
  368. if (Cache[Pkg].InstallVer != *I ||
  369. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  370. continue;
  371. Bad = !SmartConfigure(Pkg);
  372. }
  373. delete [] VList;
  374. if (Bad == true)
  375. return _error->Error("Internal Error, Couldn't configure a pre-depend");
  376. continue;
  377. }
  378. if (D->Type == pkgCache::Dep::Conflicts)
  379. {
  380. /* Look for conflicts. Two packages that are both in the install
  381. state cannot conflict so we don't check.. */
  382. Version **VList = D.AllTargets();
  383. for (Version **I = VList; *I != 0; I++)
  384. {
  385. VerIterator Ver(Cache,*I);
  386. PkgIterator Pkg = Ver.ParentPkg();
  387. // See if the current version is conflicting
  388. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
  389. {
  390. if (EarlyRemove(Pkg) == false)
  391. return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
  392. }
  393. }
  394. delete [] VList;
  395. }
  396. }
  397. // Check for reverse conflicts.
  398. CheckRConflicts(Pkg,Pkg.RevDependsList(),
  399. Cache[Pkg].InstVerIter(Cache).VerStr());
  400. for (PrvIterator P = Cache[Pkg].InstVerIter(Cache).ProvidesList();
  401. P.end() == false; P++)
  402. CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
  403. if (Install(Pkg,FileNames[Pkg->ID]) == false)
  404. return false;
  405. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  406. // Perform immedate configuration of the package.
  407. if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
  408. if (SmartConfigure(Pkg) == false)
  409. return _error->Error("Internal Error, Could not perform immediate configuraton");
  410. return true;
  411. }
  412. /*}}}*/
  413. // PM::OrderInstall - Installation ordering routine /*{{{*/
  414. // ---------------------------------------------------------------------
  415. /* */
  416. pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
  417. {
  418. if (CreateOrderList() == false)
  419. return Failed;
  420. Reset();
  421. if (Debug == true)
  422. clog << "Begining to order" << endl;
  423. if (List->OrderUnpack(FileNames) == false)
  424. {
  425. _error->Error("Internal ordering error");
  426. return Failed;
  427. }
  428. if (Debug == true)
  429. clog << "Done ordering" << endl;
  430. bool DoneSomething = false;
  431. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  432. {
  433. PkgIterator Pkg(Cache,*I);
  434. if (List->IsNow(Pkg) == false)
  435. {
  436. if (Debug == true)
  437. clog << "Skipping already done " << Pkg.Name() << endl;
  438. continue;
  439. }
  440. if (List->IsMissing(Pkg) == true)
  441. {
  442. if (Debug == true)
  443. clog << "Sequence completed at" << Pkg.Name() << endl;
  444. if (DoneSomething == false)
  445. {
  446. _error->Error("Internal Error, ordering was unable to handle the media swap");
  447. return Failed;
  448. }
  449. return Incomplete;
  450. }
  451. // Sanity check
  452. if (Cache[Pkg].Keep() == true && Pkg.State() == pkgCache::PkgIterator::NeedsNothing)
  453. {
  454. _error->Error("Internal Error, trying to manipulate a kept package");
  455. return Failed;
  456. }
  457. // Perform a delete or an install
  458. if (Cache[Pkg].Delete() == true)
  459. {
  460. if (SmartRemove(Pkg) == false)
  461. return Failed;
  462. }
  463. else
  464. if (SmartUnPack(Pkg) == false)
  465. return Failed;
  466. DoneSomething = true;
  467. }
  468. // Final run through the configure phase
  469. if (ConfigureAll() == false)
  470. return Failed;
  471. // Sanity check
  472. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  473. {
  474. if (List->IsFlag(*I,pkgOrderList::Configured) == false)
  475. {
  476. _error->Error("Internal error, packages left unconfigured. %s",
  477. PkgIterator(Cache,*I).Name());
  478. return Failed;
  479. }
  480. }
  481. return Completed;
  482. }
  483. /*}}}*/
  484. // PM::DoInstall - Does the installation /*{{{*/
  485. // ---------------------------------------------------------------------
  486. /* This uses the filenames in FileNames and the information in the
  487. DepCache to perform the installation of packages.*/
  488. pkgPackageManager::OrderResult pkgPackageManager::DoInstall()
  489. {
  490. OrderResult Res = OrderInstall();
  491. if (Res != Failed)
  492. if (Go() == false)
  493. return Failed;
  494. return Res;
  495. }
  496. /*}}}*/