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