Commit 04ea9b8a authored by Chris Lamb's avatar Chris Lamb 💬

Merge tag '0.024' into debian

Release 0.024

* tag '0.024':
  Release 0.024
  .gitignore: Add /cover_db/
  zip.pm: Don't mix high and low-precedence booleans.
  zip.pm: Use block form of grep over expression form.
  handlers/{jar,png,zip}: Ensure subroutines end with return statement.
  jar.pm: Drop unnecessary subroutine prototypes.
  handlers/{ar,zip}.pm: Explicitly use oct(..) for integers with leading zeroes.
  Preserve file attribute information of target file
parents e4d8497a cb2bc459
......@@ -5,6 +5,7 @@
/pm_to_blib
/MYMETA.yml
/MYMETA.json
/cover_db/
# Temp and old files.
*~
......
......@@ -33,7 +33,7 @@ use File::StripNondeterminism::handlers::zip;
our($VERSION, $canonical_time, $clamp_time);
$VERSION = '0.023'; # 0.023
$VERSION = '0.024'; # 0.024
sub _get_file_type {
my $file=shift;
......
#
# Copyright 2016 Ryuunosuke Ayanokouzi <i38w7i3@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;
@ISA = qw(Exporter);
@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;
......@@ -74,7 +74,7 @@ sub normalize {
# group
syswrite $fh, sprintf("%-6d", 0);
# file mode
syswrite $fh, sprintf("%-8o", ($file_mode & 0100) ? 0755 : 0644);
syswrite $fh, sprintf("%-8o", ($file_mode & oct(100)) ? oct(755) : oct(644));
# move to next member
my $padding = $file_size % 2;
......
......@@ -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;
......
......@@ -21,13 +21,14 @@ 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;
use File::StripNondeterminism::handlers::javadoc;
use File::StripNondeterminism::handlers::javaproperties;
sub _jar_filename_cmp ($$) {
sub _jar_filename_cmp {
my ($a, $b) = @_;
# META-INF/ and META-INF/MANIFEST.MF are expected to be the first entries in the Zip archive.
return 0 if $a eq $b;
......@@ -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;
......@@ -86,6 +87,8 @@ sub _jar_normalize_member {
} elsif ($member->fileName() =~ /\.jar$/) {
File::StripNondeterminism::handlers::zip::normalize_member($member, \&normalize);
}
return 1;
}
sub _jar_archive_filter {
......
......@@ -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;
......
......@@ -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;
}
......
......@@ -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);
}
......
......@@ -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,13 +56,15 @@ 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);
}
close $fh;
return 1;
}
sub _normalize {
......
......@@ -64,6 +64,8 @@ sub normalize_member {
}
unlink($filename);
return 1;
}
use constant {
......@@ -131,7 +133,7 @@ sub normalize {
my $zip = Archive::Zip->new();
my @errors;
if (try(sub { $zip->read($zip_filename) }, \@errors) != AZ_OK) {
if (grep /zip64 not supported/, @errors) {
if (grep { /zip64 not supported/ } @errors) {
# Ignore zip64 files, which aren't supported by Archive::Zip.
# Ignoring unsupported files, instead of erroring out, is
# consistent with the rest of strip-nondeterminism's behavior,
......@@ -144,7 +146,7 @@ sub normalize {
die "Reading ZIP archive failed: " . join("\n", @errors);
}
}
if (exists($options{archive_filter}) && not($options{archive_filter}->($zip))) {
if (exists($options{archive_filter}) and not($options{archive_filter}->($zip))) {
return 0;
}
my @filenames = sort $filename_cmp $zip->memberNames();
......@@ -154,12 +156,12 @@ sub normalize {
$options{member_normalizer}->($member) if exists $options{member_normalizer};
$member->setLastModFileDateTimeFromUnix($File::StripNondeterminism::canonical_time // SAFE_EPOCH);
if ($member->fileAttributeFormat() == FA_UNIX) {
$member->unixFileAttributes(($member->unixFileAttributes() & 0100) ? 0755 : 0644);
$member->unixFileAttributes(($member->unixFileAttributes() & oct(100)) ? oct(755) : oct(644));
}
$member->cdExtraField(normalize_extra_fields($member->cdExtraField(), CENTRAL_HEADER));
$member->localExtraField(normalize_extra_fields($member->localExtraField(), LOCAL_HEADER));
}
my $old_perms = (stat($zip_filename))[2] & 07777;
my $old_perms = (stat($zip_filename))[2] & oct(7777);
$zip->overwrite();
chmod($old_perms, $zip_filename);
return 1;
......
......@@ -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;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment