Skip to content

Commit 66a5c7e

Browse files
committed
Improve excluded files handling in Rex::Commands::Sync
1 parent 4b73d59 commit 66a5c7e

File tree

1 file changed

+63
-64
lines changed

1 file changed

+63
-64
lines changed

lib/Rex/Commands/Sync.pm

+63-64
Original file line numberDiff line numberDiff line change
@@ -94,70 +94,54 @@ sub sync_up {
9494
$source = get_file_path( $source, caller );
9595

9696
#
97-
# first, get all files on source side
97+
# first, build excludes list
9898
#
99-
my @local_files = _get_local_files($source);
10099

101-
#print Dumper(\@local_files);
100+
my $excludes = $options->{exclude} ||= [];
101+
$excludes = [$excludes] unless ref($excludes) eq 'ARRAY';
102102

103-
#
104-
# second, get all files from destination side
105-
#
103+
my @excluded_files = @{$excludes};
106104

107-
my @remote_files = _get_remote_files($dest);
105+
my $check_exclude_file = sub {
106+
my ($file) = @_;
107+
$file =~ s{^/}{};
108108

109-
#print Dumper(\@remote_files);
109+
for my $cmp (@excluded_files) {
110+
return 1 if match_glob( $cmp, $file );
111+
}
112+
113+
return 0;
114+
};
110115

111116
#
112-
# third, get the difference
117+
# second, get all files on source side (minus excludes)
113118
#
119+
my @local_files = _get_local_files( $source, $check_exclude_file );
114120

115-
my @diff = _diff_files( \@local_files, \@remote_files );
116-
117-
#print Dumper(\@diff);
121+
#print Dumper(\@local_files);
118122

119123
#
120-
# fourth, build excludes list
124+
# third, get all files from destination side (minus excludes)
121125
#
122126

123-
my $excludes = $options->{exclude} ||= [];
124-
$excludes = [$excludes] unless ref($excludes) eq 'ARRAY';
127+
my @remote_files = _get_remote_files( $dest, $check_exclude_file );
125128

126-
my @excluded_files = @{$excludes};
129+
#print Dumper(\@remote_files);
127130

128131
#
129-
# fifth, upload the different files
132+
# fourth, get the difference
130133
#
131134

132-
my $check_exclude_file = sub {
133-
my ( $file, $cmp ) = @_;
134-
if ( $cmp =~ m/\// ) {
135-
136-
# this is a directory exclude
137-
if ( match_glob( $cmp, $file ) || match_glob( $cmp, substr( $file, 1 ) ) )
138-
{
139-
return 1;
140-
}
141-
142-
return 0;
143-
}
135+
my @diff = _diff_files( \@local_files, \@remote_files );
144136

145-
if ( match_glob( $cmp, basename($file) ) ) {
146-
return 1;
147-
}
137+
#print Dumper(\@diff);
148138

149-
return 0;
150-
};
139+
#
140+
# fifth, upload the different files
141+
#
151142

152143
my @uploaded_files;
153144
for my $file (@diff) {
154-
next
155-
if (
156-
scalar(
157-
grep { $check_exclude_file->( $file->{name}, $_ ) } @excluded_files
158-
) > 0
159-
);
160-
161145
my ($dir) = ( $file->{path} =~ m/(.*)\/[^\/]+$/ );
162146
my ($remote_dir) = ( $file->{name} =~ m/\/(.*)\/[^\/]+$/ );
163147

@@ -252,44 +236,53 @@ sub sync_down {
252236
$dest = resolv_path($dest);
253237

254238
#
255-
# first, get all files on dest side
239+
# first, build excludes list
256240
#
257-
my @local_files = _get_local_files($dest);
258241

259-
#print Dumper(\@local_files);
242+
my $excludes = $options->{exclude} ||= [];
243+
$excludes = [$excludes] unless ref($excludes) eq 'ARRAY';
244+
245+
my @excluded_files = @{$excludes};
246+
247+
my $check_exclude_file = sub {
248+
my ($file) = @_;
249+
$file =~ s{^/}{};
250+
251+
for my $cmp (@excluded_files) {
252+
return 1 if match_glob( $cmp, $file );
253+
}
254+
255+
return 0;
256+
};
260257

261258
#
262-
# second, get all files from source side
259+
# second, get all files on dest side (minus excludes)
263260
#
261+
my @local_files = _get_local_files( $dest, $check_exclude_file );
264262

265-
my @remote_files = _get_remote_files($source);
266-
267-
#print Dumper(\@remote_files);
263+
#print Dumper(\@local_files);
268264

269265
#
270-
# third, get the difference
266+
# third, get all files from source side (minus excludes)
271267
#
272268

273-
my @diff = _diff_files( \@remote_files, \@local_files );
269+
my @remote_files = _get_remote_files( $source, $check_exclude_file );
274270

275-
#print Dumper(\@diff);
271+
#print Dumper(\@remote_files);
276272

277273
#
278-
# fourth, build excludes list
274+
# fourth, get the difference
279275
#
280276

281-
my $excludes = $options->{exclude} ||= [];
282-
$excludes = [$excludes] unless ref($excludes) eq 'ARRAY';
277+
my @diff = _diff_files( \@remote_files, \@local_files );
283278

284-
my @excluded_files = map { glob_to_regex($_); } @{$excludes};
279+
#print Dumper(\@diff);
285280

286281
#
287282
# fifth, download the different files
288283
#
289284

290285
for my $file (@diff) {
291-
next if grep { basename( $file->{name} ) =~ $_ } @excluded_files;
292-
293286
my ($dir) = ( $file->{path} =~ m/(.*)\/[^\/]+$/ );
294287
my ($remote_dir) = ( $file->{name} =~ m/\/(.*)\/[^\/]+$/ );
295288

@@ -326,7 +319,7 @@ sub sync_down {
326319
}
327320

328321
sub _get_local_files {
329-
my ($source) = @_;
322+
my ( $source, $exclude_sub ) = @_;
330323

331324
if ( !-d $source ) { die("$source : no such directory."); }
332325

@@ -337,13 +330,15 @@ sub _get_local_files {
337330
for my $entry ( list_files($dir) ) {
338331
next if ( $entry eq "." );
339332
next if ( $entry eq ".." );
333+
334+
my $name = "$dir/$entry";
335+
$name =~ s/^\Q$source\E//;
336+
next if $exclude_sub->($name);
337+
340338
if ( is_dir("$dir/$entry") ) {
341339
push( @dirs, "$dir/$entry" );
342340
next;
343341
}
344-
345-
my $name = "$dir/$entry";
346-
$name =~ s/^\Q$source\E//;
347342
push(
348343
@local_files,
349344
{
@@ -361,7 +356,7 @@ sub _get_local_files {
361356
}
362357

363358
sub _get_remote_files {
364-
my ($dest) = @_;
359+
my ( $dest, $exclude_sub ) = @_;
365360

366361
if ( !is_dir($dest) ) { die("$dest : no such directory."); }
367362

@@ -372,13 +367,16 @@ sub _get_remote_files {
372367
for my $entry ( list_files($dir) ) {
373368
next if ( $entry eq "." );
374369
next if ( $entry eq ".." );
370+
371+
my $name = "$dir/$entry";
372+
$name =~ s/^\Q$dest\E//;
373+
next if $exclude_sub->($name);
374+
375375
if ( is_dir("$dir/$entry") ) {
376376
push( @remote_dirs, "$dir/$entry" );
377377
next;
378378
}
379379

380-
my $name = "$dir/$entry";
381-
$name =~ s/^\Q$dest\E//;
382380
push(
383381
@remote_files,
384382
{
@@ -411,3 +409,4 @@ sub _diff_files {
411409
}
412410

413411
1;
412+

0 commit comments

Comments
 (0)