versionmatch.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: versionmatch.cc,v 1.9 2003/05/19 17:58:26 doogie Exp $
  4. /* ######################################################################
  5. Version Matching
  6. This module takes a matching string and a type and locates the version
  7. record that satisfies the constraint described by the matching string.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #include<config.h>
  12. #include <apt-pkg/versionmatch.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <apt-pkg/error.h>
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <fnmatch.h>
  18. #include <sys/types.h>
  19. #include <regex.h>
  20. /*}}}*/
  21. using std::string;
  22. // VersionMatch::pkgVersionMatch - Constructor /*{{{*/
  23. // ---------------------------------------------------------------------
  24. /* Break up the data string according to the selected type */
  25. pkgVersionMatch::pkgVersionMatch(string Data,MatchType Type) : Type(Type)
  26. {
  27. MatchAll = false;
  28. VerPrefixMatch = false;
  29. RelVerPrefixMatch = false;
  30. if (Type == None || Data.length() < 1)
  31. return;
  32. // Cut up the version representation
  33. if (Type == Version)
  34. {
  35. if (Data.end()[-1] == '*')
  36. {
  37. VerPrefixMatch = true;
  38. VerStr = string(Data,0,Data.length()-1);
  39. }
  40. else
  41. VerStr = Data;
  42. return;
  43. }
  44. if (Type == Release)
  45. {
  46. // All empty = match all
  47. if (Data == "*")
  48. {
  49. MatchAll = true;
  50. return;
  51. }
  52. // Are we a simple specification?
  53. string::const_iterator I = Data.begin();
  54. for (; I != Data.end() && *I != '='; ++I);
  55. if (I == Data.end())
  56. {
  57. // Temporary
  58. if (isdigit(Data[0]))
  59. RelVerStr = Data;
  60. else
  61. RelRelease = Data;
  62. if (RelVerStr.length() > 0 && RelVerStr.end()[-1] == '*')
  63. {
  64. RelVerPrefixMatch = true;
  65. RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1);
  66. }
  67. return;
  68. }
  69. char Spec[300];
  70. char *Fragments[20];
  71. snprintf(Spec,sizeof(Spec),"%s",Data.c_str());
  72. if (TokSplitString(',',Spec,Fragments,
  73. sizeof(Fragments)/sizeof(Fragments[0])) == false)
  74. {
  75. Type = None;
  76. return;
  77. }
  78. for (unsigned J = 0; Fragments[J] != 0; J++)
  79. {
  80. if (strlen(Fragments[J]) < 3)
  81. continue;
  82. if (stringcasecmp(Fragments[J],Fragments[J]+2,"v=") == 0)
  83. RelVerStr = Fragments[J]+2;
  84. else if (stringcasecmp(Fragments[J],Fragments[J]+2,"o=") == 0)
  85. RelOrigin = Fragments[J]+2;
  86. else if (stringcasecmp(Fragments[J],Fragments[J]+2,"a=") == 0)
  87. RelArchive = Fragments[J]+2;
  88. else if (stringcasecmp(Fragments[J],Fragments[J]+2,"n=") == 0)
  89. RelCodename = Fragments[J]+2;
  90. else if (stringcasecmp(Fragments[J],Fragments[J]+2,"l=") == 0)
  91. RelLabel = Fragments[J]+2;
  92. else if (stringcasecmp(Fragments[J],Fragments[J]+2,"c=") == 0)
  93. RelComponent = Fragments[J]+2;
  94. else if (stringcasecmp(Fragments[J],Fragments[J]+2,"b=") == 0)
  95. RelArchitecture = Fragments[J]+2;
  96. }
  97. if (RelVerStr.end()[-1] == '*')
  98. {
  99. RelVerPrefixMatch = true;
  100. RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1);
  101. }
  102. return;
  103. }
  104. if (Type == Origin)
  105. {
  106. if (Data[0] == '"' && Data.length() >= 2 && Data.end()[-1] == '"')
  107. OrSite = Data.substr(1, Data.length() - 2);
  108. else
  109. OrSite = Data;
  110. return;
  111. }
  112. }
  113. /*}}}*/
  114. // VersionMatch::MatchVer - Match a version string with prefixing /*{{{*/
  115. // ---------------------------------------------------------------------
  116. /* */
  117. bool pkgVersionMatch::MatchVer(const char *A,string B,bool Prefix)
  118. {
  119. const char *Ab = A;
  120. const char *Ae = Ab + strlen(A);
  121. // Strings are not a compatible size.
  122. if (((unsigned)(Ae - Ab) != B.length() && Prefix == false) ||
  123. (unsigned)(Ae - Ab) < B.length())
  124. return false;
  125. // Match (leading?)
  126. if (stringcasecmp(B,Ab,Ab + B.length()) == 0)
  127. return true;
  128. return false;
  129. }
  130. /*}}}*/
  131. // VersionMatch::Find - Locate the best match for the select type /*{{{*/
  132. // ---------------------------------------------------------------------
  133. /* */
  134. pkgCache::VerIterator pkgVersionMatch::Find(pkgCache::PkgIterator Pkg)
  135. {
  136. pkgCache::VerIterator Ver = Pkg.VersionList();
  137. for (; Ver.end() == false; ++Ver)
  138. {
  139. if (Type == Version)
  140. {
  141. if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true)
  142. return Ver;
  143. if (ExpressionMatches(VerStr, Ver.VerStr()) == true)
  144. return Ver;
  145. continue;
  146. }
  147. for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; ++VF)
  148. if (FileMatch(VF.File()) == true)
  149. return Ver;
  150. }
  151. // This will be Ended by now.
  152. return Ver;
  153. }
  154. #ifndef FNM_CASEFOLD
  155. #define FNM_CASEFOLD 0
  156. #endif
  157. bool pkgVersionMatch::ExpressionMatches(const char *pattern, const char *string)
  158. {
  159. if (pattern[0] == '/') {
  160. bool res = false;
  161. size_t length = strlen(pattern);
  162. if (pattern[length - 1] == '/') {
  163. regex_t preg;
  164. char *regex = strdup(pattern + 1);
  165. regex[length - 2] = '\0';
  166. if (regcomp(&preg, regex, REG_EXTENDED | REG_ICASE) != 0) {
  167. _error->Warning("Invalid regular expression: %s", regex);
  168. } else if (regexec(&preg, string, 0, NULL, 0) == 0) {
  169. res = true;
  170. }
  171. free(regex);
  172. regfree(&preg);
  173. return res;
  174. }
  175. }
  176. return fnmatch(pattern, string, FNM_CASEFOLD) == 0;
  177. }
  178. bool pkgVersionMatch::ExpressionMatches(const std::string& pattern, const char *string)
  179. {
  180. return ExpressionMatches(pattern.c_str(), string);
  181. }
  182. /*}}}*/
  183. // VersionMatch::FileMatch - Match against an index file /*{{{*/
  184. // ---------------------------------------------------------------------
  185. /* This matcher checks against the release file and the origin location
  186. to see if the constraints are met. */
  187. bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File)
  188. {
  189. if (Type == Release)
  190. {
  191. if (MatchAll == true)
  192. return true;
  193. /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
  194. cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
  195. if (RelVerStr.empty() == true && RelOrigin.empty() == true &&
  196. RelArchive.empty() == true && RelLabel.empty() == true &&
  197. RelRelease.empty() == true && RelCodename.empty() == true &&
  198. RelComponent.empty() == true && RelArchitecture.empty() == true)
  199. return false;
  200. if (RelVerStr.empty() == false)
  201. if (File->Version == 0 ||
  202. (MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false &&
  203. ExpressionMatches(RelVerStr, File.Version()) == false))
  204. return false;
  205. if (RelOrigin.empty() == false)
  206. if (File->Origin == 0 || !ExpressionMatches(RelOrigin,File.Origin()))
  207. return false;
  208. if (RelArchive.empty() == false)
  209. if (File->Archive == 0 ||
  210. !ExpressionMatches(RelArchive,File.Archive()))
  211. return false;
  212. if (RelCodename.empty() == false)
  213. if (File->Codename == 0 ||
  214. !ExpressionMatches(RelCodename,File.Codename()))
  215. return false;
  216. if (RelRelease.empty() == false)
  217. if ((File->Archive == 0 ||
  218. !ExpressionMatches(RelRelease,File.Archive())) &&
  219. (File->Codename == 0 ||
  220. !ExpressionMatches(RelRelease,File.Codename())))
  221. return false;
  222. if (RelLabel.empty() == false)
  223. if (File->Label == 0 ||
  224. !ExpressionMatches(RelLabel,File.Label()))
  225. return false;
  226. if (RelComponent.empty() == false)
  227. if (File->Component == 0 ||
  228. !ExpressionMatches(RelComponent,File.Component()))
  229. return false;
  230. if (RelArchitecture.empty() == false)
  231. if (File->Architecture == 0 ||
  232. !ExpressionMatches(RelArchitecture,File.Architecture()))
  233. return false;
  234. return true;
  235. }
  236. if (Type == Origin)
  237. {
  238. if (OrSite.empty() == false) {
  239. if (File->Site == 0)
  240. return false;
  241. } else // so we are talking about file:// or status file
  242. if (strcmp(File.Site(),"") == 0 && File->Archive != 0 && strcmp(File.Archive(),"now") == 0) // skip the status file
  243. return false;
  244. return (ExpressionMatches(OrSite, File.Site())); /* both strings match */
  245. }
  246. return false;
  247. }
  248. /*}}}*/