It actually took me a while to work this out -- very educational, and I'm glad to have learned it. A simple, bone-head example that should show the way (tested on linux, with perl 5.8.0):
#!/usr/bin/perl
use strict;
use Archive::Tar;
# begin by creating a test directory to tar up:
BEGIN {
mkdir "test_dir",0755;
for my $i ( 1 .. 3 ) {
open( TEST, ">test_dir/test$i.txt" );
print TEST "This is test data file number $i\n";
close TEST;
symlink "test$i.txt", "test_dir/test$i.lnk" unless ( $i % 2 );
}
}
my $tar = Archive::Tar->new();
opendir( DIR, "test_dir" ) or die $!;
my @files = grep /\w/, readdir DIR;
foreach my $f ( @files )
{
next unless ( $f =~ /test2/ ); # just for the sake of being select
+ive
# UPDATE: this doesn't actually work as hoped for:
# if ( -l "test_dir/$f" )
# { # it's a symlink!!
# my $targ_path = readlink "test_dir/$f";
# my %opt_hash = ( linkname => $targ_path );
# $tar->add_data( "test_dir/$f", '', \%opt_hash ) or
# die "failed to add $f: $!\n";
# }
# else
# { # it's just a plain file
#
# BUT: this actually does what the OP wants, and seems to
# disagree with heezy's findings:
$tar->add_files( "test_dir/$f" ) or die "failed to add $f: $!\n";
# }
}
$tar->write( "test_dir.tar", 0 ) or die "failed to write tar file: $!\
+n";
# let's end with a report of how we did
END {
my $report = `tar tvf test_dir.tar`;
print $report;
}
For me, though, the most typical case is having to make sure that I don't create tar sets that contain symlinks whose targets happen not to be contained elsewhere in the same tar set... But I presume you have that issue covered in your case.
comment on updated code: My initial post had a dumb mistake, doing (-l $f) instead of (-l "test_dir/$f") in the portion that was commented out by the update -- that is, after it produced the desired result, I looked closer and realized that it shouldn't have worked. It wasn't till I took out all the conditional stuff and just used the "add_files()" method on each iteration, that I got the desired result again. Seems like that stuff in the Archive::Tar man page about the "linkname" option for "add_data()" is just bunk. | [reply] [d/l] [select] |