This program reads a list of files from the directory specified on the command line, creates an array of hashes, where each file has key path, size, and sha256sum.
I am trying to create a gzipped tarball of the files, where each files is name the checksum appended with the file's original extension. I create a gzipped tarball of the files successfully. However, when I try to use Archive::Tar's rename method, I am met with this error:
No such file in archive: '/path/to/file1.txt' at ./program.pl line 62. This error repeats for each file in the archive.
Is it because the archive is just a flat list of files? If so, how does one use the rename method?
use strict;
use warnings;
use Data::Dumper qw(Dumper);
use File::Spec qw(catfile rel2abs);
use Digest::SHA qw(sha256_hex);
use Archive::Tar;
use Archive::Tar::File;
my $dir = $ARGV[0];
my $url = $ARGV[1];
my @AoH;
my @checksumfiles;
my $tar = Archive::Tar->new;
my $archive = "archive.tar.gz";
opendir DIR, $dir or die "cannot open dir $dir: $!\n";
chdir $dir or die "cannot navigate to dir $dir: $!\n";
while(my $file = readdir DIR) {
next unless(-f File::Spec->catfile($dir, $file));
next if($file =~ m/^\./);
my $fullpath = File::Spec->rel2abs($file);
my $fullsize = -s File::Spec->catfile($dir, $file);
my $fullid = sha256_hex($fullpath);
my %hash = (
path => $fullpath,
size => $fullsize,
id => $fullid,
);
push(@AoH, \%hash);
}
my @array;
for my $i(0..$#AoH) {
no warnings 'uninitialized';
my ($ext) = $AoH[$i]{path} =~ (/(\.[^.]+)$/);
my $idext = $AoH[$i]{id} . $ext;
push(@checksumfiles, $idext);
push(@array, $AoH[$i]{path});
}
Archive::Tar->create_archive($archive, COMPRESS_GZIP, @array);
#print Archive::Tar->list_archive($archive, COMPRESS_GZIP), "\n";
for my $i(0..$#array) {
$tar->rename($array[$i], $checksumfiles[$i]);
}
print Dumper sort \@array;
print Dumper sort \@checksumfiles;
#print Dumper sort \@AoH;