Archive::Tar && File::Find

  • Thread starter bernd.fischer-krellenberg
  • Start date
B

bernd.fischer-krellenberg

I have an issue with File::Find and Archive::Tar
while trying to create a recursive tar file over
a list of files and directories.
Any time when find returns a directory without file
aDir/bDir
it has a problem to add this or to write into the tar
archive

Use of uninitialized value in pack at
/usr/lib/perl5/vendor_perl/5.8.5/Archive/Tar.pm line 1084.


#!/usr/bin/perl -w

use strict;
use File::Find;
use Archive::Tar;

my(
@sources,
$source,
$tar,
@files
);

@sources = ( "aFile", "aDir", "bFile", "cFile", "bDir" )

$tar = Archive::Tar->new( );

foreach $source ( @sources ) {
if( -d $source || -f $source ) {
find( sub { push @files, $File::Find::name }, $source );
}
}

$tar->Archive::Tar->create_archive( 'foo.tar', 1, @files );
$tar->add_files( @files ) or die "$!\n";
$tar->write( 'foo.tar' ) or die "$!\n";


Getting problems with this when find comes to a pure directory

aFile
aDir <- problem
aDir/aFile <- no problem
aDir/bDir <- problem
aDir/bDir/cFile <- no problem


Thanks for your inputs?
 
A

A. Sinan Unur

(e-mail address removed) wrote in (e-mail address removed):
I have an issue with File::Find and Archive::Tar
while trying to create a recursive tar file over
a list of files and directories.
Any time when find returns a directory without file
aDir/bDir
it has a problem to add this or to write into the tar
archive

Use of uninitialized value in pack at
/usr/lib/perl5/vendor_perl/5.8.5/Archive/Tar.pm line 1084.
#!/usr/bin/perl -w

use strict;
use File::Find;
use Archive::Tar;

my(
@sources,
$source,
$tar,
@files
);

No reason to declare a bunch of variables like this.
@sources = ( "aFile", "aDir", "bFile", "cFile", "bDir" )

Please post code that actually compiles and runs.

Also:

my @sources = qw( aFile aDir bFile cFile bDir );

is easier to read.
$tar = Archive::Tar->new( );

foreach $source ( @sources ) {
if( -d $source || -f $source ) {

This is weird. @source contains files without path information. So, any
files that are not just in the current working directory will not pass
this test.
find( sub { push @files, $File::Find::name }, $source );

Wouldn't it be better to use a single wanted subroutine that adds files
based on a criterion? With this, you call find on aDir and then bDir
when bDir (according to the structure you gave below) is a subdirectory
of aDir and would have been found using the find call on aDir.
$tar->Archive::Tar->create_archive( 'foo.tar', 1, @files );
$tar->add_files( @files ) or die "$!\n";
$tar->write( 'foo.tar' ) or die "$!\n";

Have you read the documentation? I had not until now and according to
how I read it the create_archive call is not necessary. Also,
Archive::Tar seems to return error strings in $tar->error rather than
$!.
aFile
aDir <- problem
aDir/aFile <- no problem
aDir/bDir <- problem
aDir/bDir/cFile <- no problem

Thanks for your inputs?

Why the question mark? Aren't you sure you want to thank us for our
input?

Here is a revised version of your script:

C:\Temp\test> cat t.pl
#!/usr/bin/perl

use strict;
use warnings;

use File::Basename;
use File::Find;
use Archive::Tar;

my %sources = map { $_ => 1 } qw( aFile aDir bFile cFile bDir );

my $tar = Archive::Tar->new;

find( {wanted => \&wanted, no_chdir => 1 }, '.' );

$tar->write( 'foo.tar' ) or die $tar->error;

sub wanted {
-f or -d _ or return;

exists $sources{ fileparse $_ } or return;
warn "$_\n";

$tar->add_files( $_ ) or die $tar->error;
return;
}

__END__

C:\Temp\test> t
../aDir
../aDir/aFile
../aDir/bFile
../aDir/bDir
../aDir/bDir/cFile

C:\Temp\test> tar -tf foo.tar
tar: Record size = 7 blocks
../aDir
aDir/aFile
aDir/bFile
aDir/bDir
aDir/bDir/cFile

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top