our
$VERSION
=
'0.06'
;
our
@EXPORT
=
qw(
&whereis
&find_a_patch
&find_unzip &do_unzip
&find_untargz &do_untargz
&manify_path
&get_dir &get_file &put_file
&rmtree &mkpath
)
;
splitdir splitpath catpath)
;
sub
manify_path($) {
my
$path
=
shift
or
return
;
$path
= File::Spec->canonpath(
$path
);
my
(
undef
,
$dirs
,
$file
) = File::Spec->splitpath(
$path
);
my
@subdirs
=
grep
$_
&&
length
$_
=> File::Spec->splitdir(
$dirs
);
$^O eq
'VMS'
and
$file
=~ s/\.$//;
push
@subdirs
,
$file
;
return
join
'/'
,
@subdirs
;
}
sub
get_dir($) {
my
(
$path
) =
@_
;
my
$cwd
= cwd();
chdir
$path
or
die
"Cannot chdir($path): $!"
;
my
@files
;
find
sub
{
-f or
return
;
my
$cname
= File::Spec->canonpath(
$File::Find::name
);
push
@files
,
$cname
;
},
'.'
;
chdir
$cwd
or
die
"Cannot chdir($cwd) back: $!"
;
return
@files
;
}
sub
get_file {
my
$filename
= File::Spec->catfile(
@_
);
local
*MYFILE
;
my
@content
;
if
(
open
MYFILE,
"< $filename"
) {
@content
= <MYFILE>;
close
MYFILE;
}
else
{
Carp::carp(
"(@{[cwd]})$filename: $!"
);
}
return
wantarray
?
@content
:
join
""
,
@content
;
}
sub
put_file {
my
$contents
=
shift
;
my
$filename
= File::Spec->catfile(
@_
);
local
*MYFILE
;
if
(
open
MYFILE,
"> $filename"
) {
print
MYFILE
$contents
;
close
MYFILE or
do
{
warn
"Cannot close (@{[cwd]})$filename: $!"
;
return
;
};
}
else
{
warn
"Cannot create (@{[cwd]})$filename: $!"
;
return
;
}
return
1;
}
sub
rmtree { File::Path::rmtree(
@_
) }
sub
mkpath { File::Path::mkpath(
@_
) }
sub
find_a_patch {
my
$patch_bin
;
foreach
my
$patch
(
qw( gpatch npatch patch )
) {
$patch_bin
= whereis(
$patch
) or
next
;
my
$version
= `
$patch_bin
--version 2>&1`;
$? or
return
$patch_bin
;
}
}
sub
find_unzip {
my
$unzip
= whereis(
'gzip'
);
my
$dounzip
=
$unzip
?
"$unzip -cd "
:
""
;
unless
(
$dounzip
) {
$dounzip
=
'Compress::Zlib'
unless
$@;
}
return
$dounzip
;
}
sub
do_unzip {
my
(
$unzip
,
$uzfile
) =
@_
;
return
undef
unless
-f
$uzfile
;
my
$content
;
if
(
$unzip
eq
'Compress::Zlib'
) {
my
$unzipper
= Compress::Zlib::gzopen(
$uzfile
,
'rb'
) or
do
{
Carp::carp(
"Can't open '$uzfile': $Compress::Zlib::gzerrno"
);
return
undef
;
};
my
$buffer
;
$content
.=
$buffer
while
$unzipper
->gzread(
$buffer
) > 0;
unless
(
$Compress::Zlib::gzerrno
== Compress::Zlib::Z_STREAM_END() ) {
Carp::carp(
"Error reading '$uzfile': $Compress::Zlib::gzerrno"
);
}
$unzipper
->gzclose;
}
else
{
$content
= `
$unzip
$uzfile
`;
}
return
$content
;
}
sub
find_untargz {
my
$tar
= whereis(
'tar'
);
my
$uncompress
=
''
;
if
(
$tar
) {
my
$zip
= whereis(
'gzip'
);
$uncompress
=
"$zip -cd %s | $tar -xf -"
if
$zip
;
}
unless
(
$uncompress
) {
unless
( $@ ) {
if
(
$Archive::Tar::VERSION
>= 0.99 ) {
}
else
{
}
$uncompress
=
'Archive::Tar'
unless
$@;
}
}
if
(
$tar
&& !
$uncompress
) {
$uncompress
=
"$tar -xzf %s"
;
}
return
$uncompress
;
}
sub
do_untargz {
my
(
$untgz
,
$tgzfile
) =
@_
;
if
(
$untgz
eq
'Archive::Tar'
) {
my
$archive
= Archive::Tar->new() or
do
{
warn
"Can't Archive::Tar->new: "
.
$Archive::Tar::error
;
return
undef
;
};
$archive
->
read
(
$tgzfile
, 1 );
$Archive::Tar::error
and
do
{
warn
"Error reading '$tgzfile': "
.
$Archive::Tar::error
;
return
undef
;
};
my
@files
=
$archive
->list_files;
$archive
->extract(
@files
);
}
else
{
$^O eq
'VMS'
and
return
vms_untargz(
$untgz
,
$tgzfile
);
my
$command
=
sprintf
$untgz
,
$tgzfile
;
$command
.=
" $tgzfile"
if
$command
eq
$untgz
;
if
(
system
$command
) {
my
$error
= $? >> 8;
warn
"Error in command: $error"
;
return
undef
;
};
}
return
1;
}
sub
vms_untargz {
my
(
$cmd
,
$file
) =
@_
;
my
(
$vol
,
$path
,
$fname
) = splitpath(
$file
);
my
@parts
=
split
/[.@
if
(
@parts
> 1 ) {
my
$ext
= (
pop
@parts
) ||
''
;
$fname
=
join
(
"_"
,
@parts
) .
".$ext"
;
}
$file
= catpath(
$vol
,
$path
,
$fname
);
my
(
$gzip_cmd
,
$tar_cmd
) =
split
/\s*\|\s*/,
$cmd
;
my
$gzip
=
$gzip_cmd
=~ /^(\S+)/ ? $1 :
'GZIP'
;
my
$tar
=
$tar_cmd
=~ /^(\S+)/
? $1 : (whereis(
'vmstar'
) || whereis(
'tar'
) );
local
*TMPCOM
;
open
TMPCOM,
"> TS-UNTGZ.COM"
or
return
0;
print
TMPCOM
<<EO_UNTGZ; close TMPCOM or return 0;
\$ define/user sys\$output TS-UNTGZ.TAR
\$ $gzip "-cd" $file
\$ $tar "-xf" TS-UNTGZ.TAR
\$ delete TS-UNTGZ.TAR;*
EO_UNTGZ
my
$ret
=
system
"\@TS-UNTGZ.COM"
;
1
while
unlink
"TS-UNTGZ.COM"
;
return
!
$ret
;
}
1;