versionmatch.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. if (A == NULL)
  125. return false;
  126. const char *Ab = A;
  127. const char *Ae = Ab + strlen(A);
  128. // Strings are not a compatible size.
  129. if (((unsigned)(Ae - Ab) != B.length() && Prefix == false) ||
  130. (unsigned)(Ae - Ab) < B.length())
  131. return false;
  132. // Match (leading?)
  133. if (stringcasecmp(B,Ab,Ab + B.length()) == 0)
  134. return true;
  135. return false;
  136. }
  137. /*}}}*/
  138. // VersionMatch::Find - Locate the best match for the select type /*{{{*/
  139. // ---------------------------------------------------------------------
  140. /* */
  141. pkgCache::VerIterator pkgVersionMatch::Find(pkgCache::PkgIterator Pkg)
  142. {
  143. pkgCache::VerIterator Ver = Pkg.VersionList();
  144. for (; Ver.end() == false; ++Ver)
  145. {
  146. if (Type == Version)
  147. {
  148. if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true)
  149. return Ver;
  150. if (ExpressionMatches(VerStr, Ver.VerStr()) == true)
  151. return Ver;
  152. continue;
  153. }
  154. for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; ++VF)
  155. if (FileMatch(VF.File()) == true)
  156. return Ver;
  157. }
  158. // This will be Ended by now.
  159. return Ver;
  160. }
  161. /*}}}*/
  162. #ifndef FNM_CASEFOLD
  163. #define FNM_CASEFOLD 0
  164. #endif
  165. bool pkgVersionMatch::ExpressionMatches(const char *pattern, const char *string)/*{{{*/
  166. {
  167. if (pattern == NULL || string == NULL)
  168. return false;
  169. if (pattern[0] == '/') {
  170. size_t length = strlen(pattern);
  171. if (pattern[length - 1] == '/') {
  172. bool res = false;
  173. regex_t preg;
  174. char *regex = strdup(pattern + 1);
  175. regex[length - 2] = '\0';
  176. if (regcomp(&preg, regex, REG_EXTENDED | REG_ICASE) != 0) {
  177. _error->Warning("Invalid regular expression: %s", regex);
  178. } else if (regexec(&preg, string, 0, NULL, 0) == 0) {
  179. res = true;
  180. }
  181. free(regex);
  182. regfree(&preg);
  183. return res;
  184. }
  185. }
  186. return fnmatch(pattern, string, FNM_CASEFOLD) == 0;
  187. }
  188. bool pkgVersionMatch::ExpressionMatches(const std::string& pattern, const char *string)
  189. {
  190. return ExpressionMatches(pattern.c_str(), string);
  191. }
  192. /*}}}*/
  193. // VersionMatch::FileMatch - Match against an index file /*{{{*/
  194. // ---------------------------------------------------------------------
  195. /* This matcher checks against the release file and the origin location
  196. to see if the constraints are met. */
  197. bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File)
  198. {
  199. if (Type == Release)
  200. {
  201. if (MatchAll == true)
  202. return true;
  203. /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
  204. cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
  205. if (RelVerStr.empty() == true && RelOrigin.empty() == true &&
  206. RelArchive.empty() == true && RelLabel.empty() == true &&
  207. RelRelease.empty() == true && RelCodename.empty() == true &&
  208. RelComponent.empty() == true && RelArchitecture.empty() == true)
  209. return false;
  210. if (RelVerStr.empty() == false)
  211. if (MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false &&
  212. ExpressionMatches(RelVerStr, File.Version()) == false)
  213. return false;
  214. if (RelOrigin.empty() == false)
  215. if (!ExpressionMatches(RelOrigin,File.Origin()))
  216. return false;
  217. if (RelArchive.empty() == false)
  218. if (!ExpressionMatches(RelArchive,File.Archive()))
  219. return false;
  220. if (RelCodename.empty() == false)
  221. if (!ExpressionMatches(RelCodename,File.Codename()))
  222. return false;
  223. if (RelRelease.empty() == false)
  224. if (!ExpressionMatches(RelRelease,File.Archive()) &&
  225. !ExpressionMatches(RelRelease,File.Codename()))
  226. return false;
  227. if (RelLabel.empty() == false)
  228. if (!ExpressionMatches(RelLabel,File.Label()))
  229. return false;
  230. if (RelComponent.empty() == false)
  231. if (!ExpressionMatches(RelComponent,File.Component()))
  232. return false;
  233. if (RelArchitecture.empty() == false)
  234. if (!ExpressionMatches(RelArchitecture,File.Architecture()))
  235. return false;
  236. return true;
  237. }
  238. if (Type == Origin)
  239. {
  240. if (OrSite.empty() == false) {
  241. if (File.Site() == NULL)
  242. return false;
  243. } else // so we are talking about file:// or status file
  244. {
  245. pkgCache::RlsFileIterator const RlsFile = File.ReleaseFile();
  246. if (strcmp(File.Site(),"") == 0 && RlsFile->Archive != 0 && strcmp(RlsFile.Archive(),"now") == 0) // skip the status file
  247. return false;
  248. }
  249. return (ExpressionMatches(OrSite, File.Site())); /* both strings match */
  250. }
  251. return false;
  252. }
  253. /*}}}*/