versionmatch.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: versionmatch.cc,v 1.4 2001/05/22 06:20:06 jgg 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. 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. return;
  44. // Are we a simple specification?
  45. string::const_iterator I = Data.begin();
  46. for (; I != Data.end() && *I != '='; I++);
  47. if (I == Data.end())
  48. {
  49. // Temporary
  50. if (isdigit(Data[0]))
  51. RelVerStr = Data;
  52. else
  53. RelArchive = Data;
  54. if (RelVerStr.end()[-1] == '*')
  55. {
  56. RelVerPrefixMatch = true;
  57. RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1);
  58. }
  59. return;
  60. }
  61. char Spec[300];
  62. char *Fragments[20];
  63. snprintf(Spec,sizeof(Spec),"%s",Data.c_str());
  64. if (TokSplitString(',',Spec,Fragments,
  65. sizeof(Fragments)/sizeof(Fragments[0])) == false)
  66. {
  67. Type = None;
  68. return;
  69. }
  70. for (unsigned J = 0; Fragments[J] != 0; J++)
  71. {
  72. if (strlen(Fragments[J]) < 3)
  73. continue;
  74. if (stringcasecmp(Fragments[J],Fragments[J]+2,"v=") == 0)
  75. RelVerStr = Fragments[J]+2;
  76. else if (stringcasecmp(Fragments[J],Fragments[J]+2,"o=") == 0)
  77. RelOrigin = Fragments[J]+2;
  78. else if (stringcasecmp(Fragments[J],Fragments[J]+2,"a=") == 0)
  79. RelArchive = Fragments[J]+2;
  80. else if (stringcasecmp(Fragments[J],Fragments[J]+2,"l=") == 0)
  81. RelLabel = Fragments[J]+2;
  82. else if (stringcasecmp(Fragments[J],Fragments[J]+2,"c=") == 0)
  83. RelComponent = Fragments[J]+2;
  84. }
  85. if (RelVerStr.end()[-1] == '*')
  86. {
  87. RelVerPrefixMatch = true;
  88. RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1);
  89. }
  90. return;
  91. }
  92. if (Type == Origin)
  93. {
  94. OrSite = Data;
  95. return;
  96. }
  97. }
  98. /*}}}*/
  99. // VersionMatch::MatchVer - Match a version string with prefixing /*{{{*/
  100. // ---------------------------------------------------------------------
  101. /* */
  102. bool pkgVersionMatch::MatchVer(const char *A,string B,bool Prefix)
  103. {
  104. const char *Ab = A;
  105. const char *Ae = Ab + strlen(A);
  106. // Strings are not a compatible size.
  107. if ((unsigned)(Ae - Ab) != B.length() && Prefix == false ||
  108. (unsigned)(Ae - Ab) < B.length())
  109. return false;
  110. // Match (leading?)
  111. if (stringcasecmp(B,Ab,Ab + B.length()) == 0)
  112. return true;
  113. return false;
  114. }
  115. /*}}}*/
  116. // VersionMatch::Find - Locate the best match for the select type /*{{{*/
  117. // ---------------------------------------------------------------------
  118. /* */
  119. pkgCache::VerIterator pkgVersionMatch::Find(pkgCache::PkgIterator Pkg)
  120. {
  121. pkgCache::VerIterator Ver = Pkg.VersionList();
  122. for (; Ver.end() == false; Ver++)
  123. {
  124. if (Type == Version)
  125. {
  126. if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true)
  127. return Ver;
  128. continue;
  129. }
  130. for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
  131. if (FileMatch(VF.File()) == true)
  132. return Ver;
  133. }
  134. // This will be Ended by now.
  135. return Ver;
  136. }
  137. /*}}}*/
  138. // VersionMatch::FileMatch - Match against an index file /*{{{*/
  139. // ---------------------------------------------------------------------
  140. /* This matcher checks against the release file and the origin location
  141. to see if the constraints are met. */
  142. bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File)
  143. {
  144. if (Type == Release)
  145. {
  146. /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
  147. cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
  148. if (RelVerStr.empty() == true && RelOrigin.empty() == true &&
  149. RelArchive.empty() == true && RelLabel.empty() == true &&
  150. RelComponent.empty() == true)
  151. return false;
  152. if (RelVerStr.empty() == false)
  153. if (File->Version == 0 ||
  154. MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false)
  155. return false;
  156. if (RelOrigin.empty() == false)
  157. if (File->Origin == 0 ||
  158. stringcasecmp(RelOrigin,File.Origin()) != 0)
  159. return false;
  160. if (RelArchive.empty() == false)
  161. {
  162. if (File->Archive == 0 ||
  163. stringcasecmp(RelArchive,File.Archive()) != 0)
  164. return false;
  165. }
  166. if (RelLabel.empty() == false)
  167. if (File->Label == 0 ||
  168. stringcasecmp(RelLabel,File.Label()) != 0)
  169. return false;
  170. if (RelComponent.empty() == false)
  171. if (File->Component == 0 ||
  172. stringcasecmp(RelComponent,File.Component()) != 0)
  173. return false;
  174. return true;
  175. }
  176. if (Type == Origin)
  177. {
  178. if (OrSite.empty() == false)
  179. if (File->Site == 0 ||
  180. OrSite != File.Site())
  181. return false;
  182. return true;
  183. }
  184. return false;
  185. }
  186. /*}}}*/