-
Notifications
You must be signed in to change notification settings - Fork 422
/
DirectoryScanner.java
1847 lines (1703 loc) · 66.5 KB
/
DirectoryScanner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.tools.ant;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.apache.tools.ant.taskdefs.condition.Os;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceFactory;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.types.selectors.FileSelector;
import org.apache.tools.ant.types.selectors.SelectorScanner;
import org.apache.tools.ant.types.selectors.SelectorUtils;
import org.apache.tools.ant.types.selectors.TokenizedPath;
import org.apache.tools.ant.types.selectors.TokenizedPattern;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.VectorSet;
/**
* Class for scanning a directory for files/directories which match certain
* criteria.
* <p>
* These criteria consist of selectors and patterns which have been specified.
* With the selectors you can select which files you want to have included.
* Files which are not selected are excluded. With patterns you can include
* or exclude files based on their filename.
* </p>
* <p>
* The idea is simple. A given directory is recursively scanned for all files
* and directories. Each file/directory is matched against a set of selectors,
* including special support for matching against filenames with include and
* and exclude patterns. Only files/directories which match at least one
* pattern of the include pattern list or other file selector, and don't match
* any pattern of the exclude pattern list or fail to match against a required
* selector will be placed in the list of files/directories found.
* </p>
* <p>
* When no list of include patterns is supplied, "**" will be used, which
* means that everything will be matched. When no list of exclude patterns is
* supplied, an empty list is used, such that nothing will be excluded. When
* no selectors are supplied, none are applied.
* </p>
* <p>
* The filename pattern matching is done as follows:
* The name to be matched is split up in path segments. A path segment is the
* name of a directory or file, which is bounded by
* <code>File.separator</code> ('/' under UNIX, '\' under Windows).
* For example, "abc/def/ghi/xyz.java" is split up in the segments "abc",
* "def","ghi" and "xyz.java".
* The same is done for the pattern against which should be matched.
* </p>
* <p>
* The segments of the name and the pattern are then matched against each
* other. When '**' is used for a path segment in the pattern, it matches
* zero or more path segments of the name.
* </p>
* <p>
* There is a special case regarding the use of <code>File.separator</code>s
* at the beginning of the pattern and the string to match:
* </p>
* <ul>
* <li>When a pattern starts with a <code>File.separator</code>, the string
* to match must also start with a <code>File.separator</code>.</li>
* <li>When a pattern does not start with a <code>File.separator</code>, the
* string to match may not start with a <code>File.separator</code>.</li>
* <li>When one of the above rules is not obeyed, the string will not
* match.</li>
* </ul>
* <p>
* When a name path segment is matched against a pattern path segment, the
* following special characters can be used:<br>
* '*' matches zero or more characters<br>
* '?' matches one character.
* </p>
* <p>
* Examples:
* </p>
* <p>
* "**\*.class" matches all .class files/dirs in a directory tree.
* </p>
* <p>
* "test\a??.java" matches all files/dirs which start with an 'a', then two
* more characters and then ".java", in a directory called test.
* </p>
* <p>
* "**" matches everything in a directory tree.
* </p>
* <p>
* "**\test\**\XYZ*" matches all files/dirs which start with "XYZ" and where
* there is a parent directory called test (e.g. "abc\test\def\ghi\XYZ123").
* </p>
* <p>
* Case sensitivity may be turned off if necessary. By default, it is
* turned on.
* </p>
* <p>
* Example of usage:
* </p>
* <pre>
* String[] includes = {"**\\*.class"};
* String[] excludes = {"modules\\*\\**"};
* ds.setIncludes(includes);
* ds.setExcludes(excludes);
* ds.setBasedir(new File("test"));
* ds.setCaseSensitive(true);
* ds.scan();
*
* System.out.println("FILES:");
* String[] files = ds.getIncludedFiles();
* for (int i = 0; i < files.length; i++) {
* System.out.println(files[i]);
* }
* </pre>
* This will scan a directory called test for .class files, but excludes all
* files in all proper subdirectories of a directory called "modules".
*
*/
public class DirectoryScanner
implements FileScanner, SelectorScanner, ResourceFactory {
/** Is OpenVMS the operating system we're running on? */
private static final boolean ON_VMS = Os.isFamily("openvms");
/**
* Patterns which should be excluded by default.
*
* <p>Note that you can now add patterns to the list of default
* excludes. Added patterns will not become part of this array
* that has only been kept around for backwards compatibility
* reasons.</p>
*
* @deprecated since 1.6.x.
* Use the {@link #getDefaultExcludes getDefaultExcludes}
* method instead.
*/
@Deprecated
protected static final String[] DEFAULTEXCLUDES = { //NOSONAR
// Miscellaneous typical temporary files
SelectorUtils.DEEP_TREE_MATCH + "/*~",
SelectorUtils.DEEP_TREE_MATCH + "/#*#",
SelectorUtils.DEEP_TREE_MATCH + "/.#*",
SelectorUtils.DEEP_TREE_MATCH + "/%*%",
SelectorUtils.DEEP_TREE_MATCH + "/._*",
// CVS
SelectorUtils.DEEP_TREE_MATCH + "/CVS",
SelectorUtils.DEEP_TREE_MATCH + "/CVS/" + SelectorUtils.DEEP_TREE_MATCH,
SelectorUtils.DEEP_TREE_MATCH + "/.cvsignore",
// SCCS
SelectorUtils.DEEP_TREE_MATCH + "/SCCS",
SelectorUtils.DEEP_TREE_MATCH + "/SCCS/" + SelectorUtils.DEEP_TREE_MATCH,
// Visual SourceSafe
SelectorUtils.DEEP_TREE_MATCH + "/vssver.scc",
// Subversion
SelectorUtils.DEEP_TREE_MATCH + "/.svn",
SelectorUtils.DEEP_TREE_MATCH + "/.svn/" + SelectorUtils.DEEP_TREE_MATCH,
// Git
SelectorUtils.DEEP_TREE_MATCH + "/.git",
SelectorUtils.DEEP_TREE_MATCH + "/.git/" + SelectorUtils.DEEP_TREE_MATCH,
SelectorUtils.DEEP_TREE_MATCH + "/.gitattributes",
SelectorUtils.DEEP_TREE_MATCH + "/.gitignore",
SelectorUtils.DEEP_TREE_MATCH + "/.gitmodules",
// Mercurial
SelectorUtils.DEEP_TREE_MATCH + "/.hg",
SelectorUtils.DEEP_TREE_MATCH + "/.hg/" + SelectorUtils.DEEP_TREE_MATCH,
SelectorUtils.DEEP_TREE_MATCH + "/.hgignore",
SelectorUtils.DEEP_TREE_MATCH + "/.hgsub",
SelectorUtils.DEEP_TREE_MATCH + "/.hgsubstate",
SelectorUtils.DEEP_TREE_MATCH + "/.hgtags",
// Bazaar
SelectorUtils.DEEP_TREE_MATCH + "/.bzr",
SelectorUtils.DEEP_TREE_MATCH + "/.bzr/" + SelectorUtils.DEEP_TREE_MATCH,
SelectorUtils.DEEP_TREE_MATCH + "/.bzrignore",
// Mac
SelectorUtils.DEEP_TREE_MATCH + "/.DS_Store"
};
/**
* default value for {@link #maxLevelsOfSymlinks maxLevelsOfSymlinks}
* @since Ant 1.8.0
*/
public static final int MAX_LEVELS_OF_SYMLINKS = 5;
/**
* The end of the exception message if something that should be
* there doesn't exist.
*/
public static final String DOES_NOT_EXIST_POSTFIX = " does not exist.";
/** Helper. */
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
/**
* Patterns which should be excluded by default.
*
* @see #addDefaultExcludes()
*/
private static final Set<String> defaultExcludes = new HashSet<>();
static {
resetDefaultExcludes();
}
// CheckStyle:VisibilityModifier OFF - bc
/** The base directory to be scanned. */
protected File basedir;
/** The patterns for the files to be included. */
protected String[] includes;
/** The patterns for the files to be excluded. */
protected String[] excludes;
/** Selectors that will filter which files are in our candidate list. */
protected FileSelector[] selectors = null;
/**
* The files which matched at least one include and no excludes
* and were selected.
*/
protected Vector<String> filesIncluded;
/** The files which did not match any includes or selectors. */
protected Vector<String> filesNotIncluded;
/**
* The files which matched at least one include and at least
* one exclude.
*/
protected Vector<String> filesExcluded;
/**
* The directories which matched at least one include and no excludes
* and were selected.
*/
protected Vector<String> dirsIncluded;
/** The directories which were found and did not match any includes. */
protected Vector<String> dirsNotIncluded;
/**
* The directories which matched at least one include and at least one
* exclude.
*/
protected Vector<String> dirsExcluded;
/**
* The files which matched at least one include and no excludes and
* which a selector discarded.
*/
protected Vector<String> filesDeselected;
/**
* The directories which matched at least one include and no excludes
* but which a selector discarded.
*/
protected Vector<String> dirsDeselected;
/** Whether or not our results were built by a slow scan. */
protected boolean haveSlowResults = false;
/**
* Whether or not the file system should be treated as a case sensitive
* one.
*/
protected boolean isCaseSensitive = true;
/**
* Whether a missing base directory is an error.
* @since Ant 1.7.1
*/
protected boolean errorOnMissingDir = true;
/**
* Whether or not symbolic links should be followed.
*
* @since Ant 1.5
*/
private boolean followSymlinks = true;
/** Whether or not everything tested so far has been included. */
protected boolean everythingIncluded = true;
// CheckStyle:VisibilityModifier ON
/**
* List of all scanned directories.
*
* @since Ant 1.6
*/
private final Set<String> scannedDirs = new HashSet<>();
/**
* Map of all include patterns that are full file names and don't
* contain any wildcards.
*
* <p>Maps pattern string to TokenizedPath.</p>
*
* <p>If this instance is not case sensitive, the file names get
* turned to upper case.</p>
*
* <p>Gets lazily initialized on the first invocation of
* isIncluded or isExcluded and cleared at the end of the scan
* method (cleared in clearCaches, actually).</p>
*
* @since Ant 1.8.0
*/
private final Map<String, TokenizedPath> includeNonPatterns = new HashMap<>();
/**
* Map of all exclude patterns that are full file names and don't
* contain any wildcards.
*
* <p>Maps pattern string to TokenizedPath.</p>
*
* <p>If this instance is not case sensitive, the file names get
* turned to upper case.</p>
*
* <p>Gets lazily initialized on the first invocation of
* isIncluded or isExcluded and cleared at the end of the scan
* method (cleared in clearCaches, actually).</p>
*
* @since Ant 1.8.0
*/
private final Map<String, TokenizedPath> excludeNonPatterns = new HashMap<>();
/**
* Array of all include patterns that contain wildcards.
*
* <p>Gets lazily initialized on the first invocation of
* isIncluded or isExcluded and cleared at the end of the scan
* method (cleared in clearCaches, actually).</p>
*/
private TokenizedPattern[] includePatterns;
/**
* Array of all exclude patterns that contain wildcards.
*
* <p>Gets lazily initialized on the first invocation of
* isIncluded or isExcluded and cleared at the end of the scan
* method (cleared in clearCaches, actually).</p>
*/
private TokenizedPattern[] excludePatterns;
/**
* Have the non-pattern sets and pattern arrays for in- and
* excludes been initialized?
*
* @since Ant 1.6.3
*/
private boolean areNonPatternSetsReady = false;
/**
* Scanning flag.
*
* @since Ant 1.6.3
*/
private boolean scanning = false;
/**
* Scanning lock.
*
* @since Ant 1.6.3
*/
private final Object scanLock = new Object();
/**
* Slow scanning flag.
*
* @since Ant 1.6.3
*/
private boolean slowScanning = false;
/**
* Slow scanning lock.
*
* @since Ant 1.6.3
*/
private final Object slowScanLock = new Object();
/**
* Exception thrown during scan.
*
* @since Ant 1.6.3
*/
private IllegalStateException illegal = null;
/**
* The maximum number of times a symbolic link may be followed
* during a scan.
*
* @since Ant 1.8.0
*/
private int maxLevelsOfSymlinks = MAX_LEVELS_OF_SYMLINKS;
/**
* Absolute paths of all symlinks that haven't been followed but
* would have been if followsymlinks had been true or
* maxLevelsOfSymlinks had been higher.
*
* @since Ant 1.8.0
*/
private final Set<String> notFollowedSymlinks = new HashSet<>();
/**
* Test whether or not a given path matches the start of a given
* pattern up to the first "**".
* <p>
* This is not a general purpose test and should only be used if you
* can live with false positives. For example, <code>pattern=**\a</code>
* and <code>str=b</code> will yield <code>true</code>.
*
* @param pattern The pattern to match against. Must not be
* <code>null</code>.
* @param str The path to match, as a String. Must not be
* <code>null</code>.
*
* @return whether or not a given path matches the start of a given
* pattern up to the first "**".
*/
protected static boolean matchPatternStart(final String pattern, final String str) {
return SelectorUtils.matchPatternStart(pattern, str);
}
/**
* Test whether or not a given path matches the start of a given
* pattern up to the first "**".
* <p>
* This is not a general purpose test and should only be used if you
* can live with false positives. For example, <code>pattern=**\a</code>
* and <code>str=b</code> will yield <code>true</code>.
*
* @param pattern The pattern to match against. Must not be
* <code>null</code>.
* @param str The path to match, as a String. Must not be
* <code>null</code>.
* @param isCaseSensitive Whether or not matching should be performed
* case sensitively.
*
* @return whether or not a given path matches the start of a given
* pattern up to the first "**".
*/
protected static boolean matchPatternStart(final String pattern, final String str,
final boolean isCaseSensitive) {
return SelectorUtils.matchPatternStart(pattern, str, isCaseSensitive);
}
/**
* Test whether or not a given path matches a given pattern.
*
* @param pattern The pattern to match against. Must not be
* <code>null</code>.
* @param str The path to match, as a String. Must not be
* <code>null</code>.
*
* @return <code>true</code> if the pattern matches against the string,
* or <code>false</code> otherwise.
*/
protected static boolean matchPath(final String pattern, final String str) {
return SelectorUtils.matchPath(pattern, str);
}
/**
* Test whether or not a given path matches a given pattern.
*
* @param pattern The pattern to match against. Must not be
* <code>null</code>.
* @param str The path to match, as a String. Must not be
* <code>null</code>.
* @param isCaseSensitive Whether or not matching should be performed
* case sensitively.
*
* @return <code>true</code> if the pattern matches against the string,
* or <code>false</code> otherwise.
*/
protected static boolean matchPath(final String pattern, final String str,
final boolean isCaseSensitive) {
return SelectorUtils.matchPath(pattern, str, isCaseSensitive);
}
/**
* Test whether or not a string matches against a pattern.
* The pattern may contain two special characters:<br>
* '*' means zero or more characters<br>
* '?' means one and only one character
*
* @param pattern The pattern to match against.
* Must not be <code>null</code>.
* @param str The string which must be matched against the pattern.
* Must not be <code>null</code>.
*
* @return <code>true</code> if the string matches against the pattern,
* or <code>false</code> otherwise.
*/
public static boolean match(final String pattern, final String str) {
return SelectorUtils.match(pattern, str);
}
/**
* Test whether or not a string matches against a pattern.
* The pattern may contain two special characters:<br>
* '*' means zero or more characters<br>
* '?' means one and only one character
*
* @param pattern The pattern to match against.
* Must not be <code>null</code>.
* @param str The string which must be matched against the pattern.
* Must not be <code>null</code>.
* @param isCaseSensitive Whether or not matching should be performed
* case sensitively.
*
*
* @return <code>true</code> if the string matches against the pattern,
* or <code>false</code> otherwise.
*/
protected static boolean match(final String pattern, final String str,
final boolean isCaseSensitive) {
return SelectorUtils.match(pattern, str, isCaseSensitive);
}
/**
* Get the list of patterns that should be excluded by default.
*
* @return An array of <code>String</code> based on the current
* contents of the <code>defaultExcludes</code>
* <code>Set</code>.
*
* @since Ant 1.6
*/
public static String[] getDefaultExcludes() {
synchronized (defaultExcludes) {
return defaultExcludes.toArray(new String[defaultExcludes
.size()]);
}
}
/**
* Add a pattern to the default excludes unless it is already a
* default exclude.
*
* @param s A string to add as an exclude pattern.
* @return <code>true</code> if the string was added;
* <code>false</code> if it already existed.
*
* @since Ant 1.6
*/
public static boolean addDefaultExclude(final String s) {
synchronized (defaultExcludes) {
return defaultExcludes.add(s);
}
}
/**
* Remove a string if it is a default exclude.
*
* @param s The string to attempt to remove.
* @return <code>true</code> if <code>s</code> was a default
* exclude (and thus was removed);
* <code>false</code> if <code>s</code> was not
* in the default excludes list to begin with.
*
* @since Ant 1.6
*/
public static boolean removeDefaultExclude(final String s) {
synchronized (defaultExcludes) {
return defaultExcludes.remove(s);
}
}
/**
* Go back to the hardwired default exclude patterns.
*
* @since Ant 1.6
*/
public static void resetDefaultExcludes() {
synchronized (defaultExcludes) {
defaultExcludes.clear();
for (String element : DEFAULTEXCLUDES) {
defaultExcludes.add(element);
}
}
}
/**
* Set the base directory to be scanned. This is the directory which is
* scanned recursively. All '/' and '\' characters are replaced by
* <code>File.separatorChar</code>, so the separator used need not match
* <code>File.separatorChar</code>.
*
* @param basedir The base directory to scan.
*/
@Override
public void setBasedir(final String basedir) {
setBasedir(basedir == null ? null
: new File(basedir.replace('/', File.separatorChar).replace(
'\\', File.separatorChar)));
}
/**
* Set the base directory to be scanned. This is the directory which is
* scanned recursively.
*
* @param basedir The base directory for scanning.
*/
@Override
public synchronized void setBasedir(final File basedir) {
this.basedir = basedir;
}
/**
* Return the base directory to be scanned.
* This is the directory which is scanned recursively.
*
* @return the base directory to be scanned.
*/
@Override
public synchronized File getBasedir() {
return basedir;
}
/**
* Find out whether include exclude patterns are matched in a
* case sensitive way.
* @return whether or not the scanning is case sensitive.
* @since Ant 1.6
*/
public synchronized boolean isCaseSensitive() {
return isCaseSensitive;
}
/**
* Set whether or not include and exclude patterns are matched
* in a case sensitive way.
*
* @param isCaseSensitive whether or not the file system should be
* regarded as a case sensitive one.
*/
@Override
public synchronized void setCaseSensitive(final boolean isCaseSensitive) {
this.isCaseSensitive = isCaseSensitive;
}
/**
* Sets whether or not a missing base directory is an error
*
* @param errorOnMissingDir whether or not a missing base directory
* is an error
* @since Ant 1.7.1
*/
public void setErrorOnMissingDir(final boolean errorOnMissingDir) {
this.errorOnMissingDir = errorOnMissingDir;
}
/**
* Get whether or not a DirectoryScanner follows symbolic links.
*
* @return flag indicating whether symbolic links should be followed.
*
* @since Ant 1.6
*/
public synchronized boolean isFollowSymlinks() {
return followSymlinks;
}
/**
* Set whether or not symbolic links should be followed.
*
* @param followSymlinks whether or not symbolic links should be followed.
*/
public synchronized void setFollowSymlinks(final boolean followSymlinks) {
this.followSymlinks = followSymlinks;
}
/**
* The maximum number of times a symbolic link may be followed
* during a scan.
*
* @param max int
* @since Ant 1.8.0
*/
public void setMaxLevelsOfSymlinks(final int max) {
maxLevelsOfSymlinks = max;
}
/**
* Set the list of include patterns to use. All '/' and '\' characters
* are replaced by <code>File.separatorChar</code>, so the separator used
* need not match <code>File.separatorChar</code>.
* <p>
* When a pattern ends with a '/' or '\', "**" is appended.
*
* @param includes A list of include patterns.
* May be <code>null</code>, indicating that all files
* should be included. If a non-<code>null</code>
* list is given, all elements must be
* non-<code>null</code>.
*/
@Override
public synchronized void setIncludes(final String[] includes) {
if (includes == null) {
this.includes = null;
} else {
this.includes = Stream.of(includes)
.map(DirectoryScanner::normalizePattern).toArray(String[]::new);
}
}
/**
* Set the list of exclude patterns to use. All '/' and '\' characters
* are replaced by <code>File.separatorChar</code>, so the separator used
* need not match <code>File.separatorChar</code>.
* <p>
* When a pattern ends with a '/' or '\', "**" is appended.
*
* @param excludes A list of exclude patterns.
* May be <code>null</code>, indicating that no files
* should be excluded. If a non-<code>null</code> list is
* given, all elements must be non-<code>null</code>.
*/
@Override
public synchronized void setExcludes(final String[] excludes) {
if (excludes == null) {
this.excludes = null;
} else {
this.excludes = Stream.of(excludes).map(DirectoryScanner::normalizePattern)
.toArray(String[]::new);
}
}
/**
* Add to the list of exclude patterns to use. All '/' and '\'
* characters are replaced by <code>File.separatorChar</code>, so
* the separator used need not match <code>File.separatorChar</code>.
* <p>
* When a pattern ends with a '/' or '\', "**" is appended.
*
* @param excludes A list of exclude patterns.
* May be <code>null</code>, in which case the
* exclude patterns don't get changed at all.
*
* @since Ant 1.6.3
*/
public synchronized void addExcludes(final String[] excludes) {
if (excludes != null && excludes.length > 0) {
if (this.excludes == null || this.excludes.length == 0) {
setExcludes(excludes);
} else {
this.excludes = Stream.concat(Stream.of(this.excludes),
Stream.of(excludes).map(DirectoryScanner::normalizePattern))
.toArray(String[]::new);
}
}
}
/**
* All '/' and '\' characters are replaced by
* <code>File.separatorChar</code>, so the separator used need not
* match <code>File.separatorChar</code>.
*
* <p>When a pattern ends with a '/' or '\', "**" is appended.</p>
*
* @since Ant 1.6.3
*/
private static String normalizePattern(final String p) {
String pattern = p.replace('/', File.separatorChar)
.replace('\\', File.separatorChar);
if (pattern.endsWith(File.separator)) {
pattern += SelectorUtils.DEEP_TREE_MATCH;
}
return pattern;
}
/**
* Set the selectors that will select the filelist.
*
* @param selectors specifies the selectors to be invoked on a scan.
*/
@Override
public synchronized void setSelectors(final FileSelector[] selectors) {
this.selectors = selectors;
}
/**
* Return whether or not the scanner has included all the files or
* directories it has come across so far.
*
* @return <code>true</code> if all files and directories which have
* been found so far have been included.
*/
public synchronized boolean isEverythingIncluded() {
return everythingIncluded;
}
/**
* Scan for files which match at least one include pattern and don't match
* any exclude patterns. If there are selectors then the files must pass
* muster there, as well. Scans under basedir, if set; otherwise the
* include patterns without leading wildcards specify the absolute paths of
* the files that may be included.
*
* @exception IllegalStateException if the base directory was set
* incorrectly (i.e. if it doesn't exist or isn't a directory).
*/
@Override
public void scan() throws IllegalStateException {
synchronized (scanLock) {
if (scanning) {
while (scanning) {
try {
scanLock.wait();
} catch (final InterruptedException ignored) {
}
}
if (illegal != null) {
throw illegal;
}
return;
}
scanning = true;
}
final File savedBase = basedir;
try {
synchronized (this) {
illegal = null;
clearResults();
// set in/excludes to reasonable defaults if needed:
final boolean nullIncludes = includes == null;
includes = nullIncludes ? new String[] {SelectorUtils.DEEP_TREE_MATCH} : includes;
final boolean nullExcludes = excludes == null;
excludes = nullExcludes ? new String[0] : excludes;
if (basedir != null && !followSymlinks
&& Files.isSymbolicLink(basedir.toPath())) {
notFollowedSymlinks.add(basedir.getAbsolutePath());
basedir = null;
}
if (basedir == null) {
// if no basedir and no includes, nothing to do:
if (nullIncludes) {
return;
}
} else {
if (!basedir.exists()) {
if (errorOnMissingDir) {
illegal = new IllegalStateException("basedir "
+ basedir
+ DOES_NOT_EXIST_POSTFIX);
} else {
// Nothing to do - basedir does not exist
return;
}
} else if (!basedir.isDirectory()) {
illegal = new IllegalStateException("basedir "
+ basedir
+ " is not a directory.");
}
if (illegal != null) {
throw illegal;
}
}
if (isIncluded(TokenizedPath.EMPTY_PATH)) {
if (isExcluded(TokenizedPath.EMPTY_PATH)) {
dirsExcluded.addElement("");
} else if (isSelected("", basedir)) {
dirsIncluded.addElement("");
} else {
dirsDeselected.addElement("");
}
} else {
dirsNotIncluded.addElement("");
}
checkIncludePatterns();
clearCaches();
includes = nullIncludes ? null : includes;
excludes = nullExcludes ? null : excludes;
}
} finally {
basedir = savedBase;
synchronized (scanLock) {
scanning = false;
scanLock.notifyAll();
}
}
}
/**
* This routine is actually checking all the include patterns in
* order to avoid scanning everything under base dir.
* @since Ant 1.6
*/
private void checkIncludePatterns() {
ensureNonPatternSetsReady();
final Map<TokenizedPath, String> newroots = new HashMap<>();
// put in the newroots map the include patterns without
// wildcard tokens
for (TokenizedPattern includePattern : includePatterns) {
final String pattern = includePattern.toString();
if (!shouldSkipPattern(pattern)) {
newroots.put(includePattern.rtrimWildcardTokens(), pattern);
}
}
for (final Map.Entry<String, TokenizedPath> entry : includeNonPatterns
.entrySet()) {
final String pattern = entry.getKey();
if (!shouldSkipPattern(pattern)) {
newroots.put(entry.getValue(), pattern);
}
}
if (newroots.containsKey(TokenizedPath.EMPTY_PATH)
&& basedir != null) {
// we are going to scan everything anyway
scandir(basedir, "", true);
} else {
File canonBase = null;
if (basedir != null) {
try {
canonBase = basedir.getCanonicalFile();
} catch (final IOException ex) {
throw new BuildException(ex);
}
}
// only scan directories that can include matched files or
// directories
for (final Map.Entry<TokenizedPath, String> entry : newroots.entrySet()) {
TokenizedPath currentPath = entry.getKey();
String currentelement = currentPath.toString();
if (basedir == null && !FileUtils.isAbsolutePath(currentelement)) {
continue;
}
File myfile = new File(basedir, currentelement);
if (myfile.exists()) {
// may be on a case insensitive file system. We want
// the results to show what's really on the disk, so
// we need to double check.
try {
final String path = (basedir == null)
? myfile.getCanonicalPath()
: FILE_UTILS.removeLeadingPath(canonBase,
myfile.getCanonicalFile());
if (!path.equals(currentelement) || ON_VMS) {
myfile = currentPath.findFile(basedir, true);
if (myfile != null && basedir != null) {
currentelement = FILE_UTILS.removeLeadingPath(
basedir, myfile);
if (!currentPath.toString().equals(currentelement)) {
currentPath = new TokenizedPath(currentelement);
}
}
}
} catch (final IOException ex) {
throw new BuildException(ex);
}
}