Dpkg/Source/Package/V1.pm untested

filter: Statementstmt 0.0% 0 / 298 Branchbran 0.0% 0 / 146 Conditioncond 0.0% 0 / 69 MC/DCmcdc 0.0% 0 / 43 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 590 CC 74 SCAR 68.7
CC
74
Cov
0%
do_build
1892.0
do_extract
600.0
parse_cmdline_option
56.0
CRAP 962.2
1 # Copyright © 2008-2009 Raphaël Hertzog <hertzog@debian.org>
2 # Copyright © 2008, 2012-2015 Guillem Jover <guillem@debian.org>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
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 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 Dpkg::Source::Package::V1 - class for source format 1.0
22  
23 =head1 DESCRIPTION
24  
25 This module provides a class to handle the source package format 1.0.
26  
27 B<Note>: This is a private module, its API can change at any time.
28  
29 =cut
30  
31 package Dpkg::Source::Package::V1 0.01;
32  
33 use v5.36;
34  
35 use Errno qw(ENOENT);
36 use Cwd;
37 use Fcntl qw(:mode);
38 use File::stat ();
39 use File::Basename;
40 use File::Temp;
41 use File::Spec;
42  
43 use Dpkg ();
44 use Dpkg::Gettext;
45 use Dpkg::ErrorHandling;
46 use Dpkg::Version;
47 use Dpkg::Compression;
48 use Dpkg::Source::Archive;
49 use Dpkg::Source::Patch;
50 use Dpkg::Exit qw(push_exit_handler pop_exit_handler);
51 use Dpkg::Source::Functions qw(erasedir);
52 use Dpkg::Source::Package::V3::Native;
53 use Dpkg::Vendor qw(run_vendor_hook);
54  
55 use parent qw(Dpkg::Source::Package);
56  
57 sub CURRENT_MINOR_VERSION {
58 '0';
59 }
60  
61 sub init_options {
62 my $self = shift;
63  
64 # Do not call $self->SUPER::init_options() on purpose, V1.0 has no
65 # ignore by default.
66 if ($self->{options}{diff_ignore_regex}) {
67 $self->{options}{diff_ignore_regex} .= '|(?:^|/)debian/source/local-.*$';
68 } else {
69 $self->{options}{diff_ignore_regex} = '(?:^|/)debian/source/local-.*$';
70 }
71 $self->{options}{diff_ignore_regex} .= '|(?:^|/)debian/files(?:\.new)?$';
72 push @{$self->{options}{tar_ignore}},
73 'debian/source/local-options',
74 'debian/source/local-patch-header',
75 'debian/files',
76 'debian/files.new';
77 $self->{options}{sourcestyle} //= 'X';
78 $self->{options}{skip_debianization} //= 0;
79 $self->{options}{ignore_bad_version} //= 0;
80 $self->{options}{abort_on_upstream_changes} //= 0;
81  
82 # Set default validation checks.
83 $self->{options}{certs} //= [];
84 $self->{options}{use_vendor_certs} //= 1;
85 $self->{options}{require_valid_signature} //= 0;
86 $self->{options}{require_strong_checksums} //= 0;
87  
88 # V1.0 only supports gzip compression.
89 $self->{options}{compression} //= 'gzip';
90 $self->{options}{comp_level} //= compression_get_level('gzip');
91 $self->{options}{comp_ext} //= compression_get_file_extension('gzip');
92 }
93  
94 my @module_cmdline = (
95 {
96 name => '-sa',
97 help => N_('Auto select original source.'),
98 when => 'build',
99 }, {
100 name => '-sk',
101 help => N_('Use packed original source (unpack and keep).'),
102 when => 'build',
103 }, {
104 name => '-sp',
105 help => N_('Use packed original source (unpack and remove).'),
106 when => 'build',
107 }, {
108 name => '-su',
109 help => N_('Use unpacked original source (pack and keep).'),
110 when => 'build',
111 }, {
112 name => '-sr',
113 help => N_('Use unpacked original source (pack and remove).'),
114 when => 'build',
115 }, {
116 name => '-ss',
117 help => N_('Trust packed and unpacked original sources are same.'),
118 when => 'build',
119 }, {
120 name => '-sn',
121 help => N_('There is no diff, do main tarfile only.'),
122 when => 'build',
123 }, {
124 name => '-sA, -sK, -sP, -sU, -sR',
125 help => N_('Like -sa, -sk, -sp, -su, -sr but may overwrite.'),
126 when => 'build',
127 }, {
128 name => '--abort-on-upstream-changes',
129 help => N_('Abort if generated diff has upstream files changes.'),
130 when => 'build',
131 }, {
132 name => '-sp',
133 help => N_('Leave original source packed in current directory.'),
134 when => 'extract',
135 }, {
136 name => '-su',
137 help => N_('Do not copy original source to current directory.'),
138 when => 'extract',
139 }, {
140 name => '-sn',
141 help => N_('Unpack original source tree too.'),
142 when => 'extract',
143 }, {
144 name => '--skip-debianization',
145 help => N_('Do not apply debian diff to upstream sources.'),
146 when => 'extract',
147 },
148 );
149  
150 sub describe_cmdline_options {
151 return @module_cmdline;
152 }
153  
154 sub parse_cmdline_option {
155 my ($self, $opt) = @_;
156 my $o = $self->{options};
157 if ($opt =~ m/^-s([akpursnAKPUR])$/) {
158 warning(g_('-s%s option overrides earlier -s%s option'), $1,
159 $o->{sourcestyle}) if $o->{sourcestyle} ne 'X';
160 $o->{sourcestyle} = $1;
161 # Extract option -sn.
162 $o->{copy_orig_tarballs} = 0 if $1 eq 'n';
163 return 1;
164 } elsif ($opt eq '--skip-debianization') {
165 $o->{skip_debianization} = 1;
166 return 1;
167 } elsif ($opt eq '--ignore-bad-version') {
168 $o->{ignore_bad_version} = 1;
169 return 1;
170 } elsif ($opt eq '--abort-on-upstream-changes') {
171 $o->{abort_on_upstream_changes} = 1;
172 return 1;
173 }
174 return 0;
175 }
176  
177 sub do_extract {
178 my ($self, $newdirectory) = @_;
179 my $sourcestyle = $self->{options}{sourcestyle};
180 my $fields = $self->{fields};
181  
182 $sourcestyle =~ y/X/p/;
183 unless ($sourcestyle =~ m/[pun]/) {
184 usageerr(g_('source handling style -s%s not allowed with -x'),
185 $sourcestyle);
186 }
187  
188 my $basename = $self->get_basename();
189 my $basenamerev = $self->get_basename(1);
190  
191 # V1.0 only supports gzip compression.
192 my ($tarfile, $difffile);
193 my $tarsign;
194 foreach my $file ($self->get_files()) {
195 if ($file =~ /^(?:\Q$basename\E\.orig|\Q$basenamerev\E)\.tar\.gz$/) {
196 error(g_('multiple tarfiles in v1.0 source package')) if $tarfile;
197 $tarfile = $file;
198 } elsif ($file =~ /^\Q$basename\E\.orig\.tar\.gz\.asc$/) {
199 $tarsign = $file;
200 } elsif ($file =~ /^\Q$basenamerev\E\.diff\.gz$/) {
201 $difffile = $file;
202 } else {
203 error(g_('unrecognized file for a %s source package: %s'),
204 'v1.0', $file);
205 }
206 }
207  
208 error(g_('no tarfile in Files field')) unless $tarfile;
209 my $native = $difffile ? 0 : 1;
210 my $v = Dpkg::Version->new($fields->{'Version'});
211 if ($native) {
212 if ($tarfile =~ m/\.orig\.tar\.gz$/) {
213 # We only need to warn on this branch, because of the $native reset
214 # below, otherwise the V3::Native module will handle the warning.
215 if ($v->has_revision()) {
216 warning(g_('native package version may not have a revision'));
217 }
218  
219 warning(g_('native package with .orig.tar'));
220 # V3::Native does not handle "orig.tar".
221 $native = 0;
222 }
223 } else {
224 if (! $v->has_revision()) {
225 warning(g_('non-native package version does not contain a revision'))
226 }
227 }
228  
229 if ($native) {
230 Dpkg::Source::Package::V3::Native::do_extract($self, $newdirectory);
231 } else {
232 my $expectprefix = $newdirectory;
233 $expectprefix .= '.orig';
234  
235 if ($self->{options}{no_overwrite_dir} and -e $newdirectory) {
236 error(g_('unpack target exists: %s'), $newdirectory);
237 } else {
238 erasedir($newdirectory);
239 }
240 if (-e $expectprefix) {
241 rename($expectprefix, "$newdirectory.tmp-keep")
242 or syserr(g_("cannot rename '%s' to '%s'"), $expectprefix,
243 "$newdirectory.tmp-keep");
244 }
245  
246 info(g_('unpacking %s'), $tarfile);
247 my $tar = Dpkg::Source::Archive->new(
248 filename => File::Spec->catfile($self->{basedir}, $tarfile),
249 );
250 $tar->extract($expectprefix);
251  
252 if ($sourcestyle =~ /u/) {
253 # -su: keep .orig directory unpacked.
254 if (-e "$newdirectory.tmp-keep") {
255 error(g_('cannot keep orig directory (already exists)'));
256 }
257 system('cp', '-RPp', '--', $expectprefix, "$newdirectory.tmp-keep");
258 subprocerr("cp $expectprefix $newdirectory.tmp-keep") if $?;
259 }
260  
261 rename($expectprefix, $newdirectory)
262 or syserr(g_('cannot rename newly-extracted %s to %s'),
263 $expectprefix, $newdirectory);
264  
265 # Rename the copied .orig directory.
266 if (-e "$newdirectory.tmp-keep") {
267 rename("$newdirectory.tmp-keep", $expectprefix)
268 or syserr(g_('cannot rename saved %s to %s'),
269 "$newdirectory.tmp-keep", $expectprefix);
270 }
271 }
272  
273 if ($difffile and not $self->{options}{skip_debianization}) {
274 my $patch = File::Spec->catfile($self->{basedir}, $difffile);
275 info(g_('applying %s'), $difffile);
276 my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
277 my $analysis = $patch_obj->apply($newdirectory,
278 force_timestamp => 1,
279 );
280 my @files = grep { ! m{^debian/} }
281 map { s{^\Q$newdirectory\E/+}{}r }
282 sort keys %{$analysis->{filepatched}};
283 info(g_('upstream files that have been modified: %s'),
284 "\n " . join("\n ", @files)) if scalar @files;
285  
286 # As the diff might not represent executable permissions, we need to
287 # make sure debian/rules is executable if it exists. Otherwise the
288 # debian-rules build driver will take care of the warnings.
289 my $rules = File::Spec->catfile($newdirectory, 'debian', 'rules');
290 my $st = File::stat::lstat($rules);
291 if (! defined $st) {
292 syserr(g_('cannot stat %s'), $rules) if $! != ENOENT;
293 } elsif (-f $st) {
294 chmod S_IMODE($st->mode) | 0o111, $rules
295 or syserr(g_('cannot make %s executable'), $rules);
296 } else {
297 warning(g_('%s is not a plain file'), $rules);
298 }
299 }
300 }
301  
302 sub can_build {
303 my ($self, $dir) = @_;
304  
305 # As long as we can use gzip, we can do it as we have native packages as
306 # fallback.
307 return (0, g_('only supports gzip compression'))
308 unless $self->{options}{compression} eq 'gzip';
309 return 1;
310 }
311  
312 sub do_build {
313 my ($self, $dir) = @_;
314 my $sourcestyle = $self->{options}{sourcestyle};
315 my @argv = @{$self->{options}{ARGV}};
316 my @tar_ignore = map { "--exclude=$_" } @{$self->{options}{tar_ignore}};
317 my $diff_ignore_regex = $self->{options}{diff_ignore_regex};
318  
319 if (scalar(@argv) > 1) {
320 usageerr(g_('-b takes at most a directory and an orig source ' .
321 'argument (with v1.0 source package)'));
322 }
323  
324 $sourcestyle =~ y/X/a/;
325 unless ($sourcestyle =~ m/[akpursnAKPUR]/) {
326 usageerr(g_('source handling style -s%s not allowed with -b'),
327 $sourcestyle);
328 }
329  
330 my $sourcepackage = $self->{fields}{'Source'};
331 my $basenamerev = $self->get_basename(1);
332 my $basename = $self->get_basename();
333 my $basedirname = $self->get_basedirname();
334  
335 # Try to find a .orig tarball for the package.
336 my $origdir = "$dir.orig";
337 my $origtargz = File::Spec->catfile(
338 $self->{basedir}, $self->get_basename() . '.orig.tar.gz'
339 );
340 if (-e $origtargz) {
341 unless (-f $origtargz) {
342 error(g_("packed orig '%s' exists but is not a plain file"), $origtargz);
343 }
344 } else {
345 $origtargz = undef;
346 }
347  
348 if (@argv) {
349 # We have a second-argument <orig-dir> or <orig-targz>, check what it
350 # is to decide the mode to use.
351 my $origarg = shift(@argv);
352 if (length($origarg)) {
353 stat($origarg)
354 or syserr(g_('cannot stat orig argument %s'), $origarg);
355 if (-d _) {
356 $origdir = File::Spec->catdir($origarg);
357  
358 $sourcestyle =~ y/aA/rR/;
359 unless ($sourcestyle =~ m/[ursURS]/) {
360 error(g_('orig argument is unpacked but source handling ' .
361 'style -s%s calls for packed (.orig.tar.<ext>)'),
362 $sourcestyle);
363 }
364 } elsif (-f _) {
365 $origtargz = $origarg;
366 $sourcestyle =~ y/aA/pP/;
367 unless ($sourcestyle =~ m/[kpsKPS]/) {
368 error(g_('orig argument is packed but source handling ' .
369 'style -s%s calls for unpacked (.orig/)'),
370 $sourcestyle);
371 }
372 } else {
373 error(g_('orig argument %s is not a plain file or directory'),
374 $origarg);
375 }
376 } else {
377 $sourcestyle =~ y/aA/nn/;
378 unless ($sourcestyle =~ m/n/) {
379 error(g_('orig argument is empty (means no orig, no diff) ' .
380 'but source handling style -s%s wants something'),
381 $sourcestyle);
382 }
383 }
384 } elsif ($sourcestyle =~ m/[aA]/) {
385 # We have no explicit <orig-dir> or <orig-targz>, try to use a ".orig"
386 # tarball first, then a ".orig" directory and fall back to creating a
387 # native ".tar.gz".
388 if ($origtargz) {
389 # ".orig.tar.<ext>".
390 $sourcestyle =~ y/aA/pP/;
391 } elsif (stat($origdir)) {
392 unless (-d _) {
393 error(g_("unpacked orig '%s' exists but is not a directory"),
394 $origdir);
395 }
396 # ".orig" directory.
397 $sourcestyle =~ y/aA/rR/;
398 } elsif ($! != ENOENT) {
399 syserr(g_("cannot stat putative unpacked orig '%s'"), $origdir);
400 } else {
401 # Native "tar.gz".
402 $sourcestyle =~ y/aA/nn/;
403 }
404 }
405  
406 my $v = Dpkg::Version->new($self->{fields}->{'Version'});
407 if ($sourcestyle =~ m/[kpursKPUR]/) {
408 error(g_('non-native package version does not contain a revision'))
409 if ! $v->has_revision();
410 } else {
411 if ($v->has_revision()) {
412 if (run_vendor_hook('has-fuzzy-native-source')) {
413 warning(g_('native package version may not have a revision'));
414 } else {
415 error(g_('native package version may not have a revision'));
416 }
417 }
418 }
419  
420 my ($dirname, $dirbase) = fileparse($dir);
421 if ($dirname ne $basedirname) {
422 warning(g_("source directory '%s' is not <sourcepackage>" .
423 "-<upstreamversion> '%s'"), $dir, $basedirname);
424 }
425  
426 my ($tarname, $tardirname, $tardirbase);
427 my $tarsign;
428 if ($sourcestyle ne 'n') {
429 my ($origdirname, $origdirbase) = fileparse($origdir);
430  
431 if ($origdirname ne "$basedirname.orig") {
432 warning(g_('.orig directory name %s is not <package>' .
433 '-<upstreamversion> (wanted %s)'),
434 $origdirname, "$basedirname.orig");
435 }
436 $tardirbase = $origdirbase;
437 $tardirname = $origdirname;
438  
439 $tarname = $origtargz;
440 $tarname ||= File::Spec->catfile(
441 $self->{basedir}, "$basename.orig.tar.gz"
442 );
443 $tarsign = "$tarname.asc";
444 unless ($tarname =~ /\Q$basename\E\.orig\.tar\.gz/) {
445 warning(g_('.orig.tar name %s is not <package>_<upstreamversion>' .
446 '.orig.tar (wanted %s)'),
447 $tarname, "$basename.orig.tar.gz");
448 }
449 }
450  
451 if ($sourcestyle eq 'n') {
452 # Initialize ARGV to ensure we have no error.
453 $self->{options}{ARGV} = [];
454 Dpkg::Source::Package::V3::Native::do_build($self, $dir);
455 } elsif ($sourcestyle =~ m/[urUR]/) {
456 if (stat($tarname)) {
457 unless ($sourcestyle =~ m/[UR]/) {
458 error(g_("tarfile '%s' already exists, not overwriting, " .
459 'giving up; use -sU or -sR to override'), $tarname);
460 }
461 } elsif ($! != ENOENT) {
462 syserr(g_("cannot check for existence of '%s'"), $tarname);
463 }
464  
465 info(g_('building %s in %s'),
466 $sourcepackage, $tarname);
467  
468 my $newtar = File::Temp->new(
469 TEMPLATE => "$tarname.new.XXXXXX",
470 DIR => getcwd(),
471 UNLINK => 0,
472 );
473 my $tar = Dpkg::Source::Archive->new(
474 filename => $newtar,
475 compression => compression_guess_from_filename($tarname),
476 compression_level => $self->{options}{comp_level},
477 );
478 $tar->create(
479 options => \@tar_ignore,
480 chdir => $tardirbase,
481 );
482 $tar->add_directory($tardirname);
483 $tar->finish();
484 rename($newtar, $tarname)
485 or syserr(g_("cannot rename '%s' (newly created) to '%s'"),
486 $newtar, $tarname);
487 chmod(0o666 &~ umask(), $tarname)
488 or syserr(g_("cannot change permission of '%s'"), $tarname);
489 } else {
490 info(g_('building %s using existing %s'),
491 $sourcepackage, $tarname);
492 }
493  
494 if ($tarname) {
495 $self->add_file($tarname);
496 if (-e "$tarname.sig" and not -e "$tarname.asc") {
497 $self->armor_original_tarball_signature("$tarname.sig", "$tarname.asc");
498 }
499 }
500 if ($tarsign and -e $tarsign) {
501 $self->check_original_tarball_signature($dir, $tarsign);
502  
503 info(g_('building %s using existing %s'), $sourcepackage, $tarsign);
504 $self->add_file($tarsign);
505 } else {
506 my $key = $self->get_upstream_signing_key($dir);
507 if (-e $key) {
508 warning(g_('upstream signing key but no upstream tarball signature'));
509 }
510 }
511  
512 if ($sourcestyle =~ m/[kpKP]/) {
513 if (stat($origdir)) {
514 unless ($sourcestyle =~ m/[KP]/) {
515 error(g_("orig directory '%s' already exists, not overwriting, ".
516 'giving up; use -sA, -sK or -sP to override'),
517 $origdir);
518 }
519 erasedir($origdir);
520 } elsif ($! != ENOENT) {
521 syserr(g_("cannot check for existence of orig directory '%s'"),
522 $origdir);
523 }
524  
525 my $tar = Dpkg::Source::Archive->new(filename => $origtargz);
526 $tar->extract($origdir);
527 }
528  
529 # Unrepresentable changes.
530 my $ur;
531 if ($sourcestyle =~ m/[kpursKPUR]/) {
532 my $diffname = File::Spec->catfile(
533 $self->{basedir}, "$basenamerev.diff.gz"
534 );
535 info(g_('building %s in %s'),
536 $sourcepackage, $diffname);
537 my $newdiffgz = File::Temp->new(
538 TEMPLATE => "$diffname.new.XXXXXX",
539 DIR => getcwd(),
540 UNLINK => 0,
541 );
542 push_exit_handler(sub { unlink($newdiffgz) });
543 my $diff = Dpkg::Source::Patch->new(
544 filename => $newdiffgz,
545 compression => 'gzip',
546 compression_level => $self->{options}{comp_level},
547 );
548 $diff->create();
549 $diff->add_diff_directory($origdir, $dir,
550 basedirname => $basedirname,
551 diff_ignore_regex => $diff_ignore_regex,
552 # Force empty set of options to drop the default -p option.
553 options => [],
554 );
555  
556 $diff->finish() || $ur++;
557 pop_exit_handler();
558  
559 my $analysis = $diff->analyze($origdir);
560 my @files = grep { ! m{^debian/} }
561 map { s{^\Q$origdir\E/+}{}r }
562 sort keys %{$analysis->{filepatched}};
563 if (scalar @files) {
564 warning(g_('the diff modifies the following upstream files: %s'),
565 "\n " . join("\n ", @files));
566 info(g_("use the '3.0 (quilt)' format to have separate and " .
567 'documented changes to upstream files, see dpkg-source(1)'));
568 error(g_('aborting due to --abort-on-upstream-changes'))
569 if $self->{options}{abort_on_upstream_changes};
570 }
571  
572 rename($newdiffgz, $diffname)
573 or syserr(g_("cannot rename '%s' (newly created) to '%s'"),
574 $newdiffgz, $diffname);
575 chmod(0o666 &~ umask(), $diffname)
576 or syserr(g_("cannot change permission of '%s'"), $diffname);
577  
578 $self->add_file($diffname);
579 }
580  
581 if ($sourcestyle =~ m/[prPR]/) {
582 erasedir($origdir);
583 }
584  
585 if ($ur) {
586 errormsg(g_('unrepresentable changes to source'));
587 exit(1);
588 }
589 }
590  
591 =head1 CHANGES
592  
593 =head2 Version 0.xx
594  
595 This is a private module.
596  
597 =cut
598  
599 1;