/usr/share/perl5/Linux/KernelSort.pm is in liblinux-kernelsort-perl 0.01-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | =head1 NAME
Linux::KernelSort - Perl extension for sorting and comparing Linux
kernel versions. The expected kernel version naming convention is
the same naming convetion demonstrated by http://www.kernel.org.
NOTE: Currently, only the 2.6.x series of kernels (including -rc's,
-git's, and -mm's) are properly evaluated.
=head1 SYNOPSIS
use Linux::KernelSort;
my $kernel = new Linux::KernelSort;
my $ret;
my $version1 = "2.6.19";
my $version2 = "2.6.19-rc2-git7";
$ret = $kernel->compare($version1, $version2);
if ($ret == 0) {
print "$version1 and $version2 are the same version\n";
} elsif ($ret > 0) {
print "$version1 is newer than $version2\n";
} else {
print "$version1 is older than $version2\n";
}
my @kernel_list = ( '2.6.15',
'2.6.18',
'2.6.18-rc2',
'2.6.18-rc2-git2',
'2.6.18-mm1',
'2.6.18-rc2-mm1' );
my @sorted_list = $kernel->sort(@kernel_list);
print join( ', ', @sorted_list );
=head1 DESCRIPTION
Linux::KernelSort is intended to sort a list of kernel versions into
ascending order. It also provides the capability to compare
two kernel versions and determine if one version is newer, older,
or the same as the other version.
=head1 FUNCTIONS
=cut
package Linux::KernelSort;
use strict;
use warnings;
our $VERSION = '0.01';
sub new {
my $class = shift;
my $self = {};
$self->{debug} = 1;
bless ($self, $class);
return $self;
}
=head2 version_check()
Purpose: Verify the version is valid and follows the
proper naming convention demonstrated by
http://www.kernel.org
Input: A string containing the kernel version
Return: 0 if version is valid
1 if version is invalid
=cut
sub version_check {
my $self = shift;
my $version = shift || return undef;
if ( $version !~ m/^(v)?\d+\.\d+\.\d+(\.\d+)?(-rc\d+)?(-git\d+)?(-scsi-misc\d+)?(-scsi-rc-fixes\d+)?(-mm\d+)?(-tree)?$/ ) {
if ( $self->{debug} ) { print "Invalid Kernel Version: $version\n"; }
return 1;
}
return 0;
}
=head2 rank()
Purpose: Generate a ranking for a given kernel version
Input: A string containing the kernel version which
follows the proper naming convention demonstrated
by http://www.kernel.org
Return: Kernel ranking
=cut
sub rank {
my $self = shift;
my $version = shift || return undef;
if ( $self->version_check($version) ) {
return undef;
}
$version =~ s/\.//g;
$version =~ s/^v//;
$version =~ m/^(\d+).*/;
my $rank = $1;
if ( $version =~ m/-rc(\d+)/ ) {
my $rc = $1;
$rank = $rank - 1;
$rank = $rank . ".$rc";
} else {
$rank = $rank . ".0";
}
if ( $version =~ m/-git(\d+)/ ) {
my $git = $1;
$rank = $rank . ".$git";
} else {
$rank = $rank . ".0";
}
if ( $version =~ m/-scsi-misc(\d+)/ ) {
my $scsi_misc = $1;
$rank = $rank . ".$scsi_misc";
} else {
$rank = $rank . ".0";
}
if ( $version =~ m/-scsi-rc-fixes(\d+)/ ) {
my $rc_fixes = $1;
$rank = $rank . ".$rc_fixes";
} else {
$rank = $rank . ".0";
}
if ( $version =~ m/-mm(\d+)/ ) {
my $mm = $1;
$rank = $rank . ".$mm";
} else {
$rank = $rank . ".0";
}
return $rank;
}
=head2 compare()
Purpose: Compare two kernel versions
Input: Strings ($kernel1, $kernel2) each containing a
kernel version which follows the proper naming
conventaion demonstrated by http://www.kernel.org
Return -1 if $kernel1 < $kernel2 (ie $kernel1 is older than $kernel2)
0 if $kernel1 == $kernel2 (ie $kernel1 is the same version as $kernel2)
1 if $kernel1 > $kernel2 (ie $kernel1 is newer than $kernel2)
=cut
sub compare {
my $self = shift;
my $kernel1 = shift || return undef;
my $kernel2 = shift || return undef;
my $rank1 = $self->rank($kernel1);
my $rank2 = $self->rank($kernel2);
if ( !$rank1 || !$rank2 ) {
if ( $self->{debug} ) { print "Unable to properly compare kernel versions: $kernel1, $kernel2\n"; }
if ( !$rank1 && !$rank2 ) {
return 0;
} elsif ( !$rank1 ) {
return -1;
} else {
return 1;
}
}
while (length($rank1) && length($rank2)) {
$rank1 =~ m/^(\d+)\.?(.*)/;
my $value1 = $1;
$rank1 = $2;
$rank2 =~ m/^(\d+)\.?(.*)/;
my $value2 = $1;
$rank2 = $2;
if ($value1 == $value2) {
next;
} elsif ($value1 < $value2) {
return -1;
} else {
return 1;
}
}
return 0;
}
=head2 sort()
Purpose: Sort a list of kernel versions in ascending order.
Uses shell sort algorithm.
Input: Array of strings containing kernel versions which
follows the proper naming convention demonstrated
by http://www.kernel.org
Return: Sorted array
=cut
sub sort {
my $self = shift;
my (@kernels) = @_;
my $size = @kernels;
for (my $gap = int($size/2); $gap > 0; $gap = int($gap/2)) {
for (my $i = $gap; $i < $size; $i++) {
for (my $j = $i-$gap; ($j >= 0) && ($self->compare($kernels[$j], $kernels[$j+$gap]) > 0); $j -= $gap) {
my $temp = $kernels[$j];
$kernels[$j] = $kernels[$j+$gap];
$kernels[$j+$gap] = $temp;
}
}
}
return @kernels;
}
=head1 AUTHOR
Leann Ogasawara <lt>ogasawara@osdl.org<gt>
=head1 COPYRIGHT AND LICENSE
Linux-KernelSort is Copyright (c) 2006, by Leann Ogasawara.
All rights reserved. You may distribute this code under the terms
of either the GNU General Public License or the Artistic License,
as specified in the Perl README file.
=cut
1;
__END__
|