+// TryToInstall - Try to install a single package /*{{{*/
+// ---------------------------------------------------------------------
+/* This used to be inlined in DoInstall, but with the advent of regex package
+ name matching it was split out.. */
+bool TryToInstall(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
+ pkgProblemResolver &Fix,bool Remove,bool BrokenFix,
+ unsigned int &ExpectedInst,bool AllowFail = true)
+{
+ /* This is a pure virtual package and there is a single available
+ provides */
+ if (Cache[Pkg].CandidateVer == 0 && Pkg->ProvidesList != 0 &&
+ Pkg.ProvidesList()->NextProvides == 0)
+ {
+ pkgCache::PkgIterator Tmp = Pkg.ProvidesList().OwnerPkg();
+ ioprintf(c1out,_("Note, selecting %s instead of %s\n"),
+ Tmp.Name(),Pkg.Name());
+ Pkg = Tmp;
+ }
+
+ // Handle the no-upgrade case
+ if (_config->FindB("APT::Get::upgrade",true) == false &&
+ Pkg->CurrentVer != 0)
+ {
+ if (AllowFail == true)
+ ioprintf(c1out,_("Skipping %s, it is already installed and upgrade is not set.\n"),
+ Pkg.Name());
+ return true;
+ }
+
+ // Check if there is something at all to install
+ pkgDepCache::StateCache &State = Cache[Pkg];
+ if (Remove == true && Pkg->CurrentVer == 0)
+ {
+ Fix.Clear(Pkg);
+ Fix.Protect(Pkg);
+ Fix.Remove(Pkg);
+
+ /* We want to continue searching for regex hits, so we return false here
+ otherwise this is not really an error. */
+ if (AllowFail == false)
+ return false;
+
+ ioprintf(c1out,_("Package %s is not installed, so not removed\n"),Pkg.Name());
+ return true;
+ }
+
+ if (State.CandidateVer == 0 && Remove == false)
+ {
+ if (AllowFail == false)
+ return false;
+
+ if (Pkg->ProvidesList != 0)
+ {
+ ioprintf(c1out,_("Package %s is a virtual package provided by:\n"),
+ Pkg.Name());
+
+ pkgCache::PrvIterator I = Pkg.ProvidesList();
+ for (; I.end() == false; I++)
+ {
+ pkgCache::PkgIterator Pkg = I.OwnerPkg();
+
+ if (Cache[Pkg].CandidateVerIter(Cache) == I.OwnerVer())
+ {
+ if (Cache[Pkg].Install() == true && Cache[Pkg].NewInstall() == false)
+ c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() <<
+ _(" [Installed]") << endl;
+ else
+ c1out << " " << Pkg.Name() << " " << I.OwnerVer().VerStr() << endl;
+ }
+ }
+ c1out << _("You should explicitly select one to install.") << endl;
+ }
+ else
+ {
+ ioprintf(c1out,
+ _("Package %s has no available version, but exists in the database.\n"
+ "This typically means that the package was mentioned in a dependency and\n"
+ "never uploaded, has been obsoleted or is not available with the contents\n"
+ "of sources.list\n"),Pkg.Name());
+
+ string List;
+ string VersionsList;
+ SPtrArray<bool> Seen = new bool[Cache.Head().PackageCount];
+ memset(Seen,0,Cache.Head().PackageCount*sizeof(*Seen));
+ pkgCache::DepIterator Dep = Pkg.RevDependsList();
+ for (; Dep.end() == false; Dep++)
+ {
+ if (Dep->Type != pkgCache::Dep::Replaces)
+ continue;
+ if (Seen[Dep.ParentPkg()->ID] == true)
+ continue;
+ Seen[Dep.ParentPkg()->ID] = true;
+ List += string(Dep.ParentPkg().Name()) + " ";
+ //VersionsList += string(Dep.ParentPkg().CurVersion) + "\n"; ???
+ }
+ ShowList(c1out,_("However the following packages replace it:"),List,VersionsList);
+ }
+
+ _error->Error(_("Package %s has no installation candidate"),Pkg.Name());
+ return false;
+ }
+
+ Fix.Clear(Pkg);
+ Fix.Protect(Pkg);
+ if (Remove == true)
+ {
+ Fix.Remove(Pkg);
+ Cache.MarkDelete(Pkg,_config->FindB("APT::Get::Purge",false));
+ return true;
+ }
+
+ // Install it
+ Cache.MarkInstall(Pkg,false);
+ if (State.Install() == false)
+ {
+ if (_config->FindB("APT::Get::ReInstall",false) == true)
+ {
+ if (Pkg->CurrentVer == 0 || Pkg.CurrentVer().Downloadable() == false)
+ ioprintf(c1out,_("Reinstallation of %s is not possible, it cannot be downloaded.\n"),
+ Pkg.Name());
+ else
+ Cache.SetReInstall(Pkg,true);
+ }
+ else
+ {
+ if (AllowFail == true)
+ ioprintf(c1out,_("%s is already the newest version.\n"),
+ Pkg.Name());
+ }
+ }
+ else
+ ExpectedInst++;
+
+ // Install it with autoinstalling enabled.
+ if (State.InstBroken() == true && BrokenFix == false)
+ Cache.MarkInstall(Pkg,true);
+ return true;
+}
+ /*}}}*/
+// TryToChangeVer - Try to change a candidate version /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool TryToChangeVer(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
+ const char *VerTag,bool IsRel)
+{
+ pkgVersionMatch Match(VerTag,(IsRel == true?pkgVersionMatch::Release :
+ pkgVersionMatch::Version));
+
+ pkgCache::VerIterator Ver = Match.Find(Pkg);
+
+ if (Ver.end() == true)
+ {
+ if (IsRel == true)
+ return _error->Error(_("Release '%s' for '%s' was not found"),
+ VerTag,Pkg.Name());
+ return _error->Error(_("Version '%s' for '%s' was not found"),
+ VerTag,Pkg.Name());
+ }
+
+ if (strcmp(VerTag,Ver.VerStr()) != 0)
+ {
+ ioprintf(c1out,_("Selected version %s (%s) for %s\n"),
+ Ver.VerStr(),Ver.RelStr().c_str(),Pkg.Name());
+ }
+
+ Cache.SetCandidateVersion(Ver);
+ return true;
+}
+ /*}}}*/
+// FindSrc - Find a source record /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+pkgSrcRecords::Parser *FindSrc(const char *Name,pkgRecords &Recs,
+ pkgSrcRecords &SrcRecs,string &Src,
+ pkgDepCache &Cache)
+{
+ // We want to pull the version off the package specification..
+ string VerTag;
+ string TmpSrc = Name;
+ string::size_type Slash = TmpSrc.rfind('=');
+ if (Slash != string::npos)
+ {
+ VerTag = string(TmpSrc.begin() + Slash + 1,TmpSrc.end());
+ TmpSrc = string(TmpSrc.begin(),TmpSrc.begin() + Slash);
+ }
+
+ /* Lookup the version of the package we would install if we were to
+ install a version and determine the source package name, then look
+ in the archive for a source package of the same name. In theory
+ we could stash the version string as well and match that too but
+ today there aren't multi source versions in the archive. */
+ if (_config->FindB("APT::Get::Only-Source") == false &&
+ VerTag.empty() == true)
+ {
+ pkgCache::PkgIterator Pkg = Cache.FindPkg(TmpSrc);
+ if (Pkg.end() == false)
+ {
+ pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
+ if (Ver.end() == false)
+ {
+ pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
+ Src = Parse.SourcePkg();
+ }
+ }
+ }
+
+ // No source package name..
+ if (Src.empty() == true)
+ Src = TmpSrc;
+
+ // The best hit
+ pkgSrcRecords::Parser *Last = 0;
+ unsigned long Offset = 0;
+ string Version;
+ bool IsMatch = false;
+
+ // If we are matching by version then we need exact matches to be happy
+ if (VerTag.empty() == false)
+ IsMatch = true;
+
+ /* Iterate over all of the hits, which includes the resulting
+ binary packages in the search */
+ pkgSrcRecords::Parser *Parse;
+ SrcRecs.Restart();
+ while ((Parse = SrcRecs.Find(Src.c_str(),false)) != 0)
+ {
+ string Ver = Parse->Version();
+
+ // Skip name mismatches
+ if (IsMatch == true && Parse->Package() != Src)
+ continue;
+
+ if (VerTag.empty() == false)
+ {
+ /* Don't want to fall through because we are doing exact version
+ matching. */
+ if (Cache.VS().CmpVersion(VerTag,Ver) != 0)
+ continue;
+
+ Last = Parse;
+ Offset = Parse->Offset();
+ break;
+ }
+
+ // Newer version or an exact match
+ if (Last == 0 || Cache.VS().CmpVersion(Version,Ver) < 0 ||
+ (Parse->Package() == Src && IsMatch == false))
+ {
+ IsMatch = Parse->Package() == Src;
+ Last = Parse;
+ Offset = Parse->Offset();
+ Version = Ver;
+ }
+ }
+
+ if (Last == 0)
+ return 0;
+
+ if (Last->Jump(Offset) == false)
+ return 0;
+
+ return Last;
+}
+ /*}}}*/