X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/649d3c5b7df830a67ad946921233da349c13a826..62428dbc17ffa7b5b8188e88609a9438428d6024:/apt-pkg/deb/deblistparser.cc diff --git a/apt-pkg/deb/deblistparser.cc b/apt-pkg/deb/deblistparser.cc index 68d544e1f..8dca27a2a 100644 --- a/apt-pkg/deb/deblistparser.cc +++ b/apt-pkg/deb/deblistparser.cc @@ -18,54 +18,52 @@ #include #include #include -#include #include #include +#include +#include +#include #include +#include +#include +#include +#include +#include #include /*}}}*/ using std::string; +using APT::StringView; -static debListParser::WordList PrioList[] = { +static const debListParser::WordList PrioList[] = { {"required",pkgCache::State::Required}, {"important",pkgCache::State::Important}, {"standard",pkgCache::State::Standard}, {"optional",pkgCache::State::Optional}, {"extra",pkgCache::State::Extra}, - {}}; + {NULL, 0}}; // ListParser::debListParser - Constructor /*{{{*/ // --------------------------------------------------------------------- /* Provide an architecture and only this one and "all" will be accepted in Step(), if no Architecture is given we will accept every arch 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"); - Architectures = APT::Configuration::getArchitectures(); - MultiArchEnabled = Architectures.size() > 1; -} - /*}}}*/ -// ListParser::UniqFindTagWrite - Find the tag and write a unq string /*{{{*/ -// --------------------------------------------------------------------- -/* */ -unsigned long debListParser::UniqFindTagWrite(const char *Tag) +debListParser::debListParser(FileFd *File) : + pkgCacheListParser(), d(NULL), Tags(File) { - const char *Start; - const char *Stop; - if (Section.Find(Tag,Start,Stop) == false) - return 0; - return WriteUniqString(Start,Stop - Start); } /*}}}*/ // ListParser::Package - Return the package name /*{{{*/ // --------------------------------------------------------------------- /* This is to return the name of the package this section describes */ string debListParser::Package() { - string const Result = Section.FindS("Package"); + string Result = Section.Find("Package").to_string(); + + // Normalize mixed case package names to lower case, like dpkg does + // See Bug#807012 for details + std::transform(Result.begin(), Result.end(), Result.begin(), tolower_ascii); + if(unlikely(Result.empty() == true)) _error->Error("Encountered a section with no Package: header"); return Result; @@ -75,14 +73,15 @@ string debListParser::Package() { // --------------------------------------------------------------------- /* This will return the Architecture of the package this section describes */ string debListParser::Architecture() { - return Section.FindS("Architecture"); + auto const Arch = Section.Find("Architecture"); + return Arch.empty() ? "none" : Arch.to_string(); } /*}}}*/ // ListParser::ArchitectureAll /*{{{*/ // --------------------------------------------------------------------- /* */ bool debListParser::ArchitectureAll() { - return Section.FindS("Architecture") == "all"; + return Section.Find("Architecture") == "all"; } /*}}}*/ // ListParser::Version - Return the version string /*{{{*/ @@ -92,47 +91,111 @@ bool debListParser::ArchitectureAll() { entry is assumed to only describe package properties */ string debListParser::Version() { - return Section.FindS("Version"); + return Section.Find("Version").to_string(); } /*}}}*/ -// ListParser::NewVersion - Fill in the version structure /*{{{*/ -// --------------------------------------------------------------------- -/* */ -bool debListParser::NewVersion(pkgCache::VerIterator &Ver) +unsigned char debListParser::ParseMultiArch(bool const showErrors) /*{{{*/ { - // Parse the section - Ver->Section = UniqFindTagWrite("Section"); - - // Parse multi-arch - string const MultiArch = Section.FindS("Multi-Arch"); - if (MultiArch.empty() == true) - Ver->MultiArch = pkgCache::Version::None; + unsigned char MA; + auto const MultiArch = Section.Find("Multi-Arch"); + if (MultiArch.empty() == true || MultiArch == "no") + MA = pkgCache::Version::No; else if (MultiArch == "same") { - // Parse multi-arch if (ArchitectureAll() == true) { - /* 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; + if (showErrors == true) + _error->Warning("Architecture: all package '%s' can't be Multi-Arch: same", + Section.FindS("Package").c_str()); + MA = pkgCache::Version::No; } else - Ver->MultiArch = pkgCache::Version::Same; + MA = pkgCache::Version::Same; } else if (MultiArch == "foreign") - Ver->MultiArch = pkgCache::Version::Foreign; + MA = pkgCache::Version::Foreign; else if (MultiArch == "allowed") - Ver->MultiArch = pkgCache::Version::Allowed; + MA = 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 (showErrors == true) + _error->Warning("Unknown Multi-Arch type '%s' for package '%s'", + MultiArch.to_string().c_str(), Section.FindS("Package").c_str()); + MA = pkgCache::Version::No; } if (ArchitectureAll() == true) - Ver->MultiArch |= pkgCache::Version::All; + MA |= pkgCache::Version::All; + + return MA; +} + /*}}}*/ +// ListParser::NewVersion - Fill in the version structure /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool debListParser::NewVersion(pkgCache::VerIterator &Ver) +{ + const char *Start; + const char *Stop; + + // Parse the section + if (Section.Find("Section",Start,Stop) == true) + { + map_stringitem_t const idx = StoreString(pkgCacheGenerator::SECTION, Start, Stop - Start); + Ver->Section = idx; + } + // Parse the source package name + pkgCache::GrpIterator const G = Ver.ParentPkg().Group(); + Ver->SourcePkgName = G->Name; + Ver->SourceVerStr = Ver->VerStr; + if (Section.Find("Source",Start,Stop) == true) + { + const char * const Space = (const char * const) memchr(Start, ' ', Stop - Start); + pkgCache::VerIterator V; + + if (Space != NULL) + { + Stop = Space; + const char * const Open = (const char * const) memchr(Space, '(', Stop - Space); + if (likely(Open != NULL)) + { + const char * const Close = (const char * const) memchr(Open, ')', Stop - Open); + if (likely(Close != NULL)) + { + std::string const version(Open + 1, (Close - Open) - 1); + if (version != Ver.VerStr()) + { + map_stringitem_t const idx = StoreString(pkgCacheGenerator::VERSIONNUMBER, version); + Ver->SourceVerStr = idx; + } + } + } + } + + std::string const pkgname(Start, Stop - Start); + if (pkgname != G.Name()) + { + for (pkgCache::PkgIterator P = G.PackageList(); P.end() == false; P = G.NextPkg(P)) + { + for (V = P.VersionList(); V.end() == false; ++V) + { + if (pkgname == V.SourcePkgName()) + { + Ver->SourcePkgName = V->SourcePkgName; + break; + } + } + if (V.end() == false) + break; + } + if (V.end() == true) + { + map_stringitem_t const idx = StoreString(pkgCacheGenerator::PKGNAME, pkgname); + Ver->SourcePkgName = idx; + } + } + } + Ver->MultiArch = ParseMultiArch(true); // Archive Size Ver->Size = Section.FindULL("Size"); // Unpacked Size (in K) @@ -140,31 +203,28 @@ bool debListParser::NewVersion(pkgCache::VerIterator &Ver) Ver->InstalledSize *= 1024; // Priority - const char *Start; - const char *Stop; if (Section.Find("Priority",Start,Stop) == true) - { - if (GrabWord(string(Start,Stop-Start),PrioList,Ver->Priority) == false) + { + if (GrabWord(StringView(Start,Stop-Start),PrioList,Ver->Priority) == false) Ver->Priority = pkgCache::State::Extra; } - if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false) - return false; if (ParseDepends(Ver,"Pre-Depends",pkgCache::Dep::PreDepends) == false) return false; - if (ParseDepends(Ver,"Suggests",pkgCache::Dep::Suggests) == false) - return false; - if (ParseDepends(Ver,"Recommends",pkgCache::Dep::Recommends) == false) + if (ParseDepends(Ver,"Depends",pkgCache::Dep::Depends) == false) return false; if (ParseDepends(Ver,"Conflicts",pkgCache::Dep::Conflicts) == false) return false; if (ParseDepends(Ver,"Breaks",pkgCache::Dep::DpkgBreaks) == false) return false; + if (ParseDepends(Ver,"Recommends",pkgCache::Dep::Recommends) == false) + return false; + if (ParseDepends(Ver,"Suggests",pkgCache::Dep::Suggests) == false) + return false; if (ParseDepends(Ver,"Replaces",pkgCache::Dep::Replaces) == false) return false; if (ParseDepends(Ver,"Enhances",pkgCache::Dep::Enhances) == false) return false; - // Obsolete. if (ParseDepends(Ver,"Optional",pkgCache::Dep::Suggests) == false) return false; @@ -180,35 +240,43 @@ bool debListParser::NewVersion(pkgCache::VerIterator &Ver) /* This is to return the string describing the package in debian form. If this returns the blank string then the entry is assumed to only describe package properties */ -string debListParser::Description() +string debListParser::Description(std::string const &lang) +{ + return Description(StringView(lang)).to_string(); +} + +StringView debListParser::Description(StringView lang) { - string const lang = DescriptionLanguage(); if (lang.empty()) - return Section.FindS("Description"); + return Section.Find("Description"); else - return Section.FindS(string("Description-").append(lang).c_str()); + return Section.Find(string("Description-").append(lang.data(), lang.size())); } - /*}}}*/ -// ListParser::DescriptionLanguage - Return the description lang string /*{{{*/ -// --------------------------------------------------------------------- -/* This is to return the string describing the language of - description. If this returns the blank string then the entry is - assumed to describe original description. */ -string debListParser::DescriptionLanguage() + /*}}}*/ +// ListParser::AvailableDescriptionLanguages /*{{{*/ +std::vector debListParser::AvailableDescriptionLanguages() { - if (Section.FindS("Description").empty() == false) - return ""; - - std::vector const lang = APT::Configuration::getLanguages(true); - for (std::vector::const_iterator l = lang.begin(); - l != lang.end(); ++l) - if (Section.FindS(string("Description-").append(*l).c_str()).empty() == false) - return *l; + std::vector const understood = APT::Configuration::getLanguages(); + std::vector avail; + static constexpr int prefixLen = 12; + static constexpr int avgLanguageLen = 5; + std::string tagname; - return ""; + tagname.reserve(prefixLen + avgLanguageLen); + tagname.assign("Description-"); + if (Section.Exists("Description") == true) + avail.push_back(""); + for (std::vector::const_iterator lang = understood.begin(); lang != understood.end(); ++lang) + { + tagname.resize(prefixLen); + tagname.append(*lang); + if (Section.Exists(tagname) == true) + avail.push_back(*lang); + } + return avail; } - /*}}}*/ -// ListParser::Description - Return the description_md5 MD5SumValue /*{{{*/ + /*}}}*/ +// ListParser::Description_md5 - Return the description_md5 MD5SumValue /*{{{*/ // --------------------------------------------------------------------- /* This is to return the md5 string to allow the check if it is the right description. If no Description-md5 is found in the section it will be @@ -216,25 +284,28 @@ string debListParser::DescriptionLanguage() */ MD5SumValue debListParser::Description_md5() { - string const value = Section.FindS("Description-md5"); + auto const value = Section.Find("Description-md5"); if (value.empty() == true) { - std::string const desc = Description() + "\n"; + StringView desc = Description(StringView("", 0)); if (desc == "\n") return MD5SumValue(); MD5Summation md5; - md5.Add(desc.c_str()); + md5.Add(desc.data(), desc.size()); + md5.Add("\n"); return md5.Result(); } else if (likely(value.size() == 32)) { - if (likely(value.find_first_not_of("0123456789abcdefABCDEF") == string::npos)) - return MD5SumValue(value); - _error->Error("Malformed Description-md5 line; includes invalid character '%s'", value.c_str()); + 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(); } - _error->Error("Malformed Description-md5 line; doesn't have the required length (32 != %d) '%s'", (int)value.size(), value.c_str()); + _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(); } /*}}}*/ @@ -245,9 +316,6 @@ MD5SumValue debListParser::Description_md5() bool debListParser::UsePackage(pkgCache::PkgIterator &Pkg, pkgCache::VerIterator &Ver) { - if (Pkg->Section == 0) - Pkg->Section = UniqFindTagWrite("Section"); - string const static myArch = _config->Find("APT::Architecture"); // Possible values are: "all", "native", "installed" and "none" // The "installed" mode is handled by ParseStatus(), See #544481 and friends. @@ -278,21 +346,21 @@ bool debListParser::UsePackage(pkgCache::PkgIterator &Pkg, /* */ unsigned short debListParser::VersionHash() { - const char *Sections[] ={"Installed-Size", + static const StringView Sections[] ={"Installed-Size", "Depends", "Pre-Depends", // "Suggests", // "Recommends", "Conflicts", "Breaks", - "Replaces",0}; + "Replaces"}; unsigned long Result = INIT_FCS; char S[1024]; - for (const char * const *I = Sections; *I != 0; ++I) + for (StringView 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 || End - Start >= (signed)sizeof(S)) continue; /* Strip out any spaces from the text, this undoes dpkgs reformatting @@ -301,7 +369,7 @@ unsigned short debListParser::VersionHash() char *J = S; for (; Start != End; ++Start) { - if (isspace(*Start) != 0) + if (isspace_ascii(*Start) != 0) continue; *J++ = tolower_ascii(*Start); @@ -315,20 +383,21 @@ unsigned short debListParser::VersionHash() return Result; } /*}}}*/ -// ListParser::ParseStatus - Parse the status field /*{{{*/ +// StatusListParser::ParseStatus - Parse the status field /*{{{*/ // --------------------------------------------------------------------- /* Status lines are of the form, Status: want flag status want = unknown, install, hold, deinstall, purge - flag = ok, reinstreq, hold, hold-reinstreq - status = not-installed, unpacked, half-configured, - half-installed, config-files, post-inst-failed, - removal-failed, installed - - Some of the above are obsolete (I think?) flag = hold-* and - status = post-inst-failed, removal-failed at least. + flag = ok, reinstreq + status = not-installed, config-files, half-installed, unpacked, + half-configured, triggers-awaited, triggers-pending, installed */ -bool debListParser::ParseStatus(pkgCache::PkgIterator &Pkg, +bool debListParser::ParseStatus(pkgCache::PkgIterator &, + pkgCache::VerIterator &) +{ + return true; +} +bool debStatusListParser::ParseStatus(pkgCache::PkgIterator &Pkg, pkgCache::VerIterator &Ver) { const char *Start; @@ -354,8 +423,8 @@ bool debListParser::ParseStatus(pkgCache::PkgIterator &Pkg, {"hold",pkgCache::State::Hold}, {"deinstall",pkgCache::State::DeInstall}, {"purge",pkgCache::State::Purge}, - {}}; - if (GrabWord(string(Start,I-Start),WantList,Pkg->SelectedState) == false) + {NULL, 0}}; + if (GrabWord(StringView(Start,I-Start),WantList,Pkg->SelectedState) == false) return _error->Error("Malformed 1st word in the Status line"); // Isloate the next word @@ -370,8 +439,8 @@ bool debListParser::ParseStatus(pkgCache::PkgIterator &Pkg, {"reinstreq",pkgCache::State::ReInstReq}, {"hold",pkgCache::State::HoldInst}, {"hold-reinstreq",pkgCache::State::HoldReInstReq}, - {}}; - if (GrabWord(string(Start,I-Start),FlagList,Pkg->InstState) == false) + {NULL, 0}}; + if (GrabWord(StringView(Start,I-Start),FlagList,Pkg->InstState) == false) return _error->Error("Malformed 2nd word in the Status line"); // Isloate the last word @@ -383,17 +452,15 @@ bool debListParser::ParseStatus(pkgCache::PkgIterator &Pkg, // Process the flag field WordList StatusList[] = {{"not-installed",pkgCache::State::NotInstalled}, + {"config-files",pkgCache::State::ConfigFiles}, + {"half-installed",pkgCache::State::HalfInstalled}, {"unpacked",pkgCache::State::UnPacked}, {"half-configured",pkgCache::State::HalfConfigured}, - {"installed",pkgCache::State::Installed}, - {"half-installed",pkgCache::State::HalfInstalled}, - {"config-files",pkgCache::State::ConfigFiles}, {"triggers-awaited",pkgCache::State::TriggersAwaited}, {"triggers-pending",pkgCache::State::TriggersPending}, - {"post-inst-failed",pkgCache::State::HalfConfigured}, - {"removal-failed",pkgCache::State::HalfInstalled}, - {}}; - if (GrabWord(string(Start,I-Start),StatusList,Pkg->CurrentState) == false) + {"installed",pkgCache::State::Installed}, + {NULL, 0}}; + if (GrabWord(StringView(Start,I-Start),StatusList,Pkg->CurrentState) == false) return _error->Error("Malformed 3rd word in the Status line"); /* A Status line marks the package as indicating the current @@ -474,18 +541,47 @@ const char *debListParser::ConvertRelation(const char *I,unsigned int &Op) // --------------------------------------------------------------------- /* This parses the dependency elements out of a standard string in place, bit by bit. */ +const char *debListParser::ParseDepends(const char *Start,const char *Stop, + std::string &Package,std::string &Ver,unsigned int &Op) + { return ParseDepends(Start, Stop, Package, Ver, Op, false, true, false); } +const char *debListParser::ParseDepends(const char *Start,const char *Stop, + std::string &Package,std::string &Ver,unsigned int &Op, + bool const &ParseArchFlags) + { return ParseDepends(Start, Stop, Package, Ver, Op, ParseArchFlags, true, false); } +const char *debListParser::ParseDepends(const char *Start,const char *Stop, + std::string &Package,std::string &Ver,unsigned int &Op, + bool const &ParseArchFlags, bool const &StripMultiArch) + { return ParseDepends(Start, Stop, Package, Ver, Op, ParseArchFlags, StripMultiArch, false); } const char *debListParser::ParseDepends(const char *Start,const char *Stop, string &Package,string &Ver, unsigned int &Op, bool const &ParseArchFlags, - bool const &StripMultiArch) + bool const &StripMultiArch, + bool const &ParseRestrictionsList) +{ + StringView PackageView; + StringView VerView; + + auto res = ParseDepends(Start, Stop, PackageView, VerView, Op, (bool)ParseArchFlags, + (bool) StripMultiArch, (bool) ParseRestrictionsList); + Package = PackageView.to_string(); + Ver = VerView.to_string(); + + return res; +} +const char *debListParser::ParseDepends(const char *Start,const char *Stop, + StringView &Package,StringView &Ver, + unsigned int &Op, bool ParseArchFlags, + bool StripMultiArch, + bool ParseRestrictionsList) { // Strip off leading space - for (;Start != Stop && isspace(*Start) != 0; Start++); + for (;Start != Stop && isspace_ascii(*Start) != 0; ++Start); // Parse off the package name const char *I = Start; - for (;I != Stop && isspace(*I) == 0 && *I != '(' && *I != ')' && - *I != ',' && *I != '|' && *I != '[' && *I != ']'; I++); + for (;I != Stop && isspace_ascii(*I) == 0 && *I != '(' && *I != ')' && + *I != ',' && *I != '|' && *I != '[' && *I != ']' && + *I != '<' && *I != '>'; ++I); // Malformed, no '(' if (I != Stop && *I == ')') @@ -495,33 +591,33 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, return 0; // Stash the package name - Package.assign(Start,I - Start); + Package = StringView(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) { + string const arch = _config->Find("APT::Architecture"); size_t const found = Package.rfind(':'); - 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)) + if (found != StringView::npos && + (Package.substr(found) == ":any" || + Package.substr(found) == ":native" || + Package.substr(found +1) == arch)) Package = Package.substr(0,found); } // Skip white space to the '(' - for (;I != Stop && isspace(*I) != 0 ; I++); + for (;I != Stop && isspace_ascii(*I) != 0 ; I++); // Parse a version if (I != Stop && *I == '(') { // Skip the '(' - for (I++; I != Stop && isspace(*I) != 0 ; I++); + for (I++; I != Stop && isspace_ascii(*I) != 0 ; I++); if (I + 3 >= Stop) return 0; I = ConvertRelation(I,Op); // Skip whitespace - for (;I != Stop && isspace(*I) != 0; I++); + for (;I != Stop && isspace_ascii(*I) != 0; I++); Start = I; I = (const char*) memchr(I, ')', Stop - I); if (I == NULL || Start == I) @@ -529,22 +625,23 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, // Skip trailing whitespace const char *End = I; - for (; End > Start && isspace(End[-1]); End--); + for (; End > Start && isspace_ascii(End[-1]); End--); - Ver.assign(Start,End-Start); + Ver = StringView(Start,End-Start); I++; } else { - Ver.clear(); + Ver = StringView(); Op = pkgCache::Dep::NoOp; } // Skip whitespace - for (;I != Stop && isspace(*I) != 0; I++); + for (;I != Stop && isspace_ascii(*I) != 0; I++); - if (ParseArchFlags == true) + if (unlikely(ParseArchFlags == true)) { + string const arch = _config->Find("APT::Architecture"); APT::CacheFilter::PackageArchitectureMatchesSpecification matchesArch(arch, false); // Parse an architecture @@ -561,7 +658,7 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, while (I != Stop) { // look for whitespace or ending ']' - for (;End != Stop && !isspace(*End) && *End != ']'; ++End); + for (;End != Stop && !isspace_ascii(*End) && *End != ']'; ++End); if (unlikely(End == Stop)) return 0; @@ -588,7 +685,7 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, } I = End; - for (;I != Stop && isspace(*I) != 0; I++); + for (;I != Stop && isspace_ascii(*I) != 0; I++); } if (NegArch == true) @@ -599,7 +696,99 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, } // Skip whitespace - for (;I != Stop && isspace(*I) != 0; I++); + for (;I != Stop && isspace_ascii(*I) != 0; I++); + } + + if (unlikely(ParseRestrictionsList == true)) + { + // Parse a restrictions formula which is in disjunctive normal form: + // (foo AND bar) OR (blub AND bla) + + std::vector const profiles = APT::Configuration::getBuildProfiles(); + + // if the next character is a restriction list, then by default the + // dependency does not apply and the conditions have to be checked + // if the next character is not a restriction list, then by default the + // dependency applies + bool applies1 = (*I != '<'); + while (I != Stop) + { + if (*I != '<') + break; + + ++I; + // malformed + if (unlikely(I == Stop)) + return 0; + + const char *End = I; + + // if of the prior restriction list is already fulfilled, then + // we can just skip to the end of the current list + if (applies1) { + for (;End != Stop && *End != '>'; ++End); + I = ++End; + // skip whitespace + for (;I != Stop && isspace_ascii(*I) != 0; I++); + } else { + bool applies2 = true; + // all the conditions inside a restriction list have to be + // met so once we find one that is not met, we can skip to + // the end of this list + while (I != Stop) + { + // look for whitespace or ending '>' + // End now points to the character after the current term + for (;End != Stop && !isspace_ascii(*End) && *End != '>'; ++End); + + if (unlikely(End == Stop)) + return 0; + + bool NegRestriction = false; + if (*I == '!') + { + NegRestriction = true; + ++I; + } + + std::string restriction(I, End); + + if (restriction.empty() == false && profiles.empty() == false && + std::find(profiles.begin(), profiles.end(), restriction) != profiles.end()) + { + if (NegRestriction) { + applies2 = false; + // since one of the terms does not apply we don't have to check the others + for (; End != Stop && *End != '>'; ++End); + } + } else { + if (!NegRestriction) { + applies2 = false; + // since one of the terms does not apply we don't have to check the others + for (; End != Stop && *End != '>'; ++End); + } + } + + if (*End++ == '>') { + I = End; + // skip whitespace + for (;I != Stop && isspace_ascii(*I) != 0; I++); + break; + } + + I = End; + // skip whitespace + for (;I != Stop && isspace_ascii(*I) != 0; I++); + } + if (applies2) { + applies1 = true; + } + } + } + + if (applies1 == false) { + Package = ""; //not for this restriction + } } if (I != Stop && *I == '|') @@ -608,7 +797,7 @@ const char *debListParser::ParseDepends(const char *Start,const char *Stop, if (I == Stop || *I == ',' || *I == '|') { if (I != Stop) - for (I++; I != Stop && isspace(*I) != 0; I++); + for (I++; I != Stop && isspace_ascii(*I) != 0; I++); return I; } @@ -620,7 +809,7 @@ 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, - const char *Tag,unsigned int Type) + StringView Tag,unsigned int Type) { const char *Start; const char *Stop; @@ -631,52 +820,38 @@ bool debListParser::ParseDepends(pkgCache::VerIterator &Ver, while (1) { - string Package; - string Version; + StringView Package; + StringView Version; unsigned int Op; - Start = ParseDepends(Start, Stop, Package, Version, Op, false, false); + Start = ParseDepends(Start, Stop, Package, Version, Op, false, false, false); if (Start == 0) - return _error->Error("Problem parsing dependency %s",Tag); + return _error->Error("Problem parsing dependency %.*s",(int)Tag.length(), Tag.data()); size_t const found = Package.rfind(':'); - // If negative is unspecific it needs to apply on all architectures - if (MultiArchEnabled == true && found == string::npos && - (Type == pkgCache::Dep::Conflicts || - Type == pkgCache::Dep::DpkgBreaks || - Type == pkgCache::Dep::Replaces)) + if (found == string::npos) { - for (std::vector::const_iterator a = Architectures.begin(); - a != Architectures.end(); ++a) - if (NewDepends(Ver,Package,*a,Version,Op,Type) == false) - return false; - if (NewDepends(Ver,Package,"none",Version,Op,Type) == false) + if (NewDepends(Ver,Package,pkgArch,Version,Op,Type) == false) return false; } - else if (MultiArchEnabled == true && found != string::npos && - strcmp(Package.c_str() + found, ":any") != 0) + else if (Package.substr(found) == ":any") { - string Arch = Package.substr(found+1, string::npos); - Package = Package.substr(0, found); - // Such dependencies are not supposed to be accepted … - // … but this is probably the best thing to do. - if (Arch == "native") - Arch = _config->Find("APT::Architecture"); - if (NewDepends(Ver,Package,Arch,Version,Op,Type) == false) + if (NewDepends(Ver,Package,"any",Version,Op,Type) == false) return false; } else { - if (NewDepends(Ver,Package,pkgArch,Version,Op,Type) == false) - return false; - if ((Type == pkgCache::Dep::Conflicts || - Type == pkgCache::Dep::DpkgBreaks || - Type == pkgCache::Dep::Replaces) && - NewDepends(Ver, Package, - (pkgArch != "none") ? "none" : _config->Find("APT::Architecture"), - Version,Op,Type) == false) + // Such dependencies are not supposed to be accepted … + // … but this is probably the best thing to do anyway + std::string Pkg; + if (Package.substr(found + 1) == "native") + Pkg = Package.substr(0, found).to_string() + ':' + Ver.Cache()->NativeArch(); + else + Pkg = Package.to_string(); + if (NewDepends(Ver, Pkg, "any", Version, Op | pkgCache::Dep::ArchSpecific, Type) == false) return false; } + if (Start == Stop) break; } @@ -688,54 +863,103 @@ bool debListParser::ParseDepends(pkgCache::VerIterator &Ver, /* */ bool debListParser::ParseProvides(pkgCache::VerIterator &Ver) { + /* it is unlikely, but while parsing dependencies, we might have already + picked up multi-arch implicit provides which we do not want to duplicate here */ + bool hasProvidesAlready = false; + std::string const spzName = Ver.ParentPkg().FullName(false); + { + for (pkgCache::PrvIterator Prv = Ver.ProvidesList(); Prv.end() == false; ++Prv) + { + if (Prv.IsMultiArchImplicit() == false || (Prv->Flags & pkgCache::Flag::ArchSpecific) == 0) + continue; + if (spzName != Prv.OwnerPkg().FullName(false)) + continue; + hasProvidesAlready = true; + break; + } + } + + string const Arch = Ver.Arch(); const char *Start; const char *Stop; if (Section.Find("Provides",Start,Stop) == true) { - string Package; - string Version; - string const Arch = Ver.Arch(); + StringView Package; + StringView Version; unsigned int Op; - while (1) + do { - Start = ParseDepends(Start,Stop,Package,Version,Op); + Start = ParseDepends(Start,Stop,Package,Version,Op, false, false, false); + const size_t archfound = Package.rfind(':'); if (Start == 0) return _error->Error("Problem parsing Provides line"); - if (Op != pkgCache::Dep::NoOp) { - _error->Warning("Ignoring Provides line with DepCompareOp for package %s", Package.c_str()); + if (unlikely(Op != pkgCache::Dep::NoOp && Op != pkgCache::Dep::Equals)) { + _error->Warning("Ignoring Provides line with non-equal DepCompareOp for package %s", Package.to_string().c_str()); + } else if (archfound != string::npos) { + StringView spzArch = Package.substr(archfound + 1); + if (spzArch != "any") + { + if (NewProvides(Ver, Package.substr(0, archfound), spzArch, Version, pkgCache::Flag::MultiArchImplicit | pkgCache::Flag::ArchSpecific) == false) + return false; + } + if (NewProvides(Ver, Package, "any", Version, pkgCache::Flag::ArchSpecific) == false) + return false; } else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign) { - if (NewProvidesAllArch(Ver, Package, Version) == false) + if (APT::Configuration::checkArchitecture(Arch)) + { + if (NewProvidesAllArch(Ver, Package, Version, 0) == false) + return false; + } + else if (NewProvides(Ver, Package, Arch, Version, 0) == false) return false; } else { - if (NewProvides(Ver, Package, Arch, Version) == false) + if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed) + { + if (NewProvides(Ver, Package.to_string().append(":any"), "any", Version, pkgCache::Flag::MultiArchImplicit) == false) + return false; + } + if (NewProvides(Ver, Package, Arch, Version, 0) == false) return false; } - - if (Start == Stop) - break; - } + if (archfound == std::string::npos) + { + string spzName = Package.to_string(); + spzName.push_back(':'); + spzName.append(Ver.ParentPkg().Arch()); + pkgCache::PkgIterator const spzPkg = Ver.Cache()->FindPkg(spzName, "any"); + if (spzPkg.end() == false) + { + if (NewProvides(Ver, spzName, "any", Version, pkgCache::Flag::MultiArchImplicit | pkgCache::Flag::ArchSpecific) == false) + return false; + } + } + } while (Start != Stop); } - if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed) + if (APT::Configuration::checkArchitecture(Arch)) { - string const Package = string(Ver.ParentPkg().Name()).append(":").append("any"); - return NewProvidesAllArch(Ver, Package, Ver.VerStr()); + if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed) + { + string const Package = string(Ver.ParentPkg().Name()).append(":").append("any"); + if (NewProvides(Ver, Package, "any", Ver.VerStr(), pkgCache::Flag::MultiArchImplicit) == false) + return false; + } + else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign) + { + if (NewProvidesAllArch(Ver, Ver.ParentPkg().Name(), Ver.VerStr(), pkgCache::Flag::MultiArchImplicit) == false) + return false; + } } - else if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign) - return NewProvidesAllArch(Ver, Ver.ParentPkg().Name(), Ver.VerStr()); - return true; -} - /*}}}*/ -// ListParser::NewProvides - add provides for all architectures /*{{{*/ -bool debListParser::NewProvidesAllArch(pkgCache::VerIterator &Ver, string const &Package, - string const &Version) { - for (std::vector::const_iterator a = Architectures.begin(); - a != Architectures.end(); ++a) + if (hasProvidesAlready == false) { - if (NewProvides(Ver, Package, *a, Version) == false) - return false; + pkgCache::PkgIterator const spzPkg = Ver.Cache()->FindPkg(spzName, "any"); + if (spzPkg.end() == false) + { + if (NewProvides(Ver, spzName, "any", Ver.VerStr(), pkgCache::Flag::MultiArchImplicit | pkgCache::Flag::ArchSpecific) == false) + return false; + } } return true; } @@ -743,11 +967,12 @@ bool debListParser::NewProvidesAllArch(pkgCache::VerIterator &Ver, string const // ListParser::GrabWord - Matches a word and returns /*{{{*/ // --------------------------------------------------------------------- /* Looks for a word in a list of words - for ParseStatus */ -bool debListParser::GrabWord(string Word,WordList *List,unsigned char &Out) +bool debListParser::GrabWord(StringView Word,const WordList *List,unsigned char &Out) { for (unsigned int C = 0; List[C].Str != 0; C++) { - if (strcasecmp(Word.c_str(),List[C].Str) == 0) + if (Word.length() == strlen(List[C].Str) && + strncasecmp(Word.data(),List[C].Str,Word.length()) == 0) { Out = List[C].Val; return true; @@ -758,75 +983,11 @@ bool debListParser::GrabWord(string Word,WordList *List,unsigned char &Out) /*}}}*/ // ListParser::Step - Move to the next section in the file /*{{{*/ // --------------------------------------------------------------------- -/* This has to be carefull to only process the correct architecture */ +/* This has to be careful to only process the correct architecture */ bool debListParser::Step() { iOffset = Tags.Offset(); - while (Tags.Step(Section) == true) - { - /* See if this is the correct Architecture, if it isn't then we - drop the whole section. A missing arch tag only happens (in theory) - inside the Status file, so that is a positive return */ - string const Architecture = Section.FindS("Architecture"); - - if (Arch.empty() == true || Arch == "any" || MultiArchEnabled == false) - { - if (APT::Configuration::checkArchitecture(Architecture) == true) - return true; - /* parse version stanzas without an architecture only in the status file - (and as misfortune bycatch flat-archives) */ - if ((Arch.empty() == true || Arch == "any") && Architecture.empty() == true) - return true; - } - else - { - if (Architecture == Arch) - return true; - - if (Architecture == "all" && Arch == _config->Find("APT::Architecture")) - return true; - } - - iOffset = Tags.Offset(); - } - return false; -} - /*}}}*/ -// ListParser::LoadReleaseInfo - Load the release information /*{{{*/ -// --------------------------------------------------------------------- -/* */ -bool debListParser::LoadReleaseInfo(pkgCache::PkgFileIterator &FileI, - FileFd &File, string component) -{ - // apt-secure does no longer download individual (per-section) Release - // file. to provide Component pinning we use the section name now - map_ptrloc const storage = WriteUniqString(component); - FileI->Component = storage; - - pkgTagFile TagFile(&File, File.Size()); - pkgTagSection Section; - if (_error->PendingError() == true || TagFile.Step(Section) == false) - return false; - - std::string data; - #define APT_INRELEASE(TAG, STORE) \ - data = Section.FindS(TAG); \ - if (data.empty() == false) \ - { \ - map_ptrloc const storage = WriteUniqString(data); \ - STORE = storage; \ - } - APT_INRELEASE("Suite", FileI->Archive) - APT_INRELEASE("Component", FileI->Component) - APT_INRELEASE("Version", FileI->Version) - APT_INRELEASE("Origin", FileI->Origin) - APT_INRELEASE("Codename", FileI->Codename) - APT_INRELEASE("Label", FileI->Label) - #undef APT_INRELEASE - Section.FindFlag("NotAutomatic", FileI->Flags, pkgCache::Flag::NotAutomatic); - Section.FindFlag("ButAutomaticUpgrades", FileI->Flags, pkgCache::Flag::ButAutomaticUpgrades); - - return !_error->PendingError(); + return Tags.Step(Section); } /*}}}*/ // ListParser::GetPrio - Convert the priority from a string /*{{{*/ @@ -841,3 +1002,40 @@ unsigned char debListParser::GetPrio(string Str) return Out; } /*}}}*/ +bool debListParser::SameVersion(unsigned short const Hash, /*{{{*/ + pkgCache::VerIterator const &Ver) +{ + if (pkgCacheListParser::SameVersion(Hash, Ver) == false) + return false; + // 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) + return false; + // available everywhere, but easier to check here than to include in VersionHash + unsigned char MultiArch = ParseMultiArch(false); + if (MultiArch != Ver->MultiArch) + return false; + // for all practical proposes (we can check): same version + return true; +} + /*}}}*/ + +debDebFileParser::debDebFileParser(FileFd *File, std::string const &DebFile) + : debListParser(File), DebFile(DebFile) +{ +} + +bool debDebFileParser::UsePackage(pkgCache::PkgIterator &Pkg, + pkgCache::VerIterator &Ver) +{ + bool res = debListParser::UsePackage(Pkg, Ver); + // we use the full file path as a provides so that the file is found + // by its name + if(NewProvides(Ver, DebFile, Pkg.Cache()->NativeArch(), Ver.VerStr(), 0) == false) + return false; + return res; +} + +debListParser::~debListParser() {}