Created
May 26, 2013 03:23
-
-
Save briandfoy/5651608 to your computer and use it in GitHub Desktop.
Collect the file digests from the Perl files in @inc of the invoking Perl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Digest::SHA; | |
use Digest::MD5; | |
use File::Find; | |
use File::Spec::Functions qw(no_upwards canonpath); | |
DIR: foreach my $dir ( @INC ) { | |
next if $dir =~ /\A\.\.?\Z/; | |
my( $wanted, $files ) = find_regular_files(); | |
find( $wanted, $dir ); | |
print STDERR "Found " . @$files . " files in $dir\n"; | |
FILE: foreach my $file ( @$files ) { | |
next unless $file =~ m|\.pm\z|; | |
next if $file =~ m|/\.packlist\z|; | |
my $rel = $file =~ s|\A$dir/||r; | |
my $sha = digest_sha( $file ); | |
my $md5 = digest_md5( $file ); | |
print join "|", $rel, $sha, $md5; | |
print "\n"; | |
} | |
} | |
sub digest_sha { digest_file( $_[0], 'Digest::SHA', 256 ) } | |
sub digest_md5 { digest_file( $_[0], 'Digest::MD5' ) } | |
sub digest_file { | |
my( $file, $class, @args ) = @_; | |
open my $fh, '<', $file or return; | |
my $sha256 = $class->new( @args )->addfile($fh)->hexdigest; | |
} | |
sub find_regular_files { | |
my @files = (); | |
sub { push @files, no_upwards( $File::Find::name ) if -f $_ }, | |
\@files | |
} | |
=encoding utf8 | |
=head1 NAME | |
get_file_digests - get the SHA-256 and MD5 digests for all files | |
=head1 SYNOPSIS | |
% perl5.10.1 -le 'print for @INC' | xargs perl5.14.2 get_file_digests | |
=head1 DESCRIPTION | |
This program is a first step in reverse engineering a Perl installation. | |
The rest of the MyCPAN tools can use the output to identify the files | |
and map them to the distributions they came from. | |
=head1 AUTHOR | |
<[email protected]> | |
=head1 COPYRIGHT & LICENSE | |
Copyright 2013, brian d foy | |
You can use this program under the same terms as Perl itself. | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment