versionmatch.cc 6.9 KB

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