convert a few for-loop char finds to proper strchr and memchr
[ntk/apt.git] / apt-pkg / deb / deblistparser.cc
index 84eab44..0562be4 100644 (file)
@@ -19,6 +19,7 @@
 #include <apt-pkg/md5.h>
 #include <apt-pkg/macros.h>
 
+#include <fnmatch.h>
 #include <ctype.h>
                                                                        /*}}}*/
 
@@ -36,8 +37,10 @@ static debListParser::WordList PrioList[] = {{"important",pkgCache::State::Impor
    we would accept in general with checkArchitecture() */
 debListParser::debListParser(FileFd *File, string const &Arch) : Tags(File),
                                Arch(Arch) {
-       if (Arch == "native")
-               this->Arch = _config->Find("APT::Architecture");
+   if (Arch == "native")
+      this->Arch = _config->Find("APT::Architecture");
+   Architectures = APT::Configuration::getArchitectures();
+   MultiArchEnabled = Architectures.size() > 1;
 }
                                                                        /*}}}*/
 // ListParser::UniqFindTagWrite - Find the tag and write a unq string  /*{{{*/
@@ -56,37 +59,27 @@ unsigned long debListParser::UniqFindTagWrite(const char *Tag)
 // ---------------------------------------------------------------------
 /* This is to return the name of the package this section describes */
 string debListParser::Package() {
-       string const Result = Section.FindS("Package");
-       if(unlikely(Result.empty() == true))
-               _error->Error("Encountered a section with no Package: header");
-       return Result;
+   string const Result = Section.FindS("Package");
+   if(unlikely(Result.empty() == true))
+      _error->Error("Encountered a section with no Package: header");
+   return Result;
 }
                                                                        /*}}}*/
 // ListParser::Architecture - Return the package arch                  /*{{{*/
 // ---------------------------------------------------------------------
-/* This will return the Architecture of the package this section describes
-   Note that architecture "all" packages will get the architecture of the
-   Packages file parsed here. */
+/* This will return the Architecture of the package this section describes */
 string debListParser::Architecture() {
-       string const Result = Section.FindS("Architecture");
-       if (Result.empty() == true || Result == "all") {
-               if (Arch.empty() == true)
-                       /* FIXME: this is a problem for installed arch all
-                          packages as we don't know from which arch this
-                          package was installed - and therefore which
-                          dependency this package resolves. */
-                       return _config->Find("APT::Architecture");
-               else
-                       return Arch;
-       }
-       return Result;
+   std::string const Arch = Section.FindS("Architecture");
+   if (Arch.empty() == true)
+      return _config->Find("APT::Architecture");
+   return Arch;
 }
                                                                        /*}}}*/
 // ListParser::ArchitectureAll                                         /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 bool debListParser::ArchitectureAll() {
-       return Section.FindS("Architecture") == "all";
+   return Section.FindS("Architecture") == "all";
 }
                                                                        /*}}}*/
 // ListParser::Version - Return the version string                     /*{{{*/
@@ -102,40 +95,50 @@ string debListParser::Version()
 // ListParser::NewVersion - Fill in the version structure              /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool debListParser::NewVersion(pkgCache::VerIterator Ver)
+bool debListParser::NewVersion(pkgCache::VerIterator &Ver)
 {
    // Parse the section
    Ver->Section = UniqFindTagWrite("Section");
 
    // Parse multi-arch
-   if (Section.FindS("Architecture") == "all")
-      /* Arch all packages can't have a Multi-Arch field,
-         but we need a special treatment for them nonetheless */
-      Ver->MultiArch = pkgCache::Version::All;
-   else
-   {
-      string const MultiArch = Section.FindS("Multi-Arch");
-      if (MultiArch.empty() == true)
-        Ver->MultiArch = pkgCache::Version::None;
-      else if (MultiArch == "same")
-        Ver->MultiArch = pkgCache::Version::Same;
-      else if (MultiArch == "foreign")
-        Ver->MultiArch = pkgCache::Version::Foreign;
-      else if (MultiArch == "allowed")
-        Ver->MultiArch = pkgCache::Version::Allowed;
-      else
+   string const MultiArch = Section.FindS("Multi-Arch");
+   if (MultiArch.empty() == true)
+      Ver->MultiArch = pkgCache::Version::None;
+   else if (MultiArch == "same") {
+      // Parse multi-arch
+      if (ArchitectureAll() == true)
       {
-        _error->Warning("Unknown Multi-Arch type »%s« for package »%s«",
-                       MultiArch.c_str(), Section.FindS("Package").c_str());
+        /* Arch all packages can't be Multi-Arch: same */
+        _error->Warning("Architecture: all package '%s' can't be Multi-Arch: same",
+                       Section.FindS("Package").c_str());
         Ver->MultiArch = pkgCache::Version::None;
       }
+      else
+        Ver->MultiArch = pkgCache::Version::Same;
+   }
+   else if (MultiArch == "foreign")
+      Ver->MultiArch = pkgCache::Version::Foreign;
+   else if (MultiArch == "allowed")
+      Ver->MultiArch = pkgCache::Version::Allowed;
+   else
+   {
+      _error->Warning("Unknown Multi-Arch type '%s' for package '%s'",
+                       MultiArch.c_str(), Section.FindS("Package").c_str());
+      Ver->MultiArch = pkgCache::Version::None;
    }
 
+   if (ArchitectureAll() == true)
+      switch (Ver->MultiArch)
+      {
+        case pkgCache::Version::Foreign: Ver->MultiArch = pkgCache::Version::AllForeign; break;
+        case pkgCache::Version::Allowed: Ver->MultiArch = pkgCache::Version::AllAllowed; break;
+        default: Ver->MultiArch = pkgCache::Version::All;
+      }
+
    // Archive Size
-   Ver->Size = (unsigned)Section.FindI("Size");
-   
+   Ver->Size = Section.FindULL("Size");
    // Unpacked Size (in K)
-   Ver->InstalledSize = (unsigned)Section.FindI("Installed-Size");
+   Ver->InstalledSize = Section.FindULL("Installed-Size");
    Ver->InstalledSize *= 1024;
 
    // Priority
@@ -147,25 +150,6 @@ bool debListParser::NewVersion(pkgCache::VerIterator Ver)
         Ver->Priority = pkgCache::State::Extra;
    }
 
-   if (Ver->MultiArch == pkgCache::Version::All)
-   {
-      /* We maintain a "pseudo" arch=all package for architecture all versions
-        on which these versions can depend on. This pseudo package is many used
-        for downloading/installing: The other pseudo-packages will degenerate
-        to a NOP in the download/install step - this package will ensure that
-        it is downloaded only one time and installed only one time -- even if
-        the architecture bound versions coming in and out on regular basis. */
-      bool const static multiArch = APT::Configuration::getArchitectures().size() > 1;
-      if (strcmp(Ver.Arch(true),"all") == 0)
-        return true;
-      else if (multiArch == true)
-      {
-        // our pseudo packages have no size to not confuse the fetcher
-        Ver->Size = 0;
-        Ver->InstalledSize = 0;
-      }
-   }
-
    if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false)
       return false;
    if (ParseDepends(Ver,"Pre-Depends",pkgCache::Dep::PreDepends) == false)
@@ -217,9 +201,9 @@ string debListParser::DescriptionLanguage()
    if (Section.FindS("Description").empty() == false)
       return "";
 
-   std::vector<string> const lang = APT::Configuration::getLanguages();
+   std::vector<string> const lang = APT::Configuration::getLanguages(true);
    for (std::vector<string>::const_iterator l = lang.begin();
-       l != lang.end(); l++)
+       l != lang.end(); ++l)
       if (Section.FindS(string("Description-").append(*l).c_str()).empty() == false)
         return *l;
 
@@ -249,15 +233,19 @@ MD5SumValue debListParser::Description_md5()
 // ---------------------------------------------------------------------
 /* This is called to update the package with any new information 
    that might be found in the section */
-bool debListParser::UsePackage(pkgCache::PkgIterator Pkg,
-                              pkgCache::VerIterator Ver)
+bool debListParser::UsePackage(pkgCache::PkgIterator &Pkg,
+                              pkgCache::VerIterator &Ver)
 {
    if (Pkg->Section == 0)
       Pkg->Section = UniqFindTagWrite("Section");
 
-   // Packages which are not from "our" arch doesn't get the essential flag
+   // Packages which are not from the "native" arch doesn't get the essential flag
+   // in the default "native" mode - it is also possible to mark "all" or "none".
+   // The "installed" mode is handled by ParseStatus(), See #544481 and friends.
    string const static myArch = _config->Find("APT::Architecture");
-   if (Pkg->Arch != 0 && myArch == Pkg.Arch())
+   string const static essential = _config->Find("pkgCacheGen::Essential", "native");
+   if ((essential == "native" && Pkg->Arch != 0 && myArch == Pkg.Arch()) ||
+       essential == "all")
       if (Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
         return false;
    if (Section.FindFlag("Important",Pkg->Flags,pkgCache::Flag::Important) == false)
@@ -296,18 +284,18 @@ unsigned short debListParser::VersionHash()
       /* Strip out any spaces from the text, this undoes dpkgs reformatting
          of certain fields. dpkg also has the rather interesting notion of
          reformatting depends operators < -> <= */
-      char *I = S;
+      char *J = S;
       for (; Start != End; Start++)
       {
         if (isspace(*Start) == 0)
-           *I++ = tolower_ascii(*Start);
+           *J++ = tolower_ascii(*Start);
         if (*Start == '<' && Start[1] != '<' && Start[1] != '=')
-           *I++ = '=';
+           *J++ = '=';
         if (*Start == '>' && Start[1] != '>' && Start[1] != '=')
-           *I++ = '=';
+           *J++ = '=';
       }
 
-      Result = AddCRC16(Result,S,I - S);
+      Result = AddCRC16(Result,S,J - S);
    }
    
    return Result;
@@ -326,14 +314,20 @@ unsigned short debListParser::VersionHash()
    Some of the above are obsolete (I think?) flag = hold-* and 
    status = post-inst-failed, removal-failed at least.
  */
-bool debListParser::ParseStatus(pkgCache::PkgIterator Pkg,
-                               pkgCache::VerIterator Ver)
+bool debListParser::ParseStatus(pkgCache::PkgIterator &Pkg,
+                               pkgCache::VerIterator &Ver)
 {
    const char *Start;
    const char *Stop;
    if (Section.Find("Status",Start,Stop) == false)
       return true;
-   
+
+   // UsePackage() is responsible for setting the flag in the default case
+   bool const static essential = _config->Find("pkgCacheGen::Essential", "") == "installed";
+   if (essential == true &&
+       Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
+      return false;
+
    // Isolate the first word
    const char *I = Start;
    for(; I < Stop && *I != ' '; I++);
@@ -462,6 +456,21 @@ const char *debListParser::ConvertRelation(const char *I,unsigned int &Op)
    return I;
 }
 
+/*
+ * CompleteArch:
+ *
+ * The complete architecture, consisting of <kernel>-<cpu>.
+ */
+static string CompleteArch(std::string const &arch) {
+    if (arch == "armel")              return "linux-arm";
+    if (arch == "armhf")              return "linux-arm";
+    if (arch == "lpia")               return "linux-i386";
+    if (arch == "powerpcspe")         return "linux-powerpc";
+    if (arch == "uclibc-linux-armel") return "linux-arm";
+    if (arch == "uclinux-armel")      return "uclinux-arm";
+
+    return (arch.find("-") != string::npos) ? arch : "linux-" + arch;
+}
                                                                        /*}}}*/
 // ListParser::ParseDepends - Parse a dependency element               /*{{{*/
 // ---------------------------------------------------------------------
@@ -478,7 +487,7 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop,
    // Parse off the package name
    const char *I = Start;
    for (;I != Stop && isspace(*I) == 0 && *I != '(' && *I != ')' &&
-       *I != ',' && *I != '|'; I++);
+       *I != ',' && *I != '|' && *I != '[' && *I != ']'; I++);
    
    // Malformed, no '('
    if (I != Stop && *I == ')')
@@ -491,9 +500,13 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop,
    Package.assign(Start,I - Start);
 
    // We don't want to confuse library users which can't handle MultiArch
+   string const arch = _config->Find("APT::Architecture");
    if (StripMultiArch == true) {
       size_t const found = Package.rfind(':');
-      if (found != string::npos)
+      if (found != string::npos &&
+         (strcmp(Package.c_str() + found, ":any") == 0 ||
+          strcmp(Package.c_str() + found, ":native") == 0 ||
+          strcmp(Package.c_str() + found + 1, arch.c_str()) == 0))
         Package = Package.substr(0,found);
    }
 
@@ -512,9 +525,9 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop,
       // Skip whitespace
       for (;I != Stop && isspace(*I) != 0; I++);
       Start = I;
-      for (;I != Stop && *I != ')'; I++);
-      if (I == Stop || Start == I)
-        return 0;     
+      I = (const char*) memchr(I, ')', Stop - I);
+      if (I == NULL || Start == I)
+        return 0;
       
       // Skip trailing whitespace
       const char *End = I;
@@ -534,7 +547,7 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop,
 
    if (ParseArchFlags == true)
    {
-      string arch = _config->Find("APT::Architecture");
+      string completeArch = CompleteArch(arch);
 
       // Parse an architecture
       if (I != Stop && *I == '[')
@@ -562,8 +575,13 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop,
               I++;
             }
 
-           if (stringcmp(arch,I,End) == 0)
+           if (stringcmp(arch,I,End) == 0) {
               Found = true;
+           } else {
+              std::string wildcard = SubstVar(string(I, End), "any", "*");
+              if (fnmatch(wildcard.c_str(), completeArch.c_str(), 0) == 0)
+                 Found = true;
+           }
            
            if (*End++ == ']') {
               I = End;
@@ -602,26 +620,36 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop,
 // ---------------------------------------------------------------------
 /* This is the higher level depends parser. It takes a tag and generates
    a complete depends tree for the given version. */
-bool debListParser::ParseDepends(pkgCache::VerIterator Ver,
+bool debListParser::ParseDepends(pkgCache::VerIterator &Ver,
                                 const char *Tag,unsigned int Type)
 {
    const char *Start;
    const char *Stop;
    if (Section.Find(Tag,Start,Stop) == false)
       return true;
-   
+
    string Package;
-   string const pkgArch = Ver.Arch(true);
+   string const pkgArch = Ver.Arch();
    string Version;
    unsigned int Op;
 
    while (1)
    {
-      Start = ParseDepends(Start,Stop,Package,Version,Op);
+      Start = ParseDepends(Start,Stop,Package,Version,Op,false,!MultiArchEnabled);
       if (Start == 0)
         return _error->Error("Problem parsing dependency %s",Tag);
-      
-      if (NewDepends(Ver,Package,pkgArch,Version,Op,Type) == false)
+
+      if (MultiArchEnabled == true &&
+         (Type == pkgCache::Dep::Conflicts ||
+          Type == pkgCache::Dep::DpkgBreaks ||
+          Type == pkgCache::Dep::Replaces))
+      {
+        for (std::vector<std::string>::const_iterator a = Architectures.begin();
+             a != Architectures.end(); ++a)
+           if (NewDepends(Ver,Package,*a,Version,Op,Type) == false)
+              return false;
+      }
+      else if (NewDepends(Ver,Package,pkgArch,Version,Op,Type) == false)
         return false;
       if (Start == Stop)
         break;
@@ -632,7 +660,7 @@ bool debListParser::ParseDepends(pkgCache::VerIterator Ver,
 // ListParser::ParseProvides - Parse the provides list                 /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool debListParser::ParseProvides(pkgCache::VerIterator Ver)
+bool debListParser::ParseProvides(pkgCache::VerIterator &Ver)
 {
    const char *Start;
    const char *Stop;
@@ -640,7 +668,7 @@ bool debListParser::ParseProvides(pkgCache::VerIterator Ver)
    {
       string Package;
       string Version;
-      string const Arch = Ver.Arch(true);
+      string const Arch = Ver.Arch();
       unsigned int Op;
 
       while (1)
@@ -660,28 +688,28 @@ bool debListParser::ParseProvides(pkgCache::VerIterator Ver)
       }
    }
 
-   if (Ver->MultiArch == pkgCache::Version::Allowed)
+   if (MultiArchEnabled == false)
+      return true;
+   else if (Ver->MultiArch == pkgCache::Version::Allowed || Ver->MultiArch == pkgCache::Version::AllAllowed)
    {
       string const Package = string(Ver.ParentPkg().Name()).append(":").append("any");
-      NewProvides(Ver, Package, "any", Ver.VerStr());
+      return NewProvidesAllArch(Ver, Package, Ver.VerStr());
    }
+   else if (Ver->MultiArch == pkgCache::Version::Foreign || Ver->MultiArch == pkgCache::Version::AllForeign)
+      return NewProvidesAllArch(Ver, Ver.ParentPkg().Name(), Ver.VerStr());
 
-   if (Ver->MultiArch != pkgCache::Version::Foreign)
-      return true;
-
-   std::vector<string> const archs = APT::Configuration::getArchitectures();
-   if (archs.size() <= 1)
-      return true;
-
-   string const Package = Ver.ParentPkg().Name();
-   string const Version = Ver.VerStr();
-   for (std::vector<string>::const_iterator a = archs.begin();
-       a != archs.end(); ++a)
+   return true;
+}
+                                                                       /*}}}*/
+// ListParser::NewProvides - add provides for all architectures                /*{{{*/
+bool debListParser::NewProvidesAllArch(pkgCache::VerIterator &Ver, string const &Package,
+                               string const &Version) {
+   for (std::vector<string>::const_iterator a = Architectures.begin();
+       a != Architectures.end(); ++a)
    {
       if (NewProvides(Ver, Package, *a, Version) == false)
         return false;
    }
-
    return true;
 }
                                                                        /*}}}*/
@@ -716,7 +744,7 @@ bool debListParser::Step()
       if (Architecture.empty() == true)
         return true;
 
-      if (Arch.empty() == true)
+      if (Arch.empty() == true || Arch == "any" || MultiArchEnabled == false)
       {
         if (APT::Configuration::checkArchitecture(Architecture) == true)
            return true;
@@ -726,7 +754,7 @@ bool debListParser::Step()
         if (Architecture == Arch)
            return true;
 
-        if (Architecture == "all")
+        if (Architecture == "all" && Arch == _config->Find("APT::Architecture"))
            return true;
       }
 
@@ -738,42 +766,101 @@ bool debListParser::Step()
 // ListParser::LoadReleaseInfo - Load the release information          /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool debListParser::LoadReleaseInfo(pkgCache::PkgFileIterator FileI,
+bool debListParser::LoadReleaseInfo(pkgCache::PkgFileIterator &FileI,
                                    FileFd &File, string component)
 {
-   pkgTagFile Tags(&File, File.Size() + 256); // XXX
-   pkgTagSection Section;
-   if (Tags.Step(Section) == false)
-      return false;
-
-   // FIXME: Do we need it now for multi-arch?
-   // mvo: I don't think we need to fill that in (it's unused since apt-0.6)
-//    FileI->Architecture = WriteUniqString(Arch);
-   
    // apt-secure does no longer download individual (per-section) Release
    // file. to provide Component pinning we use the section name now
    FileI->Component = WriteUniqString(component);
 
-   const char *Start;
-   const char *Stop;
-   if (Section.Find("Suite",Start,Stop) == true)
-      FileI->Archive = WriteUniqString(Start,Stop - Start);
-   if (Section.Find("Component",Start,Stop) == true)
-      FileI->Component = WriteUniqString(Start,Stop - Start);
-   if (Section.Find("Version",Start,Stop) == true)
-      FileI->Version = WriteUniqString(Start,Stop - Start);
-   if (Section.Find("Origin",Start,Stop) == true)
-      FileI->Origin = WriteUniqString(Start,Stop - Start);
-   if (Section.Find("Codename",Start,Stop) == true)
-      FileI->Codename = WriteUniqString(Start,Stop - Start);
-   if (Section.Find("Label",Start,Stop) == true)
-      FileI->Label = WriteUniqString(Start,Stop - Start);
-   if (Section.Find("Architecture",Start,Stop) == true)
-      FileI->Architecture = WriteUniqString(Start,Stop - Start);
-   
-   if (Section.FindFlag("NotAutomatic",FileI->Flags,
-                       pkgCache::Flag::NotAutomatic) == false)
-      _error->Warning("Bad NotAutomatic flag");
+   FILE* release = fdopen(dup(File.Fd()), "r");
+   if (release == NULL)
+      return false;
+
+   char buffer[101];
+   bool gpgClose = false;
+   while (fgets(buffer, sizeof(buffer), release) != NULL)
+   {
+      size_t len = 0;
+
+      // Skip empty lines
+      for (; buffer[len] == '\r' && buffer[len] == '\n'; ++len)
+         /* nothing */
+         ;
+      if (buffer[len] == '\0')
+        continue;
+
+      // only evalute the first GPG section
+      if (strncmp("-----", buffer, 5) == 0)
+      {
+        if (gpgClose == true)
+           break;
+        gpgClose = true;
+        continue;
+      }
+
+      // seperate the tag from the data
+      const char* dataStart = strchr(buffer + len, ':');
+      if (dataStart == NULL)
+        continue;
+      len = dataStart - buffer;
+      for (++dataStart; *dataStart == ' '; ++dataStart)
+         /* nothing */
+         ;
+      const char* dataEnd = (const char*)rawmemchr(dataStart, '\0');
+      // The last char should be a newline, but we can never be sure: #633350
+      const char* lineEnd = dataEnd;
+      for (--lineEnd; *lineEnd == '\r' || *lineEnd == '\n'; --lineEnd)
+         /* nothing */
+         ;
+      ++lineEnd;
+
+      // which datastorage need to be updated
+      map_ptrloc* writeTo = NULL;
+      if (buffer[0] == ' ')
+        ;
+      #define APT_PARSER_WRITETO(X, Y) else if (strncmp(Y, buffer, len) == 0) writeTo = &X;
+      APT_PARSER_WRITETO(FileI->Archive, "Suite")
+      APT_PARSER_WRITETO(FileI->Component, "Component")
+      APT_PARSER_WRITETO(FileI->Version, "Version")
+      APT_PARSER_WRITETO(FileI->Origin, "Origin")
+      APT_PARSER_WRITETO(FileI->Codename, "Codename")
+      APT_PARSER_WRITETO(FileI->Label, "Label")
+      #undef APT_PARSER_WRITETO
+      #define APT_PARSER_FLAGIT(X) else if (strncmp(#X, buffer, len) == 0) \
+        pkgTagSection::FindFlag(FileI->Flags, pkgCache::Flag:: X, dataStart, lineEnd);
+      APT_PARSER_FLAGIT(NotAutomatic)
+      APT_PARSER_FLAGIT(ButAutomaticUpgrades)
+      #undef APT_PARSER_FLAGIT
+
+      // load all data from the line and save it
+      string data;
+      if (writeTo != NULL)
+        data.append(dataStart, dataEnd);
+      if (sizeof(buffer) - 1 == (dataEnd - buffer))
+      {
+        while (fgets(buffer, sizeof(buffer), release) != NULL)
+        {
+           if (writeTo != NULL)
+              data.append(buffer);
+           if (strlen(buffer) != sizeof(buffer) - 1)
+              break;
+        }
+      }
+      if (writeTo != NULL)
+      {
+        // remove spaces and stuff from the end of the data line
+        for (std::string::reverse_iterator s = data.rbegin();
+             s != data.rend(); ++s)
+        {
+           if (*s != '\r' && *s != '\n' && *s != ' ')
+              break;
+           *s = '\0';
+        }
+        *writeTo = WriteUniqString(data);
+      }
+   }
+   fclose(release);
 
    return !_error->PendingError();
 }