summaryrefslogtreecommitdiff
path: root/usr.sbin/pkg_add/pkg_merge
blob: 9b71291b599b99ff9220e1854ffb9b920e68766a (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
#! /usr/bin/perl
# Copyright (c) 2005 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 OpenBSD::PackageLocator;
use OpenBSD::PackageInfo;
use OpenBSD::PackingList;
use OpenBSD::Getopt;
use OpenBSD::Error;
use OpenBSD::Ustar;
use OpenBSD::ArcCheck;
use File::Copy;
use File::Path;

package OpenBSD::PackingElement;
sub copy_over {}

sub mark_tocopy {}

sub make_alias {}
package OpenBSD::PackingElement::FileBase;
sub mark_tocopy
{
	my ($self, $list) = @_;
	push(@$list, $self);
}

sub copy_over
{
	my ($self, $wrarc, $prefix, $pkg) = @_;
	my $e = $pkg->{pkg}->next();
	if (!$e->check_name($self->{name})) {
		die "Names don't match: ", $e->{name}, " ", $self->{name};
	}
	$e->{name} = $prefix."/".$e->{name};
	$e->copy_long($wrarc);
}

sub make_alias
{
	my ($self, $wrarc, $prefix, $pkg, $alias) = @_;
	my $e = $pkg->{pkg}->next();
	if (!$e->check_name($self->{name})) {
		die "Names don't match: ", $e->{name}, " ", $self->{name};
	}
	$e->{name} = $prefix."/".$e->{name};
	$e->alias($wrarc, "$prefix/$alias");
}

package OpenBSD::PackingElement::SpecialFile;
use File::Copy;

sub mark_tocopy
{
	my ($self, $list) = @_;
	push(@$list, $self);
}
sub copy_over
{
	my ($self, $wrarc, $prefix, $pkg) = @_;
	if (defined $wrarc) {
		$wrarc->destdir($pkg->{dir});
		my $e = $wrarc->prepare($self->{name});
		$e->{name} = "$prefix/".$e->{name};
		$e->write();
	}
}


package main;

sub find_equal
{
	my $list = shift;
	my $name = $list->[0]->{tocopy}->[0]->{name};
	for my $pkg (@$list) {
		if ($pkg->{tocopy}->[0]->{name} ne $name) {
			return undef;
		}
	}
	return $name;
}

sub occurs_first_or_not
{
	my ($name, $list) = @_;
	for my $pkg (@$list) {
		if ($pkg->{tocopy}->[0]->{name} eq $name) {
			next;
		}
		for my $i (@{$pkg->{tocopy}}) {
			if ($i->{name} eq $name) {
				return 0;
			}
		}
	}
	return 1;
}

sub occurs_first
{
	my $list= shift;

	for my $pkg (@$list) {
		my $name = $pkg->{tocopy}->[0]->{name};
		if (occurs_first_or_not($name, $list)) {
			return $name;
		}
	}
	return undef;
}


set_usage('pkg_merge [-v] -o result pkg pkg2 ...');

our ($opt_o, $opt_v);

try {
	getopts('o:v');
} catchall {
	Usage($_);
};

if (!defined $opt_o) {
	Usage "Missing -o result";
}

if (@ARGV<2) {
	Usage "Can't merge less than two packages";
}
my @tomerge;

my $prefix = 'a';
my $allprefix = '';
open( my $outfh, "|gzip -c >$opt_o");

my $wrarc = OpenBSD::Ustar->new($outfh, ".");
for my $pkgname (@ARGV) {
	my $true_package = OpenBSD::PackageLocator->find($pkgname);
	die "No such package $pkgname" unless $true_package;
	my $dir = $true_package->info();
	my $plist = OpenBSD::PackingList->fromfile($dir.CONTENTS);

	my $in = {
			plist => $plist,
			dir => $dir,
			prefix => $prefix,
			tocopy => [],
			pkg => $true_package
		};
	my $e = OpenBSD::PackingElement::FCONTENTS->new(CONTENTS);
	$e->copy_over($wrarc, $prefix, $true_package);
	$plist->visit('mark_tocopy', $in->{tocopy});
	push(@tomerge, $in);
	$prefix++;
}

my $total_files = 0;
my $total_size = 0;

# For now, we assume packing-lists contain the same items.
while(1) {
	# kill empty lists
	my @n = ();
	for my $pkg (@tomerge) {
		if (@{$pkg->{tocopy}} > 0) {
			push(@n, $pkg);
		}
	}
	@tomerge = @n;
	if (@tomerge == 0) {
		last;
	}
	# determine which item we want to copy (by name)
	my $name;

	# easiest case: same name all around.
	$name = find_equal(\@tomerge);

	# second case: a name that occurs first in some lists,
	# and not in the others
	if (!defined $name) {
		$name = occurs_first(\@tomerge);
	}

	# else, try random
	if (!defined $name) {
		$name = $tomerge[0]->{tocopy}->[0]->{name};
	}

	my $allprefix='';
	my $ref;
	my @mergeable = ();
	# select the mergeable items
	for my $pkg (@tomerge) {
		if ($pkg->{tocopy}->[0]->{name} eq $name) {
			push(@mergeable, $pkg);
		}
	}

	my $all_copies = 0;
	while (@mergeable > 0) {
		my $pkg = shift @mergeable;
		my $ref = shift @{$pkg->{tocopy}};
		my $copies = 0;
		my $currentprefix = $pkg->{prefix};
		my @todo = ();
		my @merged = ();
		for my $cmp (@mergeable) {
		    if (defined $ref->{md5} && $cmp->{tocopy}->[0]->{md5} eq $ref->{md5}) {
		    	push(@merged, $cmp);
			$currentprefix .= $cmp->{prefix};
			$copies++;
		    } else {
			    push(@todo, $cmp);
		    }
		}
		$total_files += $copies;
		$total_size += $ref->{size} * $copies;
		$ref->copy_over($wrarc, $currentprefix, $pkg);
		@mergeable = @todo;
		$all_copies += $copies;
		for my $pkg2 (@merged) {
			my $i = shift @{$pkg2->{tocopy}};
			$i->make_alias($wrarc, $currentprefix, $pkg2, $name);
		}
	}
	if ($opt_v) {
		if ($all_copies) {
			print "$name (shared: $all_copies)\n";
		} else {
			print $name, "\n";
		}
	}
}
$wrarc->close();
print "Shared $total_files files for $total_size bytes\n" if $opt_v;