Debian Codegraph @

lib/DebianLinux.pm

raw
1 # Copyright 2011 Ben Hutchings
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
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 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
17 package DebianLinux;
18
19 use strict;
20 use warnings;
21 use POSIX qw(uname);
22 use FileHandle;
23
24 BEGIN {
25 use Exporter ();
26 our @ISA = qw(Exporter);
27 our @EXPORT_OK = qw(version_cmp image_stem image_list read_kernelimg_conf);
28 }
29
30 sub version_split {
31 # Split into numbers and non-numeric strings, but break the non-
32 # numeric strings at hyphens
33 my $version = shift;
34 return $version =~ /(?:\d+|-?[^-\d]*)/g;
35 }
36
37 sub version_cmp {
38 my ($left_ver, $right_ver) = @_;
39 my @left_comp = version_split($left_ver);
40 my @right_comp = version_split($right_ver);
41
42 for (my $i = 0; ; $i++) {
43 my $left = $left_comp[$i];
44 my $right = $right_comp[$i];
45 # Do the components indicate pre-releases?
46 my $left_pre = defined($left) && $left =~ /^-(?:rc|trunk)$/;
47 my $right_pre = defined($right) && $right =~ /^-(?:rc|trunk)$/;
48 # Are the components numeric?
49 my $left_num = defined($left) && $left =~ /^\d+/;
50 my $right_num = defined($right) && $right =~ /^\d+/;
51
52 # Pre-releases sort before anything, even end-of-string
53 if ($left_pre or $right_pre) {
54 return -1 if !$right_pre;
55 return 1 if !$left_pre;
56 }
57 # End-of-string sorts before anything else.
58 # End-of-string on both sides means equality.
59 if (!defined($left) or !defined($right)) {
60 return -1 if defined($right);
61 return defined($left) || 0;
62 }
63 # Use numeric comparison if both sides numeric.
64 # Otherwise use ASCII comparison.
65 if ($left_num && $right_num) {
66 return -1 if $left < $right;
67 return 1 if $left > $right;
68 } else {
69 # Note that '.' > '-' thus 2.6.x.y > 2.6.x-z for any y, z.
70 return -1 if $left lt $right;
71 return 1 if $left gt $right;
72 }
73 }
74 }
75
76 # Return the historical kernel image name stem for this architecture.
77 # This should only be used by linux-update-symlinks. The actual image
78 # name stem used by kernel packages may change at any time.
79 sub image_stem {
80 if ((uname())[4] =~ /^(?:mips|parisc|powerpc|ppc)/) {
81 return 'vmlinux';
82 } else {
83 return 'vmlinuz';
84 }
85 }
86
87 sub image_list {
88 my @results;
89
90 for (glob("/boot/vmlinu[xz]-*")) {
91 next if /\.sig$/; # skip detached signatures
92 push @results, [substr($_, length("/boot/vmlinu?-")), $_];
93 }
94 return @results;
95 }
96
97 sub read_kernelimg_conf {
98 my $conf_loc = shift || '/etc/kernel-img.conf';
99 my @bool_param = qw(do_symlinks link_in_boot no_symlinks);
100 my @path_param = qw(image_dest);
101 # These are still set in the jessie installer even though they
102 # have no effect. Ignore them quietly.
103 my @quiet_param = qw(do_bootloader do_initrd);
104 # These are used only by kernel-package, and are not relevant to
105 # anything that linux-base does. Ignore them quietly.
106 push @quiet_param, qw(clobber_modules force_build_link
107 relink_build_link relink_src_link
108 silent_modules warn_reboot);
109
110 # Initialise configuration to defaults
111 my $conf = {
112 do_symlinks => 1,
113 image_dest => '/',
114 link_in_boot => 0,
115 no_symlinks => 0,
116 };
117
118 if (my $fh = new FileHandle($conf_loc, 'r')) {
119 while (<$fh>) {
120 # Delete line endings, comments and blank lines
121 chomp;
122 s/\#.*$//g;
123 next if /^\s*$/;
124
125 # Historically this was done by matching against one
126 # (path) or two (bool) regexps per parameter, with no
127 # attempt to ensure that each line matched one. We now
128 # warn about syntax errors, but for backward compatibility
129 # we never treat them as fatal.
130
131 # Parse into name = value
132 if (!/^\s*(\w+)\s*=\s*(.*)/) {
133 print STDERR "$conf_loc:$.: W: ignoring line with syntax error\n";
134 next;
135 }
136 my ($name, $value) = (lc($1), $2);
137
138 # Parse value according to expected type
139 if (grep({$_ eq $name} @bool_param)) {
140 if ($value =~ /^(?:no|false|0)\s*$/i) {
141 $conf->{$name} = 0;
142 } elsif ($value =~ /^(?:yes|true|1)\s*$/i) {
143 $conf->{$name} = 1;
144 } else {
145 print STDERR "$conf_loc:$.: W: ignoring invalid value for $name\n";
146 }
147 } elsif (grep({$_ eq $name} @path_param)) {
148 # Only one space-separated word is supported
149 $value =~ /^(\S*)(.*)/;
150 ($conf->{$name}, my $excess) = ($1, $2);
151 if ($excess =~ /\S/) {
152 print STDERR "$conf_loc:$.: W: ignoring excess values for $name\n";
153 }
154 } elsif (grep({$_ eq $name} @quiet_param)) {
155 ;
156 } else {
157 print STDERR "$conf_loc:$.: W: ignoring unknown parameter $name\n";
158 }
159 }
160 $fh->close();
161 }
162
163 # This is still set (to 0) by default in jessie so we should only
164 # warn if the default is changed
165 if ($conf->{no_symlinks}) {
166 print STDERR "$conf_loc: W: ignoring no_symlinks; only symlinks are supported\n";
167 }
168 delete $conf->{no_symlinks};
169
170 if ($conf->{link_in_boot}) {
171 $conf->{image_dest} = '/boot';
172 }
173 delete $conf->{link_in_boot};
174
175 return $conf;
176 }
177
178 1;