versionmatch.cc 6.1 KB

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