versionmatch.cc 6.2 KB

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