versionmatch.cc 7.9 KB

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