versionmatch.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 (VersionMatches(Ver))
  147. return Ver;
  148. }
  149. // This will be Ended by now.
  150. return Ver;
  151. }
  152. /*}}}*/
  153. // VersionMatch::Find - Locate the best match for the select type /*{{{*/
  154. // ---------------------------------------------------------------------
  155. /* */
  156. bool pkgVersionMatch::VersionMatches(pkgCache::VerIterator Ver)
  157. {
  158. if (Type == Version)
  159. {
  160. if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true)
  161. return true;
  162. if (ExpressionMatches(VerStr, Ver.VerStr()) == true)
  163. return true;
  164. return false;
  165. }
  166. for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; ++VF)
  167. if (FileMatch(VF.File()) == true)
  168. return true;
  169. return false;
  170. }
  171. /*}}}*/
  172. #ifndef FNM_CASEFOLD
  173. #define FNM_CASEFOLD 0
  174. #endif
  175. bool pkgVersionMatch::ExpressionMatches(const char *pattern, const char *string)/*{{{*/
  176. {
  177. if (pattern == NULL || string == NULL)
  178. return false;
  179. if (pattern[0] == '/') {
  180. size_t length = strlen(pattern);
  181. if (pattern[length - 1] == '/') {
  182. bool res = false;
  183. regex_t preg;
  184. char *regex = strdup(pattern + 1);
  185. regex[length - 2] = '\0';
  186. if (regcomp(&preg, regex, REG_EXTENDED | REG_ICASE) != 0) {
  187. _error->Warning("Invalid regular expression: %s", regex);
  188. } else if (regexec(&preg, string, 0, NULL, 0) == 0) {
  189. res = true;
  190. }
  191. free(regex);
  192. regfree(&preg);
  193. return res;
  194. }
  195. }
  196. return fnmatch(pattern, string, FNM_CASEFOLD) == 0;
  197. }
  198. bool pkgVersionMatch::ExpressionMatches(const std::string& pattern, const char *string)
  199. {
  200. return ExpressionMatches(pattern.c_str(), string);
  201. }
  202. /*}}}*/
  203. // VersionMatch::FileMatch - Match against an index file /*{{{*/
  204. // ---------------------------------------------------------------------
  205. /* This matcher checks against the release file and the origin location
  206. to see if the constraints are met. */
  207. bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File)
  208. {
  209. if (Type == Release)
  210. {
  211. if (MatchAll == true)
  212. return true;
  213. /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
  214. cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
  215. if (RelVerStr.empty() == true && RelOrigin.empty() == true &&
  216. RelArchive.empty() == true && RelLabel.empty() == true &&
  217. RelRelease.empty() == true && RelCodename.empty() == true &&
  218. RelComponent.empty() == true && RelArchitecture.empty() == true)
  219. return false;
  220. if (RelVerStr.empty() == false)
  221. if (MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false &&
  222. ExpressionMatches(RelVerStr, File.Version()) == false)
  223. return false;
  224. if (RelOrigin.empty() == false)
  225. if (!ExpressionMatches(RelOrigin,File.Origin()))
  226. return false;
  227. if (RelArchive.empty() == false)
  228. if (!ExpressionMatches(RelArchive,File.Archive()))
  229. return false;
  230. if (RelCodename.empty() == false)
  231. if (!ExpressionMatches(RelCodename,File.Codename()))
  232. return false;
  233. if (RelRelease.empty() == false)
  234. if (!ExpressionMatches(RelRelease,File.Archive()) &&
  235. !ExpressionMatches(RelRelease,File.Codename()))
  236. return false;
  237. if (RelLabel.empty() == false)
  238. if (!ExpressionMatches(RelLabel,File.Label()))
  239. return false;
  240. if (RelComponent.empty() == false)
  241. if (!ExpressionMatches(RelComponent,File.Component()))
  242. return false;
  243. if (RelArchitecture.empty() == false)
  244. if (!ExpressionMatches(RelArchitecture,File.Architecture()))
  245. return false;
  246. return true;
  247. }
  248. if (Type == Origin)
  249. {
  250. if (OrSite.empty() == false) {
  251. if (File.Site() == NULL)
  252. return false;
  253. }
  254. else if (File->Release == 0)// only 'bad' files like dpkg.status file has no release file
  255. return false;
  256. return (ExpressionMatches(OrSite, File.Site())); /* both strings match */
  257. }
  258. return false;
  259. }
  260. /*}}}*/