1 package org.apache.maven.shared.utils.io;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29
30 import javax.annotation.Nonnull;
31 import javax.annotation.Nullable;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 public class DirectoryScanner
112 {
113
114
115
116
117
118 public static final String[] DEFAULTEXCLUDES = {
119
120 "**/*~", "**/#*#", "**/.#*", "**/%*%", "**/._*",
121
122
123 "**/CVS", "**/CVS/**", "**/.cvsignore",
124
125
126 "**/.svn", "**/.svn/**",
127
128
129 "**/.arch-ids", "**/.arch-ids/**",
130
131
132 "**/.bzr", "**/.bzr/**",
133
134
135 "**/.MySCMServerInfo",
136
137
138 "**/.DS_Store",
139
140
141 "**/.metadata", "**/.metadata/**",
142
143
144 "**/.hg", "**/.hg/**",
145
146
147 "**/.git", "**/.git/**",
148
149
150 "**/BitKeeper", "**/BitKeeper/**", "**/ChangeSet", "**/ChangeSet/**",
151
152
153 "**/_darcs", "**/_darcs/**", "**/.darcsrepo", "**/.darcsrepo/**", "**/-darcs-backup*", "**/.darcs-temp-mail" };
154
155
156
157
158 private File basedir;
159
160
161
162
163 private String[] includes;
164
165
166
167
168 private String[] excludes;
169
170 private MatchPatterns excludesPatterns;
171
172 private MatchPatterns includesPatterns;
173
174
175
176
177
178 private List<String> filesIncluded;
179
180
181
182
183 private List<String> filesNotIncluded;
184
185
186
187
188 private List<String> filesExcluded;
189
190
191
192
193 private List<String> dirsIncluded;
194
195
196
197
198 private List<String> dirsNotIncluded;
199
200
201
202
203 private List<String> dirsExcluded;
204
205
206
207
208 private boolean haveSlowResults = false;
209
210
211
212
213 private boolean isCaseSensitive = true;
214
215
216
217
218
219
220 private boolean followSymlinks = true;
221
222
223
224
225
226 private ScanConductor scanConductor = null;
227
228
229
230
231 private ScanConductor.ScanAction scanAction = null;
232
233
234
235
236 public DirectoryScanner()
237 {
238 }
239
240
241
242
243
244
245
246
247 public void setBasedir( final String basedir )
248 {
249 setBasedir( new File( basedir.replace( '/', File.separatorChar ).replace( '\\', File.separatorChar ) ) );
250 }
251
252
253
254
255
256
257 public void setBasedir( @Nonnull final File basedir )
258 {
259 this.basedir = basedir;
260 }
261
262
263
264
265
266
267 public File getBasedir()
268 {
269 return basedir;
270 }
271
272
273
274
275
276
277 public void setCaseSensitive( final boolean isCaseSensitiveParameter )
278 {
279 this.isCaseSensitive = isCaseSensitiveParameter;
280 }
281
282
283
284
285
286
287 public void setFollowSymlinks( final boolean followSymlinks )
288 {
289 this.followSymlinks = followSymlinks;
290 }
291
292
293
294
295
296
297
298
299
300
301 public void setIncludes( final String... includes )
302 {
303 if ( includes == null )
304 {
305 this.includes = null;
306 }
307 else
308 {
309 this.includes = new String[includes.length];
310 for ( int i = 0; i < includes.length; i++ )
311 {
312 String pattern;
313 pattern = includes[i].trim().replace( '/', File.separatorChar ).replace( '\\', File.separatorChar );
314 if ( pattern.endsWith( File.separator ) )
315 {
316 pattern += "**";
317 }
318 this.includes[i] = pattern;
319 }
320 }
321 }
322
323
324
325
326
327
328
329
330
331
332 public void setExcludes( final String... excludes )
333 {
334 if ( excludes == null )
335 {
336 this.excludes = null;
337 }
338 else
339 {
340 this.excludes = new String[excludes.length];
341 for ( int i = 0; i < excludes.length; i++ )
342 {
343 String pattern;
344 pattern = excludes[i].trim().replace( '/', File.separatorChar ).replace( '\\', File.separatorChar );
345 if ( pattern.endsWith( File.separator ) )
346 {
347 pattern += "**";
348 }
349 this.excludes[i] = pattern;
350 }
351 }
352 }
353
354
355
356
357 public void setScanConductor( final ScanConductor scanConductor )
358 {
359 this.scanConductor = scanConductor;
360 }
361
362
363
364
365
366
367
368
369 public void scan()
370 throws IllegalStateException
371 {
372 if ( basedir == null )
373 {
374 throw new IllegalStateException( "No basedir set" );
375 }
376 if ( !basedir.exists() )
377 {
378 throw new IllegalStateException( "basedir " + basedir + " does not exist" );
379 }
380 if ( !basedir.isDirectory() )
381 {
382 throw new IllegalStateException( "basedir " + basedir + " is not a directory" );
383 }
384
385 setupDefaultFilters();
386 setupMatchPatterns();
387
388 filesIncluded = new ArrayList<String>();
389 filesNotIncluded = new ArrayList<String>();
390 filesExcluded = new ArrayList<String>();
391 dirsIncluded = new ArrayList<String>();
392 dirsNotIncluded = new ArrayList<String>();
393 dirsExcluded = new ArrayList<String>();
394 scanAction = ScanConductor.ScanAction.CONTINUE;
395
396 if ( isIncluded( "" ) )
397 {
398 if ( !isExcluded( "" ) )
399 {
400 if ( scanConductor != null )
401 {
402 scanAction = scanConductor.visitDirectory( "", basedir );
403
404 if ( ScanConductor.ScanAction.ABORT.equals( scanAction )
405 || ScanConductor.ScanAction.ABORT_DIRECTORY.equals( scanAction )
406 || ScanConductor.ScanAction.NO_RECURSE.equals( scanAction ) )
407 {
408 return;
409 }
410 }
411
412 dirsIncluded.add( "" );
413 }
414 else
415 {
416 dirsExcluded.add( "" );
417 }
418 }
419 else
420 {
421 dirsNotIncluded.add( "" );
422 }
423 scandir( basedir, "", true );
424 }
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442 public DirectoryScanResult diffIncludedFiles( String... oldFiles )
443 {
444 if ( filesIncluded == null )
445 {
446
447 scan();
448 }
449
450 return diffFiles( oldFiles, filesIncluded.toArray( new String[filesIncluded.size()] ) );
451 }
452
453
454
455
456
457
458 public static DirectoryScanResult diffFiles( @Nullable String[] oldFiles, @Nullable String[] newFiles )
459 {
460 Set<String> oldFileSet = arrayAsHashSet( oldFiles );
461 Set<String> newFileSet = arrayAsHashSet( newFiles );
462
463 List<String> added = new ArrayList<String>();
464 List<String> removed = new ArrayList<String>();
465
466 for ( String oldFile : oldFileSet )
467 {
468 if ( !newFileSet.contains( oldFile ) )
469 {
470 removed.add( oldFile );
471 }
472 }
473
474 for ( String newFile : newFileSet )
475 {
476 if ( !oldFileSet.contains( newFile ) )
477 {
478 added.add( newFile );
479 }
480 }
481
482 String[] filesAdded = added.toArray( new String[added.size()] );
483 String[] filesRemoved = removed.toArray( new String[removed.size()] );
484
485 return new DirectoryScanResult( filesAdded, filesRemoved );
486 }
487
488
489
490
491
492
493
494
495
496 private static <T> Set<T> arrayAsHashSet( @Nullable T[] array )
497 {
498 if ( array == null || array.length == 0 )
499 {
500 return Collections.emptySet();
501 }
502
503 Set<T> set = new HashSet<T>( array.length );
504 Collections.addAll( set, array );
505
506 return set;
507 }
508
509
510
511
512
513
514
515
516 void slowScan()
517 {
518 if ( haveSlowResults )
519 {
520 return;
521 }
522
523 final String[] excl = dirsExcluded.toArray( new String[dirsExcluded.size()] );
524
525 final String[] notIncl = dirsNotIncluded.toArray( new String[dirsNotIncluded.size()] );
526
527 for ( String anExcl : excl )
528 {
529 if ( !couldHoldIncluded( anExcl ) )
530 {
531 scandir( new File( basedir, anExcl ), anExcl + File.separator, false );
532 }
533 }
534
535 for ( String aNotIncl : notIncl )
536 {
537 if ( !couldHoldIncluded( aNotIncl ) )
538 {
539 scandir( new File( basedir, aNotIncl ), aNotIncl + File.separator, false );
540 }
541 }
542
543 haveSlowResults = true;
544 }
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563 void scandir( @Nonnull final File dir, @Nonnull final String vpath, final boolean fast )
564 {
565 String[] newfiles = dir.list();
566
567 if ( newfiles == null )
568 {
569
570
571
572
573
574
575
576
577
578
579 newfiles = new String[0];
580
581
582 }
583
584 if ( !followSymlinks )
585 {
586 newfiles = doNotFollowSymbolicLinks( dir, vpath, newfiles );
587 }
588
589 for ( final String newfile : newfiles )
590 {
591 final String name = vpath + newfile;
592 final File file = new File( dir, newfile );
593 if ( file.isDirectory() )
594 {
595 if ( isIncluded( name ) )
596 {
597 if ( !isExcluded( name ) )
598 {
599 if ( scanConductor != null )
600 {
601 scanAction = scanConductor.visitDirectory( name, file );
602
603 if ( ScanConductor.ScanAction.ABORT.equals( scanAction )
604 || ScanConductor.ScanAction.ABORT_DIRECTORY.equals( scanAction ) )
605 {
606 return;
607 }
608 }
609
610 if ( !ScanConductor.ScanAction.NO_RECURSE.equals( scanAction ) )
611 {
612 dirsIncluded.add( name );
613 if ( fast )
614 {
615 scandir( file, name + File.separator, fast );
616
617 if ( ScanConductor.ScanAction.ABORT.equals( scanAction ) )
618 {
619 return;
620 }
621 }
622 }
623 scanAction = null;
624
625 }
626 else
627 {
628 dirsExcluded.add( name );
629 if ( fast && couldHoldIncluded( name ) )
630 {
631 scandir( file, name + File.separator, fast );
632 if ( ScanConductor.ScanAction.ABORT.equals( scanAction ) )
633 {
634 return;
635 }
636 scanAction = null;
637 }
638 }
639 }
640 else
641 {
642 if ( fast && couldHoldIncluded( name ) )
643 {
644 if ( scanConductor != null )
645 {
646 scanAction = scanConductor.visitDirectory( name, file );
647
648 if ( ScanConductor.ScanAction.ABORT.equals( scanAction )
649 || ScanConductor.ScanAction.ABORT_DIRECTORY.equals( scanAction ) )
650 {
651 return;
652 }
653 }
654 if ( !ScanConductor.ScanAction.NO_RECURSE.equals( scanAction ) )
655 {
656 dirsNotIncluded.add( name );
657
658 scandir( file, name + File.separator, fast );
659 if ( ScanConductor.ScanAction.ABORT.equals( scanAction ) )
660 {
661 return;
662 }
663 }
664 scanAction = null;
665 }
666 }
667 if ( !fast )
668 {
669 scandir( file, name + File.separator, fast );
670 if ( ScanConductor.ScanAction.ABORT.equals( scanAction ) )
671 {
672 return;
673 }
674 scanAction = null;
675 }
676 }
677 else if ( file.isFile() )
678 {
679 if ( isIncluded( name ) )
680 {
681 if ( !isExcluded( name ) )
682 {
683 if ( scanConductor != null )
684 {
685 scanAction = scanConductor.visitFile( name, file );
686 }
687
688 if ( ScanConductor.ScanAction.ABORT.equals( scanAction )
689 || ScanConductor.ScanAction.ABORT_DIRECTORY.equals( scanAction ) )
690 {
691 return;
692 }
693
694 filesIncluded.add( name );
695 }
696 else
697 {
698 filesExcluded.add( name );
699 }
700 }
701 else
702 {
703 filesNotIncluded.add( name );
704 }
705 }
706 }
707 }
708
709 private String[] doNotFollowSymbolicLinks( final File dir, final String vpath, String[] newfiles )
710 {
711 final List<String> noLinks = new ArrayList<String>();
712 for ( final String newfile : newfiles )
713 {
714 try
715 {
716 if ( isSymbolicLink( dir, newfile ) )
717 {
718 final String name = vpath + newfile;
719 final File file = new File( dir, newfile );
720 if ( file.isDirectory() )
721 {
722 dirsExcluded.add( name );
723 }
724 else
725 {
726 filesExcluded.add( name );
727 }
728 }
729 else
730 {
731 noLinks.add( newfile );
732 }
733 }
734 catch ( final IOException ioe )
735 {
736 final String msg =
737 "IOException caught while checking " + "for links, couldn't get cannonical path!";
738
739 System.err.println( msg );
740 noLinks.add( newfile );
741 }
742 }
743 newfiles = noLinks.toArray( new String[noLinks.size()] );
744 return newfiles;
745 }
746
747
748
749
750
751
752
753
754 boolean isIncluded( final String name )
755 {
756 return includesPatterns.matches( name, isCaseSensitive );
757 }
758
759
760
761
762
763
764
765
766 boolean couldHoldIncluded( @Nonnull final String name )
767 {
768 return includesPatterns.matchesPatternStart( name, isCaseSensitive );
769 }
770
771
772
773
774
775
776
777
778 boolean isExcluded( @Nonnull final String name )
779 {
780 return excludesPatterns.matches( name, isCaseSensitive );
781 }
782
783
784
785
786
787
788
789
790 public String[] getIncludedFiles()
791 {
792 if ( filesIncluded == null )
793 {
794 return new String[0];
795 }
796 return filesIncluded.toArray( new String[filesIncluded.size()] );
797 }
798
799
800
801
802
803
804
805
806 public String[] getNotIncludedFiles()
807 {
808 slowScan();
809 return filesNotIncluded.toArray( new String[filesNotIncluded.size()] );
810 }
811
812
813
814
815
816
817
818
819
820
821 public String[] getExcludedFiles()
822 {
823 slowScan();
824 return filesExcluded.toArray( new String[filesExcluded.size()] );
825 }
826
827
828
829
830
831
832
833
834 public String[] getIncludedDirectories()
835 {
836 return dirsIncluded.toArray( new String[dirsIncluded.size()] );
837 }
838
839
840
841
842
843
844
845
846 public String[] getNotIncludedDirectories()
847 {
848 slowScan();
849 return dirsNotIncluded.toArray( new String[dirsNotIncluded.size()] );
850 }
851
852
853
854
855
856
857
858
859
860
861 public String[] getExcludedDirectories()
862 {
863 slowScan();
864 return dirsExcluded.toArray( new String[dirsExcluded.size()] );
865 }
866
867
868
869
870 public void addDefaultExcludes()
871 {
872 final int excludesLength = excludes == null ? 0 : excludes.length;
873 String[] newExcludes;
874 newExcludes = new String[excludesLength + DEFAULTEXCLUDES.length];
875 if ( excludesLength > 0 )
876 {
877 System.arraycopy( excludes, 0, newExcludes, 0, excludesLength );
878 }
879 for ( int i = 0; i < DEFAULTEXCLUDES.length; i++ )
880 {
881 newExcludes[i + excludesLength] =
882 DEFAULTEXCLUDES[i].replace( '/', File.separatorChar ).replace( '\\', File.separatorChar );
883 }
884 excludes = newExcludes;
885 }
886
887
888
889
890
891
892
893
894
895
896
897
898 boolean isSymbolicLink( final File parent, final String name )
899 throws IOException
900 {
901 if ( Java7Support.isAtLeastJava7() )
902 {
903 return Java7Support.isSymLink( parent );
904 }
905 final File resolvedParent = new File( parent.getCanonicalPath() );
906 final File toTest = new File( resolvedParent, name );
907 return !toTest.getAbsolutePath().equals( toTest.getCanonicalPath() );
908 }
909
910 private void setupDefaultFilters()
911 {
912 if ( includes == null )
913 {
914
915 includes = new String[1];
916 includes[0] = "**";
917 }
918 if ( excludes == null )
919 {
920 excludes = new String[0];
921 }
922 }
923
924
925 private void setupMatchPatterns()
926 {
927 includesPatterns = MatchPatterns.from( includes );
928 excludesPatterns = MatchPatterns.from( excludes );
929 }
930
931 }