1#!/usr/bin/perl
2#
3# dpkg-gencontrol
4#
5# Copyright © 1996 Ian Jackson
6# Copyright © 2000,2002 Wichert Akkerman
7# Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
22use strict;
23use warnings;
24
25use List::Util qw(none);
26use POSIX qw(:errno_h :fcntl_h);
27use File::Find;
28
29use Dpkg ();
30use Dpkg::Gettext;
31use Dpkg::ErrorHandling;
32use Dpkg::Lock;
33use Dpkg::Arch qw(get_host_arch debarch_eq debarch_is debarch_list_parse);
34use Dpkg::Package;
35use Dpkg::BuildProfiles qw(get_build_profiles);
36use Dpkg::Deps;
37use Dpkg::Control;
38use Dpkg::Control::Info;
39use Dpkg::Control::Fields;
40use Dpkg::Substvars;
41use Dpkg::Vars;
42use Dpkg::Changelog::Parse;
43use Dpkg::Dist::Files;
44
45textdomain('dpkg-dev');
46
47
48my $controlfile = 'debian/control';
49my $changelogfile = 'debian/changelog';
50my $changelogformat;
51my $fileslistfile = 'debian/files';
52my $packagebuilddir = 'debian/tmp';
53my $outputfile;
54
55my $sourceversion;
56my $binaryversion;
57my $forceversion;
58my $forcefilename;
59my $stdout;
60my %remove;
61my %override;
62my $oppackage;
63my $substvars = Dpkg::Substvars->new();
64my $substvars_loaded = 0;
65
66
67sub version {
68    printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
69
70    printf g_('
71This is free software; see the GNU General Public License version 2 or
72later for copying conditions. There is NO warranty.
73');
74}
75
76sub usage {
77    printf g_(
78'Usage: %s [<option>...]')
79    . "\n\n" . g_(
80'Options:
81  -p<package>              print control file for package.
82  -c<control-file>         get control info from this file.
83  -l<changelog-file>       get per-version info from this file.
84  -F<changelog-format>     force changelog format.
85  -v<force-version>        set version of binary package.
86  -f<files-list-file>      write files here instead of debian/files.
87  -P<package-build-dir>    temporary build directory instead of debian/tmp.
88  -n<filename>             assume the package filename will be <filename>.
89  -O[<file>]               write to stdout (or <file>), not .../DEBIAN/control.
90  -is, -ip, -isp, -ips     deprecated, ignored for compatibility.
91  -D<field>=<value>        override or add a field and value.
92  -U<field>                remove a field.
93  -V<name>=<value>         set a substitution variable.
94  -T<substvars-file>       read variables here, not debian/substvars.
95  -?, --help               show this help message.
96      --version            show the version.
97'), $Dpkg::PROGNAME;
98}
99
100while (@ARGV) {
101    $_=shift(@ARGV);
102    if (m/^-p/p) {
103        $oppackage = ${^POSTMATCH};
104        my $err = pkg_name_is_illegal($oppackage);
105        error(g_("illegal package name '%s': %s"), $oppackage, $err) if $err;
106    } elsif (m/^-c/p) {
107        $controlfile = ${^POSTMATCH};
108    } elsif (m/^-l/p) {
109        $changelogfile = ${^POSTMATCH};
110    } elsif (m/^-P/p) {
111        $packagebuilddir = ${^POSTMATCH};
112    } elsif (m/^-f/p) {
113        $fileslistfile = ${^POSTMATCH};
114    } elsif (m/^-v(.+)$/) {
115        $forceversion= $1;
116    } elsif (m/^-O$/) {
117        $stdout= 1;
118    } elsif (m/^-O(.+)$/) {
119        $outputfile = $1;
120    } elsif (m/^-i([sp][sp]?)$/) {
121        warning(g_('-i%s is deprecated; it is without effect'), $1);
122    } elsif (m/^-F([0-9a-z]+)$/) {
123        $changelogformat=$1;
124    } elsif (m/^-D([^\=:]+)[=:]/p) {
125        $override{$1} = ${^POSTMATCH};
126    } elsif (m/^-U([^\=:]+)$/) {
127        $remove{$1}= 1;
128    } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:]/p) {
129        $substvars->set_as_used($1, ${^POSTMATCH});
130    } elsif (m/^-T(.*)$/) {
131	$substvars->load($1) if -e $1;
132	$substvars_loaded = 1;
133    } elsif (m/^-n/p) {
134        $forcefilename = ${^POSTMATCH};
135    } elsif (m/^-(?:\?|-help)$/) {
136        usage();
137        exit(0);
138    } elsif (m/^--version$/) {
139        version();
140        exit(0);
141    } else {
142        usageerr(g_("unknown option '%s'"), $_);
143    }
144}
145
146umask 0022; # ensure sane default permissions for created files
147my %options = (file => $changelogfile);
148$options{changelogformat} = $changelogformat if $changelogformat;
149my $changelog = changelog_parse(%options);
150if ($changelog->{'Binary-Only'}) {
151    $options{count} = 1;
152    $options{offset} = 1;
153    my $prev_changelog = changelog_parse(%options);
154    $sourceversion = $prev_changelog->{'Version'};
155} else {
156    $sourceversion = $changelog->{'Version'};
157}
158
159if (defined $forceversion) {
160    $binaryversion = $forceversion;
161} else {
162    $binaryversion = $changelog->{'Version'};
163}
164
165$substvars->set_version_substvars($sourceversion, $binaryversion);
166$substvars->set_arch_substvars();
167$substvars->load('debian/substvars') if -e 'debian/substvars' and not $substvars_loaded;
168my $control = Dpkg::Control::Info->new($controlfile);
169my $fields = Dpkg::Control->new(type => CTRL_PKG_DEB);
170
171# Old-style bin-nmus change the source version submitted to
172# set_version_substvars()
173$sourceversion = $substvars->get('source:Version');
174
175my $pkg;
176
177if (defined($oppackage)) {
178    $pkg = $control->get_pkg_by_name($oppackage);
179    if (not defined $pkg) {
180        error(g_('package %s not in control info'), $oppackage)
181    }
182} else {
183    my @packages = map { $_->{'Package'} } $control->get_packages();
184    if (@packages == 0) {
185        error(g_('no package stanza found in control info'));
186    } elsif (@packages > 1) {
187        error(g_('must specify package since control info has many (%s)'),
188              "@packages");
189    }
190    $pkg = $control->get_pkg_by_idx(1);
191}
192$substvars->set_msg_prefix(sprintf(g_('package %s: '), $pkg->{Package}));
193
194# Scan source package
195my $src_fields = $control->get_source();
196foreach (keys %{$src_fields}) {
197    if (m/^Source$/) {
198	set_source_package($src_fields->{$_});
199    } elsif (m/^Description$/) {
200        # Description in binary packages is not inherited, do not copy this
201        # field, only initialize the description substvars.
202        $substvars->set_desc_substvars($src_fields->{$_});
203    } else {
204        field_transfer_single($src_fields, $fields);
205    }
206}
207$substvars->set_field_substvars($src_fields, 'S');
208
209# Scan binary package
210foreach (keys %{$pkg}) {
211    my $v = $pkg->{$_};
212    if (field_get_dep_type($_)) {
213	# Delay the parsing until later
214    } elsif (m/^Architecture$/) {
215	my $host_arch = get_host_arch();
216
217	if (debarch_eq('all', $v)) {
218	    $fields->{$_} = $v;
219	} else {
220	    my @archlist = debarch_list_parse($v, positive => 1);
221
222	    if (none { debarch_is($host_arch, $_) } @archlist) {
223		error(g_("current host architecture '%s' does not " .
224			 "appear in package's architecture list (%s)"),
225		      $host_arch, "@archlist");
226	    }
227	    $fields->{$_} = $host_arch;
228	}
229    } else {
230        field_transfer_single($pkg, $fields);
231    }
232}
233
234# Scan fields of dpkg-parsechangelog
235foreach (keys %{$changelog}) {
236    my $v = $changelog->{$_};
237
238    if (m/^Source$/) {
239	set_source_package($v);
240    } elsif (m/^Version$/) {
241        # Already handled previously.
242    } elsif (m/^Maintainer$/) {
243        # That field must not be copied from changelog even if it's
244        # allowed in the binary package control information
245    } else {
246        field_transfer_single($changelog, $fields);
247    }
248}
249
250$fields->{'Version'} = $binaryversion;
251
252# Process dependency fields in a second pass, now that substvars have been
253# initialized.
254
255my $facts = Dpkg::Deps::KnownFacts->new();
256$facts->add_installed_package($fields->{'Package'}, $fields->{'Version'},
257                              $fields->{'Architecture'}, $fields->{'Multi-Arch'});
258if (exists $pkg->{'Provides'}) {
259    my $provides = deps_parse($substvars->substvars($pkg->{'Provides'}, no_warn => 1),
260                              reduce_restrictions => 1, union => 1);
261    if (defined $provides) {
262	foreach my $subdep ($provides->get_deps()) {
263	    if ($subdep->isa('Dpkg::Deps::Simple')) {
264		$facts->add_provided_package($subdep->{package},
265                        $subdep->{relation}, $subdep->{version},
266                        $fields->{'Package'});
267	    }
268	}
269    }
270}
271
272my (@seen_deps);
273foreach my $field (field_list_pkg_dep()) {
274    # Arch: all can't be simplified as the host architecture is not known
275    my $reduce_arch = debarch_eq('all', $pkg->{Architecture} || 'all') ? 0 : 1;
276    if (exists $pkg->{$field}) {
277	my $dep;
278	my $field_value = $substvars->substvars($pkg->{$field},
279	    msg_prefix => sprintf(g_('%s field of package %s: '), $field, $pkg->{Package}));
280	if (field_get_dep_type($field) eq 'normal') {
281	    $dep = deps_parse($field_value, use_arch => 1,
282	                      reduce_arch => $reduce_arch,
283	                      reduce_profiles => 1);
284	    error(g_('error occurred while parsing %s field: %s'), $field,
285                  $field_value) unless defined $dep;
286	    $dep->simplify_deps($facts, @seen_deps);
287	    # Remember normal deps to simplify even further weaker deps
288	    push @seen_deps, $dep;
289	} else {
290	    $dep = deps_parse($field_value, use_arch => 1,
291	                      reduce_arch => $reduce_arch,
292	                      reduce_profiles => 1, union => 1);
293	    error(g_('error occurred while parsing %s field: %s'), $field,
294                  $field_value) unless defined $dep;
295	    $dep->simplify_deps($facts);
296            $dep->sort();
297	}
298	error(g_('the %s field contains an arch-specific dependency but the ' .
299	         'package is architecture all'), $field)
300	    if $dep->has_arch_restriction();
301	$fields->{$field} = $dep->output();
302	delete $fields->{$field} unless $fields->{$field}; # Delete empty field
303    }
304}
305
306for my $f (qw(Package Version Architecture)) {
307    error(g_('missing information for output field %s'), $f)
308        unless defined $fields->{$f};
309}
310for my $f (qw(Maintainer Description)) {
311    warning(g_('missing information for output field %s'), $f)
312        unless defined $fields->{$f};
313}
314
315my $pkg_type = $pkg->{'Package-Type'} ||
316               $pkg->get_custom_field('Package-Type') || 'deb';
317
318if ($pkg_type eq 'udeb') {
319    delete $fields->{'Package-Type'};
320    delete $fields->{'Homepage'};
321} else {
322    for my $f (qw(Subarchitecture Kernel-Version Installer-Menu-Item)) {
323        warning(g_('%s package with udeb specific field %s'), $pkg_type, $f)
324            if defined($fields->{$f});
325    }
326}
327
328my $sourcepackage = get_source_package();
329my $binarypackage = $override{'Package'} // $fields->{'Package'};
330my $verdiff = $binaryversion ne $sourceversion;
331if ($binarypackage ne $sourcepackage || $verdiff) {
332    $fields->{'Source'} = $sourcepackage;
333    $fields->{'Source'} .= ' (' . $sourceversion . ')' if $verdiff;
334}
335
336if (!defined($substvars->get('Installed-Size'))) {
337    my $installed_size = 0;
338    my $scan_installed_size = sub {
339        lstat or syserr(g_('cannot stat %s'), $File::Find::name);
340
341        if (-f _ or -l _) {
342            # For filesystem objects with actual content accumulate the size
343            # in 1 KiB units.
344            $installed_size += POSIX::ceil((-s _) / 1024);
345        } else {
346            # For other filesystem objects assume a minimum 1 KiB baseline,
347            # as directories are shared resources between packages, and other
348            # object types are mainly metadata-only, supposedly consuming
349            # at most an inode.
350            $installed_size += 1;
351        }
352    };
353    find($scan_installed_size, $packagebuilddir) if -d $packagebuilddir;
354
355    $substvars->set_as_auto('Installed-Size', $installed_size);
356}
357if (defined($substvars->get('Extra-Size'))) {
358    my $size = $substvars->get('Extra-Size') + $substvars->get('Installed-Size');
359    $substvars->set_as_auto('Installed-Size', $size);
360}
361if (defined($substvars->get('Installed-Size'))) {
362    $fields->{'Installed-Size'} = $substvars->get('Installed-Size');
363}
364
365for my $f (keys %override) {
366    $fields->{$f} = $override{$f};
367}
368for my $f (keys %remove) {
369    delete $fields->{$f};
370}
371
372$fields->apply_substvars($substvars);
373
374if ($stdout) {
375    $fields->output(\*STDOUT);
376} else {
377    $outputfile //= "$packagebuilddir/DEBIAN/control";
378
379    my $sversion = $fields->{'Version'};
380    $sversion =~ s/^\d+://;
381    $forcefilename //= sprintf('%s_%s_%s.%s', $fields->{'Package'}, $sversion,
382                               $fields->{'Architecture'}, $pkg_type);
383    my $section = $fields->{'Section'} || '-';
384    my $priority = $fields->{'Priority'} || '-';
385
386    # Obtain a lock on debian/control to avoid simultaneous updates
387    # of debian/files when parallel building is in use
388    my $lockfh;
389    my $lockfile = 'debian/control';
390    $lockfile = $controlfile if not -e $lockfile;
391
392    sysopen $lockfh, $lockfile, O_WRONLY
393        or syserr(g_('cannot write %s'), $lockfile);
394    file_lock($lockfh, $lockfile);
395
396    my $dist = Dpkg::Dist::Files->new();
397    $dist->load($fileslistfile) if -e $fileslistfile;
398
399    foreach my $file ($dist->get_files()) {
400        if (defined $file->{package} &&
401            ($file->{package} eq $fields->{'Package'}) &&
402            ($file->{package_type} eq $pkg_type) &&
403            (debarch_eq($file->{arch}, $fields->{'Architecture'}) ||
404             debarch_eq($file->{arch}, 'all'))) {
405            $dist->del_file($file->{filename});
406        }
407    }
408
409    my %fileattrs;
410    $fileattrs{automatic} = 'yes' if $fields->{'Auto-Built-Package'};
411
412    $dist->add_file($forcefilename, $section, $priority, %fileattrs);
413    $dist->save("$fileslistfile.new");
414
415    rename "$fileslistfile.new", $fileslistfile
416        or syserr(g_('install new files list file'));
417
418    # Release the lock
419    close $lockfh or syserr(g_('cannot close %s'), $lockfile);
420
421    $fields->save("$outputfile.new");
422
423    rename "$outputfile.new", $outputfile
424        or syserr(g_("cannot install output control file '%s'"), $outputfile);
425}
426
427$substvars->warn_about_unused();
428