Debian Codegraph @

perllib/Debian/PkgKde/SymbolsHelper/Patching.pm

raw
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::Patching;
17
18 use strict;
19 use warnings;
20 use base 'Exporter';
21
22 use Dpkg::ErrorHandling;
23
24 our @EXPORT = qw(parse_patches_from_handle parse_patches_from_file);
25
26 sub parse_patches_from_handle {
27 my ($fh) = @_;
28 my $reparse_line;
29 my @patches;
30 my $patch;
31
32 while ($reparse_line || ($_ = <$fh>)) {
33 $reparse_line = 0;
34 if (defined $patch) {
35 if ($patch->has_header()) {
36 if (m/^@@ /) {
37 unless ($patch->is_valid()) {
38 warning("patch '".$patch->get_name()."' hunk is invalid at line $.");
39 }
40 $patch->append_line($_);
41 } elsif (!$patch->is_valid() && m/^[+ -]/) {
42 # Patch continues
43 $patch->append_line($_);
44 } else {
45 # Patch ended
46 if ($patch->complete()) {
47 push @patches, $patch;
48 } else {
49 warning("patch '".$patch->get_name()."' is invalid");
50 }
51 $patch = undef;
52 $reparse_line = 1;
53 next;
54 }
55 } elsif (defined $patch->{source}) {
56 if (m/^[+]{3}\s+(\S+)/) {
57 # Found the patch header portion
58 $patch->set_target($1);
59 } else {
60 $patch = undef;
61 $reparse_line = 1;
62 }
63 }
64 } elsif (m/^[-]{3}\s+(\S+)(?:\s+\(([^_]+)_([^_]+)_([^_]+)\))?/) {
65 $patch = Debian::PkgKde::SymbolsHelper::Patch->new();
66 $patch->set_source($1);
67 $patch->set_info($2, $3, $4);
68 }
69 }
70 if (defined $patch) {
71 if ($patch->complete()) {
72 push @patches, $patch;
73 } else {
74 warning("patch '".$patch->get_name()."' is invalid");
75 }
76 }
77 return @patches;
78 }
79
80 sub parse_patches_from_file {
81 my ($filename) = @_;
82 open(my $fh, "<", $filename) or error("unable to open patch file '$filename'");
83 my @ret = parse_patches_from_handle($fh);
84 close $fh;
85 return @ret;
86 }
87
88 package Debian::PkgKde::SymbolsHelper::Patch;
89
90 use strict;
91 use warnings;
92
93 use Dpkg::ErrorHandling;
94 use Dpkg::IPC;
95
96 sub new {
97 my $class = shift;
98 return bless {
99 file => undef,
100 source => undef,
101 target => undef,
102 package => undef,
103 version => undef,
104 arch => undef,
105 patch => undef,
106 hunk_minus => 0,
107 hunk_plus => 0,
108 }, $class;
109 }
110
111 sub set_source {
112 my ($self, $srcfile) = @_;
113 $self->{source} = $srcfile;
114 }
115
116 sub set_info {
117 my ($self, $package, $version, $arch) = @_;
118 $self->{package} = $package;
119 $self->{version} = $version;
120 $self->{arch} = $arch;
121 }
122
123 sub get_info {
124 my $self = shift;
125 return (
126 package => $self->{package},
127 version => $self->{version},
128 arch => $self->{arch},
129 );
130 }
131
132 sub has_info {
133 my $self = shift;
134 return defined $self->{package};
135 }
136
137 sub set_target {
138 my ($self, $target) = @_;
139 $self->{target} = $target;
140 }
141
142 sub has_header {
143 my $self = shift;
144 return defined $self->{source} && defined $self->{target};
145 }
146
147 sub get_name {
148 my $self = shift;
149 if ($self->{source}) {
150 if ($self->has_info()) {
151 return sprintf("%s_%s_%s (--- %s)", $self->{package},
152 $self->{version}, $self->{arch}, $self->{source});
153 } else {
154 return sprintf("--- %s +++ %s", $self->{source}, $self->{target});
155 }
156 } else {
157 return "<empty patch>";
158 }
159 }
160
161 sub is_valid {
162 my $self = shift;
163 return (defined $self->{hunk_minus} &&
164 $self->{hunk_minus} + $self->{hunk_plus} == 0);
165 }
166
167 sub open_patch_fh {
168 my ($self, $mode) = @_;
169 my $patch = $self->{patch};
170 if (!defined $patch) {
171 my $var;
172 $patch = $self->{patch} = \$var;
173 }
174 open(my $fh, $mode, $patch)
175 or systemerr("unable to open in-memory patch file");
176 return $fh;
177 }
178
179 sub append_line {
180 my ($self, $line) = @_;
181 my $fh = $self->{fh};
182 unless (defined $fh) {
183 $fh = $self->open_patch_fh(">");
184 $self->{fh} = $fh;
185 }
186 if (defined $self->{hunk_minus}) {
187 if ($line =~ /^@@\s*-\d+,(\d+)\s+[+]\d+,(\d+)\s*@@/) {
188 if ($self->{hunk_minus} + $self->{hunk_plus} == 0) {
189 $self->{hunk_minus} = $1;
190 $self->{hunk_plus} = $2;
191 } else {
192 # Bogus patch
193 $self->{hunk_minus} = undef;
194 $self->{hunk_plus} = undef;
195 }
196 } elsif ($line =~ /^-/) {
197 $self->{hunk_minus}--;
198 } elsif ($line =~ /^\+/) {
199 $self->{hunk_plus}--;
200 } elsif ($line =~ /^ /) {
201 $self->{hunk_minus}--;
202 $self->{hunk_plus}--;
203 } else {
204 warning("patch ignored. Invalid patch line: $line");
205 $self->{hunk_minus} = undef;
206 $self->{hunk_plus} = undef;
207 }
208 }
209 print $fh $line;
210 }
211
212 sub complete {
213 my $self = shift;
214 close $self->{fh};
215 delete $self->{fh};
216 return $self->is_valid();
217 }
218
219 sub output {
220 my ($self, $outfh, $filename) = @_;
221 $filename = $self->{target} unless $filename;
222
223 print $outfh "--- ", $filename, "\n";
224 print $outfh "+++ ", $filename, "\n";
225
226 my $infh = $self->open_patch_fh("<");
227 while (<$infh>) {
228 print $outfh $_;
229 }
230 close $infh;
231 }
232
233 sub apply {
234 my ($self, $filename) = @_;
235
236 my $outfile = File::Temp->new(TEMPLATE => "${filename}_patch.out.XXXXXX");
237 my $to_patch_process;
238 my $pid = spawn(exec => [ "patch", "--posix", "--force", "-r-", "-p0" ],
239 from_pipe => \$to_patch_process,
240 to_handle => $outfile,
241 wait_child => 0
242 );
243 my $ret = $self->output($to_patch_process, $filename);
244 close $to_patch_process;
245 wait_child($pid, no_check => 1);
246 $ret &&= !$?;
247 if ($ret) {
248 $self->{applied} = $filename;
249 } else {
250 open(my $outputfd, "<", $outfile->filename)
251 or syserr("unable to reopen temporary file");
252 my $output;
253 while (<$outputfd>) {
254 $output .= $_;
255 }
256 close $outputfd;
257 chop $output;
258 $self->{apply_output} = $output;
259 }
260 return $ret;
261 }
262
263 sub is_applied {
264 my $self = shift;
265 return $self->{applied};
266 }
267
268 sub get_apply_output {
269 my $self = shift;
270 return $self->{apply_output};
271 }