You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

591 lines
17 KiB

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