summaryrefslogtreecommitdiffstats
path: root/scripts/Dpkg/Source/Package/V1.pm
blob: 566fd24c4f933dc1ccc7a04618adf5cfbd2ff3d2 (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# Copyright © 2008-2009 Raphaël Hertzog <hertzog@debian.org>
# Copyright © 2008, 2012-2015 Guillem Jover <guillem@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

=encoding utf8

=head1 NAME

Dpkg::Source::Package::V1 - class for source format 1.0

=head1 DESCRIPTION

This module provides a class to handle the source package format 1.0.

B<Note>: This is a private module, its API can change at any time.

=cut

package Dpkg::Source::Package::V1 0.01;

use strict;
use warnings;

use Errno qw(ENOENT);
use Cwd;
use File::Basename;
use File::Temp qw(tempfile);
use File::Spec;

use Dpkg ();
use Dpkg::Gettext;
use Dpkg::ErrorHandling;
use Dpkg::Compression;
use Dpkg::Source::Archive;
use Dpkg::Source::Patch;
use Dpkg::Exit qw(push_exit_handler pop_exit_handler);
use Dpkg::Source::Functions qw(erasedir);
use Dpkg::Source::Package::V3::Native;

use parent qw(Dpkg::Source::Package);

our $CURRENT_MINOR_VERSION = '0';

sub init_options {
    my $self = shift;

    # Don't call $self->SUPER::init_options() on purpose, V1.0 has no
    # ignore by default
    if ($self->{options}{diff_ignore_regex}) {
	$self->{options}{diff_ignore_regex} .= '|(?:^|/)debian/source/local-.*$';
    } else {
	$self->{options}{diff_ignore_regex} = '(?:^|/)debian/source/local-.*$';
    }
    $self->{options}{diff_ignore_regex} .= '|(?:^|/)debian/files(?:\.new)?$';
    push @{$self->{options}{tar_ignore}},
         'debian/source/local-options',
         'debian/source/local-patch-header',
         'debian/files',
         'debian/files.new';
    $self->{options}{sourcestyle} //= 'X';
    $self->{options}{skip_debianization} //= 0;
    $self->{options}{ignore_bad_version} //= 0;
    $self->{options}{abort_on_upstream_changes} //= 0;

    # Set default validation checks.
    $self->{options}{require_valid_signature} //= 0;
    $self->{options}{require_strong_checksums} //= 0;

    # V1.0 only supports gzip compression.
    $self->{options}{compression} //= 'gzip';
    $self->{options}{comp_level} //= compression_get_level('gzip');
    $self->{options}{comp_ext} //= compression_get_file_extension('gzip');
}

my @module_cmdline = (
    {
        name => '-sa',
        help => N_('auto select original source'),
        when => 'build',
    }, {
        name => '-sk',
        help => N_('use packed original source (unpack and keep)'),
        when => 'build',
    }, {
        name => '-sp',
        help => N_('use packed original source (unpack and remove)'),
        when => 'build',
    }, {
        name => '-su',
        help => N_('use unpacked original source (pack and keep)'),
        when => 'build',
    }, {
        name => '-sr',
        help => N_('use unpacked original source (pack and remove)'),
        when => 'build',
    }, {
        name => '-ss',
        help => N_('trust packed and unpacked original sources are same'),
        when => 'build',
    }, {
        name => '-sn',
        help => N_('there is no diff, do main tarfile only'),
        when => 'build',
    }, {
        name => '-sA, -sK, -sP, -sU, -sR',
        help => N_('like -sa, -sk, -sp, -su, -sr but may overwrite'),
        when => 'build',
    }, {
        name => '--abort-on-upstream-changes',
        help => N_('abort if generated diff has upstream files changes'),
        when => 'build',
    }, {
        name => '-sp',
        help => N_('leave original source packed in current directory'),
        when => 'extract',
    }, {
        name => '-su',
        help => N_('do not copy original source to current directory'),
        when => 'extract',
    }, {
        name => '-sn',
        help => N_('unpack original source tree too'),
        when => 'extract',
    }, {
        name => '--skip-debianization',
        help => N_('do not apply debian diff to upstream sources'),
        when => 'extract',
    },
);

sub describe_cmdline_options {
    return @module_cmdline;
}

sub parse_cmdline_option {
    my ($self, $opt) = @_;
    my $o = $self->{options};
    if ($opt =~ m/^-s([akpursnAKPUR])$/) {
        warning(g_('-s%s option overrides earlier -s%s option'), $1,
                $o->{sourcestyle}) if $o->{sourcestyle} ne 'X';
        $o->{sourcestyle} = $1;
        $o->{copy_orig_tarballs} = 0 if $1 eq 'n'; # Extract option -sn
        return 1;
    } elsif ($opt eq '--skip-debianization') {
        $o->{skip_debianization} = 1;
        return 1;
    } elsif ($opt eq '--ignore-bad-version') {
        $o->{ignore_bad_version} = 1;
        return 1;
    } elsif ($opt eq '--abort-on-upstream-changes') {
        $o->{abort_on_upstream_changes} = 1;
        return 1;
    }
    return 0;
}

sub do_extract {
    my ($self, $newdirectory) = @_;
    my $sourcestyle = $self->{options}{sourcestyle};
    my $fields = $self->{fields};

    $sourcestyle =~ y/X/p/;
    unless ($sourcestyle =~ m/[pun]/) {
	usageerr(g_('source handling style -s%s not allowed with -x'),
	         $sourcestyle);
    }

    my $basename = $self->get_basename();
    my $basenamerev = $self->get_basename(1);

    # V1.0 only supports gzip compression
    my ($tarfile, $difffile);
    my $tarsign;
    foreach my $file ($self->get_files()) {
	if ($file =~ /^(?:\Q$basename\E\.orig|\Q$basenamerev\E)\.tar\.gz$/) {
            error(g_('multiple tarfiles in v1.0 source package')) if $tarfile;
            $tarfile = $file;
        } elsif ($file =~ /^\Q$basename\E\.orig\.tar\.gz\.asc$/) {
            $tarsign = $file;
	} elsif ($file =~ /^\Q$basenamerev\E\.diff\.gz$/) {
	    $difffile = $file;
	} else {
	    error(g_('unrecognized file for a %s source package: %s'),
                  'v1.0', $file);
	}
    }

    error(g_('no tarfile in Files field')) unless $tarfile;
    my $native = $difffile ? 0 : 1;
    if ($native and ($tarfile =~ /\.orig\.tar\.gz$/)) {
        warning(g_('native package with .orig.tar'));
        $native = 0; # V3::Native doesn't handle orig.tar
    }

    if ($native) {
        Dpkg::Source::Package::V3::Native::do_extract($self, $newdirectory);
    } else {
        my $expectprefix = $newdirectory;
        $expectprefix .= '.orig';

        if ($self->{options}{no_overwrite_dir} and -e $newdirectory) {
            error(g_('unpack target exists: %s'), $newdirectory);
        } else {
            erasedir($newdirectory);
        }
        if (-e $expectprefix) {
            rename($expectprefix, "$newdirectory.tmp-keep")
                or syserr(g_("unable to rename '%s' to '%s'"), $expectprefix,
                          "$newdirectory.tmp-keep");
        }

        info(g_('unpacking %s'), $tarfile);
        my $tar = Dpkg::Source::Archive->new(
            filename => File::Spec->catfile($self->{basedir}, $tarfile),
        );
        $tar->extract($expectprefix);

        if ($sourcestyle =~ /u/) {
            # -su: keep .orig directory unpacked
            if (-e "$newdirectory.tmp-keep") {
                error(g_('unable to keep orig directory (already exists)'));
            }
            system('cp', '-ar', '--', $expectprefix, "$newdirectory.tmp-keep");
            subprocerr("cp $expectprefix to $newdirectory.tmp-keep") if $?;
        }

	rename($expectprefix, $newdirectory)
	    or syserr(g_('failed to rename newly-extracted %s to %s'),
	              $expectprefix, $newdirectory);

	# rename the copied .orig directory
	if (-e "$newdirectory.tmp-keep") {
	    rename("$newdirectory.tmp-keep", $expectprefix)
	        or syserr(g_('failed to rename saved %s to %s'),
	                  "$newdirectory.tmp-keep", $expectprefix);
        }
    }

    if ($difffile and not $self->{options}{skip_debianization}) {
        my $patch = File::Spec->catfile($self->{basedir}, $difffile);
	info(g_('applying %s'), $difffile);
	my $patch_obj = Dpkg::Source::Patch->new(filename => $patch);
	my $analysis = $patch_obj->apply($newdirectory, force_timestamp => 1);
	my @files = grep { ! m{^\Q$newdirectory\E/debian/} }
		    sort keys %{$analysis->{filepatched}};
	info(g_('upstream files that have been modified: %s'),
	     "\n " . join("\n ", @files)) if scalar @files;
    }
}

sub can_build {
    my ($self, $dir) = @_;

    # As long as we can use gzip, we can do it as we have
    # native packages as fallback
    return (0, g_('only supports gzip compression'))
        unless $self->{options}{compression} eq 'gzip';
    return 1;
}

sub do_build {
    my ($self, $dir) = @_;
    my $sourcestyle = $self->{options}{sourcestyle};
    my @argv = @{$self->{options}{ARGV}};
    my @tar_ignore = map { "--exclude=$_" } @{$self->{options}{tar_ignore}};
    my $diff_ignore_regex = $self->{options}{diff_ignore_regex};

    if (scalar(@argv) > 1) {
        usageerr(g_('-b takes at most a directory and an orig source ' .
                    'argument (with v1.0 source package)'));
    }

    $sourcestyle =~ y/X/a/;
    unless ($sourcestyle =~ m/[akpursnAKPUR]/) {
        usageerr(g_('source handling style -s%s not allowed with -b'),
                 $sourcestyle);
    }

    my $sourcepackage = $self->{fields}{'Source'};
    my $basenamerev = $self->get_basename(1);
    my $basename = $self->get_basename();
    my $basedirname = $self->get_basedirname();

    # Try to find a .orig tarball for the package
    my $origdir = "$dir.orig";
    my $origtargz = $self->get_basename() . '.orig.tar.gz';
    if (-e $origtargz) {
        unless (-f $origtargz) {
            error(g_("packed orig '%s' exists but is not a plain file"), $origtargz);
        }
    } else {
        $origtargz = undef;
    }

    if (@argv) {
	# We have a second-argument <orig-dir> or <orig-targz>, check what it
	# is to decide the mode to use
        my $origarg = shift(@argv);
        if (length($origarg)) {
            stat($origarg)
                or syserr(g_('cannot stat orig argument %s'), $origarg);
            if (-d _) {
                $origdir = File::Spec->catdir($origarg);

                $sourcestyle =~ y/aA/rR/;
                unless ($sourcestyle =~ m/[ursURS]/) {
                    error(g_('orig argument is unpacked but source handling ' .
                             'style -s%s calls for packed (.orig.tar.<ext>)'),
                          $sourcestyle);
                }
            } elsif (-f _) {
                $origtargz = $origarg;
                $sourcestyle =~ y/aA/pP/;
                unless ($sourcestyle =~ m/[kpsKPS]/) {
                    error(g_('orig argument is packed but source handling ' .
                             'style -s%s calls for unpacked (.orig/)'),
                          $sourcestyle);
                }
            } else {
                error(g_('orig argument %s is not a plain file or directory'),
                      $origarg);
            }
        } else {
            $sourcestyle =~ y/aA/nn/;
            unless ($sourcestyle =~ m/n/) {
                error(g_('orig argument is empty (means no orig, no diff) ' .
                         'but source handling style -s%s wants something'),
                      $sourcestyle);
            }
        }
    } elsif ($sourcestyle =~ m/[aA]/) {
	# We have no explicit <orig-dir> or <orig-targz>, try to use
	# a .orig tarball first, then a .orig directory and fall back to
	# creating a native .tar.gz
	if ($origtargz) {
	    $sourcestyle =~ y/aA/pP/; # .orig.tar.<ext>
        } elsif (stat($origdir)) {
            unless (-d _) {
                error(g_("unpacked orig '%s' exists but is not a directory"),
                      $origdir);
            }
            $sourcestyle =~ y/aA/rR/; # .orig directory
        } elsif ($! != ENOENT) {
            syserr(g_("unable to stat putative unpacked orig '%s'"), $origdir);
        } else {
            $sourcestyle =~ y/aA/nn/; # Native tar.gz
	}
    }

    my $v = Dpkg::Version->new($self->{fields}->{'Version'});
    if ($sourcestyle =~ m/[kpursKPUR]/) {
        error(g_('non-native package version does not contain a revision'))
            if $v->is_native();
    } else {
        # TODO: This will become fatal in the near future.
        warning(g_('native package version may not have a revision'))
            unless $v->is_native();
    }

    my ($dirname, $dirbase) = fileparse($dir);
    if ($dirname ne $basedirname) {
	warning(g_("source directory '%s' is not <sourcepackage>" .
	           "-<upstreamversion> '%s'"), $dir, $basedirname);
    }

    my ($tarname, $tardirname, $tardirbase);
    my $tarsign;
    if ($sourcestyle ne 'n') {
	my ($origdirname, $origdirbase) = fileparse($origdir);

        if ($origdirname ne "$basedirname.orig") {
            warning(g_('.orig directory name %s is not <package>' .
	               '-<upstreamversion> (wanted %s)'),
	            $origdirname, "$basedirname.orig");
        }
        $tardirbase = $origdirbase;
        $tardirname = $origdirname;

	$tarname = $origtargz || "$basename.orig.tar.gz";
	$tarsign = "$tarname.asc";
	unless ($tarname =~ /\Q$basename\E\.orig\.tar\.gz/) {
	    warning(g_('.orig.tar name %s is not <package>_<upstreamversion>' .
	               '.orig.tar (wanted %s)'),
	            $tarname, "$basename.orig.tar.gz");
	}
    }

    if ($sourcestyle eq 'n') {
        $self->{options}{ARGV} = []; # ensure we have no error
        Dpkg::Source::Package::V3::Native::do_build($self, $dir);
    } elsif ($sourcestyle =~ m/[urUR]/) {
        if (stat($tarname)) {
            unless ($sourcestyle =~ m/[UR]/) {
		error(g_("tarfile '%s' already exists, not overwriting, " .
		         'giving up; use -sU or -sR to override'), $tarname);
            }
        } elsif ($! != ENOENT) {
	    syserr(g_("unable to check for existence of '%s'"), $tarname);
        }

	info(g_('building %s in %s'),
	     $sourcepackage, $tarname);

	my ($ntfh, $newtar) = tempfile("$tarname.new.XXXXXX",
				       DIR => getcwd(), UNLINK => 0);
	my $tar = Dpkg::Source::Archive->new(filename => $newtar,
		    compression => compression_guess_from_filename($tarname),
		    compression_level => $self->{options}{comp_level});
	$tar->create(options => \@tar_ignore, chdir => $tardirbase);
	$tar->add_directory($tardirname);
	$tar->finish();
	rename($newtar, $tarname)
	    or syserr(g_("unable to rename '%s' (newly created) to '%s'"),
	              $newtar, $tarname);
	chmod(0666 &~ umask(), $tarname)
	    or syserr(g_("unable to change permission of '%s'"), $tarname);
    } else {
	info(g_('building %s using existing %s'),
	     $sourcepackage, $tarname);
    }

    if ($tarname) {
        $self->add_file($tarname);
        if (-e "$tarname.sig" and not -e "$tarname.asc") {
            $self->armor_original_tarball_signature("$tarname.sig", "$tarname.asc");
        }
    }
    if ($tarsign and -e $tarsign) {
        $self->check_original_tarball_signature($dir, $tarsign);

        info(g_('building %s using existing %s'), $sourcepackage, $tarsign);
        $self->add_file($tarsign);
    } else {
        my $key = $self->get_upstream_signing_key($dir);
        if (-e $key) {
            warning(g_('upstream signing key but no upstream tarball signature'));
        }
    }

    if ($sourcestyle =~ m/[kpKP]/) {
        if (stat($origdir)) {
            unless ($sourcestyle =~ m/[KP]/) {
                error(g_("orig directory '%s' already exists, not overwriting, ".
                         'giving up; use -sA, -sK or -sP to override'),
                      $origdir);
            }
            erasedir($origdir);
        } elsif ($! != ENOENT) {
            syserr(g_("unable to check for existence of orig directory '%s'"),
                    $origdir);
        }

	my $tar = Dpkg::Source::Archive->new(filename => $origtargz);
	$tar->extract($origdir);
    }

    my $ur; # Unrepresentable changes
    if ($sourcestyle =~ m/[kpursKPUR]/) {
	my $diffname = "$basenamerev.diff.gz";
	info(g_('building %s in %s'),
	     $sourcepackage, $diffname);
	my ($ndfh, $newdiffgz) = tempfile("$diffname.new.XXXXXX",
					DIR => getcwd(), UNLINK => 0);
        push_exit_handler(sub { unlink($newdiffgz) });
        my $diff = Dpkg::Source::Patch->new(filename => $newdiffgz,
                                            compression => 'gzip',
                                            compression_level => $self->{options}{comp_level});
        $diff->create();
        $diff->add_diff_directory($origdir, $dir,
                basedirname => $basedirname,
                diff_ignore_regex => $diff_ignore_regex,
                options => []); # Force empty set of options to drop the
                                # default -p option
        $diff->finish() || $ur++;
        pop_exit_handler();

	my $analysis = $diff->analyze($origdir);
	my @files = grep { ! m{^debian/} }
		    map { s{^[^/]+/+}{}r }
		    sort keys %{$analysis->{filepatched}};
	if (scalar @files) {
	    warning(g_('the diff modifies the following upstream files: %s'),
	            "\n " . join("\n ", @files));
	    info(g_("use the '3.0 (quilt)' format to have separate and " .
	            'documented changes to upstream files, see dpkg-source(1)'));
	    error(g_('aborting due to --abort-on-upstream-changes'))
		if $self->{options}{abort_on_upstream_changes};
	}

	rename($newdiffgz, $diffname)
	    or syserr(g_("unable to rename '%s' (newly created) to '%s'"),
	              $newdiffgz, $diffname);
	chmod(0666 &~ umask(), $diffname)
	    or syserr(g_("unable to change permission of '%s'"), $diffname);

	$self->add_file($diffname);
    }

    if ($sourcestyle =~ m/[prPR]/) {
        erasedir($origdir);
    }

    if ($ur) {
        errormsg(g_('unrepresentable changes to source'));
        exit(1);
    }
}

=head1 CHANGES

=head2 Version 0.xx

This is a private module.

=cut

1;