Version.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. # Copyright © Colin Watson <cjwatson@debian.org>
  2. # Copyright © Ian Jackson <iwj@debian.org>
  3. # Copyright © 2007 Don Armstrong <don@donarmstrong.com>.
  4. # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License along
  14. # with this program; if not, write to the Free Software Foundation, Inc.,
  15. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. package Dpkg::Version;
  17. use strict;
  18. use warnings;
  19. use Dpkg::ErrorHandling;
  20. use Dpkg::Gettext;
  21. use base qw(Exporter);
  22. our @EXPORT = qw(version_compare version_compare_op
  23. version_normalize_cmp_op version_compare_string
  24. version_compare_part version_split_digits version_check
  25. CMP_OP_LT CMP_OP_LE CMP_OP_EQ CMP_OP_GE CMP_OP_GT);
  26. our @EXPORT_OK = qw(vercmp compare_versions check_version parseversion);
  27. use constant {
  28. CMP_OP_LT => '<<',
  29. CMP_OP_LE => '<=',
  30. CMP_OP_EQ => '=',
  31. CMP_OP_GE => '>=',
  32. CMP_OP_GT => '>>',
  33. };
  34. use overload
  35. '<=>' => \&comparison,
  36. 'cmp' => \&comparison,
  37. '""' => \&as_string,
  38. 'bool' => sub { return 1 };
  39. =head1 NAME
  40. Dpkg::Version - handling and comparing dpkg-style version numbers
  41. =head1 DESCRIPTION
  42. The Dpkg::Version module provides pure-Perl routines to compare
  43. dpkg-style version numbers (as used in Debian packages) and also
  44. an object oriented interface overriding perl operators
  45. to do the right thing when you compare Dpkg::Version object between
  46. them.
  47. =head1 OBJECT INTERFACE
  48. =over 4
  49. =item my $v = Dpkg::Version->new($version)
  50. Create a new Dpkg::Version object corresponding to the version indicated in
  51. the string (scalar) $version. Returns undef if the string doesn't contain
  52. a valid version (see version_check for details).
  53. =cut
  54. sub new {
  55. my ($this, $ver) = @_;
  56. my $class = ref($this) || $this;
  57. $ver = "$ver" if ref($ver); # Try to stringify objects
  58. return undef unless version_check($ver);
  59. my $self = {};
  60. if ($ver =~ /^(\d*):(.+)$/) {
  61. $self->{'epoch'} = $1;
  62. $ver = $2;
  63. } else {
  64. $self->{'epoch'} = 0;
  65. $self->{'no_epoch'} = 1;
  66. }
  67. if ($ver =~ /(.+)-(.*)$/) {
  68. $self->{'version'} = $1;
  69. $self->{'revision'} = $2;
  70. } else {
  71. $self->{'version'} = $ver;
  72. $self->{'revision'} = 0;
  73. $self->{'no_revision'} = 1;
  74. }
  75. return bless $self, $class;
  76. }
  77. =item $v->epoch(), $v->version(), $v->revision()
  78. Returns the corresponding part of the full version string.
  79. =cut
  80. sub epoch {
  81. my $self = shift;
  82. return $self->{'epoch'};
  83. }
  84. sub version {
  85. my $self = shift;
  86. return $self->{'version'};
  87. }
  88. sub revision {
  89. my $self = shift;
  90. return $self->{'revision'};
  91. }
  92. =item $v1 <=> $v2, $v1 < $v2, $v1 <= $v2, $v1 > $v2, $v1 >= $v2
  93. Numerical comparison of various versions numbers. One of the two operands
  94. needs to be a Dpkg::Version, the other one can be anything provided that
  95. its string representation is a version number.
  96. =cut
  97. sub comparison {
  98. my ($a, $b, $inverted) = @_;
  99. if (not ref($b) or not $b->isa("Dpkg::Version")) {
  100. $b = Dpkg::Version->new($b);
  101. }
  102. ($a, $b) = ($b, $a) if $inverted;
  103. my $r = $a->epoch() <=> $b->epoch();
  104. return $r if $r;
  105. $r = version_compare_part($a->version(), $b->version());
  106. return $r if $r;
  107. return version_compare_part($a->revision(), $b->revision());
  108. }
  109. =item "$v"
  110. =item $v->as_string()
  111. Returns the string representation of the version number.
  112. =cut
  113. sub as_string {
  114. my ($self) = @_;
  115. my $str = "";
  116. $str .= $self->{epoch} . ":" unless $self->{no_epoch};
  117. $str .= $self->{version};
  118. $str .= "-" . $self->{revision} unless $self->{no_revision};
  119. return $str;
  120. }
  121. =back
  122. =head1 FUNCTIONS
  123. All the functions are exported by default.
  124. =over 4
  125. =item version_compare($a, $b)
  126. Returns -1 is $a is smaller than $b, 0 if they are equal and 1 if $a
  127. is bigger than $b.
  128. If $a or $b are not valid version numbers, it dies with an error.
  129. =cut
  130. sub version_compare($$) {
  131. my ($a, $b) = @_;
  132. my $va = Dpkg::Version->new($a) || error(_g("%s is not a valid version"), "$a");
  133. my $vb = Dpkg::Version->new($b) || error(_g("%s is not a valid version"), "$b");
  134. return $va <=> $vb;
  135. }
  136. =item version_compare_op($a, $op, $b)
  137. Returns the result (0 or 1) of the given comparison operation. This
  138. function is implemented on top of version_compare().
  139. Allowed values for $op are the exported constants CMP_OP_GT, CMP_OP_GE,
  140. CMP_OP_EQ, CMP_OP_LE, CMP_OP_LT. Use version_normalize_cmp_op() if you
  141. have an input string containing the operator.
  142. =cut
  143. sub version_compare_op($$$) {
  144. my ($a, $op, $b) = @_;
  145. my $res = version_compare($a, $b);
  146. if ($op eq CMP_OP_GT) {
  147. return $res > 0;
  148. } elsif ($op eq CMP_OP_GE) {
  149. return $res >= 0;
  150. } elsif ($op eq CMP_OP_EQ) {
  151. return $res == 0;
  152. } elsif ($op eq CMP_OP_LE) {
  153. return $res <= 0;
  154. } elsif ($op eq CMP_OP_LT) {
  155. return $res < 0;
  156. } else {
  157. internerr("unsupported operator for version_compare_op(): '$op'");
  158. }
  159. }
  160. =item my $cmp_op = version_normalize_cmp_op($op)
  161. Returns the normalized constant of the comparison operator $op (a value
  162. among CMP_OP_GT, CMP_OP_GE, CMP_OP_EQ, CMP_OP_LE and CMP_OP_LT). Supported
  163. operators names in input are: "gt", "ge", "eq", "le", "lt", ">>", ">=",
  164. "=", "<=", "<<". ">" and "<" are also supported but should not be used as
  165. they are obsolete aliases of ">=" and "<=".
  166. =cut
  167. sub version_normalize_cmp_op($) {
  168. my $op = shift;
  169. warning("operator %s is deprecated: use %s or %s",
  170. $op, "$op$op", "$op=") if ($op eq '>' or $op eq '<');
  171. if ($op eq '>>' or $op eq 'gt') {
  172. return CMP_OP_GT;
  173. } elsif ($op eq '>=' or $op eq 'ge' or $op eq '>') {
  174. return CMP_OP_GE;
  175. } elsif ($op eq '=' or $op eq 'eq') {
  176. return CMP_OP_EQ;
  177. } elsif ($op eq '<=' or $op eq 'le' or $op eq '<') {
  178. return CMP_OP_LE;
  179. } elsif ($op eq '<<' or $op eq 'lt') {
  180. return CMP_OP_LT;
  181. } else {
  182. internerr("bad comparison operator '$op'");
  183. }
  184. }
  185. =item version_compare_string($a, $b)
  186. String comparison function used for comparing non-numerical parts of version
  187. numbers. Returns -1 is $a is smaller than $b, 0 if they are equal and 1 if $a
  188. is bigger than $b.
  189. The "~" character always sort lower than anything else. Digits sort lower
  190. than non-digits. Among remaining characters alphabetic characters (A-Za-z)
  191. sort lower than the other ones. Within each range, the ASCII decimal value
  192. of the character is used to sort between characters.
  193. =cut
  194. sub version_compare_string($$) {
  195. sub order(_) {
  196. my ($x) = @_;
  197. if ($x eq '~') {
  198. return -1;
  199. } elsif ($x =~ /^\d$/) {
  200. return $x * 1 + 1;
  201. } elsif ($x =~ /^[A-Za-z]$/) {
  202. return ord($x);
  203. } else {
  204. return ord($x) + 256;
  205. }
  206. }
  207. my @a = map(order, split(//, shift));
  208. my @b = map(order, split(//, shift));
  209. while (1) {
  210. my ($a, $b) = (shift @a, shift @b);
  211. return 0 if not defined($a) and not defined($b);
  212. $a ||= 0; # Default order for "no character"
  213. $b ||= 0;
  214. return 1 if $a > $b;
  215. return -1 if $a < $b;
  216. }
  217. }
  218. =item version_compare_part($a, $b)
  219. Compare two corresponding sub-parts of a version number (either upstream
  220. version or debian revision).
  221. Each parameter is split by version_split_digits() and resulting items
  222. are compared together.in digits and non-digits items that are compared
  223. together. As soon as a difference happens, it returns -1 if $a is smaller
  224. than $b, 0 if they are equal and 1 if $a is bigger than $b.
  225. =cut
  226. sub version_compare_part($$) {
  227. my @a = version_split_digits(shift);
  228. my @b = version_split_digits(shift);
  229. while (1) {
  230. my ($a, $b) = (shift @a, shift @b);
  231. return 0 if not defined($a) and not defined($b);
  232. $a ||= 0; # Default value for lack of version
  233. $b ||= 0;
  234. if ($a =~ /^\d+$/ and $b =~ /^\d+$/) {
  235. # Numerical comparison
  236. my $cmp = $a <=> $b;
  237. return $cmp if $cmp;
  238. } else {
  239. # String comparison
  240. my $cmp = version_compare_string($a, $b);
  241. return $cmp if $cmp;
  242. }
  243. }
  244. }
  245. =item my @items = version_split_digits($version)
  246. Splits a string in items that are each entirely composed either
  247. of digits or of non-digits. For instance for "1.024~beta1+svn234" it would
  248. return ("1", ".", "024", "~beta", "1", "+svn", "234").
  249. =cut
  250. sub version_split_digits($) {
  251. return split(/(?<=\d)(?=\D)|(?<=\D)(?=\d)/, $_[0]);
  252. }
  253. =item my ($ok, $msg) = version_check($version)
  254. =item my $ok = version_check($version)
  255. Checks the validity of $version as a version number. Returns 1 in $ok
  256. if the version is valid, 0 otherwise. In the latter case, $msg
  257. contains a description of the problem with the $version scalar.
  258. =cut
  259. sub version_check($) {
  260. my $version = shift;
  261. $version = "$version" if ref($version);
  262. if (not defined($version) or not length($version)) {
  263. my $msg = _g("version number cannot be empty");
  264. return (0, $msg) if wantarray;
  265. return 0;
  266. }
  267. if ($version =~ m/([^-+:.0-9a-zA-Z~])/o) {
  268. my $msg = sprintf(_g("version number contains illegal character `%s'"), $1);
  269. return (0, $msg) if wantarray;
  270. return 0;
  271. }
  272. if ($version =~ /:/ and $version !~ /^\d*:/) {
  273. $version =~ /^([^:]*):/;
  274. my $msg = sprintf(_g("epoch part of the version number " .
  275. "is not a number: '%s'"), $1);
  276. return (0, $msg) if wantarray;
  277. return 0;
  278. }
  279. return (1, "") if wantarray;
  280. return 1;
  281. }
  282. sub parseversion ($)
  283. {
  284. my $ver = shift;
  285. my %verhash;
  286. if ($ver =~ /:/)
  287. {
  288. $ver =~ /^(\d+):(.+)/ or die "bad version number '$ver'";
  289. $verhash{epoch} = $1;
  290. $ver = $2;
  291. }
  292. else
  293. {
  294. $verhash{epoch} = 0;
  295. $verhash{no_epoch} = 1;
  296. }
  297. if ($ver =~ /(.+)-(.*)$/)
  298. {
  299. $verhash{version} = $1;
  300. $verhash{revision} = $2;
  301. }
  302. else
  303. {
  304. $verhash{version} = $ver;
  305. $verhash{revision} = 0;
  306. $verhash{no_revision} = 1;
  307. }
  308. return %verhash;
  309. }
  310. # verrevcmp
  311. # This function is almost exactly equivalent
  312. # to dpkg's verrevcmp function, including the
  313. # order subroutine which it uses.
  314. sub verrevcmp($$)
  315. {
  316. sub _order{
  317. my ($x) = @_;
  318. ##define order(x) ((x) == '~' ? -1 \
  319. # : cisdigit((x)) ? 0 \
  320. # : !(x) ? 0 \
  321. # : cisalpha((x)) ? (x) \
  322. # : (x) + 256)
  323. # This comparison is out of dpkg's order to avoid
  324. # comparing things to undef and triggering warnings.
  325. if (not defined $x or not length $x) {
  326. return 0;
  327. }
  328. elsif ($x eq '~') {
  329. return -1;
  330. }
  331. elsif ($x =~ /^\d$/) {
  332. return 0;
  333. }
  334. elsif ($x =~ /^[A-Za-z]$/) {
  335. return ord($x);
  336. }
  337. else {
  338. return ord($x) + 256;
  339. }
  340. }
  341. my ($val, $ref) = @_;
  342. $val = "" if not defined $val;
  343. $ref = "" if not defined $ref;
  344. my @val = split //,$val;
  345. my @ref = split //,$ref;
  346. my $vc = shift @val;
  347. my $rc = shift @ref;
  348. while (defined $vc or defined $rc) {
  349. my $first_diff = 0;
  350. while ((defined $vc and $vc !~ /^\d$/) or
  351. (defined $rc and $rc !~ /^\d$/)) {
  352. my $vo = _order($vc); my $ro = _order($rc);
  353. # Unlike dpkg's verrevcmp, we only return 1 or -1 here.
  354. return (($vo - $ro > 0) ? 1 : -1) if $vo != $ro;
  355. $vc = shift @val; $rc = shift @ref;
  356. }
  357. while (defined $vc and $vc eq '0') {
  358. $vc = shift @val;
  359. }
  360. while (defined $rc and $rc eq '0') {
  361. $rc = shift @ref;
  362. }
  363. while (defined $vc and $vc =~ /^\d$/ and
  364. defined $rc and $rc =~ /^\d$/) {
  365. $first_diff = ord($vc) - ord($rc) if !$first_diff;
  366. $vc = shift @val; $rc = shift @ref;
  367. }
  368. return 1 if defined $vc and $vc =~ /^\d$/;
  369. return -1 if defined $rc and $rc =~ /^\d$/;
  370. return (($first_diff > 0) ? 1 : -1) if $first_diff;
  371. }
  372. return 0;
  373. }
  374. =item vercmp
  375. Compare the two arguments as dpkg-style version numbers. Returns -1 if the
  376. first argument represents a lower version number than the second, 1 if the
  377. first argument represents a higher version number than the second, and 0 if
  378. the two arguments represent equal version numbers.
  379. =cut
  380. sub vercmp ($$)
  381. {
  382. my %version = parseversion $_[0];
  383. my %refversion = parseversion $_[1];
  384. return 1 if $version{epoch} > $refversion{epoch};
  385. return -1 if $version{epoch} < $refversion{epoch};
  386. my $r = verrevcmp($version{version}, $refversion{version});
  387. return $r if $r;
  388. return verrevcmp($version{revision}, $refversion{revision});
  389. }
  390. =item compare_versions
  391. Emulates dpkg --compare-versions. Takes two versions as arguments
  392. one and three and one operator as argument two. Supports the following
  393. operators: 'gt', 'ge', 'eq', 'le', 'lt', and '>>', '>=', '=', '<=', '<<'.
  394. Returns a true value if the specified condition is true, a false value
  395. otherwise.
  396. =cut
  397. sub compare_versions ($$$)
  398. {
  399. my $rel = $_[1];
  400. my $res = vercmp($_[0], $_[2]);
  401. warning("operator %s is deprecated in compare_versions(): use %s or %s",
  402. $rel, "$rel$rel", "$rel=") if ($rel eq '>' or $rel eq '<');
  403. if ($rel eq 'gt' or $rel eq '>>') {
  404. return $res > 0;
  405. } elsif ($rel eq 'ge' or $rel eq '>=' or $rel eq '>') {
  406. return $res >= 0;
  407. } elsif ($rel eq 'eq' or $rel eq '=') {
  408. return $res == 0;
  409. } elsif ($rel eq 'le' or $rel eq '<=' or $rel eq '<') {
  410. return $res <= 0;
  411. } elsif ($rel eq 'lt' or $rel eq '<<') {
  412. return $res < 0;
  413. } else {
  414. die "bad relation '$rel'";
  415. }
  416. }
  417. =item check_version($version, $die)
  418. If $die is false (or unset), returns true if the version is valid and
  419. false if it contains illegal characters. If $die is true, it dies with
  420. an error message if it contains illegal characters, otherwise it returns
  421. true.
  422. =cut
  423. sub check_version ($;$) {
  424. my ($version, $die) = @_;
  425. $version ||= "";
  426. if ($version =~ m/[^-+:.0-9a-zA-Z~]/o) {
  427. error(_g("version number contains illegal character `%s'"), $&) if $die;
  428. return 0;
  429. }
  430. return 1;
  431. }
  432. =back
  433. =head1 AUTHOR
  434. Don Armstrong <don@donarmstrong.com>, Colin Watson
  435. <cjwatson@debian.org> and Raphaël Hertzog <hertzog@debian.org>, based on
  436. the implementation in C<dpkg/lib/vercmp.c> by Ian Jackson and others.
  437. =cut
  438. 1;