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