versionmatch.cc 8.2 KB

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