[Reproducible-builds] [strip-nondeterminism] 05/06: Add helpers to Zip handler for normalizing members of a Zip archive

Andrew Ayer agwa at andrewayer.name
Sun Sep 14 05:14:42 UTC 2014


This is an automated email from the git hooks/post-receive script.

agwa-guest pushed a commit to branch master
in repository strip-nondeterminism.

commit 3127d5d89956ceaca1573fd3d32903add6d41daa
Author: Andrew Ayer <agwa at andrewayer.name>
Date:   Sat Sep 13 21:29:17 2014 -0700

    Add helpers to Zip handler for normalizing members of a Zip archive
    
    It will be used by Jar handler for normalizing Javadoc files inside
    the Jar.
---
 lib/StripNondeterminism/handlers/zip.pm | 35 ++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/lib/StripNondeterminism/handlers/zip.pm b/lib/StripNondeterminism/handlers/zip.pm
index 027c70c..faf5a94 100644
--- a/lib/StripNondeterminism/handlers/zip.pm
+++ b/lib/StripNondeterminism/handlers/zip.pm
@@ -21,7 +21,8 @@ package StripNondeterminism::handlers::zip;
 use strict;
 use warnings;
 
-use Archive::Zip;
+use File::Temp;
+use Archive::Zip qw/:CONSTANTS :ERROR_CODES/;
 
 # A magic number from Archive::Zip for the earliest timestamp that
 # can be represented by a Zip file.  From the Archive::Zip source:
@@ -29,6 +30,38 @@ use Archive::Zip;
 # minute so that nothing timezoney can muck us up."
 use constant SAFE_EPOCH => 315576060;
 
+# Extract and return the first $nbytes of $member (an Archive::Zip::Member)
+sub peek_member {
+	my ($member, $nbytes) = @_;
+	my $old_compression_method = $member->desiredCompressionMethod(COMPRESSION_STORED);
+	$member->rewindData() == AZ_OK or die "failed to rewind ZIP member";
+	my ($buffer, $status) = $member->readChunk($nbytes);
+	die "failed to read ZIP member" if $status != AZ_OK && $status != AZ_STREAM_END;
+	$member->endRead();
+	$member->desiredCompressionMethod($old_compression_method);
+	return $$buffer;
+}
+
+# Normalize the contents of $member (an Archive::Zip::Member) with $normalizer
+sub normalize_member {
+	my ($member, $normalizer) = @_;
+
+	# Extract the member to a temporary file.
+	my $tempdir = File::Temp->newdir();
+	my $filename = "$tempdir/member";
+	$member->extractToFileNamed($filename);
+
+	# Normalize the temporary file.
+	if ($normalizer->($filename)) {
+		# Normalizer modified the temporary file.
+		# Replace the member's contents with the temporary file's contents.
+		open(my $fh, '<', $filename) or die "Unable to open $filename: $!";
+		$member->contents(do { local $/; <$fh> });
+	}
+
+	unlink($filename);
+}
+
 sub normalize {
 	my ($zip_filename, $filename_cmp) = @_;
 	$filename_cmp ||= sub { $a cmp $b };

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/strip-nondeterminism.git



More information about the Reproducible-builds mailing list