Version.pm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # Copyright © Colin Watson <cjwatson@debian.org>
  2. # Copyright © Ian Jackson <iwj@debian.org>
  3. # Copyright © 2007 Don Armstrong <don@donarmstrong.com>.
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License along
  13. # with this program; if not, write to the Free Software Foundation, Inc.,
  14. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. package Dpkg::Version;
  16. use strict;
  17. use warnings;
  18. use Dpkg::ErrorHandling;
  19. use Dpkg::Gettext;
  20. use base qw(Exporter);
  21. our @EXPORT_OK = qw(vercmp compare_versions check_version parseversion);
  22. =head1 NAME
  23. Dpkg::Version - pure-Perl dpkg-style version comparison
  24. =head1 DESCRIPTION
  25. The Dpkg::Version module provides pure-Perl routines to compare
  26. dpkg-style version numbers, as used in Debian packages. If you have the
  27. libapt-pkg Perl bindings available (Debian package libapt-pkg-perl), they
  28. may offer better performance.
  29. =head1 METHODS
  30. =over 8
  31. =cut
  32. sub parseversion ($)
  33. {
  34. my $ver = shift;
  35. my %verhash;
  36. if ($ver =~ /:/)
  37. {
  38. $ver =~ /^(\d+):(.+)/ or die "bad version number '$ver'";
  39. $verhash{epoch} = $1;
  40. $ver = $2;
  41. }
  42. else
  43. {
  44. $verhash{epoch} = 0;
  45. }
  46. if ($ver =~ /(.+)-(.*)$/)
  47. {
  48. $verhash{version} = $1;
  49. $verhash{revision} = $2;
  50. }
  51. else
  52. {
  53. $verhash{version} = $ver;
  54. $verhash{revision} = 0;
  55. }
  56. return %verhash;
  57. }
  58. # verrevcmp
  59. # This function is almost exactly equivalent
  60. # to dpkg's verrevcmp function, including the
  61. # order subroutine which it uses.
  62. sub verrevcmp($$)
  63. {
  64. sub order{
  65. my ($x) = @_;
  66. ##define order(x) ((x) == '~' ? -1 \
  67. # : cisdigit((x)) ? 0 \
  68. # : !(x) ? 0 \
  69. # : cisalpha((x)) ? (x) \
  70. # : (x) + 256)
  71. # This comparison is out of dpkg's order to avoid
  72. # comparing things to undef and triggering warnings.
  73. if (not defined $x or not length $x) {
  74. return 0;
  75. }
  76. elsif ($x eq '~') {
  77. return -1;
  78. }
  79. elsif ($x =~ /^\d$/) {
  80. return 0;
  81. }
  82. elsif ($x =~ /^[A-Za-z]$/) {
  83. return ord($x);
  84. }
  85. else {
  86. return ord($x) + 256;
  87. }
  88. }
  89. my ($val, $ref) = @_;
  90. $val = "" if not defined $val;
  91. $ref = "" if not defined $ref;
  92. my @val = split //,$val;
  93. my @ref = split //,$ref;
  94. my $vc = shift @val;
  95. my $rc = shift @ref;
  96. while (defined $vc or defined $rc) {
  97. my $first_diff = 0;
  98. while ((defined $vc and $vc !~ /^\d$/) or
  99. (defined $rc and $rc !~ /^\d$/)) {
  100. my $vo = order($vc); my $ro = order($rc);
  101. # Unlike dpkg's verrevcmp, we only return 1 or -1 here.
  102. return (($vo - $ro > 0) ? 1 : -1) if $vo != $ro;
  103. $vc = shift @val; $rc = shift @ref;
  104. }
  105. while (defined $vc and $vc eq '0') {
  106. $vc = shift @val;
  107. }
  108. while (defined $rc and $rc eq '0') {
  109. $rc = shift @ref;
  110. }
  111. while (defined $vc and $vc =~ /^\d$/ and
  112. defined $rc and $rc =~ /^\d$/) {
  113. $first_diff = ord($vc) - ord($rc) if !$first_diff;
  114. $vc = shift @val; $rc = shift @ref;
  115. }
  116. return 1 if defined $vc and $vc =~ /^\d$/;
  117. return -1 if defined $rc and $rc =~ /^\d$/;
  118. return (($first_diff > 0) ? 1 : -1) if $first_diff;
  119. }
  120. return 0;
  121. }
  122. =item vercmp
  123. Compare the two arguments as dpkg-style version numbers. Returns -1 if the
  124. first argument represents a lower version number than the second, 1 if the
  125. first argument represents a higher version number than the second, and 0 if
  126. the two arguments represent equal version numbers.
  127. =cut
  128. sub vercmp ($$)
  129. {
  130. my %version = parseversion $_[0];
  131. my %refversion = parseversion $_[1];
  132. return 1 if $version{epoch} > $refversion{epoch};
  133. return -1 if $version{epoch} < $refversion{epoch};
  134. my $r = verrevcmp($version{version}, $refversion{version});
  135. return $r if $r;
  136. return verrevcmp($version{revision}, $refversion{revision});
  137. }
  138. =item compare_versions
  139. Emulates dpkg --compare-versions. Takes two versions as arguments
  140. one and three and one operator as argument two. Supports the following
  141. operators: 'gt', 'ge', 'eq', 'le', 'lt', and '>>', '>=', '=', '<=', '<<'.
  142. Returns a true value if the specified condition is true, a false value
  143. otherwise.
  144. =cut
  145. sub compare_versions ($$$)
  146. {
  147. my $rel = $_[1];
  148. my $res = vercmp($_[0], $_[2]);
  149. warning("operator %s is deprecated in compare_versions(): use %s or %s",
  150. $rel, "$rel$rel", "$rel=") if ($rel eq '>' or $rel eq '<');
  151. if ($rel eq 'gt' or $rel eq '>>') {
  152. return $res > 0;
  153. } elsif ($rel eq 'ge' or $rel eq '>=' or $rel eq '>') {
  154. return $res >= 0;
  155. } elsif ($rel eq 'eq' or $rel eq '=') {
  156. return $res == 0;
  157. } elsif ($rel eq 'le' or $rel eq '<=' or $rel eq '<') {
  158. return $res <= 0;
  159. } elsif ($rel eq 'lt' or $rel eq '<<') {
  160. return $res < 0;
  161. } else {
  162. die "bad relation '$rel'";
  163. }
  164. }
  165. =item check_version($version, $die)
  166. If $die is false (or unset), returns true if the version is valid and
  167. false if it contains illegal characters. If $die is true, it dies with
  168. an error message if it contains illegal characters, otherwise it returns
  169. true.
  170. =cut
  171. sub check_version ($;$) {
  172. my ($version, $die) = @_;
  173. $version ||= "";
  174. if ($version =~ m/[^-+:.0-9a-zA-Z~]/o) {
  175. error(_g("version number contains illegal character `%s'"), $&) if $die;
  176. return 0;
  177. }
  178. return 1;
  179. }
  180. =back
  181. =head1 AUTHOR
  182. Don Armstrong <don@donarmstrong.com> and Colin Watson
  183. E<lt>cjwatson@debian.orgE<gt>, based on the implementation in
  184. C<dpkg/lib/vercmp.c> by Ian Jackson and others.
  185. =cut
  186. 1;