You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

272 lines
6.1 KiB

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