debversion.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: debversion.cc,v 1.8 2003/09/10 23:39:49 mdz Exp $
  4. /* ######################################################################
  5. Debian Version - Versioning system for Debian
  6. This implements the standard Debian versioning system.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #define APT_COMPATIBILITY 986
  11. #include <config.h>
  12. #include <apt-pkg/debversion.h>
  13. #include <apt-pkg/pkgcache.h>
  14. #include <stdlib.h>
  15. #include <ctype.h>
  16. /*}}}*/
  17. debVersioningSystem debVS;
  18. // debVS::debVersioningSystem - Constructor /*{{{*/
  19. // ---------------------------------------------------------------------
  20. /* */
  21. debVersioningSystem::debVersioningSystem()
  22. {
  23. Label = "Standard .deb";
  24. }
  25. /*}}}*/
  26. // debVS::CmpFragment - Compare versions /*{{{*/
  27. // ---------------------------------------------------------------------
  28. /* This compares a fragment of the version. This is a slightly adapted
  29. version of what dpkg uses. */
  30. #define order(x) ((x) == '~' ? -1 \
  31. : isdigit((x)) ? 0 \
  32. : !(x) ? 0 \
  33. : isalpha((x)) ? (x) \
  34. : (x) + 256)
  35. int debVersioningSystem::CmpFragment(const char *A,const char *AEnd,
  36. const char *B,const char *BEnd)
  37. {
  38. if (A >= AEnd && B >= BEnd)
  39. return 0;
  40. if (A >= AEnd)
  41. {
  42. if (*B == '~') return 1;
  43. return -1;
  44. }
  45. if (B >= BEnd)
  46. {
  47. if (*A == '~') return -1;
  48. return 1;
  49. }
  50. /* Iterate over the whole string
  51. What this does is to split the whole string into groups of
  52. numeric and non numeric portions. For instance:
  53. a67bhgs89
  54. Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
  55. 2.7.2-linux-1
  56. Has '2', '.', '7', '.' ,'-linux-','1' */
  57. const char *lhs = A;
  58. const char *rhs = B;
  59. while (lhs != AEnd && rhs != BEnd)
  60. {
  61. int first_diff = 0;
  62. while (lhs != AEnd && rhs != BEnd &&
  63. (!isdigit(*lhs) || !isdigit(*rhs)))
  64. {
  65. int vc = order(*lhs);
  66. int rc = order(*rhs);
  67. if (vc != rc)
  68. return vc - rc;
  69. lhs++; rhs++;
  70. }
  71. while (*lhs == '0')
  72. lhs++;
  73. while (*rhs == '0')
  74. rhs++;
  75. while (isdigit(*lhs) && isdigit(*rhs))
  76. {
  77. if (!first_diff)
  78. first_diff = *lhs - *rhs;
  79. lhs++;
  80. rhs++;
  81. }
  82. if (isdigit(*lhs))
  83. return 1;
  84. if (isdigit(*rhs))
  85. return -1;
  86. if (first_diff)
  87. return first_diff;
  88. }
  89. // The strings must be equal
  90. if (lhs == AEnd && rhs == BEnd)
  91. return 0;
  92. // lhs is shorter
  93. if (lhs == AEnd)
  94. {
  95. if (*rhs == '~') return 1;
  96. return -1;
  97. }
  98. // rhs is shorter
  99. if (rhs == BEnd)
  100. {
  101. if (*lhs == '~') return -1;
  102. return 1;
  103. }
  104. // Shouldnt happen
  105. return 1;
  106. }
  107. /*}}}*/
  108. // debVS::CmpVersion - Comparison for versions /*{{{*/
  109. // ---------------------------------------------------------------------
  110. /* This fragments the version into E:V-R triples and compares each
  111. portion separately. */
  112. int debVersioningSystem::DoCmpVersion(const char *A,const char *AEnd,
  113. const char *B,const char *BEnd)
  114. {
  115. // Strip off the epoch and compare it
  116. const char *lhs = A;
  117. const char *rhs = B;
  118. for (;lhs != AEnd && *lhs != ':'; lhs++);
  119. for (;rhs != BEnd && *rhs != ':'; rhs++);
  120. if (lhs == AEnd)
  121. lhs = A;
  122. if (rhs == BEnd)
  123. rhs = B;
  124. // Special case: a zero epoch is the same as no epoch,
  125. // so remove it.
  126. if (lhs != A)
  127. {
  128. for (; *A == '0'; ++A);
  129. if (A == lhs)
  130. {
  131. ++A;
  132. ++lhs;
  133. }
  134. }
  135. if (rhs != B)
  136. {
  137. for (; *B == '0'; ++B);
  138. if (B == rhs)
  139. {
  140. ++B;
  141. ++rhs;
  142. }
  143. }
  144. // Compare the epoch
  145. int Res = CmpFragment(A,lhs,B,rhs);
  146. if (Res != 0)
  147. return Res;
  148. // Skip the :
  149. if (lhs != A)
  150. lhs++;
  151. if (rhs != B)
  152. rhs++;
  153. // Find the last -
  154. const char *dlhs = AEnd-1;
  155. const char *drhs = BEnd-1;
  156. for (;dlhs > lhs && *dlhs != '-'; dlhs--);
  157. for (;drhs > rhs && *drhs != '-'; drhs--);
  158. if (dlhs == lhs)
  159. dlhs = AEnd;
  160. if (drhs == rhs)
  161. drhs = BEnd;
  162. // Compare the main version
  163. Res = CmpFragment(lhs,dlhs,rhs,drhs);
  164. if (Res != 0)
  165. return Res;
  166. // Skip the -
  167. if (dlhs != lhs)
  168. dlhs++;
  169. if (drhs != rhs)
  170. drhs++;
  171. // no debian revision need to be treated like -0
  172. if (*(dlhs-1) == '-' && *(drhs-1) == '-')
  173. return CmpFragment(dlhs,AEnd,drhs,BEnd);
  174. else if (*(dlhs-1) == '-')
  175. {
  176. const char* null = "0";
  177. return CmpFragment(dlhs,AEnd,null, null+1);
  178. }
  179. else if (*(drhs-1) == '-')
  180. {
  181. const char* null = "0";
  182. return CmpFragment(null, null+1, drhs, BEnd);
  183. }
  184. else
  185. return 0;
  186. }
  187. /*}}}*/
  188. // debVS::CheckDep - Check a single dependency /*{{{*/
  189. // ---------------------------------------------------------------------
  190. /* This simply preforms the version comparison and switch based on
  191. operator. If DepVer is 0 then we are comparing against a provides
  192. with no version. */
  193. bool debVersioningSystem::CheckDep(const char *PkgVer,
  194. int Op,const char *DepVer)
  195. {
  196. if (DepVer == 0 || DepVer[0] == 0)
  197. return true;
  198. if (PkgVer == 0 || PkgVer[0] == 0)
  199. return false;
  200. // Perform the actual comparision.
  201. int Res = CmpVersion(PkgVer,DepVer);
  202. switch (Op & 0x0F)
  203. {
  204. case pkgCache::Dep::LessEq:
  205. if (Res <= 0)
  206. return true;
  207. break;
  208. case pkgCache::Dep::GreaterEq:
  209. if (Res >= 0)
  210. return true;
  211. break;
  212. case pkgCache::Dep::Less:
  213. if (Res < 0)
  214. return true;
  215. break;
  216. case pkgCache::Dep::Greater:
  217. if (Res > 0)
  218. return true;
  219. break;
  220. case pkgCache::Dep::Equals:
  221. if (Res == 0)
  222. return true;
  223. break;
  224. case pkgCache::Dep::NotEquals:
  225. if (Res != 0)
  226. return true;
  227. break;
  228. }
  229. return false;
  230. }
  231. /*}}}*/
  232. // debVS::UpstreamVersion - Return the upstream version string /*{{{*/
  233. // ---------------------------------------------------------------------
  234. /* This strips all the debian specific information from the version number */
  235. std::string debVersioningSystem::UpstreamVersion(const char *Ver)
  236. {
  237. // Strip off the bit before the first colon
  238. const char *I = Ver;
  239. for (; *I != 0 && *I != ':'; I++);
  240. if (*I == ':')
  241. Ver = I + 1;
  242. // Chop off the trailing -
  243. I = Ver;
  244. unsigned Last = strlen(Ver);
  245. for (; *I != 0; I++)
  246. if (*I == '-')
  247. Last = I - Ver;
  248. return std::string(Ver,Last);
  249. }
  250. /*}}}*/