File: pristine-bz2

package info (click to toggle)
pristine-tar 1.03
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 380 kB
  • ctags: 679
  • sloc: ansic: 4,116; perl: 1,071; makefile: 21
file content (395 lines) | stat: -rwxr-xr-x 8,925 bytes parent folder | download
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
#!/usr/bin/perl

=head1 NAME

pristine-bz2 - regenerate pristine bz2 files

=head1 SYNOPSIS

B<pristine-bz2> [-vdk] gendelta I<file.bz2> I<delta>

B<pristine-bz2> [-vdk] genbz2 I<delta> I<file>

=head1 DESCRIPTION

This is a complement to the pristine-tar(1) command. Normally you
don't need to run it by hand, since pristine-tar calls it as necessary
to handle .tar.bz2 files.

pristine-bz2 gendelta takes the specified I<bz2> file, and generates a
small binary I<delta> file that can later be used by pristine-bz2 genbz2
to recreate the original file.

pristine-bz2 genbz2 takes the specified I<delta> file, and compresses the
specified input I<file> (which must be identical to the contents of the
original bz2 file). The resulting file will be identical to
the original gz file used to create the delta.

The approach used to regenerate the original bz2 file is to figure out
how it was produced -- what compression level was used, whether it was
built with bzip2(1) or with pbzip2(1).

Note that other tools exist, like bzip2smp or dbzip2, but they are
said to be bit-identical with bzip2. Anyway, bzip2 looks like the most
widespread implementation, so it's hard to find bzip2 files that make
pristine-bz2 fail. Please report!

The deprecated bzip1 compression method hasn't been implemented.

If the delta filename is "-", pristine-bz2 reads or writes it to stdio.

=head1 OPTIONS

=over 4

=item -v

Verbose mode, show each command that is run.

=item -d

Debug mode.

=item -k

Don't clean up the temporary directory on exit.

=item -t

Try harder to determine how to generate deltas of difficult bz2 files.

=back

=head1 ENVIRONMENT

=over 4

=item B<TMPDIR>

Specifies a location to place temporary files, other than the default.

=back

=head1 AUTHOR

Joey Hess <joeyh@debian.org>,
Faidon Liambotis <paravoid@debian.org>,
Cyril Brulebois <cyril.brulebois@enst-bretagne.fr>

Licensed under the GPL, version 2.

=cut

use warnings;
use strict;
use File::Temp;
use Getopt::Long;
use File::Basename qw/basename/;
use IPC::Open2;
use IO::Handle;

use constant BZIP2_DEBUG	 => 1;

# magic identification
use constant BZIP2_ID1		 => 0x42;
use constant BZIP2_ID2		 => 0x5a;

# compression methods, 'h' for Bzip2 ('H'uffman coding), '0' for Bzip1 (deprecated)
use constant BZIP2_METHOD_HUFFMAN => 0x68;

my @supported_bzip2_programs = qw(bzip2 pbzip2 zgz);

my $verbose=0;
my $debug=0;
my $keep=0;
my $try=0;

sub usage {
	print STDERR "Usage: pristine-bz2 [-vdkt] gendelta file.bz2 delta\n";
	print STDERR "       pristine-bz2 [-vdkt] genbz2 delta file\n";
}

sub debug {
	print STDERR "debug: @_\n" if $debug;
}

sub vprint {
	print STDERR "pristine-bz2: @_\n" if $verbose;
}

sub doit {
	vprint(@_);
	if (system(@_) != 0) {
		die "command failed: @_\n";
	}
}

sub doit_redir {
	no warnings 'once';
	my ($in, $out, @args) = @_;
	vprint(@args, "<", $in, ">", $out);
	open INFILE, "<", $in or die("Could not open '$in' for reading: $!\n");
	open OUTFILE, ">", $out or die("Could not open '$out' for reading: $!\n");
	my $pid = open2(">&OUTFILE", "<&INFILE", @args);
	waitpid $pid, 0;
}

sub tempdir {
	return File::Temp::tempdir("pristine-bz2.XXXXXXXXXX",
		TMPDIR => 1, CLEANUP => !$keep);
}

sub readbzip2 {
	my $filename = shift;
	my $chars;

	open(BZIP2, "< $filename")
		or die("Could not open '$filename' for reading: $!\n");

	if (read(BZIP2, $chars, 4) != 4) {
		die("Unable to read from input\n");
	}

	my ($id1, $id2, $method, $level)
		= unpack("CCCC", $chars);
	# we actually want the value, not the ascii position
	$level-=48;

	if ($id1 != BZIP2_ID1 || $id2 != BZIP2_ID2 || $method != BZIP2_METHOD_HUFFMAN || $level !~ /^[1-9]$/) {
		die("This is not a valid BZip2 archive.\n");
	}

	close(BZIP2);

	return ($level);
}

sub predictbzip2args {
	my ($level, $program) = @_;

	my @args;
	push @args, "-$level";

	if ($program eq 'zgz') {
		push @args, '--old-bzip2';
	}

	return @args;
}

sub comparefiles {
	my ($old, $new) = (shift, shift);
	system('cmp', '-s', $old, $new);

	if ($? == -1 || $? & 127) {
		die("Failed to execute cmp: $!\n");
	}

	return $? >> 8;
}

sub testvariant {
	my ($old, $tmpin, $bzip2_program, @args) = @_;

	# some compressors eat the uncompressed file, some
	# do not; restore as needed. (Note that file name,
	# mode, mtime do not matter to bzip2.)
	if (! -e $tmpin) {
		doit("cp", "$tmpin.bak", "$tmpin");
	}

	my $new=$tmpin.'.bz2';
	unlink($new);

	# try bzip2'ing with the arguments passed
	if ($bzip2_program ne 'zgz') {
		doit($bzip2_program, @args, $tmpin);
	}
	else {
		doit_redir($tmpin, $new, $bzip2_program, @args);
	}
	unless (-e $new) {
		die("$bzip2_program failed, aborting");
	}

	# and compare the generated with the original
	return !comparefiles($old, $new);
}

sub reproducebzip2 {
	my ($wd, $orig) = (shift, shift);
	
	my $tmpin="$wd/test";
	doit_redir($orig, "$tmpin.bak", "bzip2", "-dc");

	# read fields from bzip2 headers
	my ($level) = readbzip2($orig);
	debug("level: $level");

	foreach my $program (@supported_bzip2_programs) {
		# try to guess the bzip2 arguments that are needed by the
		# header information
		my @args = predictbzip2args($level, $program);

		testvariant($orig, $tmpin, $program, @args)
			&& return $program, @args;
	}

	# 7z has a weird syntax, not supported yet, as not seen in the wild
	#testvariant($orig, $tmpin, "7z", "-mx$level", "a", "$tmpin.bz2")
	#	&& return "7z", "-mx$level", "a" ; # XXX need to include outfile

	# pbzip2 -b option affects output, but cannot be detected from a 
	# header.
	if ($try) {
		my @args = predictbzip2args($level, "pbzip2");
		print STDERR "pristine-bz2 will have to try especially hard to reproduce $orig\n";
		print STDERR "(This could take a long time.)\n";
		my %tried;
		$tried{9}=1; # default
 		# Try searching for likely candidates first, and fill in.
		# It could go higher than 100, but have to stop somewhere.
		STDERR->autoflush(1);
		foreach my $try (1..10, 
		                 15, 20, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95,
				 1..100) {
			next if $tried{$try};
			$tried{$try}=1;
			print STDERR "\r\tblock size: $try   ";
			testvariant($orig, $tmpin, "pbzip2", "-b${try}", @args) &&
				return "pbzip2", "-b${try}", @args;
		}
		print STDERR "\n";
	}

	print STDERR "pristine-bz2 failed to reproduce build of $orig\n";
	print STDERR "(Please file a bug report.)\n";
	exit 1;
}

sub genbz2 {
	my $delta=shift;
	my $file=shift;

	my $tempdir=tempdir();

	if ($delta eq "-") {
		$delta="$tempdir/in";
		open (OUT, ">", $delta) || die "$delta: $!";
		while (<STDIN>) {
			print OUT $_;
		}
		close OUT;
	}

	doit("tar", "xf", File::Spec->rel2abs($delta), "-C", $tempdir);
	if (! -e "$tempdir/type") {
		die "failed to genbz2 delta $delta\n";
	}

	open (IN, "$tempdir/version") || die "delta lacks version number ($!)";
	my $version=<IN>;
	if ($version >= 3) {
		die "delta is version $version, not supported\n";
	}
	close IN;
	if (open (IN, "$tempdir/type")) {
		my $type=<IN>;
		chomp $type;
		if ($type ne "bz2") {
			die "delta is for a $type, not a bz2\n";
		}
		close IN;
	}

	open (IN, "$tempdir/params") || die "delta lacks params file ($!)";
	my $params=<IN>;
	chomp $params;
	my @params=split(' ', $params);
	while (@params) {
		$_=shift @params;
		next if /^(-[1-9])$/;
		next if $_ eq '--old-bzip2';
		die "paranoia check failed on params file from delta ($params)";
	}
	@params=split(' ', $params);
	close IN;

	open (IN, "$tempdir/program") || die "delta lacks program file ($!)";
	my $program=<IN>;
	chomp $program;
	if (! grep { $program eq $_ } @supported_bzip2_programs) {
		die "paranoia check failed on program file from delta ($program)";
	}
	close IN;

	if ($program eq 'zgz') {
		# unlike bzip2, zgz only uses sdio
		doit_redir($file, "$file.bz2", $program, @params);
	}
	else {
		doit($program, @params, $file);
	}
	doit("rm", "-f", $file);
}

sub gendelta {
	my $bzip2file=shift;
	my $delta=shift;

	my $tempdir=tempdir();

	my $stdout=0;
	if ($delta eq "-") {
		$stdout=1;
		$delta="$tempdir/out";
	}

	my @files=qw(version type params program);

	my ($program, @params)=
		reproducebzip2($tempdir, $bzip2file);

	open(OUT, ">", "$tempdir/version") || die "$!";
	print OUT "2.0\n";
	close OUT;
	open(OUT, ">", "$tempdir/type") || die "$!";
	print OUT "bz2\n";
	close OUT;
	open(OUT, ">", "$tempdir/params") || die "$!";
	print OUT "@params\n";
	close OUT;
	open(OUT, ">", "$tempdir/program") || die "$!";
	print OUT "$program\n";
	close OUT;

	doit("tar", "czf", $delta, "-C", $tempdir, @files);

	if ($stdout) {
		doit("cat", $delta);
	}
}

Getopt::Long::Configure("bundling");
if (! GetOptions(
	"v|verbose!" => \$verbose,
	"d|debug!" => \$debug,
	"k|keep!" => \$keep,
	"t|try!" => \$try,
   ) || @ARGV != 3) {
	usage();
	exit 1;
}

my $command=shift;
if ($command eq 'genbz2') {
	genbz2(@ARGV);
}
elsif ($command eq 'gendelta') {
	gendelta(@ARGV);
}
else {
	print STDERR "Unknown subcommand \"$command\"\n";
	usage();
	exit 1;
}