versionmatch.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: versionmatch.cc,v 1.3 2001/04/29 05:13:51 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. /*}}}*/
  19. // VersionMatch::pkgVersionMatch - Constructor /*{{{*/
  20. // ---------------------------------------------------------------------
  21. /* Break up the data string according to the selected type */
  22. pkgVersionMatch::pkgVersionMatch(string Data,MatchType Type) : Type(Type)
  23. {
  24. if (Type == None || Data.length() < 1)
  25. return;
  26. // Cut up the version representation
  27. if (Type == Version)
  28. {
  29. if (Data.end()[-1] == '*')
  30. {
  31. VerPrefixMatch = true;
  32. VerStr = string(Data.begin(),Data.end()-1);
  33. }
  34. else
  35. VerStr = Data;
  36. return;
  37. }
  38. if (Type == Release)
  39. {
  40. // All empty = match all
  41. if (Data == "*")
  42. return;
  43. // Are we a simple specification?
  44. const char *I = Data.begin();
  45. for (; I < Data.end() && *I != '='; I++);
  46. if (I == Data.end())
  47. {
  48. // Temporary
  49. if (isdigit(Data[0]))
  50. RelVerStr = Data;
  51. else
  52. RelArchive = Data;
  53. if (RelVerStr.end()[-1] == '*')
  54. {
  55. RelVerPrefixMatch = true;
  56. RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1);
  57. }
  58. return;
  59. }
  60. char Spec[300];
  61. char *Fragments[20];
  62. snprintf(Spec,sizeof(Spec),"%s",Data.c_str());
  63. if (TokSplitString(',',Spec,Fragments,
  64. sizeof(Fragments)/sizeof(Fragments[0])) == false)
  65. {
  66. Type = None;
  67. return;
  68. }
  69. for (unsigned J = 0; Fragments[J] != 0; J++)
  70. {
  71. if (strlen(Fragments[J]) < 3)
  72. continue;
  73. if (stringcasecmp(Fragments[J],Fragments[J]+2,"v=") == 0)
  74. RelVerStr = Fragments[J]+2;
  75. else if (stringcasecmp(Fragments[J],Fragments[J]+2,"o=") == 0)
  76. RelOrigin = Fragments[J]+2;
  77. else if (stringcasecmp(Fragments[J],Fragments[J]+2,"a=") == 0)
  78. RelArchive = Fragments[J]+2;
  79. else if (stringcasecmp(Fragments[J],Fragments[J]+2,"l=") == 0)
  80. RelLabel = Fragments[J]+2;
  81. else if (stringcasecmp(Fragments[J],Fragments[J]+2,"c=") == 0)
  82. RelComponent = Fragments[J]+2;
  83. }
  84. if (RelVerStr.end()[-1] == '*')
  85. {
  86. RelVerPrefixMatch = true;
  87. RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1);
  88. }
  89. return;
  90. }
  91. if (Type == Origin)
  92. {
  93. OrSite = Data;
  94. return;
  95. }
  96. }
  97. /*}}}*/
  98. // VersionMatch::MatchVer - Match a version string with prefixing /*{{{*/
  99. // ---------------------------------------------------------------------
  100. /* */
  101. bool pkgVersionMatch::MatchVer(const char *A,string B,bool Prefix)
  102. {
  103. const char *Ab = A;
  104. const char *Ae = Ab + strlen(A);
  105. // Strings are not a compatible size.
  106. if ((unsigned)(Ae - Ab) != B.length() && Prefix == false ||
  107. (unsigned)(Ae - Ab) < B.length())
  108. return false;
  109. // Match (leading?)
  110. if (stringcasecmp(B.begin(),B.end(),
  111. 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. /*}}}*/