packagemanager.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: packagemanager.cc,v 1.17 1999/07/09 04:11:34 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. continue;
  134. // Append it to the list
  135. List->push_back(I);
  136. }
  137. return true;
  138. }
  139. /*}}}*/
  140. // PM::DepAlwaysTrue - Returns true if this dep is irrelevent /*{{{*/
  141. // ---------------------------------------------------------------------
  142. /* The restriction on provides is to eliminate the case when provides
  143. are transitioning between valid states [ie exim to smail] */
  144. bool pkgPackageManager::DepAlwaysTrue(DepIterator D)
  145. {
  146. if (D.TargetPkg()->ProvidesList != 0)
  147. return false;
  148. if ((Cache[D] & pkgDepCache::DepInstall) != 0 &&
  149. (Cache[D] & pkgDepCache::DepNow) != 0)
  150. return true;
  151. return false;
  152. }
  153. /*}}}*/
  154. // PM::CheckRConflicts - Look for reverse conflicts /*{{{*/
  155. // ---------------------------------------------------------------------
  156. /* This looks over the reverses for a conflicts line that needs early
  157. removal. */
  158. bool pkgPackageManager::CheckRConflicts(PkgIterator Pkg,DepIterator D,
  159. const char *Ver)
  160. {
  161. for (;D.end() == false; D++)
  162. {
  163. if (D->Type != pkgCache::Dep::Conflicts)
  164. continue;
  165. if (D.ParentPkg() == Pkg)
  166. continue;
  167. if (pkgCheckDep(D.TargetVer(),Ver,D->CompareOp) == false)
  168. continue;
  169. if (List->IsNow(Pkg) == false)
  170. continue;
  171. if (EarlyRemove(D.ParentPkg()) == false)
  172. return false;
  173. }
  174. return true;
  175. }
  176. /*}}}*/
  177. // PM::ConfigureAll - Run the all out configuration /*{{{*/
  178. // ---------------------------------------------------------------------
  179. /* This configures every package. It is assumed they are all unpacked and
  180. that the final configuration is valid. */
  181. bool pkgPackageManager::ConfigureAll()
  182. {
  183. pkgOrderList OList(Cache);
  184. // Populate the order list
  185. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  186. if (List->IsFlag(pkgCache::PkgIterator(Cache,*I),
  187. pkgOrderList::UnPacked) == true)
  188. OList.push_back(*I);
  189. if (OList.OrderConfigure() == false)
  190. return false;
  191. // Perform the configuring
  192. for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
  193. {
  194. PkgIterator Pkg(Cache,*I);
  195. if (Configure(Pkg) == false)
  196. return false;
  197. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  198. }
  199. return true;
  200. }
  201. /*}}}*/
  202. // PM::SmartConfigure - Perform immediate configuration of the pkg /*{{{*/
  203. // ---------------------------------------------------------------------
  204. /* This routine scheduals the configuration of the given package and all
  205. of it's dependents. */
  206. bool pkgPackageManager::SmartConfigure(PkgIterator Pkg)
  207. {
  208. pkgOrderList OList(Cache);
  209. if (DepAdd(OList,Pkg) == false)
  210. return false;
  211. if (OList.OrderConfigure() == false)
  212. return false;
  213. // Perform the configuring
  214. for (pkgOrderList::iterator I = OList.begin(); I != OList.end(); I++)
  215. {
  216. PkgIterator Pkg(Cache,*I);
  217. if (Configure(Pkg) == false)
  218. return false;
  219. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  220. }
  221. // Sanity Check
  222. if (List->IsFlag(Pkg,pkgOrderList::Configured) == false)
  223. return _error->Error("Internal error, could not immediate configure %s",Pkg.Name());
  224. return true;
  225. }
  226. /*}}}*/
  227. // PM::DepAdd - Add all dependents to the oder list /*{{{*/
  228. // ---------------------------------------------------------------------
  229. /* This recursively adds all dependents to the order list */
  230. bool pkgPackageManager::DepAdd(pkgOrderList &OList,PkgIterator Pkg,int Depth)
  231. {
  232. if (OList.IsFlag(Pkg,pkgOrderList::Added) == true)
  233. return true;
  234. if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
  235. return true;
  236. if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == false)
  237. return false;
  238. // Put the package on the list
  239. OList.push_back(Pkg);
  240. OList.Flag(Pkg,pkgOrderList::Added);
  241. Depth++;
  242. // Check the dependencies to see if they are all satisfied.
  243. bool Bad = false;
  244. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList(); D.end() == false;)
  245. {
  246. if (D->Type != pkgCache::Dep::Depends && D->Type != pkgCache::Dep::PreDepends)
  247. {
  248. D++;
  249. continue;
  250. }
  251. // Grok or groups
  252. Bad = true;
  253. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  254. {
  255. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  256. if (Bad == false)
  257. continue;
  258. Version **VList = D.AllTargets();
  259. for (Version **I = VList; *I != 0 && Bad == true; I++)
  260. {
  261. VerIterator Ver(Cache,*I);
  262. PkgIterator Pkg = Ver.ParentPkg();
  263. // See if the current version is ok
  264. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  265. Pkg.State() == PkgIterator::NeedsNothing)
  266. {
  267. Bad = false;
  268. continue;
  269. }
  270. // Not the install version
  271. if (Cache[Pkg].InstallVer != *I ||
  272. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  273. continue;
  274. if (List->IsFlag(Pkg,pkgOrderList::UnPacked) == true)
  275. Bad = !DepAdd(OList,Pkg,Depth);
  276. if (List->IsFlag(Pkg,pkgOrderList::Configured) == true)
  277. Bad = false;
  278. }
  279. delete [] VList;
  280. }
  281. if (Bad == true)
  282. {
  283. OList.Flag(Pkg,0,pkgOrderList::Added);
  284. OList.pop_back();
  285. Depth--;
  286. return false;
  287. }
  288. }
  289. Depth--;
  290. return true;
  291. }
  292. /*}}}*/
  293. // PM::EarlyRemove - Perform removal of packages before their time /*{{{*/
  294. // ---------------------------------------------------------------------
  295. /* This is called to deal with conflicts arising from unpacking */
  296. bool pkgPackageManager::EarlyRemove(PkgIterator Pkg)
  297. {
  298. if (List->IsNow(Pkg) == false)
  299. return true;
  300. // Already removed it
  301. if (List->IsFlag(Pkg,pkgOrderList::Removed) == true)
  302. return true;
  303. // Woops, it will not be re-installed!
  304. if (List->IsFlag(Pkg,pkgOrderList::InList) == false)
  305. return false;
  306. bool Res = SmartRemove(Pkg);
  307. if (Cache[Pkg].Delete() == false)
  308. List->Flag(Pkg,pkgOrderList::Removed,pkgOrderList::States);
  309. return Res;
  310. }
  311. /*}}}*/
  312. // PM::SmartRemove - Removal Helper /*{{{*/
  313. // ---------------------------------------------------------------------
  314. /* */
  315. bool pkgPackageManager::SmartRemove(PkgIterator Pkg)
  316. {
  317. if (List->IsNow(Pkg) == false)
  318. return true;
  319. List->Flag(Pkg,pkgOrderList::Configured,pkgOrderList::States);
  320. return Remove(Pkg,(Cache[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge);
  321. }
  322. /*}}}*/
  323. // PM::SmartUnPack - Install helper /*{{{*/
  324. // ---------------------------------------------------------------------
  325. /* This performs the task of handling pre-depends. */
  326. bool pkgPackageManager::SmartUnPack(PkgIterator Pkg)
  327. {
  328. // Check if it is already unpacked
  329. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  330. Cache[Pkg].Keep() == true)
  331. {
  332. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  333. if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
  334. if (SmartConfigure(Pkg) == false)
  335. return _error->Error("Internal Error, Could not perform immediate configuraton");
  336. return true;
  337. }
  338. /* See if this packages install version has any predependencies
  339. that are not met by 'now' packages. */
  340. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
  341. D.end() == false; D++)
  342. {
  343. if (D->Type == pkgCache::Dep::PreDepends)
  344. {
  345. // Look for possible ok targets.
  346. Version **VList = D.AllTargets();
  347. bool Bad = true;
  348. for (Version **I = VList; *I != 0 && Bad == true; I++)
  349. {
  350. VerIterator Ver(Cache,*I);
  351. PkgIterator Pkg = Ver.ParentPkg();
  352. // See if the current version is ok
  353. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true &&
  354. Pkg.State() == PkgIterator::NeedsNothing)
  355. {
  356. Bad = false;
  357. continue;
  358. }
  359. }
  360. // Look for something that could be configured.
  361. for (Version **I = VList; *I != 0 && Bad == true; I++)
  362. {
  363. VerIterator Ver(Cache,*I);
  364. PkgIterator Pkg = Ver.ParentPkg();
  365. // Not the install version
  366. if (Cache[Pkg].InstallVer != *I ||
  367. (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing))
  368. continue;
  369. Bad = !SmartConfigure(Pkg);
  370. }
  371. delete [] VList;
  372. if (Bad == true)
  373. return _error->Error("Internal Error, Couldn't configure a pre-depend");
  374. continue;
  375. }
  376. if (D->Type == pkgCache::Dep::Conflicts)
  377. {
  378. /* Look for conflicts. Two packages that are both in the install
  379. state cannot conflict so we don't check.. */
  380. Version **VList = D.AllTargets();
  381. for (Version **I = VList; *I != 0; I++)
  382. {
  383. VerIterator Ver(Cache,*I);
  384. PkgIterator Pkg = Ver.ParentPkg();
  385. // See if the current version is conflicting
  386. if (Pkg.CurrentVer() == Ver && List->IsNow(Pkg) == true)
  387. {
  388. if (EarlyRemove(Pkg) == false)
  389. return _error->Error("Internal Error, Could not early remove %s",Pkg.Name());
  390. }
  391. }
  392. delete [] VList;
  393. }
  394. }
  395. // Check for reverse conflicts.
  396. CheckRConflicts(Pkg,Pkg.RevDependsList(),
  397. Cache[Pkg].InstVerIter(Cache).VerStr());
  398. for (PrvIterator P = Cache[Pkg].InstVerIter(Cache).ProvidesList();
  399. P.end() == false; P++)
  400. CheckRConflicts(Pkg,P.ParentPkg().RevDependsList(),P.ProvideVersion());
  401. if (Install(Pkg,FileNames[Pkg->ID]) == false)
  402. return false;
  403. List->Flag(Pkg,pkgOrderList::UnPacked,pkgOrderList::States);
  404. // Perform immedate configuration of the package.
  405. if (List->IsFlag(Pkg,pkgOrderList::Immediate) == true)
  406. if (SmartConfigure(Pkg) == false)
  407. return _error->Error("Internal Error, Could not perform immediate configuraton");
  408. return true;
  409. }
  410. /*}}}*/
  411. // PM::OrderInstall - Installation ordering routine /*{{{*/
  412. // ---------------------------------------------------------------------
  413. /* */
  414. pkgPackageManager::OrderResult pkgPackageManager::OrderInstall()
  415. {
  416. if (CreateOrderList() == false)
  417. return Failed;
  418. Reset();
  419. if (Debug == true)
  420. clog << "Begining to order" << endl;
  421. if (List->OrderUnpack(FileNames) == false)
  422. {
  423. _error->Error("Internal ordering error");
  424. return Failed;
  425. }
  426. if (Debug == true)
  427. clog << "Done ordering" << endl;
  428. bool DoneSomething = false;
  429. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  430. {
  431. PkgIterator Pkg(Cache,*I);
  432. if (List->IsNow(Pkg) == false)
  433. {
  434. if (Debug == true)
  435. clog << "Skipping already done " << Pkg.Name() << endl;
  436. continue;
  437. }
  438. if (List->IsMissing(Pkg) == true)
  439. {
  440. if (Debug == true)
  441. clog << "Sequence completed at" << Pkg.Name() << endl;
  442. if (DoneSomething == false)
  443. {
  444. _error->Error("Internal Error, ordering was unable to handle the media swap");
  445. return Failed;
  446. }
  447. return Incomplete;
  448. }
  449. // Sanity check
  450. if (Cache[Pkg].Keep() == true && Pkg.State() == pkgCache::PkgIterator::NeedsNothing)
  451. {
  452. _error->Error("Internal Error, trying to manipulate a kept package");
  453. return Failed;
  454. }
  455. // Perform a delete or an install
  456. if (Cache[Pkg].Delete() == true)
  457. {
  458. if (SmartRemove(Pkg) == false)
  459. return Failed;
  460. }
  461. else
  462. if (SmartUnPack(Pkg) == false)
  463. return Failed;
  464. DoneSomething = true;
  465. }
  466. // Final run through the configure phase
  467. if (ConfigureAll() == false)
  468. return Failed;
  469. // Sanity check
  470. for (pkgOrderList::iterator I = List->begin(); I != List->end(); I++)
  471. {
  472. if (List->IsFlag(*I,pkgOrderList::Configured) == false)
  473. {
  474. _error->Error("Internal error, packages left unconfigured. %s",
  475. PkgIterator(Cache,*I).Name());
  476. return Failed;
  477. }
  478. }
  479. return Completed;
  480. }
  481. /*}}}*/
  482. // PM::DoInstall - Does the installation /*{{{*/
  483. // ---------------------------------------------------------------------
  484. /* This uses the filenames in FileNames and the information in the
  485. DepCache to perform the installation of packages.*/
  486. pkgPackageManager::OrderResult pkgPackageManager::DoInstall()
  487. {
  488. OrderResult Res = OrderInstall();
  489. if (Res != Failed)
  490. if (Go() == false)
  491. return Failed;
  492. return Res;
  493. }
  494. /*}}}*/