2016-03-09 18:00:31 -03:00
|
|
|
|
|
|
|
=pod
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
RecursiveCopy - simple recursive copy implementation
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
use RecursiveCopy;
|
|
|
|
|
|
|
|
RecursiveCopy::copypath($from, $to, filterfn => sub { return 1; });
|
|
|
|
RecursiveCopy::copypath($from, $to);
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 18:46:16 -03:00
|
|
|
package RecursiveCopy;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use File::Basename;
|
|
|
|
use File::Copy;
|
|
|
|
|
2016-03-09 18:00:31 -03:00
|
|
|
=pod
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
=head2 copypath($from, $to, %params)
|
|
|
|
|
|
|
|
Recursively copy all files and directories from $from to $to.
|
|
|
|
|
|
|
|
Only regular files and subdirectories are copied. Trying to copy other types
|
|
|
|
of directory entries raises an exception.
|
|
|
|
|
|
|
|
Raises an exception if a file would be overwritten, the source directory can't
|
|
|
|
be read, or any I/O operation fails. Always returns true.
|
|
|
|
|
|
|
|
If the B<filterfn> parameter is given, it must be a subroutine reference.
|
|
|
|
This subroutine will be called for each entry in the source directory with its
|
|
|
|
relative path as only parameter; if the subroutine returns true the entry is
|
|
|
|
copied, otherwise the file is skipped.
|
|
|
|
|
|
|
|
On failure the target directory may be in some incomplete state; no cleanup is
|
|
|
|
attempted.
|
|
|
|
|
|
|
|
=head1 EXAMPLES
|
|
|
|
|
|
|
|
RecursiveCopy::copypath('/some/path', '/empty/dir',
|
|
|
|
filterfn => sub {
|
2017-03-27 10:34:33 -04:00
|
|
|
# omit log/ and contents
|
2016-03-09 18:00:31 -03:00
|
|
|
my $src = shift;
|
2017-03-27 10:34:33 -04:00
|
|
|
return $src ne 'log';
|
2016-03-09 18:00:31 -03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 18:46:16 -03:00
|
|
|
sub copypath
|
|
|
|
{
|
2016-03-09 18:00:31 -03:00
|
|
|
my ($base_src_dir, $base_dest_dir, %params) = @_;
|
|
|
|
my $filterfn;
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 18:46:16 -03:00
|
|
|
|
2016-03-09 18:00:31 -03:00
|
|
|
if (defined $params{filterfn})
|
|
|
|
{
|
|
|
|
die "if specified, filterfn must be a subroutine reference"
|
|
|
|
unless defined(ref $params{filterfn})
|
2016-06-12 04:19:56 -04:00
|
|
|
and (ref $params{filterfn} eq 'CODE');
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 18:46:16 -03:00
|
|
|
|
2016-03-09 18:00:31 -03:00
|
|
|
$filterfn = $params{filterfn};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$filterfn = sub { return 1; };
|
|
|
|
}
|
|
|
|
|
|
|
|
# Start recursive copy from current directory
|
|
|
|
return _copypath_recurse($base_src_dir, $base_dest_dir, "", $filterfn);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Recursive private guts of copypath
|
|
|
|
sub _copypath_recurse
|
|
|
|
{
|
|
|
|
my ($base_src_dir, $base_dest_dir, $curr_path, $filterfn) = @_;
|
|
|
|
my $srcpath = "$base_src_dir/$curr_path";
|
|
|
|
my $destpath = "$base_dest_dir/$curr_path";
|
|
|
|
|
|
|
|
# invoke the filter and skip all further operation if it returns false
|
|
|
|
return 1 unless &$filterfn($curr_path);
|
|
|
|
|
|
|
|
# Check for symlink -- needed only on source dir
|
|
|
|
die "Cannot operate on symlinks" if -l $srcpath;
|
|
|
|
|
|
|
|
# Can't handle symlinks or other weird things
|
|
|
|
die "Source path \"$srcpath\" is not a regular file or directory"
|
2016-06-12 04:19:56 -04:00
|
|
|
unless -f $srcpath
|
|
|
|
or -d $srcpath;
|
2016-03-09 18:00:31 -03:00
|
|
|
|
|
|
|
# Abort if destination path already exists. Should we allow directories
|
|
|
|
# to exist already?
|
|
|
|
die "Destination path \"$destpath\" already exists" if -e $destpath;
|
|
|
|
|
|
|
|
# If this source path is a file, simply copy it to destination with the
|
|
|
|
# same name and we're done.
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 18:46:16 -03:00
|
|
|
if (-f $srcpath)
|
|
|
|
{
|
|
|
|
copy($srcpath, $destpath)
|
|
|
|
or die "copy $srcpath -> $destpath failed: $!";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:00:31 -03:00
|
|
|
# Otherwise this is directory: create it on dest and recurse onto it.
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 18:46:16 -03:00
|
|
|
mkdir($destpath) or die "mkdir($destpath) failed: $!";
|
|
|
|
|
|
|
|
opendir(my $directory, $srcpath) or die "could not opendir($srcpath): $!";
|
|
|
|
while (my $entry = readdir($directory))
|
|
|
|
{
|
2016-03-09 18:00:31 -03:00
|
|
|
next if ($entry eq '.' or $entry eq '..');
|
|
|
|
_copypath_recurse($base_src_dir, $base_dest_dir,
|
|
|
|
$curr_path eq '' ? $entry : "$curr_path/$entry", $filterfn)
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 18:46:16 -03:00
|
|
|
or die "copypath $srcpath/$entry -> $destpath/$entry failed";
|
|
|
|
}
|
|
|
|
closedir($directory);
|
2016-03-09 18:00:31 -03:00
|
|
|
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 18:46:16 -03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|