Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8fd42e6

Browse files
committed
TAP: Add filtering to RecursiveCopy
Allow RecursiveCopy to accept a filter function so callers canexclude unwanted files. Also POD-ify it.
1 parent26fdff1 commit8fd42e6

File tree

1 file changed

+70
-6
lines changed

1 file changed

+70
-6
lines changed

‎src/test/perl/RecursiveCopy.pm

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
# RecursiveCopy, a simple recursive copy implementation
1+
2+
=pod
3+
4+
=head1NAME
5+
6+
RecursiveCopy - simple recursive copy implementation
7+
8+
=head1SYNOPSIS
9+
10+
use RecursiveCopy;
11+
12+
RecursiveCopy::copypath($from, $to);
13+
14+
=cut
15+
216
packageRecursiveCopy;
317

418
use strict;
@@ -7,10 +21,56 @@ use warnings;
721
use File::Basename;
822
use File::Copy;
923

24+
=pod
25+
26+
=head2copypath($from, $to)
27+
28+
Copy all files and directories from $from to $to. Raises an exception
29+
if a file would be overwritten, the source dir can't be read, or any
30+
I/O operation fails. Always returns true. On failure the copy may be
31+
in some incomplete state; no cleanup is attempted.
32+
33+
If the keyword param 'filterfn' is defined it's invoked as a sub that
34+
returns true if the file/directory should be copied, false otherwise.
35+
The passed path is the full path to the file relative to the source
36+
directory.
37+
38+
e.g.
39+
40+
RecursiveCopy::copypath('/some/path', '/empty/dir',
41+
filterfn => sub {^
42+
# omit children of pg_log
43+
my $src = shift;
44+
return ! $src ~= /\/pg_log\//
45+
}
46+
);
47+
48+
=cut
49+
1050
subcopypath
1151
{
12-
my$srcpath =shift;
13-
my$destpath =shift;
52+
my ($srcpath,$destpath,%params) =@_;
53+
54+
die("if specified, 'filterfn' must be a sub ref")
55+
ifdefined$params{filterfn} && !ref$params{filterfn};
56+
57+
my$filterfn;
58+
if (defined$params{filterfn})
59+
{
60+
$filterfn =$params{filterfn};
61+
}
62+
else
63+
{
64+
$filterfn =sub {return 1; };
65+
}
66+
67+
return _copypath_recurse($srcpath,$destpath,$filterfn);
68+
}
69+
70+
# Recursive private guts of copypath
71+
sub_copypath_recurse
72+
{
73+
my ($srcpath,$destpath,$filterfn) =@_;
1474

1575
die"Cannot operate on symlinks"if-l$srcpathor-l$destpath;
1676

@@ -19,8 +79,11 @@ sub copypath
1979
die"Destination path$destpath exists as file"if-f$destpath;
2080
if (-f$srcpath)
2181
{
22-
copy($srcpath,$destpath)
23-
ordie"copy$srcpath ->$destpath failed:$!";
82+
if ($filterfn->($srcpath))
83+
{
84+
copy($srcpath,$destpath)
85+
ordie"copy$srcpath ->$destpath failed:$!";
86+
}
2487
return 1;
2588
}
2689

@@ -32,7 +95,8 @@ sub copypath
3295
while (my$entry =readdir($directory))
3396
{
3497
nextif ($entryeq'.' ||$entryeq'..');
35-
RecursiveCopy::copypath("$srcpath/$entry","$destpath/$entry")
98+
RecursiveCopy::_copypath_recurse("$srcpath/$entry",
99+
"$destpath/$entry",$filterfn)
36100
ordie"copypath$srcpath/$entry ->$destpath/$entry failed";
37101
}
38102
closedir($directory);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp