| 1 |
# Copyright (C) 2008-2010 Modestas Vainius <[email protected]> |
| 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 3 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, see <http://www.gnu.org/licenses/> |
| 15 |
|
| 16 |
package Debian::PkgKde::SymbolsHelper::SymbolFile; |
| 17 |
|
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
use base 'Dpkg::Shlibs::SymbolFile'; |
| 21 |
|
| 22 |
use File::Temp qw(); |
| 23 |
use File::Copy qw(); |
| 24 |
use Storable qw(); |
| 25 |
use IO::Handle; |
| 26 |
use Dpkg::ErrorHandling; |
| 27 |
use Dpkg::Version; |
| 28 |
use Debian::PkgKde::SymbolsHelper::Symbol; |
| 29 |
use Debian::PkgKde::SymbolsHelper::Substs; |
| 30 |
|
| 31 |
# Use Debian::PkgKde::SymbolsHelper::Symbol as base symbol |
| 32 |
sub parse { |
| 33 |
my ($self, $fh, $file, %opts) = @_; |
| 34 |
my $state = $opts{state} //= {}; |
| 35 |
unless (defined $state->{base_symbol}) { |
| 36 |
$state->{base_symbol} = 'Debian::PkgKde::SymbolsHelper::Symbol'; |
| 37 |
} |
| 38 |
unless (defined $state->{seen}) { |
| 39 |
# Read 'SymbolsHelper-Confirmed' header |
| 40 |
open(my $fh, "<", $file) |
| 41 |
or error("unable to open symbol file '$file' for reading"); |
| 42 |
my $line = <$fh>; |
| 43 |
close $fh; |
| 44 |
|
| 45 |
chop $line; |
| 46 |
if ($line =~ /^#\s*SymbolsHelper-Confirmed:\s+(.+)$/) { |
| 47 |
$self->set_confirmed(split(/\s+/, $1)); |
| 48 |
} |
| 49 |
} |
| 50 |
return $self->SUPER::parse($fh, $file, %opts); |
| 51 |
} |
| 52 |
|
| 53 |
sub set_confirmed { |
| 54 |
my ($self, $version, @arches) = @_; |
| 55 |
$self->{h_confirmed_version} = $version; |
| 56 |
$self->{h_confirmed_arches} = (@arches) ? \@arches : undef; |
| 57 |
} |
| 58 |
|
| 59 |
sub get_confirmed_version { |
| 60 |
my $self = shift; |
| 61 |
return $self->{h_confirmed_version}; |
| 62 |
} |
| 63 |
|
| 64 |
sub get_confirmed_arches { |
| 65 |
my $self = shift; |
| 66 |
return (defined $self->{h_confirmed_arches}) ? |
| 67 |
@{$self->{h_confirmed_arches}} : (); |
| 68 |
} |
| 69 |
|
| 70 |
sub create_symbol { |
| 71 |
my ($self, $spec, %opts) = @_; |
| 72 |
$opts{base} = Debian::PkgKde::SymbolsHelper::Symbol->new() |
| 73 |
unless exists $opts{base}; |
| 74 |
return $self->SUPER::create_symbol($spec, %opts); |
| 75 |
} |
| 76 |
|
| 77 |
sub fork_symbol { |
| 78 |
my ($self, $sym, $arch) = @_; |
| 79 |
$arch = $self->get_arch() unless $arch; |
| 80 |
my $nsym = $sym->clone(symbol => $sym->get_symboltempl()); |
| 81 |
$nsym->initialize(arch => $arch); |
| 82 |
return $nsym; |
| 83 |
} |
| 84 |
|
| 85 |
sub output { |
| 86 |
my ($self, $fh, %opts) = @_; |
| 87 |
$opts{with_confirmed} = 1 unless exists $opts{with_confirmed}; |
| 88 |
# Write SymbolsHelper-Confirmed header |
| 89 |
if ($opts{with_confirmed}) { |
| 90 |
my @carches = $self->get_confirmed_arches(); |
| 91 |
if (@carches) { |
| 92 |
print $fh '# SymbolsHelper-Confirmed: ', $self->get_confirmed_version(), |
| 93 |
" ", join(" ", sort @carches), "\n" if defined $fh; |
| 94 |
} |
| 95 |
} |
| 96 |
return $self->SUPER::output($fh, %opts); |
| 97 |
} |
| 98 |
|
| 99 |
sub _resync_symbol_cache { |
| 100 |
my ($self, $soname, $cache) = @_; |
| 101 |
my %rename; |
| 102 |
|
| 103 |
foreach my $symkey (keys %$cache) { |
| 104 |
my $sym = $cache->{$symkey}; |
| 105 |
if ($sym->get_symbolname() ne $symkey) { |
| 106 |
$rename{$sym->get_symbolname()} = $sym; |
| 107 |
delete $cache->{$symkey}; |
| 108 |
} |
| 109 |
} |
| 110 |
foreach my $newname (keys %rename) { |
| 111 |
my $e = $self->get_symbol_object($rename{$newname}, $soname); |
| 112 |
if ($e && ! $rename{$newname}->equals($e)) { |
| 113 |
warning("caution: newly generated symbol '%s' will replace not exactly equal '%s'. Please readd if unappropriate", |
| 114 |
$rename{$newname}->get_symbolspec(1), |
| 115 |
$e->get_symbolspec(1)); |
| 116 |
} |
| 117 |
$self->add_symbol($rename{$newname}, $soname); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
sub resync_soname_symbol_caches { |
| 122 |
my ($self, $soname) = @_; |
| 123 |
my $obj = $self->get_object($soname); |
| 124 |
|
| 125 |
# We need this to avoid removal of symbols which names clash when renaming |
| 126 |
$self->_resync_symbol_cache($obj, $obj->{syms}); |
| 127 |
|
| 128 |
# Resync aliases too |
| 129 |
foreach my $alias (values %{$obj->{patterns}{aliases}}) { |
| 130 |
$self->_resync_symbol_cache($obj, $alias); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
sub resync_soname_with_h_name { |
| 135 |
my ($self, $soname) = @_; |
| 136 |
my $obj = $self->get_object($soname); |
| 137 |
|
| 138 |
sub _resync_with_h_name { |
| 139 |
my $cache = shift; |
| 140 |
foreach my $symkey (keys %$cache) { |
| 141 |
$cache->{$symkey}->resync_name_with_h_name(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
# First resync h_name with symbol name and templ |
| 146 |
_resync_with_h_name($obj->{syms}); |
| 147 |
foreach my $alias (values %{$obj->{patterns}{aliases}}) { |
| 148 |
_resync_with_h_name($alias); |
| 149 |
} |
| 150 |
return $self->resync_soname_symbol_caches($soname); |
| 151 |
} |
| 152 |
|
| 153 |
# Detects (or just neutralizes) substitutes which can be guessed from the |
| 154 |
# symbol name alone. Currently unused. |
| 155 |
#sub detect_standalone_substs { |
| 156 |
# my ($self, $detect) = @_; |
| 157 |
# |
| 158 |
# foreach my $sym ($self->get_symbols()) { |
| 159 |
# my $str = $sym->get_h_name(); |
| 160 |
# foreach my $subst (@STANDALONE_SUBSTS) { |
| 161 |
# if ($detect) { |
| 162 |
# $subst->detect($str, $self->{arch}); |
| 163 |
# } else { |
| 164 |
# $subst->neutralize($str); |
| 165 |
# } |
| 166 |
# } |
| 167 |
# } |
| 168 |
# foreach my $soname (keys %{$self->{objects}}) { |
| 169 |
# # Rename soname object with data in h_name |
| 170 |
# $self->resync_soname_with_h_name($soname); |
| 171 |
# } |
| 172 |
#} |
| 173 |
|
| 174 |
# Upgrade virtual table symbols. Needed for templating. |
| 175 |
sub prepare_for_templating { |
| 176 |
my $self = shift; |
| 177 |
my %sonames; |
| 178 |
|
| 179 |
foreach my $soname ($self->get_sonames()) { |
| 180 |
foreach my $sym ($self->get_symbols($soname)) { |
| 181 |
if ($sym->upgrade_virtual_table_symbol($self->get_arch())) { |
| 182 |
$sonames{$soname} = 1; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
foreach my $soname (keys %sonames) { |
| 188 |
$self->resync_soname_symbol_caches($soname); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
sub patch_template { |
| 193 |
my ($self, @patches) = @_; |
| 194 |
my @symfiles; |
| 195 |
my %dumped; |
| 196 |
|
| 197 |
foreach my $patch (@patches) { |
| 198 |
my $package = $patch->{package} || ''; |
| 199 |
my $tmpfile; |
| 200 |
if (!exists $dumped{$package}) { |
| 201 |
$tmpfile = File::Temp->new( |
| 202 |
TEMPLATE => "${package}_orig.symbolsXXXXXX", |
| 203 |
UNLINK => 0, |
| 204 |
); |
| 205 |
$self->output($tmpfile, |
| 206 |
package => $package, |
| 207 |
template_mode => 1, |
| 208 |
with_confirmed => 0, |
| 209 |
); |
| 210 |
$tmpfile->close(); |
| 211 |
$dumped{$package} = $tmpfile->filename; |
| 212 |
} |
| 213 |
$tmpfile = File::Temp->new( |
| 214 |
TEMPLATE => "${package}_patched.symbolsXXXXXX", |
| 215 |
UNLINK => 1, |
| 216 |
); |
| 217 |
$tmpfile->close(); |
| 218 |
unless (File::Copy::copy($dumped{$package}, $tmpfile->filename)) { |
| 219 |
syserror("unable to copy file '%s' to '%s'", |
| 220 |
$dumped{$package}, $tmpfile->filename); |
| 221 |
} |
| 222 |
if ($patch->apply($tmpfile->filename)) { |
| 223 |
# Patching was successful. Parse new SymbolFile and return it |
| 224 |
my $symfile = Debian::PkgKde::SymbolsHelper::SymbolFile->new( |
| 225 |
filename => $tmpfile->filename, |
| 226 |
arch => $patch->{arch}, |
| 227 |
); |
| 228 |
if ($patch->has_info()) { |
| 229 |
$symfile->set_confirmed($patch->{version}, $patch->{arch}); |
| 230 |
} else { |
| 231 |
$symfile->set_confirmed(undef); |
| 232 |
} |
| 233 |
push @symfiles, $symfile; |
| 234 |
last unless wantarray; |
| 235 |
} |
| 236 |
} |
| 237 |
foreach my $file (values %dumped) { |
| 238 |
unless ($File::Temp::KEEP_ALL) { |
| 239 |
unlink $file; |
| 240 |
} |
| 241 |
} |
| 242 |
return (wantarray) ? @symfiles : $symfiles[0]; |
| 243 |
} |
| 244 |
|
| 245 |
sub _dclone_exclude { |
| 246 |
my ($target, @exclude) = @_; |
| 247 |
my %saved; |
| 248 |
foreach my $e (@exclude) { |
| 249 |
if (exists $target->{$e}) { |
| 250 |
$saved{$e} = $target->{$e}; |
| 251 |
delete $target->{$e}; |
| 252 |
} |
| 253 |
} |
| 254 |
my $clone = Storable::dclone($target); |
| 255 |
$target->{$_} = $saved{$_} foreach @exclude; |
| 256 |
return $clone; |
| 257 |
} |
| 258 |
|
| 259 |
# Forks an empty symbol file (without symbols and patterns) from the current |
| 260 |
# one. Other properties are retained. |
| 261 |
sub fork_empty { |
| 262 |
my $self = shift; |
| 263 |
|
| 264 |
my $symfile = _dclone_exclude($self, qw(objects)); |
| 265 |
$symfile->clear(); |
| 266 |
foreach my $soname ($self->get_sonames()) { |
| 267 |
$symfile->create_object($soname); |
| 268 |
my $obj = $symfile->get_object($soname); |
| 269 |
my $cloned = _dclone_exclude($self->get_object($soname), |
| 270 |
qw(syms patterns minver_cache)); |
| 271 |
$obj->{$_} = $cloned->{$_} foreach keys %$cloned; |
| 272 |
} |
| 273 |
return $symfile; |
| 274 |
} |
| 275 |
|
| 276 |
sub fork { |
| 277 |
my ($self, @optinstances) = @_; |
| 278 |
unshift @optinstances, {} unless @optinstances; |
| 279 |
@optinstances = ( $optinstances[0] ) unless wantarray; |
| 280 |
|
| 281 |
my @symfiles; |
| 282 |
foreach my $opts (@optinstances) { |
| 283 |
my $symfile = $self->fork_empty(); |
| 284 |
$symfile->{$_} = $opts->{$_} foreach keys %$opts; |
| 285 |
$symfile->{filename} = ''; |
| 286 |
push @symfiles, $symfile; |
| 287 |
} |
| 288 |
|
| 289 |
# Fork symbols |
| 290 |
foreach my $soname ($self->get_sonames()) { |
| 291 |
foreach my $sym ($self->get_symbols($soname), |
| 292 |
$self->get_patterns($soname)) |
| 293 |
{ |
| 294 |
foreach my $symfile (@symfiles) { |
| 295 |
my $nsym = $self->fork_symbol($sym, $symfile->get_arch()); |
| 296 |
$nsym->{h_origin_symbol} = $sym; |
| 297 |
$symfile->add_symbol($nsym, $soname); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
return (wantarray) ? @symfiles : shift @symfiles; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
sub get_highest_version { |
| 306 |
my $self = shift; |
| 307 |
my $maxver; |
| 308 |
|
| 309 |
foreach my $sym ($self->get_symbols(), |
| 310 |
$self->get_patterns()) { |
| 311 |
if (!$sym->{deprecated} && |
| 312 |
(!defined $maxver || version_compare($sym->{minver}, $maxver) > 0)) |
| 313 |
{ |
| 314 |
$maxver = $sym->{minver}; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
return $maxver; |
| 319 |
} |
| 320 |
|
| 321 |
1; |