X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/0a5e4a031a039329cc0d27268be6a623e892c452..503c7d594125f963b92047ad2d9d0644f39136c7:/apt-pkg/versionmatch.cc diff --git a/apt-pkg/versionmatch.cc b/apt-pkg/versionmatch.cc index 8e8e2d2d1..2376ca8fd 100644 --- a/apt-pkg/versionmatch.cc +++ b/apt-pkg/versionmatch.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: versionmatch.cc,v 1.5 2001/05/29 03:07:12 jgg Exp $ +// $Id: versionmatch.cc,v 1.9 2003/05/19 17:58:26 doogie Exp $ /* ###################################################################### Version Matching @@ -11,23 +11,35 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ -#ifdef __GNUG__ -#pragma implementation "apt-pkg/versionmatch.h" -#endif -#include +#include +#include #include #include +#include +#include +#include +#include +#include +#include #include #include +#include +#include /*}}}*/ +using std::string; + // VersionMatch::pkgVersionMatch - Constructor /*{{{*/ // --------------------------------------------------------------------- /* Break up the data string according to the selected type */ pkgVersionMatch::pkgVersionMatch(string Data,MatchType Type) : Type(Type) { + MatchAll = false; + VerPrefixMatch = false; + RelVerPrefixMatch = false; + if (Type == None || Data.length() < 1) return; @@ -53,20 +65,18 @@ pkgVersionMatch::pkgVersionMatch(string Data,MatchType Type) : Type(Type) return; } - MatchAll = false; - // Are we a simple specification? string::const_iterator I = Data.begin(); - for (; I != Data.end() && *I != '='; I++); + for (; I != Data.end() && *I != '='; ++I); if (I == Data.end()) { // Temporary if (isdigit(Data[0])) RelVerStr = Data; else - RelArchive = Data; - - if (RelVerStr.end()[-1] == '*') + RelRelease = Data; + + if (RelVerStr.length() > 0 && RelVerStr.end()[-1] == '*') { RelVerPrefixMatch = true; RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1); @@ -88,19 +98,23 @@ pkgVersionMatch::pkgVersionMatch(string Data,MatchType Type) : Type(Type) { if (strlen(Fragments[J]) < 3) continue; - + if (stringcasecmp(Fragments[J],Fragments[J]+2,"v=") == 0) RelVerStr = Fragments[J]+2; else if (stringcasecmp(Fragments[J],Fragments[J]+2,"o=") == 0) RelOrigin = Fragments[J]+2; else if (stringcasecmp(Fragments[J],Fragments[J]+2,"a=") == 0) RelArchive = Fragments[J]+2; + else if (stringcasecmp(Fragments[J],Fragments[J]+2,"n=") == 0) + RelCodename = Fragments[J]+2; else if (stringcasecmp(Fragments[J],Fragments[J]+2,"l=") == 0) RelLabel = Fragments[J]+2; else if (stringcasecmp(Fragments[J],Fragments[J]+2,"c=") == 0) RelComponent = Fragments[J]+2; + else if (stringcasecmp(Fragments[J],Fragments[J]+2,"b=") == 0) + RelArchitecture = Fragments[J]+2; } - + if (RelVerStr.end()[-1] == '*') { RelVerPrefixMatch = true; @@ -111,7 +125,10 @@ pkgVersionMatch::pkgVersionMatch(string Data,MatchType Type) : Type(Type) if (Type == Origin) { - OrSite = Data; + if (Data[0] == '"' && Data.length() >= 2 && Data.end()[-1] == '"') + OrSite = Data.substr(1, Data.length() - 2); + else + OrSite = Data; return; } } @@ -120,12 +137,15 @@ pkgVersionMatch::pkgVersionMatch(string Data,MatchType Type) : Type(Type) // --------------------------------------------------------------------- /* */ bool pkgVersionMatch::MatchVer(const char *A,string B,bool Prefix) -{ +{ + if (A == NULL) + return false; + const char *Ab = A; const char *Ae = Ab + strlen(A); // Strings are not a compatible size. - if ((unsigned)(Ae - Ab) != B.length() && Prefix == false || + if (((unsigned)(Ae - Ab) != B.length() && Prefix == false) || (unsigned)(Ae - Ab) < B.length()) return false; @@ -142,24 +162,71 @@ bool pkgVersionMatch::MatchVer(const char *A,string B,bool Prefix) pkgCache::VerIterator pkgVersionMatch::Find(pkgCache::PkgIterator Pkg) { pkgCache::VerIterator Ver = Pkg.VersionList(); - for (; Ver.end() == false; Ver++) + for (; Ver.end() == false; ++Ver) { - if (Type == Version) - { - if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true) - return Ver; - continue; - } - - for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++) - if (FileMatch(VF.File()) == true) - return Ver; + if (VersionMatches(Ver)) + return Ver; } - + // This will be Ended by now. return Ver; } /*}}}*/ + +// VersionMatch::Find - Locate the best match for the select type /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool pkgVersionMatch::VersionMatches(pkgCache::VerIterator Ver) +{ + if (Type == Version) + { + if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true) + return true; + if (ExpressionMatches(VerStr, Ver.VerStr()) == true) + return true; + return false; + } + + for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; ++VF) + if (FileMatch(VF.File()) == true) + return true; + + return false; +} + /*}}}*/ + +#ifndef FNM_CASEFOLD +#define FNM_CASEFOLD 0 +#endif + +bool pkgVersionMatch::ExpressionMatches(const char *pattern, const char *string)/*{{{*/ +{ + if (pattern == NULL || string == NULL) + return false; + if (pattern[0] == '/') { + size_t length = strlen(pattern); + if (pattern[length - 1] == '/') { + bool res = false; + regex_t preg; + char *regex = strdup(pattern + 1); + regex[length - 2] = '\0'; + if (regcomp(&preg, regex, REG_EXTENDED | REG_ICASE) != 0) { + _error->Warning("Invalid regular expression: %s", regex); + } else if (regexec(&preg, string, 0, NULL, 0) == 0) { + res = true; + } + free(regex); + regfree(&preg); + return res; + } + } + return fnmatch(pattern, string, FNM_CASEFOLD) == 0; +} +bool pkgVersionMatch::ExpressionMatches(const std::string& pattern, const char *string) +{ + return ExpressionMatches(pattern.c_str(), string); +} + /*}}}*/ // VersionMatch::FileMatch - Match against an index file /*{{{*/ // --------------------------------------------------------------------- /* This matcher checks against the release file and the origin location @@ -170,49 +237,56 @@ bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File) { if (MatchAll == true) return true; - + /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl; cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/ - + if (RelVerStr.empty() == true && RelOrigin.empty() == true && RelArchive.empty() == true && RelLabel.empty() == true && - RelComponent.empty() == true) + RelRelease.empty() == true && RelCodename.empty() == true && + RelComponent.empty() == true && RelArchitecture.empty() == true) return false; - + if (RelVerStr.empty() == false) - if (File->Version == 0 || - MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false) + if (MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false && + ExpressionMatches(RelVerStr, File.Version()) == false) return false; if (RelOrigin.empty() == false) - if (File->Origin == 0 || - stringcasecmp(RelOrigin,File.Origin()) != 0) + if (!ExpressionMatches(RelOrigin,File.Origin())) return false; if (RelArchive.empty() == false) - { - if (File->Archive == 0 || - stringcasecmp(RelArchive,File.Archive()) != 0) - return false; - } + if (!ExpressionMatches(RelArchive,File.Archive())) + return false; + if (RelCodename.empty() == false) + if (!ExpressionMatches(RelCodename,File.Codename())) + return false; + if (RelRelease.empty() == false) + if (!ExpressionMatches(RelRelease,File.Archive()) && + !ExpressionMatches(RelRelease,File.Codename())) + return false; if (RelLabel.empty() == false) - if (File->Label == 0 || - stringcasecmp(RelLabel,File.Label()) != 0) + if (!ExpressionMatches(RelLabel,File.Label())) return false; if (RelComponent.empty() == false) - if (File->Component == 0 || - stringcasecmp(RelComponent,File.Component()) != 0) + if (!ExpressionMatches(RelComponent,File.Component())) + return false; + if (RelArchitecture.empty() == false) + if (!ExpressionMatches(RelArchitecture,File.Architecture())) return false; return true; } - + if (Type == Origin) { - if (OrSite.empty() == false) - if (File->Site == 0 || - OrSite != File.Site()) + if (OrSite.empty() == false) { + if (File.Site() == NULL) return false; - return true; + } + else if (File->Release == 0)// only 'bad' files like dpkg.status file has no release file + return false; + return (ExpressionMatches(OrSite, File.Site())); /* both strings match */ } - + return false; } /*}}}*/