22
33import com .fizzed .jsync .vfs .*;
44import com .fizzed .jsync .vfs .util .Permissions ;
5+ import com .fizzed .jsync .vfs .util .VirtualPathMatchers ;
56import org .slf4j .Logger ;
67import org .slf4j .LoggerFactory ;
78
@@ -31,9 +32,10 @@ public class JsyncEngine {
3132 private List <String > ignores ;
3233 // when running a sync
3334 private Checksum negotiatedChecksum ;
34- private List <VirtualPath > excludePaths ;
35- private List <VirtualPath > ignoreSourcePaths ;
36- private List <VirtualPath > ignoreTargetPaths ;
35+ private VirtualPathMatchers excludeMatchers ;
36+ private VirtualPathMatchers ignoreMatchers ;
37+ private VirtualPath sourceRootPath ;
38+ private VirtualPath targetRootPath ;
3739
3840 public JsyncEngine () {
3941 this .eventHandler = new DefaultJsyncEventHandler ();
@@ -225,6 +227,9 @@ public JsyncResult sync(VirtualFileSystem sourceVfs, String sourcePath, VirtualF
225227 final VirtualPath sourcePathAbsFinal = sourcePathAbs .normalize ();
226228 final VirtualPath targetPathAbsFinal = targetPathAbs .normalize ();
227229
230+ this .sourceRootPath = sourcePathAbsFinal ;
231+ this .targetRootPath = targetPathAbsFinal ;
232+
228233
229234 //
230235 // Negotiate checksum methods between source and target filesystems if necessary
@@ -236,29 +241,9 @@ public JsyncResult sync(VirtualFileSystem sourceVfs, String sourcePath, VirtualF
236241 log .debug ("Source filesystem stat mode: {}" , sourceVfs .getStatModel ());
237242 log .debug ("Target filesystem stat mode: {}" , targetVfs .getStatModel ());
238243
239- // build exclude and ignore paths
240- if (this .excludes != null ) {
241- this .excludePaths = this .excludes .stream ()
242- .map (VirtualPath ::parse )
243- .map (sourcePathAbsFinal ::resolve )
244- .collect (toList ());
245- } else {
246- this .excludePaths = Collections .emptyList ();
247- }
248-
249- if (this .ignores != null ) {
250- this .ignoreSourcePaths = this .ignores .stream ()
251- .map (VirtualPath ::parse )
252- .map (sourcePathAbsFinal ::resolve )
253- .collect (toList ());
254- this .ignoreTargetPaths = this .ignores .stream ()
255- .map (VirtualPath ::parse )
256- .map (targetPathAbsFinal ::resolve )
257- .collect (toList ());
258- } else {
259- this .ignoreSourcePaths = Collections .emptyList ();
260- this .ignoreTargetPaths = Collections .emptyList ();
261- }
244+ // build exclude and ignore matchers
245+ this .excludeMatchers = VirtualPathMatchers .compile (this .excludes );
246+ this .ignoreMatchers = VirtualPathMatchers .compile (this .ignores );
262247
263248
264249 final long now = System .currentTimeMillis ();
@@ -272,17 +257,6 @@ public JsyncResult sync(VirtualFileSystem sourceVfs, String sourcePath, VirtualF
272257 final List <VirtualPathPair > deferredFiles = new ArrayList <>();
273258
274259 if (sourcePathAbsFinal .isDirectory ()) {
275- // any excludes, let's resolve them against pwd of the source to make it easier to exclude them
276- final List <VirtualPath > excludePaths ;
277- if (this .excludes != null ) {
278- excludePaths = this .excludes .stream ()
279- .map (VirtualPath ::parse )
280- .map (sourcePathAbsFinal ::resolve )
281- .collect (toList ());
282- } else {
283- excludePaths = Collections .emptyList ();
284- }
285-
286260 // as we process files, only a subset may require more advanced methods of detecting whether they were modified
287261 // since that process could be "expensive", we keep a list of files on source/target that we will defer processing
288262 // until we have a chance to do some bulk processing of checksums, etc.
@@ -409,23 +383,19 @@ protected void syncDirectory(int level, JsyncResult result, List<VirtualPathPair
409383
410384
411385 // we need a list of files in both directories, so we can see what to add/delete
412- List <VirtualPath > sourceChildPaths = sourceVfs .ls (sourcePath ).stream ()
386+ final List <VirtualPath > sourceChildPaths = sourceVfs .ls (sourcePath ).stream ()
413387 // apply filter to source files if they are on the exclude list
414388 .filter (v -> {
415- for (VirtualPath p : this .excludePaths ) {
416- if (v .startsWith (p )) {
417- this .eventHandler .willExcludePath (v );
418- return false ;
419- }
389+ if (this .excludeMatchers .matches (this .sourceRootPath , v )) {
390+ this .eventHandler .willExcludePath (v );
391+ return false ;
420392 }
421393 return true ;
422394 })
423395 .filter (v -> {
424- for (VirtualPath p : this .ignoreSourcePaths ) {
425- if (v .startsWith (p )) {
426- this .eventHandler .willIgnorePath (v );
427- return false ;
428- }
396+ if (this .ignoreMatchers .matches (this .sourceRootPath , v )) {
397+ this .eventHandler .willIgnoreSourcePath (v );
398+ return false ;
429399 }
430400 return true ;
431401 })
@@ -446,10 +416,9 @@ protected void syncDirectory(int level, JsyncResult result, List<VirtualPathPair
446416
447417 final List <VirtualPath > targetChildPaths = targetVfs .ls (targetPath ).stream ()
448418 .filter (v -> {
449- for (VirtualPath p : this .ignoreTargetPaths ) {
450- if (v .startsWith (p )) {
451- return false ;
452- }
419+ if (this .ignoreMatchers .matches (this .targetRootPath , v )) {
420+ this .eventHandler .willIgnoreTargetPath (v );
421+ return false ;
453422 }
454423 return true ;
455424 })
0 commit comments