merged from debian-apt
[ntk/apt.git] / apt-pkg / pkgcache.cc
index 93d09a1..2b8cb6b 100644 (file)
@@ -84,6 +84,8 @@ pkgCache::Header::Header()
    memset(PkgHashTable,0,sizeof(PkgHashTable));
    memset(GrpHashTable,0,sizeof(GrpHashTable));
    memset(Pools,0,sizeof(Pools));
+
+   CacheFileSize = 0;
 }
                                                                        /*}}}*/
 // Cache::Header::CheckSizes - Check if the two headers have same *sz  /*{{{*/
@@ -155,6 +157,9 @@ bool pkgCache::ReMap(bool const &Errorchecks)
        HeaderP->CheckSizes(DefHeader) == false)
       return _error->Error(_("The package cache file is an incompatible version"));
 
+   if (Map.Size() < HeaderP->CacheFileSize)
+      return _error->Error(_("The package cache file is corrupted, it is too small"));
+
    // Locate our VS..
    if (HeaderP->VerSysName == 0 ||
        (VS = pkgVersioningSystem::GetVS(StrP + HeaderP->VerSysName)) == 0)
@@ -220,6 +225,9 @@ pkgCache::PkgIterator pkgCache::FindPkg(const string &Name) {
                        return FindPkg(Name, "native");
        }
        string const Arch = Name.substr(found+1);
+       /* Beware: This is specialcased to handle pkg:any in dependencies as
+          these are linked to virtual pkg:any named packages with all archs.
+          If you want any arch from a given pkg, use FindPkg(pkg,arch) */
        if (Arch == "any")
                return FindPkg(Name, "any");
        return FindPkg(Name.substr(0, found), Arch);
@@ -231,7 +239,7 @@ pkgCache::PkgIterator pkgCache::FindPkg(const string &Name) {
 pkgCache::PkgIterator pkgCache::FindPkg(const string &Name, string const &Arch) {
        if (MultiArchCache() == false) {
                if (Arch == "native" || Arch == "all" || Arch == "any" ||
-                   Arch == _config->Find("APT::Architecture"))
+                   Arch == NativeArch())
                        return SingleArchFindPkg(Name);
                else
                        return PkgIterator(*this,0);
@@ -325,15 +333,15 @@ pkgCache::PkgIterator pkgCache::GrpIterator::FindPkg(string Arch) const {
        if (Arch == "any")
                return PkgIterator(*Owner, Owner->PkgP + S->FirstPackage);
 
-       static string const myArch = _config->Find("APT::Architecture");
+       char const* const myArch = Owner->NativeArch();
        /* Most of the time the package for our native architecture is
           the one we add at first to the cache, but this would be the
           last one we check, so we do it now. */
        if (Arch == "native" || Arch == myArch || Arch == "all") {
-               Arch = myArch;
                pkgCache::Package *Pkg = Owner->PkgP + S->LastPackage;
-               if (stringcasecmp(Arch, Owner->StrP + Pkg->Arch) == 0)
+               if (strcasecmp(myArch, Owner->StrP + Pkg->Arch) == 0)
                        return PkgIterator(*Owner, Pkg);
+               Arch = myArch;
        }
 
        /* Iterate over the list to find the matching arch
@@ -506,7 +514,8 @@ std::string pkgCache::PkgIterator::FullName(bool const &Pretty) const
 {
    string fullname = Name();
    if (Pretty == false ||
-       (strcmp(Arch(), "all") != 0 && _config->Find("APT::Architecture") != Arch()))
+       (strcmp(Arch(), "all") != 0 &&
+       strcmp(Owner->NativeArch(), Arch()) != 0))
       return fullname.append(":").append(Arch());
    return fullname;
 }
@@ -517,15 +526,24 @@ std::string pkgCache::PkgIterator::FullName(bool const &Pretty) const
    conflicts (including dpkg's Breaks fields). */
 bool pkgCache::DepIterator::IsCritical() const
 {
-   if (S->Type == pkgCache::Dep::Conflicts ||
-       S->Type == pkgCache::Dep::DpkgBreaks ||
-       S->Type == pkgCache::Dep::Obsoletes ||
+   if (IsNegative() == true ||
        S->Type == pkgCache::Dep::Depends ||
        S->Type == pkgCache::Dep::PreDepends)
       return true;
    return false;
 }
                                                                        /*}}}*/
+// DepIterator::IsNegative - Returns true if the dep is a negative one /*{{{*/
+// ---------------------------------------------------------------------
+/* Some dependencies are positive like Depends and Recommends, others
+   are negative like Conflicts which can and should be handled differently */
+bool pkgCache::DepIterator::IsNegative() const
+{
+   return S->Type == Dep::DpkgBreaks ||
+         S->Type == Dep::Conflicts ||
+         S->Type == Dep::Obsoletes;
+}
+                                                                       /*}}}*/
 // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides        /*{{{*/
 // ---------------------------------------------------------------------
 /* This intellegently looks at dep target packages and tries to figure
@@ -603,9 +621,7 @@ pkgCache::Version **pkgCache::DepIterator::AllTargets() const
         if (Owner->VS->CheckDep(I.VerStr(),S->CompareOp,TargetVer()) == false)
            continue;
 
-        if ((S->Type == pkgCache::Dep::Conflicts ||
-             S->Type == pkgCache::Dep::DpkgBreaks ||
-             S->Type == pkgCache::Dep::Obsoletes) &&
+        if (IsNegative() == true &&
             ParentPkg() == I.ParentPkg())
            continue;
         
@@ -620,9 +636,7 @@ pkgCache::Version **pkgCache::DepIterator::AllTargets() const
         if (Owner->VS->CheckDep(I.ProvideVersion(),S->CompareOp,TargetVer()) == false)
            continue;
         
-        if ((S->Type == pkgCache::Dep::Conflicts ||
-             S->Type == pkgCache::Dep::DpkgBreaks ||
-             S->Type == pkgCache::Dep::Obsoletes) &&
+        if (IsNegative() == true &&
             ParentPkg() == I.OwnerPkg())
            continue;
         
@@ -740,9 +754,6 @@ bool pkgCache::VerIterator::Automatic() const
    return false;
 }
                                                                        /*}}}*/
-// VerIterator::Pseudo - deprecated no-op method                       /*{{{*/
-bool pkgCache::VerIterator::Pseudo() const { return false; }
-                                                                       /*}}}*/
 // VerIterator::NewestFile - Return the newest file version relation   /*{{{*/
 // ---------------------------------------------------------------------
 /* This looks at the version numbers associated with all of the sources