packagemanager.cc 16 KB

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