versionmatch.cc 5.9 KB

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