| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
=pod |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
PostgreSQL::Test::RecursiveCopy - simple recursive copy implementation |
| 9 |
|
| 10 |
=head1 SYNOPSIS |
| 11 |
|
| 12 |
use PostgreSQL::Test::RecursiveCopy; |
| 13 |
|
| 14 |
PostgreSQL::Test::RecursiveCopy::copypath($from, $to, filterfn => sub { return 1; }); |
| 15 |
PostgreSQL::Test::RecursiveCopy::copypath($from, $to); |
| 16 |
|
| 17 |
=cut |
| 18 |
|
| 19 |
package PostgreSQL::Test::RecursiveCopy; |
| 20 |
|
| 21 |
use strict; |
| 22 |
use warnings FATAL => 'all'; |
| 23 |
|
| 24 |
use Carp; |
| 25 |
use File::Basename; |
| 26 |
use File::Copy; |
| 27 |
|
| 28 |
=pod |
| 29 |
|
| 30 |
=head1 DESCRIPTION |
| 31 |
|
| 32 |
=head2 copypath($from, $to, %params) |
| 33 |
|
| 34 |
Recursively copy all files and directories from $from to $to. |
| 35 |
Does not preserve file metadata (e.g., permissions). |
| 36 |
|
| 37 |
Only regular files and subdirectories are copied. Trying to copy other types |
| 38 |
of directory entries raises an exception. |
| 39 |
|
| 40 |
Raises an exception if a file would be overwritten, the source directory can't |
| 41 |
be read, or any I/O operation fails. However, we silently ignore ENOENT on |
| 42 |
open, because when copying from a live database it's possible for a file/dir |
| 43 |
to be deleted after we see its directory entry but before we can open it. |
| 44 |
|
| 45 |
Always returns true. |
| 46 |
|
| 47 |
If the B<filterfn> parameter is given, it must be a subroutine reference. |
| 48 |
This subroutine will be called for each entry in the source directory with its |
| 49 |
relative path as only parameter; if the subroutine returns true the entry is |
| 50 |
copied, otherwise the file is skipped. |
| 51 |
|
| 52 |
On failure the target directory may be in some incomplete state; no cleanup is |
| 53 |
attempted. |
| 54 |
|
| 55 |
=head1 EXAMPLES |
| 56 |
|
| 57 |
PostgreSQL::Test::RecursiveCopy::copypath('/some/path', '/empty/dir', |
| 58 |
filterfn => sub { |
| 59 |
# omit log/ and contents |
| 60 |
my $src = shift; |
| 61 |
return $src ne 'log'; |
| 62 |
} |
| 63 |
); |
| 64 |
|
| 65 |
=cut |
| 66 |
|
| 67 |
sub copypath |
| 68 |
{ |
| 69 |
my ($base_src_dir, $base_dest_dir, %params) = @_; |
| 70 |
my $filterfn; |
| 71 |
|
| 72 |
if (defined $params{filterfn}) |
| 73 |
{ |
| 74 |
croak "if specified, filterfn must be a subroutine reference" |
| 75 |
unless defined(ref $params{filterfn}) |
| 76 |
and (ref $params{filterfn} eq 'CODE'); |
| 77 |
|
| 78 |
$filterfn = $params{filterfn}; |
| 79 |
} |
| 80 |
else |
| 81 |
{ |
| 82 |
$filterfn = sub { return 1; }; |
| 83 |
} |
| 84 |
|
| 85 |
# Complain if original path is bogus, because _copypath_recurse won't. |
| 86 |
croak "\"$base_src_dir\" does not exist" if !-e $base_src_dir; |
| 87 |
|
| 88 |
# Start recursive copy from current directory |
| 89 |
return _copypath_recurse($base_src_dir, $base_dest_dir, "", $filterfn); |
| 90 |
} |
| 91 |
|
| 92 |
# Recursive private guts of copypath |
| 93 |
sub _copypath_recurse |
| 94 |
{ |
| 95 |
my ($base_src_dir, $base_dest_dir, $curr_path, $filterfn) = @_; |
| 96 |
my $srcpath = "$base_src_dir/$curr_path"; |
| 97 |
my $destpath = "$base_dest_dir/$curr_path"; |
| 98 |
|
| 99 |
# invoke the filter and skip all further operation if it returns false |
| 100 |
return 1 unless &$filterfn($curr_path); |
| 101 |
|
| 102 |
# Check for symlink -- needed only on source dir |
| 103 |
# (note: this will fall through quietly if file is already gone) |
| 104 |
croak "Cannot operate on symlink \"$srcpath\"" if -l $srcpath; |
| 105 |
|
| 106 |
# Abort if destination path already exists. Should we allow directories |
| 107 |
# to exist already? |
| 108 |
croak "Destination path \"$destpath\" already exists" if -e $destpath; |
| 109 |
|
| 110 |
# If this source path is a file, simply copy it to destination with the |
| 111 |
# same name and we're done. |
| 112 |
if (-f $srcpath) |
| 113 |
{ |
| 114 |
my $fh; |
| 115 |
unless (open($fh, '<', $srcpath)) |
| 116 |
{ |
| 117 |
return 1 if ($!{ENOENT}); |
| 118 |
die "open($srcpath) failed: $!"; |
| 119 |
} |
| 120 |
copy($fh, $destpath) |
| 121 |
or die "copy $srcpath -> $destpath failed: $!"; |
| 122 |
close $fh; |
| 123 |
return 1; |
| 124 |
} |
| 125 |
|
| 126 |
# If it's a directory, create it on dest and recurse into it. |
| 127 |
if (-d $srcpath) |
| 128 |
{ |
| 129 |
my $directory; |
| 130 |
unless (opendir($directory, $srcpath)) |
| 131 |
{ |
| 132 |
return 1 if ($!{ENOENT}); |
| 133 |
die "opendir($srcpath) failed: $!"; |
| 134 |
} |
| 135 |
|
| 136 |
mkdir($destpath) or die "mkdir($destpath) failed: $!"; |
| 137 |
|
| 138 |
while (my $entry = readdir($directory)) |
| 139 |
{ |
| 140 |
next if ($entry eq '.' or $entry eq '..'); |
| 141 |
_copypath_recurse($base_src_dir, $base_dest_dir, |
| 142 |
$curr_path eq '' ? $entry : "$curr_path/$entry", $filterfn) |
| 143 |
or die "copypath $srcpath/$entry -> $destpath/$entry failed"; |
| 144 |
} |
| 145 |
|
| 146 |
closedir($directory); |
| 147 |
return 1; |
| 148 |
} |
| 149 |
|
| 150 |
# If it disappeared from sight, that's OK. |
| 151 |
return 1 if !-e $srcpath; |
| 152 |
|
| 153 |
# Else it's some weird file type; complain. |
| 154 |
croak "Source path \"$srcpath\" is not a regular file or directory"; |
| 155 |
} |
| 156 |
|
| 157 |
1; |