void BPackageManager::_AnalyzeResult() { BSolverResult result; status_t error = fSolver->GetResult(result); if (error != B_OK) DIE(error, "failed to compute packages to un/-install"); InstalledRepository& installationRepository = InstallationRepository(); PackageList& packagesToActivate = installationRepository.PackagesToActivate(); PackageList& packagesToDeactivate = installationRepository.PackagesToDeactivate(); PackageList potentialBasePackages; for (int32 i = 0; const BSolverResultElement* element = result.ElementAt(i); i++) { BSolverPackage* package = element->Package(); switch (element->Type()) { case BSolverResultElement::B_TYPE_INSTALL: { PackageList& packageList = dynamic_cast<InstalledRepository*>(package->Repository()) != NULL ? potentialBasePackages : packagesToActivate; if (!packageList.AddItem(package)) throw std::bad_alloc(); break; } case BSolverResultElement::B_TYPE_UNINSTALL: if (!packagesToDeactivate.AddItem(package)) throw std::bad_alloc(); break; } } // Make sure base packages are installed in the same location. for (int32 i = 0; i < packagesToActivate.CountItems(); i++) { BSolverPackage* package = packagesToActivate.ItemAt(i); int32 index = _FindBasePackage(potentialBasePackages, package->Info()); if (index < 0) continue; BSolverPackage* basePackage = potentialBasePackages.RemoveItemAt(index); if (!packagesToActivate.AddItem(basePackage)) throw std::bad_alloc(); } fInstallationInterface->ResultComputed(installationRepository); }
int32 BPackageManager::_FindBasePackage(const PackageList& packages, const BPackageInfo& info) { if (info.BasePackage().IsEmpty()) return -1; // find the requirement matching the base package BPackageResolvableExpression* basePackage = NULL; int32 count = info.RequiresList().CountItems(); for (int32 i = 0; i < count; i++) { BPackageResolvableExpression* requires = info.RequiresList().ItemAt(i); if (requires->Name() == info.BasePackage()) { basePackage = requires; break; } } if (basePackage == NULL) { fUserInteractionHandler->Warn(B_OK, "package %s-%s doesn't have a " "matching requires for its base package \"%s\"", info.Name().String(), info.Version().ToString().String(), info.BasePackage().String()); return -1; } // find the first package matching the base package requires count = packages.CountItems(); for (int32 i = 0; i < count; i++) { BSolverPackage* package = packages.ItemAt(i); if (package->Name() == basePackage->Name() && package->Info().Matches(*basePackage)) { return i; } } return -1; }
void BPackageManager::Uninstall(const BSolverPackageSpecifierList& packages) { Init(B_ADD_INSTALLED_REPOSITORIES); // find the packages that match the specification const BSolverPackageSpecifier* unmatchedSpecifier; PackageList foundPackages; status_t error = fSolver->FindPackages(packages, BSolver::B_FIND_INSTALLED_ONLY, foundPackages, &unmatchedSpecifier); if (error != B_OK) { if (unmatchedSpecifier != NULL) { DIE(error, "failed to find a match for \"%s\"", unmatchedSpecifier->SelectString().String()); } else DIE(error, "failed to compute packages to uninstall"); } // determine the inverse base package closure for the found packages // TODO: Optimize! InstalledRepository& installationRepository = InstallationRepository(); bool foundAnotherPackage; do { foundAnotherPackage = false; int32 count = installationRepository.CountPackages(); for (int32 i = 0; i < count; i++) { BSolverPackage* package = installationRepository.PackageAt(i); if (foundPackages.HasItem(package)) continue; if (_FindBasePackage(foundPackages, package->Info()) >= 0) { foundPackages.AddItem(package); foundAnotherPackage = true; } } } while (foundAnotherPackage); // remove the packages from the repository for (int32 i = 0; BSolverPackage* package = foundPackages.ItemAt(i); i++) installationRepository.DisablePackage(package); for (;;) { error = fSolver->VerifyInstallation(BSolver::B_VERIFY_ALLOW_UNINSTALL); if (error != B_OK) DIE(error, "failed to compute packages to uninstall"); _HandleProblems(); // (virtually) apply the result to this repository _AnalyzeResult(); for (int32 i = foundPackages.CountItems() - 1; i >= 0; i--) { if (!installationRepository.PackagesToDeactivate() .AddItem(foundPackages.ItemAt(i))) { throw std::bad_alloc(); } } installationRepository.ApplyChanges(); // verify the next specific respository if (!_NextSpecificInstallationLocation()) break; foundPackages.MakeEmpty(); // NOTE: In theory, after verifying a more specific location, it would // be more correct to compute the inverse base package closure for the // packages we need to uninstall and (if anything changed) verify again. // In practice, however, base packages are always required with an exact // version (ATM). If that base package still exist in a more general // location (the only reason why the package requiring the base package // wouldn't be marked to be uninstalled as well) there shouldn't have // been any reason to remove it from the more specific location in the // first place. } _ConfirmChanges(true); _ApplyPackageChanges(true); }
void MainWindow::_RefreshPackageList() { BPackageRoster roster; BStringList repositoryNames; status_t result = roster.GetRepositoryNames(repositoryNames); if (result != B_OK) return; DepotInfoMap depots; for (int32 i = 0; i < repositoryNames.CountStrings(); i++) { const BString& repoName = repositoryNames.StringAt(i); depots[repoName] = DepotInfo(repoName); } PackageManager manager(B_PACKAGE_INSTALLATION_LOCATION_HOME); try { manager.Init(PackageManager::B_ADD_INSTALLED_REPOSITORIES | PackageManager::B_ADD_REMOTE_REPOSITORIES); } catch (BException ex) { BString message(B_TRANSLATE("An error occurred while " "initializing the package manager: %message%")); message.ReplaceFirst("%message%", ex.Message()); _NotifyUser("Error", message.String()); return; } BObjectList<BSolverPackage> packages; result = manager.Solver()->FindPackages("", BSolver::B_FIND_CASE_INSENSITIVE | BSolver::B_FIND_IN_NAME | BSolver::B_FIND_IN_SUMMARY | BSolver::B_FIND_IN_DESCRIPTION | BSolver::B_FIND_IN_PROVIDES, packages); if (result != B_OK) { // TODO: notify user return; } if (packages.IsEmpty()) return; PackageInfoMap foundPackages; // if a given package is installed locally, we will potentially // get back multiple entries, one for each local installation // location, and one for each remote repository the package // is available in. The above map is used to ensure that in such // cases we consolidate the information, rather than displaying // duplicates PackageInfoMap remotePackages; // any package that we find in a remote repository goes in this map. // this is later used to discern which packages came from a local // installation only, as those must be handled a bit differently // upon uninstallation, since we'd no longer be able to pull them // down remotely. BStringList systemFlaggedPackages; // any packages flagged as a system package are added to this list. // such packages cannot be uninstalled, nor can any of their deps. PackageInfoMap systemInstalledPackages; // any packages installed in system are added to this list. // This is later used for dependency resolution of the actual // system packages in order to compute the list of protected // dependencies indicated above. BitmapRef defaultIcon(new(std::nothrow) SharedBitmap( "application/x-vnd.haiku-package"), true); for (int32 i = 0; i < packages.CountItems(); i++) { BSolverPackage* package = packages.ItemAt(i); const BPackageInfo& repoPackageInfo = package->Info(); PackageInfoRef modelInfo; PackageInfoMap::iterator it = foundPackages.find( repoPackageInfo.Name()); if (it != foundPackages.end()) modelInfo.SetTo(it->second); else { // Add new package info BString publisherURL; if (repoPackageInfo.URLList().CountStrings() > 0) publisherURL = repoPackageInfo.URLList().StringAt(0); BString publisherName = repoPackageInfo.Vendor(); const BStringList& rightsList = repoPackageInfo.CopyrightList(); if (rightsList.CountStrings() > 0) publisherName = rightsList.StringAt(0); modelInfo.SetTo(new(std::nothrow) PackageInfo( repoPackageInfo.Name(), repoPackageInfo.Version().ToString(), PublisherInfo(BitmapRef(), publisherName, "", publisherURL), repoPackageInfo.Summary(), repoPackageInfo.Description(), repoPackageInfo.Flags()), true); if (modelInfo.Get() == NULL) return; foundPackages[repoPackageInfo.Name()] = modelInfo; } modelInfo->SetIcon(defaultIcon); modelInfo->AddListener(this); BSolverRepository* repository = package->Repository(); if (dynamic_cast<BPackageManager::RemoteRepository*>(repository) != NULL) { depots[repository->Name()].AddPackage(modelInfo); remotePackages[modelInfo->Title()] = modelInfo; } else { if (repository == static_cast<const BSolverRepository*>( manager.SystemRepository())) { modelInfo->AddInstallationLocation( B_PACKAGE_INSTALLATION_LOCATION_SYSTEM); if (!modelInfo->IsSystemPackage()) { systemInstalledPackages[repoPackageInfo.FileName()] = modelInfo; } } else if (repository == static_cast<const BSolverRepository*>( manager.HomeRepository())) { modelInfo->AddInstallationLocation( B_PACKAGE_INSTALLATION_LOCATION_HOME); } } if (modelInfo->IsSystemPackage()) systemFlaggedPackages.Add(repoPackageInfo.FileName()); } BAutolock lock(fModel.Lock()); fModel.Clear(); // filter remote packages from the found list // any packages remaining will be locally installed packages // that weren't acquired from a repository for (PackageInfoMap::iterator it = remotePackages.begin(); it != remotePackages.end(); it++) { foundPackages.erase(it->first); } if (!foundPackages.empty()) { BString repoName = B_TRANSLATE("Local"); depots[repoName] = DepotInfo(repoName); DepotInfoMap::iterator depot = depots.find(repoName); for (PackageInfoMap::iterator it = foundPackages.begin(); it != foundPackages.end(); ++it) { depot->second.AddPackage(it->second); } } for (DepotInfoMap::iterator it = depots.begin(); it != depots.end(); it++) { fModel.AddDepot(it->second); } // start retrieving package icons and average ratings fModel.PopulateAllPackages(); // compute the OS package dependencies try { // create the solver BSolver* solver; status_t error = BSolver::Create(solver); if (error != B_OK) throw BFatalErrorException(error, "Failed to create solver."); ObjectDeleter<BSolver> solverDeleter(solver); BPath systemPath; error = find_directory(B_SYSTEM_PACKAGES_DIRECTORY, &systemPath); if (error != B_OK) { throw BFatalErrorException(error, "Unable to retrieve system packages directory."); } // add the "installed" repository with the given packages BSolverRepository installedRepository; { BRepositoryBuilder installedRepositoryBuilder(installedRepository, "installed"); for (int32 i = 0; i < systemFlaggedPackages.CountStrings(); i++) { BPath packagePath(systemPath); packagePath.Append(systemFlaggedPackages.StringAt(i)); installedRepositoryBuilder.AddPackage(packagePath.Path()); } installedRepositoryBuilder.AddToSolver(solver, true); } // add system repository BSolverRepository systemRepository; { BRepositoryBuilder systemRepositoryBuilder(systemRepository, "system"); for (PackageInfoMap::iterator it = systemInstalledPackages.begin(); it != systemInstalledPackages.end(); it++) { BPath packagePath(systemPath); packagePath.Append(it->first); systemRepositoryBuilder.AddPackage(packagePath.Path()); } systemRepositoryBuilder.AddToSolver(solver, false); } // solve error = solver->VerifyInstallation(); if (error != B_OK) { throw BFatalErrorException(error, "Failed to compute packages to " "install."); } BSolverResult solverResult; error = solver->GetResult(solverResult); if (error != B_OK) { throw BFatalErrorException(error, "Failed to retrieve system " "package dependency list."); } for (int32 i = 0; const BSolverResultElement* element = solverResult.ElementAt(i); i++) { BSolverPackage* package = element->Package(); if (element->Type() == BSolverResultElement::B_TYPE_INSTALL) { PackageInfoMap::iterator it = systemInstalledPackages.find( package->Info().FileName()); if (it != systemInstalledPackages.end()) it->second->SetSystemDependency(true); } } } catch (BFatalErrorException ex) { printf("Fatal exception occurred while resolving system dependencies: " "%s, details: %s\n", strerror(ex.Error()), ex.Details().String()); } catch (BNothingToDoException) { // do nothing } catch (BException ex) { printf("Exception occurred while resolving system dependencies: %s\n", ex.Message().String()); } catch (...) { printf("Unknown exception occurred while resolving system " "dependencies.\n"); } }