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