]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/deb/deblistparser.cc
Store tags in the cache (they are very useful :/).
[apt.git] / apt-pkg / deb / deblistparser.cc
index ed5484ad97a0a5720134dffcc137ee1763ff1ce4..878170d0074eeabecb098efa34a94b4f047908b7 100644 (file)
@@ -23,6 +23,7 @@
 #include <apt-pkg/pkgcache.h>
 #include <apt-pkg/cacheiterators.h>
 #include <apt-pkg/tagfile.h>
+#include <apt-pkg/tagfile-keys.h>
 #include <apt-pkg/macros.h>
 
 #include <stddef.h>
@@ -50,15 +51,25 @@ static const debListParser::WordList PrioList[] = {
    in Step(), if no Architecture is given we will accept every arch
    we would accept in general with checkArchitecture() */
 debListParser::debListParser(FileFd *File) :
-   pkgCacheListParser(), d(NULL), Tags(File)
+   pkgCacheListParser(), Tags(File)
 {
+   // this dance allows an empty value to override the default
+   if (_config->Exists("pkgCacheGen::ForceEssential"))
+   {
+      forceEssential = _config->FindVector("pkgCacheGen::ForceEssential");
+      if (forceEssential.empty() == false && _config->Find("pkgCacheGen::ForceEssential").empty())
+        forceEssential.emplace_back("apt");
+   }
+   else
+      forceEssential.emplace_back("apt");
+   forceImportant = _config->FindVector("pkgCacheGen::ForceImportant");
 }
                                                                        /*}}}*/
 // ListParser::Package - Return the package name                       /*{{{*/
 // ---------------------------------------------------------------------
 /* This is to return the name of the package this section describes */
 string debListParser::Package() {
-   string Result = Section.Find("Package").to_string();
+   string Result = Section.Find(pkgTagSection::Key::Package).to_string();
 
    // Normalize mixed case package names to lower case, like dpkg does
    // See Bug#807012 for details
@@ -73,7 +84,7 @@ string debListParser::Package() {
 // ---------------------------------------------------------------------
 /* This will return the Architecture of the package this section describes */
 APT::StringView debListParser::Architecture() {
-   auto const Arch = Section.Find("Architecture");
+   auto const Arch = Section.Find(pkgTagSection::Key::Architecture);
    return Arch.empty() ? "none" : Arch;
 }
                                                                        /*}}}*/
@@ -81,7 +92,7 @@ APT::StringView debListParser::Architecture() {
 // ---------------------------------------------------------------------
 /* */
 bool debListParser::ArchitectureAll() {
-   return Section.Find("Architecture") == "all";
+   return Section.Find(pkgTagSection::Key::Architecture) == "all";
 }
                                                                        /*}}}*/
 // ListParser::Version - Return the version string                     /*{{{*/
@@ -91,13 +102,13 @@ bool debListParser::ArchitectureAll() {
    entry is assumed to only describe package properties */
 APT::StringView debListParser::Version()
 {
-   return Section.Find("Version");
+   return Section.Find(pkgTagSection::Key::Version);
 }
                                                                        /*}}}*/
 unsigned char debListParser::ParseMultiArch(bool const showErrors)     /*{{{*/
 {
    unsigned char MA;
-   auto const MultiArch = Section.Find("Multi-Arch");
+   auto const MultiArch = Section.Find(pkgTagSection::Key::Multi_Arch);
    if (MultiArch.empty() == true || MultiArch == "no")
       MA = pkgCache::Version::No;
    else if (MultiArch == "same") {
@@ -138,7 +149,7 @@ bool debListParser::NewVersion(pkgCache::VerIterator &Ver)
    const char *Stop;
 
    // Parse the section
-   if (Section.Find("Section",Start,Stop) == true)
+   if (Section.Find(pkgTagSection::Key::Section,Start,Stop) == true)
    {
       map_stringitem_t const idx = StoreString(pkgCacheGenerator::SECTION, Start, Stop - Start);
       Ver->Section = idx;
@@ -147,7 +158,7 @@ bool debListParser::NewVersion(pkgCache::VerIterator &Ver)
    pkgCache::GrpIterator G = Ver.ParentPkg().Group();
    Ver->SourcePkgName = G->Name;
    Ver->SourceVerStr = Ver->VerStr;
-   if (Section.Find("Source",Start,Stop) == true)
+   if (Section.Find(pkgTagSection::Key::Source,Start,Stop) == true)
    {
       const char * const Space = (const char * const) memchr(Start, ' ', Stop - Start);
       pkgCache::VerIterator V;
@@ -199,40 +210,42 @@ bool debListParser::NewVersion(pkgCache::VerIterator &Ver)
 
    Ver->MultiArch = ParseMultiArch(true);
    // Archive Size
-   Ver->Size = Section.FindULL("Size");
+   Ver->Size = Section.FindULL(pkgTagSection::Key::Size);
    // Unpacked Size (in K)
-   Ver->InstalledSize = Section.FindULL("Installed-Size");
+   Ver->InstalledSize = Section.FindULL(pkgTagSection::Key::Installed_Size);
    Ver->InstalledSize *= 1024;
 
    // Priority
-   if (Section.Find("Priority",Start,Stop) == true)
+   if (Section.Find(pkgTagSection::Key::Priority,Start,Stop) == true)
    {
       if (GrabWord(StringView(Start,Stop-Start),PrioList,Ver->Priority) == false)
         Ver->Priority = pkgCache::State::Extra;
    }
 
-   if (ParseDepends(Ver,"Pre-Depends",pkgCache::Dep::PreDepends) == false)
+   if (ParseDepends(Ver,pkgTagSection::Key::Pre_Depends,pkgCache::Dep::PreDepends) == false)
       return false;
-   if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false)
+   if (ParseDepends(Ver,pkgTagSection::Key::Depends,pkgCache::Dep::Depends) == false)
       return false;
-   if (ParseDepends(Ver,"Conflicts",pkgCache::Dep::Conflicts) == false)
+   if (ParseDepends(Ver,pkgTagSection::Key::Conflicts,pkgCache::Dep::Conflicts) == false)
       return false;
-   if (ParseDepends(Ver,"Breaks",pkgCache::Dep::DpkgBreaks) == false)
+   if (ParseDepends(Ver,pkgTagSection::Key::Breaks,pkgCache::Dep::DpkgBreaks) == false)
       return false;
-   if (ParseDepends(Ver,"Recommends",pkgCache::Dep::Recommends) == false)
+   if (ParseDepends(Ver,pkgTagSection::Key::Recommends,pkgCache::Dep::Recommends) == false)
       return false;
-   if (ParseDepends(Ver,"Suggests",pkgCache::Dep::Suggests) == false)
+   if (ParseDepends(Ver,pkgTagSection::Key::Suggests,pkgCache::Dep::Suggests) == false)
       return false;
-   if (ParseDepends(Ver,"Replaces",pkgCache::Dep::Replaces) == false)
+   if (ParseDepends(Ver,pkgTagSection::Key::Replaces,pkgCache::Dep::Replaces) == false)
       return false;
-   if (ParseDepends(Ver,"Enhances",pkgCache::Dep::Enhances) == false)
+   if (ParseDepends(Ver,pkgTagSection::Key::Enhances,pkgCache::Dep::Enhances) == false)
       return false;
    // Obsolete.
-   if (ParseDepends(Ver,"Optional",pkgCache::Dep::Suggests) == false)
+   if (ParseDepends(Ver,pkgTagSection::Key::Optional,pkgCache::Dep::Suggests) == false)
       return false;
    
    if (ParseProvides(Ver) == false)
       return false;
+   if (ParseTag(Ver) == false)
+      return false;
    
    return true;
 }
@@ -243,18 +256,17 @@ std::vector<std::string> debListParser::AvailableDescriptionLanguages()
    std::vector<std::string> const understood = APT::Configuration::getLanguages();
    std::vector<std::string> avail;
    static constexpr int prefixLen = 12;
-   static constexpr int avgLanguageLen = 5;
-   std::string tagname;
-
-   tagname.reserve(prefixLen + avgLanguageLen);
-   tagname.assign("Description-");
+   char buf[32] = "Description-";
    if (Section.Exists("Description") == true)
       avail.push_back("");
    for (std::vector<std::string>::const_iterator lang = understood.begin(); lang != understood.end(); ++lang)
    {
-      tagname.resize(prefixLen);
-      tagname.append(*lang);
-      if (Section.Exists(tagname) == true)
+      if (unlikely(lang->size() > sizeof(buf) - prefixLen)) {
+        _error->Warning("Ignoring translated description %s", lang->c_str());
+        continue;
+      }
+      memcpy(buf + prefixLen, lang->c_str(), lang->size());
+      if (Section.Exists(StringView(buf, prefixLen + lang->size())) == true)
         avail.push_back(*lang);
    }
    return avail;
@@ -266,31 +278,27 @@ std::vector<std::string> debListParser::AvailableDescriptionLanguages()
    description. If no Description-md5 is found in the section it will be
    calculated.
  */
-MD5SumValue debListParser::Description_md5()
+APT::StringView debListParser::Description_md5()
 {
-   StringView const value = Section.Find("Description-md5");
-   if (value.empty() == true)
+   StringView const value = Section.Find(pkgTagSection::Key::Description_md5);
+   if (unlikely(value.empty() == true))
    {
-      StringView const desc = Section.Find("Description");
+      StringView const desc = Section.Find(pkgTagSection::Key::Description);
       if (desc == "\n")
-        return MD5SumValue();
+        return StringView();
 
       MD5Summation md5;
       md5.Add(desc.data(), desc.size());
       md5.Add("\n");
-      return md5.Result();
+      MD5Buffer = md5.Result();
+      return StringView(MD5Buffer);
    }
    else if (likely(value.size() == 32))
    {
-      MD5SumValue sumvalue;
-      if (sumvalue.Set(value))
-        return sumvalue;
-
-      _error->Error("Malformed Description-md5 line; includes invalid character '%.*s'", (int)value.length(), value.data());
-      return MD5SumValue();
+      return value;
    }
    _error->Error("Malformed Description-md5 line; doesn't have the required length (32 != %d) '%.*s'", (int)value.size(), (int)value.length(), value.data());
-   return MD5SumValue();
+   return StringView();
 }
                                                                         /*}}}*/
 // ListParser::UsePackage - Update a package structure                 /*{{{*/
@@ -306,12 +314,12 @@ bool debListParser::UsePackage(pkgCache::PkgIterator &Pkg,
    string const static essential = _config->Find("pkgCacheGen::Essential", "all");
    if (essential == "all" ||
        (essential == "native" && Pkg->Arch != 0 && myArch == Pkg.Arch()))
-      if (Section.FindFlag("Essential",Pkg->Flags,pkgCache::Flag::Essential) == false)
+      if (Section.FindFlag(pkgTagSection::Key::Essential,Pkg->Flags,pkgCache::Flag::Essential) == false)
         return false;
-   if (Section.FindFlag("Important",Pkg->Flags,pkgCache::Flag::Important) == false)
+   if (Section.FindFlag(pkgTagSection::Key::Important,Pkg->Flags,pkgCache::Flag::Important) == false)
       return false;
 
-   if (strcmp(Pkg.Name(),"apt") == 0)
+   if (std::find(forceEssential.begin(), forceEssential.end(), Pkg.Name()) != forceEssential.end())
    {
       if ((essential == "native" && Pkg->Arch != 0 && myArch == Pkg.Arch()) ||
          essential == "all")
@@ -319,6 +327,8 @@ bool debListParser::UsePackage(pkgCache::PkgIterator &Pkg,
       else
         Pkg->Flags |= pkgCache::Flag::Important;
    }
+   else if (std::find(forceImportant.begin(), forceImportant.end(), Pkg.Name()) != forceImportant.end())
+      Pkg->Flags |= pkgCache::Flag::Important;
 
    if (ParseStatus(Pkg,Ver) == false)
       return false;
@@ -330,38 +340,35 @@ bool debListParser::UsePackage(pkgCache::PkgIterator &Pkg,
 /* */
 unsigned short debListParser::VersionHash()
 {
-   static const StringView Sections[] ={"Installed-Size",
-                            "Depends",
-                            "Pre-Depends",
-//                            "Suggests",
-//                            "Recommends",
-                            "Conflicts",
-                            "Breaks",
-                            "Replaces"};
+   static constexpr pkgTagSection::Key Sections[] ={
+      pkgTagSection::Key::Installed_Size,
+      pkgTagSection::Key::Depends,
+      pkgTagSection::Key::Pre_Depends,
+//    pkgTagSection::Key::Suggests,
+//    pkgTagSection::Key::Recommends",
+      pkgTagSection::Key::Conflicts,
+      pkgTagSection::Key::Breaks,
+      pkgTagSection::Key::Replaces};
    unsigned long Result = INIT_FCS;
-   char S[1024];
-   for (StringView I : Sections)
+   for (auto I : Sections)
    {
       const char *Start;
       const char *End;
-      if (Section.Find(I,Start,End) == false || End - Start >= (signed)sizeof(S))
+      if (Section.Find(I,Start,End) == false)
         continue;
       
       /* 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 *J = S;
+         reformatting depends operators < -> <=, so we drop all = from the
+        string to make that not matter. */
       for (; Start != End; ++Start)
       {
-        if (isspace_ascii(*Start) != 0)
+        if (isspace_ascii(*Start) != 0 || *Start == '=')
            continue;
-        *J++ = tolower_ascii(*Start);
-
-        if ((*Start == '<' || *Start == '>') && Start[1] != *Start && Start[1] != '=')
-           *J++ = '=';
+        Result = AddCRC16Byte(Result, tolower_ascii_unsafe(*Start));
       }
 
-      Result = AddCRC16(Result,S,J - S);
+
    }
    
    return Result;
@@ -386,13 +393,13 @@ bool debStatusListParser::ParseStatus(pkgCache::PkgIterator &Pkg,
 {
    const char *Start;
    const char *Stop;
-   if (Section.Find("Status",Start,Stop) == false)
+   if (Section.Find(pkgTagSection::Key::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)
+       Section.FindFlag(pkgTagSection::Key::Essential,Pkg->Flags,pkgCache::Flag::Essential) == false)
       return false;
 
    // Isolate the first word
@@ -792,11 +799,11 @@ 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,
-                                StringView Tag,unsigned int Type)
+                                pkgTagSection::Key Key,unsigned int Type)
 {
    const char *Start;
    const char *Stop;
-   if (Section.Find(Tag,Start,Stop) == false || Start == Stop)
+   if (Section.Find(Key,Start,Stop) == false || Start == Stop)
       return true;
 
    string const pkgArch = Ver.Arch();
@@ -809,7 +816,7 @@ bool debListParser::ParseDepends(pkgCache::VerIterator &Ver,
 
       Start = ParseDepends(Start, Stop, Package, Version, Op, false, false, false);
       if (Start == 0)
-        return _error->Error("Problem parsing dependency %.*s",(int)Tag.length(), Tag.data());
+        return _error->Error("Problem parsing dependency %zu",static_cast<size_t>(Key)); // TODO
       size_t const found = Package.rfind(':');
 
       if (found == string::npos)
@@ -866,7 +873,7 @@ bool debListParser::ParseProvides(pkgCache::VerIterator &Ver)
    string const Arch = Ver.Arch();
    const char *Start;
    const char *Stop;
-   if (Section.Find("Provides",Start,Stop) == true)
+   if (Section.Find(pkgTagSection::Key::Provides,Start,Stop) == true)
    {
       StringView Package;
       StringView Version;
@@ -945,6 +952,46 @@ bool debListParser::ParseProvides(pkgCache::VerIterator &Ver)
            return false;
       }
    }
+   return true;
+}
+                                                                       /*}}}*/
+// ListParser::ParseTag - Parse the tag list                           /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool debListParser::ParseTag(pkgCache::VerIterator &Ver)
+{
+   const char *Start;
+   const char *Stop;
+   if (Section.Find("Tag",Start,Stop) == false)
+      return true;
+
+   while (1) {
+      while (1) {
+         if (Start == Stop)
+            return true;
+         if (Stop[-1] != ' ' && Stop[-1] != '\t')
+            break;
+         --Stop;
+      }
+
+      const char *Begin = Stop - 1;
+      while (Begin != Start && Begin[-1] != ' ' && Begin[-1] != ',')
+         --Begin;
+
+      if (NewTag(Ver, Begin, Stop - Begin) == false)
+         return false;
+
+      while (1) {
+         if (Begin == Start)
+            return true;
+         if (Begin[-1] == ',')
+            break;
+         --Begin;
+      }
+
+      Stop = Begin - 1;
+   }
+
    return true;
 }
                                                                        /*}}}*/
@@ -994,8 +1041,8 @@ bool debListParser::SameVersion(unsigned short const Hash,         /*{{{*/
    // status file has no (Download)Size, but all others are fair game
    // status file is parsed last, so the first version we encounter is
    // probably also the version we have downloaded
-   unsigned long long const Size = Section.FindULL("Size");
-   if (Size != 0 && Size != Ver->Size)
+   unsigned long long const Size = Section.FindULL(pkgTagSection::Key::Size);
+   if (Size != 0 && Ver->Size != 0 && Size != Ver->Size)
       return false;
    // available everywhere, but easier to check here than to include in VersionHash
    unsigned char MultiArch = ParseMultiArch(false);