2016-03-09 18:00:31 -03:00
|
|
|
|
2023-01-02 15:00:37 -05:00
|
|
|
# Copyright (c) 2021-2023, PostgreSQL Global Development Group
|
2021-05-07 10:56:14 -04:00
|
|
|
|
2016-03-09 18:00:31 -03:00
|
|
|
=pod
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
2021-10-24 10:28:19 -04:00
|
|
|
PostgreSQL::Test::RecursiveCopy - simple recursive copy implementation
|
2016-03-09 18:00:31 -03:00
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
2021-10-24 10:28:19 -04:00
|
|
|
use PostgreSQL::Test::RecursiveCopy;
|
2016-03-09 18:00:31 -03:00
|
|
|
|
2021-10-24 10:28:19 -04:00
|
|
|
PostgreSQL::Test::RecursiveCopy::copypath($from, $to, filterfn => sub { return 1; });
|
|
|
|
PostgreSQL::Test::RecursiveCopy::copypath($from, $to);
|
2016-03-09 18:00:31 -03:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
2021-10-24 10:28:19 -04:00
|
|
|
package PostgreSQL::Test::RecursiveCopy;
|
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
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2018-02-24 14:35:54 -05:00
|
|
|
use Carp;
|
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
|
|
|
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.
|
2017-09-11 22:02:58 -04:00
|
|
|
Does not preserve file metadata (e.g., permissions).
|
2016-03-09 18:00:31 -03:00
|
|
|
|
|
|
|
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
|
2017-09-11 22:02:58 -04:00
|
|
|
be read, or any I/O operation fails. However, we silently ignore ENOENT on
|
|
|
|
open, because when copying from a live database it's possible for a file/dir
|
|
|
|
to be deleted after we see its directory entry but before we can open it.
|
|
|
|
|
|
|
|
Always returns true.
|
2016-03-09 18:00:31 -03:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-10-24 10:28:19 -04:00
|
|
|
PostgreSQL::Test::RecursiveCopy::copypath('/some/path', '/empty/dir',
|
2016-03-09 18:00:31 -03:00
|
|
|
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})
|
|
|
|
{
|
2018-02-24 14:35:54 -05:00
|
|
|
croak "if specified, filterfn must be a subroutine reference"
|
2016-03-09 18:00:31 -03:00
|
|
|
unless defined(ref $params{filterfn})
|
|
|
|
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; };
|
|
|
|
}
|
|
|
|
|
2017-09-11 22:02:58 -04:00
|
|
|
# Complain if original path is bogus, because _copypath_recurse won't.
|
2018-02-24 14:35:54 -05:00
|
|
|
croak "\"$base_src_dir\" does not exist" if !-e $base_src_dir;
|
2017-09-11 22:02:58 -04:00
|
|
|
|
2016-03-09 18:00:31 -03:00
|
|
|
# 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
|
2017-09-11 22:02:58 -04:00
|
|
|
# (note: this will fall through quietly if file is already gone)
|
2018-02-24 14:35:54 -05:00
|
|
|
croak "Cannot operate on symlink \"$srcpath\"" if -l $srcpath;
|
2016-03-09 18:00:31 -03:00
|
|
|
|
|
|
|
# Abort if destination path already exists. Should we allow directories
|
|
|
|
# to exist already?
|
2018-02-24 14:35:54 -05:00
|
|
|
croak "Destination path \"$destpath\" already exists" if -e $destpath;
|
2016-03-09 18:00:31 -03:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
{
|
2017-09-11 22:02:58 -04:00
|
|
|
my $fh;
|
|
|
|
unless (open($fh, '<', $srcpath))
|
|
|
|
{
|
|
|
|
return 1 if ($!{ENOENT});
|
|
|
|
die "open($srcpath) failed: $!";
|
|
|
|
}
|
|
|
|
copy($fh, $destpath)
|
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 "copy $srcpath -> $destpath failed: $!";
|
2017-09-11 22:02:58 -04:00
|
|
|
close $fh;
|
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;
|
|
|
|
}
|
|
|
|
|
2017-09-11 22:02:58 -04:00
|
|
|
# If it's a directory, create it on dest and recurse into it.
|
|
|
|
if (-d $srcpath)
|
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
|
|
|
{
|
2017-09-11 22:02:58 -04:00
|
|
|
my $directory;
|
|
|
|
unless (opendir($directory, $srcpath))
|
|
|
|
{
|
|
|
|
return 1 if ($!{ENOENT});
|
|
|
|
die "opendir($srcpath) failed: $!";
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir($destpath) or die "mkdir($destpath) failed: $!";
|
|
|
|
|
|
|
|
while (my $entry = readdir($directory))
|
|
|
|
{
|
|
|
|
next if ($entry eq '.' or $entry eq '..');
|
|
|
|
_copypath_recurse($base_src_dir, $base_dest_dir,
|
|
|
|
$curr_path eq '' ? $entry : "$curr_path/$entry", $filterfn)
|
|
|
|
or die "copypath $srcpath/$entry -> $destpath/$entry failed";
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir($directory);
|
|
|
|
return 1;
|
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
|
|
|
|
2017-09-11 22:02:58 -04:00
|
|
|
# If it disappeared from sight, that's OK.
|
|
|
|
return 1 if !-e $srcpath;
|
|
|
|
|
|
|
|
# Else it's some weird file type; complain.
|
2018-02-24 14:35:54 -05:00
|
|
|
croak "Source path \"$srcpath\" is not a regular file or directory";
|
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
|
|
|
}
|
|
|
|
|
|
|
|
1;
|