versionmatch.cc 8.0 KB

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