summaryrefslogtreecommitdiff
path: root/usr.sbin/pkg_add/pkg_create
blob: d0847b1fcda1b1b1fbdc786a8fb5c6b64e994ae1 (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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#! /usr/bin/perl
# ex:ts=8 sw=4:
# $OpenBSD: pkg_create,v 1.47 2006/10/15 15:37:28 jmc Exp $
#
# Copyright (c) 2003-2004 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use strict;
use warnings;
use OpenBSD::PackingList;
use OpenBSD::PackageInfo;
use OpenBSD::Getopt;
use OpenBSD::md5;
use OpenBSD::Temp;
use OpenBSD::Error;
use OpenBSD::Ustar;
use OpenBSD::ArcCheck;
use Symbol;
use File::Basename;

# Extra stuff needed to archive files
package OpenBSD::PackingElement;

sub archive {}
sub anything { ${$_[1]}++; }

sub print_file {}
package OpenBSD::PackingElement::FileBase;
sub archive
{
	my ($self, $arc, $base, $verbose) = @_;

	if (defined $arc) {
	    my $o = $arc->prepare_long($self);
	    $o->write();
	}
	if ($verbose) {
		print "Adding ", $self->{name}, "\n";
	}
}

sub print_file
{
	my ($item) = @_;
	print '@', $item->keyword(), " ", $item->fullname(), "\n";
}

package OpenBSD::PackingElement::Cwd;
use OpenBSD::Temp;
sub archive
{
	my ($self, $arc, $base, $verbose) = @_;
	if (defined $arc) {
	    $arc->destdir($base."/".$self->{name});
	}
	if ($verbose) {
		print "Cwd: ", $self->{name}, "\n";
	}
}

package OpenBSD::PackingElement::DirRm;
my $warned;

sub compute_checksum
{
	if (!$warned) {
		print STDERR "Warning: \@dirrm is deprecated\n";
		$warned=1;
	}
}

package OpenBSD::PackingElement;

sub compute_checksum
{
}

sub verify_checksum
{
}

package OpenBSD::PackingElement::FileBase;
use OpenBSD::md5;

sub compute_checksum
{
	my ($self, $plist, $base, $stash) = @_;
	my $fname = $self->fullname();
	if (-l "$base/$fname") {
		my $value = readlink "$base/$fname";
		$self->make_symlink($value);
		return if $base eq '/' or $base eq '';
		if ($value =~ m/^\Q$base/) {
			print STDERR "Error in package: symlink $base/$fname refers to $value\n";
			$main::errors++;
		}
	} elsif (-f _) {
		my ($dev, $ino, $size) = (stat _)[0,1,7];
		if (defined $stash->{"$dev/$ino"}) {
			$self->make_hardlink($stash->{"$dev/$ino"});
		} else {
			$stash->{"$dev/$ino"} = $fname;
			$self->{md5} = OpenBSD::md5::fromfile("$base/$fname");
			$self->{size} = $size;
		}
	} else {
		print STDERR "Error in package: \"$base/$fname\" does not exist\n";
		$main::errors++;
	}
}

sub verify_checksum
{
	my ($self, $base, $stash) = @_;
	my $fname = $self->fullname();
	my $check = {};
	if (-l "$base/$fname") {
		my $value = readlink "$base/$fname";
		$check->{symlink} = $value;
	} elsif (-f _) {
		my ($dev, $ino, $size) = (stat _)[0,1,7];
		if (defined $stash->{"$dev/$ino"}) {
			$check->{link} = $stash->{"$dev/$ino"};
		} else {
			$stash->{"$dev/$ino"} = $fname;
			$check->{md5} = OpenBSD::md5::fromfile("$base/$fname");
			$check->{size} = $size;
		}
	} else {
		print STDERR "Error in package: $base/$fname does not exist\n";
		$main::errors++;
	}
	for my $field (qw(symlink link md5 size)) {
		if ((defined $check->{$field} && defined $self->{$field} &&
		    $check->{$field} ne $self->{$field}) ||
		    (defined $check->{$field} xor defined $self->{$field})) {
			print STDERR "Error: $field inconsistency for $fname\n";
			$main::errors++;
		}
	}
}

package OpenBSD::PackingElement::InfoFile;
sub compute_checksum
{
	my ($self, $plist, $base, $stash) = @_;
	$self->SUPER::compute_checksum($plist, $base, $stash);
	my $fname = $self->fullname();
	for (my $i = 1; ; $i++) {
		if (-e "$base/$fname-$i") {
			my $file = OpenBSD::PackingElement::File->add($plist, $self->{name}."-".$i);
			$file->compute_checksum($plist, $base, $stash);
		} else {
			last;
		}
	}
}

package OpenBSD::PackingList;

sub archive
{
	my ($self, $wrarc, $base, $verbose) = @_;
	for my $item (@{$self->{items}}) {
		$item->archive($wrarc, $base, $verbose);
	}
}

sub makesum
{
	my ($self, $base) = @_;
	my $stash = {};
	my $oldlist = $self->{items};
	$self->{items} = [];
	for my $item (@$oldlist) {
		push @{$self->{items}}, $item;
		$self->{state}->{cwd} = $item->{cwd} if defined $item->{cwd};
		$item->compute_checksum($self, $base, $stash);
	}
}

sub checksum
{
	my ($self, $base) = @_;
	my $stash = {};
	for my $item (@{$self->{items}}) {
		$self->{state}->{cwd} = $item->{cwd} if defined $item->{cwd};
		$item->verify_checksum($base, $stash);
	}
}

sub avert_duplicates
{
	my ($self) = @_;
	my $allfiles = {};
	for my $item (@{$self->{items}}) {
		if ($item->NoDuplicateNames()) {
			my $n = $item->fullname();
			if (defined $allfiles->{$n}) {
				print STDERR "Error in packing-list: duplicate file $n\n";
				$main::errors++;
			}
			$allfiles->{$n} = 1;
		} 
	}
}


package main;

my %defines;

sub dosubst
{
	local $_ = shift;
	while (my ($k, $v) = each %defines) {
		s/\$\{\Q$k\E\}/$v/g;
	}
	s/\$\\/\$/g;
	return $_;
}

sub copy_subst
{
	my ($srcname, $mode, $destname) = @_;
	open my $src, '<', $srcname or die "can't open $srcname";
	open my $dest, $mode, $destname or die "can't open $destname";
	local $_;
	while (<$src>) {
		print $dest dosubst($_);
	}
}

our ($opt_p, $opt_f, $opt_c, $opt_d, $opt_v, $opt_i, $opt_k, $opt_r, 
	$opt_S, $opt_h, $opt_s, $opt_O, $opt_A, $opt_L,
	$opt_m, $opt_M, $opt_U, $opt_P, $opt_W, $opt_n,
	$opt_B, $opt_q, $opt_Q);

sub deduce_name
{
	my ($o, $frag, $not) = @_;

	my $noto = $o;
	my $nofrag = "no-$frag";

	$o =~ s/PFRAG\./PFRAG.$frag-/ or
	    $o =~ s/PLIST/PFRAG.$frag/;

	$noto =~ s/PFRAG\./PFRAG.no-$frag-/ or
	    $noto =~ s/PLIST/PFRAG.no-$frag/;
	unless (-e $o or -e $noto) {
		die "Missing fragments for $frag: $o and $noto don't exist";
	}
	if ($not) {
		print "Switching to $noto\n" if !defined $opt_q;
		return $noto if -e $noto;
    	} else {
		print "Switching to $o\n" if !defined $opt_q;
		return $o if -e $o;
	}
	return;
}

our $errors = 0;

my @contents;
my $regen_package = 0;

set_usage(
'pkg_create [-hnQqv] [-A arches] [-B pkg-destdir] [-D name=value]',
'[-i iscript] [-k dscript] [-L localbase] [-M module] [-M displayfile]',
'[-P pkg-dependency] [-p prefix] [-r rscript] [-S pkg-destdir]',
'[-U undisplayfile] [-W wantedlib] -c desc -d desc -f packinglist pkg-name');

my $plist = new OpenBSD::PackingList;
try { 
	getopts('hp:f:c:d:vi:k:r:m:M:U:S:hs:OA:L:B:D:P:W:nqQ', 
	{'D' => 
		sub { 
			local $_ = shift;
			if (m/\=/) {
				$defines{$`} = $';
			} else {
				$defines{$_} = 1;
			}
		},
	 'f' =>
	 	sub {
			push(@contents, shift);
		},
	 'h' => sub {	Usage(); },
	 'P' => sub {
	 		OpenBSD::PackingElement::Dependency->add($plist, shift);
		},
	 'W' => sub {
	 		OpenBSD::PackingElement::Wantlib->add($plist, shift);
		}
	}); 
} catchall {
	Usage($_);
};

if (@ARGV == 0) {
	$regen_package = 1;
} elsif (@ARGV != 1) {
	Usage "Exactly one single package name is required: ", join(' ', @ARGV);
}

try {
my $dir;

$dir = OpenBSD::Temp::dir() unless $opt_q;

if (defined $opt_s) {
	Usage "Option s is no longer supported";
}

if (defined $opt_O) {
	Usage "Option O is no longer supported";
}

if (defined $opt_Q) {
	$opt_q = 1;
}

if (!@contents) {
	Usage "Packing list required";
}

my @extra_files = ();

if (defined $opt_q) {
	for my $special (info_names()) {
		if ($special eq DESC or
		    $special eq INSTALL and (defined $opt_i) or
		    $special eq DEINSTALL and (defined $opt_k) or
		    $special eq REQUIRE and (defined $opt_r) or
		    $special eq DISPLAY and (defined $opt_M) or
		    $special eq MODULE and (defined $opt_m) or
		    $special eq UNDISPLAY and (defined $opt_U)) {
			OpenBSD::PackingElement::File->add($plist, $special);
		}
	}
} else {
	if (defined $opt_c) {
	    if ($opt_c =~ /^\-/) {
		open(my $fh, '>', $dir.DESC) or die "Can't write COMMENT to DESC file: $!";
		print $fh $';
		close($fh);
	    } else {
		copy_subst($opt_c, '>', $dir.DESC);
	    }
	} else {
		Usage "Comment required" unless $regen_package;
	}

	if (defined $opt_d) {
	    if ($opt_d =~ /^\-/) {
		open(my $fh, '>>', $dir.DESC) or die "Can't write to DESC: $!";
		print $fh $';
		close($fh);
	    } else {
		copy_subst($opt_d, '>>', $dir.DESC);
	    }
	} else {
		Usage "Description required" unless $regen_package;
	}

	print "Creating package $ARGV[0]\n" if $opt_v && !$regen_package;

	if (defined $opt_i) {
	    copy_subst($opt_i, '>', $dir.INSTALL);
	}
		
	if (defined $opt_k) {
	    copy_subst($opt_k, '>', $dir.DEINSTALL);
	}

	if (defined $opt_r) {
	    copy_subst($opt_r, '>', $dir.REQUIRE);
	}

	if (defined $opt_M) {
	    copy_subst($opt_M, '>', $dir.DISPLAY);
	}

	if (defined $opt_m) {
	    copy_subst($opt_m, '>', $dir.MODULE);
	}
	if (defined $opt_U) {
	    copy_subst($opt_U, '>', $dir.UNDISPLAY);
	}

	for my $special (info_names()) {
	    next unless -f $dir.$special;
	    push(@extra_files, $special);
	    my $f = OpenBSD::PackingElement::File->add($plist, $special);
	    $f->{ignore} = 1;
	    $f->{md5} = OpenBSD::md5::fromfile($dir.$special);
	    $f->{size} = (stat $dir.$special)[7];
	}
}

if (defined $opt_p) {
	OpenBSD::PackingElement::Cwd->add($plist, $opt_p);
} elsif (!$regen_package) {
	Usage "Prefix required";
}

if (defined $opt_A) {
	OpenBSD::PackingElement::Arch->add($plist, $opt_A);
}

if (defined $opt_L) {
	OpenBSD::PackingElement::LocalBase->add($plist, $opt_L);
}

if ($regen_package) {
	my $v = 0;
	$plist->visit('anything', \$v);
	if ($v != 0 || @contents != 1) {
		Usage "Exactly one single packing list is required";
	}
	$dir = dirname($contents[0]);
}

for my $contentsfile (@contents) {
    $plist->fromfile($contentsfile,
	sub {
		my ($fh, $cont) = @_;
		local $_;
		my (@fhstack, @namestack);
		push(@fhstack, $fh);
		push(@namestack, $contentsfile);
		while($fh = pop @fhstack) {
			my $fname = pop @namestack;
			GETLINE:
			while (<$fh>) {
				if (m/^(\!)?\%\%(.*)\%\%$/) {
					my ($not, $frag) = ($1, $2);
					my $def = $frag;
					if ($frag eq 'SHARED') {
						$def = 'SHARED_LIBS';
						$frag = 'shared';
					}
					if (!defined $defines{$def}) {
						die "Error: unknown fragment $frag";
					} elsif ($defines{$def} == 1) {
						next GETLINE if defined $not;
					} elsif ($defines{$def} == 0) {
						next GETLINE unless defined $not;
					} else {
						die "Incorrect define for $frag";
					}
					my $newname = deduce_name($fname, $frag, $not);
					if (defined $newname) {
						push(@fhstack, $fh);
						push(@namestack, $fname);
						$fname = $newname;
						$fh = gensym;
						open($fh, '<', $fname) or die "missing file $fname";
					}
					next GETLINE;
				}
				if (m/^(\@comment\s+\$(?:Open)BSD\$)$/) {
					$_ = '@comment $'.'OpenBSD: '.basename($fname).',v$';
				}
				if (m,^\@lib\s+.*/lib[^/]+\.so\.\d+\.\d+$,) {
					Warn "Shared library without SHARED_LIBS: $_";
					$main::errors++;
				}
				&$cont(dosubst($_));
			}
		}
	}
    ) or Fatal "Can't open packing list $contentsfile";
}

if (!$plist->has('name') && $ARGV[0] =~ m|([^/]+)$|) {
	my $pkgname = $1;
	$pkgname =~ s/\.tgz$//;
	OpenBSD::PackingElement::Name->add($plist, $pkgname);
} 


my $base = '/';
if (defined $opt_B) {
	$base = $opt_B;
} elsif (defined $opt_S) {
	$base = $opt_S;
} elsif (defined $ENV{'PKG_PREFIX'}) {
	$base = $ENV{'PKG_PREFIX'};
}

my $fullpkgpath = $defines{'FULLPKGPATH'};
my $cdrom = $defines{'PERMIT_PACKAGE_CDROM'};
my $ftp = $defines{'PERMIT_PACKAGE_FTP'};
if (!defined $plist->{extrainfo} && 
    defined $fullpkgpath && defined $cdrom && defined $ftp) {
	$cdrom = 'yes' if $cdrom =~ m/^yes$/i;
	$ftp = 'yes' if $ftp =~ m/^yes$/i;

	OpenBSD::PackingElement::ExtraInfo->add($plist, 
	    $fullpkgpath, $cdrom, $ftp);
}

if ($plist->{need_modules}) {
	print STDERR "Error: some needed modules were not found\n";
	$errors++;
}
	

unless (defined $opt_q && defined $opt_n) {
	if ($regen_package) {
		$plist->checksum($base);
	} else {
		$plist->makesum($base);
	}
}
$plist->avert_duplicates();
if (defined $plist->{pkgcfl}) {
	print STDERR "Error: \@pkgcfl is obsolete, use \@conflict instead\n";
	$errors++;
}
if (defined $plist->{pkgdep}) {
	print STDERR "\@pkgdep is obsolete, use \@depend instead\n";
	$errors++;
}
if ($errors) {
	exit(1);
}

if (!defined $plist->{name}) {
	print STDERR "Can't write unnamed packing list\n";
	exit 1;
}

if (defined $opt_q) {
	if (defined $opt_Q) {
		$plist->visit('print_file');
	} else {
		$plist->write(\*STDOUT);
	}
	exit 0 if defined $opt_n;
}
my $wname;
if ($regen_package) {
	$wname = $plist->pkgname().".tgz";
} else {
	$plist->tofile($dir.CONTENTS) or Fatal "Can't write packing list";
	$wname = $ARGV[0];
}

unshift(@extra_files, CONTENTS);

if ($opt_n) {
	for my $special (@extra_files) {
		print "Adding $special\n" unless defined $opt_q;
	}
	$plist->archive(undef, $base, 1);
} else {
	print "Creating gzip'd tar ball in '$wname'\n" if $opt_v;
	open(my $fh, "|gzip >$wname");
	my $wrarc = OpenBSD::Ustar->new($fh, $dir);
	for my $special (@extra_files) {
		print "Adding $special\n" if $opt_v;
		my $o = $wrarc->prepare($special);
		$o->write();
	}

	$plist->archive($wrarc, $base, $opt_v);
	$wrarc->pad();
	close($fh);
}
} catch {
	print STDERR "$0: $_\n";
	exit(1);
};