[strip-nondeterminism] 01/01: Preserve file attribute information of target file

Chris Lamb chris at chris-lamb.co.uk
Tue Aug 30 14:20:42 UTC 2016


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

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

commit 4902feb268aa571401fe6b6418d4ff3ce3cb7cdc
Author: AYANOKOUZI, Ryuunosuke <i38w7i3 at yahoo.co.jp>
Date:   Tue Aug 30 09:00:00 2016 +0900

    Preserve file attribute information of target file
    
    Signed-off-by: Chris Lamb <lamby at debian.org>
---
 lib/File/StripNondeterminism/Common.pm             | 45 ++++++++++++++++++++++
 lib/File/StripNondeterminism/handlers/gzip.pm      |  6 ++-
 lib/File/StripNondeterminism/handlers/jar.pm       |  7 ++--
 lib/File/StripNondeterminism/handlers/javadoc.pm   |  7 ++--
 .../StripNondeterminism/handlers/javaproperties.pm |  7 ++--
 .../StripNondeterminism/handlers/pearregistry.pm   |  7 ++--
 lib/File/StripNondeterminism/handlers/png.pm       |  7 ++--
 t/fixtures.t                                       | 25 ++++++++++--
 8 files changed, 90 insertions(+), 21 deletions(-)

diff --git a/lib/File/StripNondeterminism/Common.pm b/lib/File/StripNondeterminism/Common.pm
new file mode 100644
index 0000000..91397be
--- /dev/null
+++ b/lib/File/StripNondeterminism/Common.pm
@@ -0,0 +1,45 @@
+#
+# Copyright 2016 Ryuunosuke Ayanokouzi <i38w7i3 at yahoo.co.jp>
+#
+# This file is part of strip-nondeterminism.
+#
+# strip-nondeterminism is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# strip-nondeterminism is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with strip-nondeterminism.  If not, see <http://www.gnu.org/licenses/>.
+#
+package File::StripNondeterminism::Common;
+
+require Exporter;
+ at ISA = qw(Exporter);
+ at EXPORT_OK = qw(copy_data);
+
+use strict;
+use warnings;
+
+use File::Copy;
+
+# Copy ONLY data from "$from_path" to "$to_path".
+# It means that...
+# 1. This function preserves as much of file "attributes"
+#    (information stored in "inode" in Unix-style file system term)
+#    of "$to_path" as possible.
+# 2. This function simulates file editing of "$to_path".
+# 3. After this function, "$to_path" would be a file associated with
+#    a copied "data" from "from_path" and a "inode" from "$to_path".
+sub copy_data {
+	my $from_path = shift;
+	my $to_path = shift;
+
+	return copy($from_path, $to_path);
+}
+
+1;
diff --git a/lib/File/StripNondeterminism/handlers/gzip.pm b/lib/File/StripNondeterminism/handlers/gzip.pm
index e0f370d..f59efd1 100644
--- a/lib/File/StripNondeterminism/handlers/gzip.pm
+++ b/lib/File/StripNondeterminism/handlers/gzip.pm
@@ -21,6 +21,7 @@ package File::StripNondeterminism::handlers::gzip;
 use strict;
 use warnings;
 
+use File::StripNondeterminism::Common qw(copy_data);
 use File::Temp;
 use File::Basename;
 
@@ -124,8 +125,9 @@ sub normalize {
 		last if $bytes_read == 0;
 	}
 
-	chmod((stat($fh))[2] & 07777, $tempfile->filename);
-	rename($tempfile->filename, $filename) or die "$filename: unable to overwrite: rename: $!";
+	$tempfile->close;
+	copy_data($tempfile->filename, $filename)
+		or die "$filename: unable to overwrite: copy_data: $!";
 	$tempfile->unlink_on_destroy(0);
 
 	return 1;
diff --git a/lib/File/StripNondeterminism/handlers/jar.pm b/lib/File/StripNondeterminism/handlers/jar.pm
index 8b3270d..f9263ad 100644
--- a/lib/File/StripNondeterminism/handlers/jar.pm
+++ b/lib/File/StripNondeterminism/handlers/jar.pm
@@ -21,6 +21,7 @@ package File::StripNondeterminism::handlers::jar;
 use strict;
 use warnings;
 
+use File::StripNondeterminism::Common qw(copy_data);
 use Archive::Zip;
 use File::Basename;
 use File::StripNondeterminism::handlers::zip;
@@ -57,9 +58,9 @@ sub _jar_normalize_manifest {
 	}
 
 	if ($modified) {
-		# Rename temporary file over the file
-		chmod((stat($fh))[2] & 07777, $tempfile->filename);
-		rename($tempfile->filename, $filename) or die "$filename: unable to overwrite: rename: $!";
+		$tempfile->close;
+		copy_data($tempfile->filename, $filename)
+			or die "$filename: unable to overwrite: copy_data: $!";
 		$tempfile->unlink_on_destroy(0);
 	}
 	return $modified;
diff --git a/lib/File/StripNondeterminism/handlers/javadoc.pm b/lib/File/StripNondeterminism/handlers/javadoc.pm
index 8b15ecf..90e00a4 100644
--- a/lib/File/StripNondeterminism/handlers/javadoc.pm
+++ b/lib/File/StripNondeterminism/handlers/javadoc.pm
@@ -22,6 +22,7 @@ package File::StripNondeterminism::handlers::javadoc;
 use strict;
 use warnings;
 
+use File::StripNondeterminism::Common qw(copy_data);
 use File::Temp;
 use File::Basename;
 use POSIX qw(strftime);
@@ -80,9 +81,9 @@ sub normalize {
 	}
 	defined($bytes_read) or die "$filename: read failed: $!";
 
-	# Rename temporary file over the file
-	chmod((stat($fh))[2] & 07777, $tempfile->filename);
-	rename($tempfile->filename, $filename) or die "$filename: unable to overwrite: rename: $!";
+	$tempfile->close;
+	copy_data($tempfile->filename, $filename)
+		or die "$filename: unable to overwrite: copy_data: $!";
 	$tempfile->unlink_on_destroy(0);
 
 	return 1;
diff --git a/lib/File/StripNondeterminism/handlers/javaproperties.pm b/lib/File/StripNondeterminism/handlers/javaproperties.pm
index b271830..f062003 100644
--- a/lib/File/StripNondeterminism/handlers/javaproperties.pm
+++ b/lib/File/StripNondeterminism/handlers/javaproperties.pm
@@ -22,6 +22,7 @@ package File::StripNondeterminism::handlers::javaproperties;
 use strict;
 use warnings;
 
+use File::StripNondeterminism::Common qw(copy_data);
 use File::Temp;
 use File::Basename;
 
@@ -63,9 +64,9 @@ sub normalize {
 			}
 			defined($bytes_read) or die "$filename: read failed: $!";
 
-			# Rename temporary file over the file
-			chmod((stat($fh))[2] & 07777, $tempfile->filename);
-			rename($tempfile->filename, $filename) or die "$filename: unable to overwrite: rename: $!";
+			$tempfile->close;
+			copy_data($tempfile->filename, $filename)
+				or die "$filename: unable to overwrite: copy_data: $!";
 			$tempfile->unlink_on_destroy(0);
 			return 1;
 		}
diff --git a/lib/File/StripNondeterminism/handlers/pearregistry.pm b/lib/File/StripNondeterminism/handlers/pearregistry.pm
index 22e4eb0..674c8f2 100644
--- a/lib/File/StripNondeterminism/handlers/pearregistry.pm
+++ b/lib/File/StripNondeterminism/handlers/pearregistry.pm
@@ -21,6 +21,7 @@ package File::StripNondeterminism::handlers::pearregistry;
 use strict;
 use warnings;
 
+use File::StripNondeterminism::Common qw(copy_data);
 use File::Temp;
 use File::Basename;
 
@@ -53,9 +54,9 @@ sub normalize {
 	}
 
 	if ($modified) {
-		chmod((stat($fh))[2] & 07777, $tempfile->filename);
-		rename($tempfile->filename, $filename)
-			or die "$filename: unable to overwrite: rename: $!";
+		$tempfile->close;
+		copy_data($tempfile->filename, $filename)
+			or die "$filename: unable to overwrite: copy_data: $!";
 		$tempfile->unlink_on_destroy(0);
 	}
 
diff --git a/lib/File/StripNondeterminism/handlers/png.pm b/lib/File/StripNondeterminism/handlers/png.pm
index d9954bd..8af0c53 100644
--- a/lib/File/StripNondeterminism/handlers/png.pm
+++ b/lib/File/StripNondeterminism/handlers/png.pm
@@ -22,6 +22,7 @@ package File::StripNondeterminism::handlers::png;
 use strict;
 use warnings;
 
+use File::StripNondeterminism::Common qw(copy_data);
 use File::Basename qw/dirname/;
 use POSIX qw/strftime/;
 use List::Util qw/min/;
@@ -55,9 +56,9 @@ sub normalize {
 	open(my $fh, '+<', $filename) or die "$filename: open: $!";
 
 	if (_normalize($filename, $fh, $tempfile)) {
-		chmod((stat($fh))[2] & 07777, $tempfile->filename);
-		rename($tempfile->filename, $filename)
-			or die "$filename: unable to overwrite: rename: $!";
+		$tempfile->close;
+		copy_data($tempfile->filename, $filename)
+			or die "$filename: unable to overwrite: copy_data: $!";
 		$tempfile->unlink_on_destroy(0);
 	}
 
diff --git a/t/fixtures.t b/t/fixtures.t
index 7412209..83112b0 100644
--- a/t/fixtures.t
+++ b/t/fixtures.t
@@ -19,6 +19,9 @@
 # along with strip-nondeterminism.  If not, see <http://www.gnu.org/licenses/>.
 #
 
+use strict;
+use warnings;
+
 use File::Basename qw(basename);
 use File::Compare qw(compare);
 use File::Copy qw(copy);
@@ -26,11 +29,10 @@ use File::Temp qw(tempdir);
 use File::StripNondeterminism;
 use Test::More;
 
-$temp = tempdir( CLEANUP => 1 );
+my $temp = tempdir( CLEANUP => 1 );
 
 my @fixtures = glob('t/fixtures/*/*.in');
 
-plan tests => scalar @fixtures;
 $File::StripNondeterminism::canonical_time = 1423159771;
 
 foreach my $filename (@fixtures) {
@@ -42,10 +44,25 @@ foreach my $filename (@fixtures) {
 	my $normalizer = File::StripNondeterminism::get_normalizer_for_file($in);
 
 	subtest $filename => sub {
-		plan tests => 2;
-
 		isnt(undef, $normalizer, "Normalizer found for $in");
+
+		my @stat_before = lstat $in;
 		$normalizer->($in) if defined $normalizer;
+		my @stat_after = lstat $in;
+		foreach (my $i = 0; $i < @stat_after; $i++) {
+			next if (
+				$i == 7 # size
+				|| $i == 8 # atime
+				|| $i == 9 # mtime
+				|| $i == 10 # ctime
+			);
+			is($stat_before[$i], $stat_after[$i], "$filename: stat[$i]");
+		}
+
 		ok(compare($in, $out) == 0, "Got expected output");
+
+		done_testing;
 	}
 }
+
+done_testing;

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



More information about the Reproducible-commits mailing list