versionmatch.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. continue;
  139. }
  140. for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
  141. if (FileMatch(VF.File()) == true)
  142. return Ver;
  143. }
  144. // This will be Ended by now.
  145. return Ver;
  146. }
  147. #ifndef FNM_CASEFOLD
  148. #define FNM_CASEFOLD 0
  149. #endif
  150. bool pkgVersionMatch::ExpressionMatches(const char *pattern, const char *string)
  151. {
  152. std::cerr << "MATCH " << pattern;
  153. if (pattern[0] == '/') {
  154. bool res = false;
  155. size_t length = strlen(pattern);
  156. if (pattern[length - 1] == '/') {
  157. regex_t preg;
  158. char *regex = strdup(pattern + 1);
  159. regex[length - 2] = '\0';
  160. if (regcomp(&preg, regex, REG_EXTENDED | REG_ICASE) != 0) {
  161. std::cerr << "E: Invalid regular expression: " << regex << "\n";
  162. } else if (regexec(&preg, string, 0, NULL, 0) == 0) {
  163. res = true;
  164. }
  165. free(regex);
  166. return res;
  167. }
  168. }
  169. return fnmatch(pattern, string, FNM_CASEFOLD) == 0;
  170. }
  171. bool pkgVersionMatch::ExpressionMatches(const std::string& pattern, const char *string)
  172. {
  173. return ExpressionMatches(pattern.c_str(), string);
  174. }
  175. /*}}}*/
  176. // VersionMatch::FileMatch - Match against an index file /*{{{*/
  177. // ---------------------------------------------------------------------
  178. /* This matcher checks against the release file and the origin location
  179. to see if the constraints are met. */
  180. bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File)
  181. {
  182. if (Type == Release)
  183. {
  184. if (MatchAll == true)
  185. return true;
  186. /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
  187. cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
  188. if (RelVerStr.empty() == true && RelOrigin.empty() == true &&
  189. RelArchive.empty() == true && RelLabel.empty() == true &&
  190. RelRelease.empty() == true && RelCodename.empty() == true &&
  191. RelComponent.empty() == true && RelArchitecture.empty() == true)
  192. return false;
  193. if (RelVerStr.empty() == false)
  194. if (File->Version == 0 ||
  195. (MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false &&
  196. ExpressionMatches(RelVerStr, File.Version()) == false))
  197. return false;
  198. if (RelOrigin.empty() == false)
  199. if (File->Origin == 0 || !ExpressionMatches(RelOrigin,File.Origin()))
  200. return false;
  201. if (RelArchive.empty() == false)
  202. if (File->Archive == 0 ||
  203. !ExpressionMatches(RelArchive,File.Archive()))
  204. return false;
  205. if (RelCodename.empty() == false)
  206. if (File->Codename == 0 ||
  207. !ExpressionMatches(RelCodename,File.Codename()))
  208. return false;
  209. if (RelRelease.empty() == false)
  210. if ((File->Archive == 0 ||
  211. !ExpressionMatches(RelRelease,File.Archive())) &&
  212. (File->Codename == 0 ||
  213. !ExpressionMatches(RelRelease,File.Codename())))
  214. return false;
  215. if (RelLabel.empty() == false)
  216. if (File->Label == 0 ||
  217. !ExpressionMatches(RelLabel,File.Label()))
  218. return false;
  219. if (RelComponent.empty() == false)
  220. if (File->Component == 0 ||
  221. !ExpressionMatches(RelComponent,File.Component()))
  222. return false;
  223. if (RelArchitecture.empty() == false)
  224. if (File->Architecture == 0 ||
  225. !ExpressionMatches(RelArchitecture,File.Architecture()))
  226. return false;
  227. return true;
  228. }
  229. if (Type == Origin)
  230. {
  231. if (OrSite.empty() == false) {
  232. if (File->Site == 0 || !ExpressionMatches(OrSite, File.Site()))
  233. return false;
  234. } else // so we are talking about file:// or status file
  235. if (strcmp(File.Site(),"") == 0 && File->Archive != 0) // skip the status file
  236. return false;
  237. return (ExpressionMatches(OrSite, File.Site())); /* both strings match */
  238. }
  239. return false;
  240. }
  241. /*}}}*/