versionmatch.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. }
  90. if (RelVerStr.end()[-1] == '*')
  91. {
  92. RelVerPrefixMatch = true;
  93. RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1);
  94. }
  95. return;
  96. }
  97. if (Type == Origin)
  98. {
  99. OrSite = Data;
  100. return;
  101. }
  102. }
  103. /*}}}*/
  104. // VersionMatch::MatchVer - Match a version string with prefixing /*{{{*/
  105. // ---------------------------------------------------------------------
  106. /* */
  107. bool pkgVersionMatch::MatchVer(const char *A,string B,bool Prefix)
  108. {
  109. const char *Ab = A;
  110. const char *Ae = Ab + strlen(A);
  111. // Strings are not a compatible size.
  112. if (((unsigned)(Ae - Ab) != B.length() && Prefix == false) ||
  113. (unsigned)(Ae - Ab) < B.length())
  114. return false;
  115. // Match (leading?)
  116. if (stringcasecmp(B,Ab,Ab + B.length()) == 0)
  117. return true;
  118. return false;
  119. }
  120. /*}}}*/
  121. // VersionMatch::Find - Locate the best match for the select type /*{{{*/
  122. // ---------------------------------------------------------------------
  123. /* */
  124. pkgCache::VerIterator pkgVersionMatch::Find(pkgCache::PkgIterator Pkg)
  125. {
  126. pkgCache::VerIterator Ver = Pkg.VersionList();
  127. for (; Ver.end() == false; Ver++)
  128. {
  129. if (Type == Version)
  130. {
  131. if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true)
  132. return Ver;
  133. continue;
  134. }
  135. for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
  136. if (FileMatch(VF.File()) == true)
  137. return Ver;
  138. }
  139. // This will be Ended by now.
  140. return Ver;
  141. }
  142. /*}}}*/
  143. // VersionMatch::FileMatch - Match against an index file /*{{{*/
  144. // ---------------------------------------------------------------------
  145. /* This matcher checks against the release file and the origin location
  146. to see if the constraints are met. */
  147. bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File)
  148. {
  149. if (Type == Release)
  150. {
  151. if (MatchAll == true)
  152. return true;
  153. /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
  154. cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
  155. if (RelVerStr.empty() == true && RelOrigin.empty() == true &&
  156. RelArchive.empty() == true && RelLabel.empty() == true &&
  157. RelRelease.empty() == true && RelCodename.empty() == true &&
  158. RelComponent.empty() == true)
  159. return false;
  160. if (RelVerStr.empty() == false)
  161. if (File->Version == 0 ||
  162. MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false)
  163. return false;
  164. if (RelOrigin.empty() == false)
  165. if (File->Origin == 0 ||
  166. stringcasecmp(RelOrigin,File.Origin()) != 0)
  167. return false;
  168. if (RelArchive.empty() == false)
  169. if (File->Archive == 0 ||
  170. stringcasecmp(RelArchive,File.Archive()) != 0)
  171. return false;
  172. if (RelCodename.empty() == false)
  173. if (File->Codename == 0 ||
  174. stringcasecmp(RelCodename,File.Codename()) != 0)
  175. return false;
  176. if (RelRelease.empty() == false)
  177. if ((File->Archive == 0 ||
  178. stringcasecmp(RelRelease,File.Archive()) != 0) &&
  179. (File->Codename == 0 ||
  180. stringcasecmp(RelRelease,File.Codename()) != 0))
  181. return false;
  182. if (RelLabel.empty() == false)
  183. if (File->Label == 0 ||
  184. stringcasecmp(RelLabel,File.Label()) != 0)
  185. return false;
  186. if (RelComponent.empty() == false)
  187. if (File->Component == 0 ||
  188. stringcasecmp(RelComponent,File.Component()) != 0)
  189. return false;
  190. return true;
  191. }
  192. if (Type == Origin)
  193. {
  194. if (OrSite.empty() == false) {
  195. if (File->Site == 0 || OrSite != File.Site())
  196. return false;
  197. } else // so we are talking about file:// or status file
  198. if (strcmp(File.Site(),"") == 0 && File->Archive != 0) // skip the status file
  199. return false;
  200. return (OrSite == File.Site()); /* both strings match */
  201. }
  202. return false;
  203. }
  204. /*}}}*/