forked from pranjalbajaj/java7-sourcecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTreeWalker.java
More file actions
248 lines (225 loc) · 7.84 KB
/
Copy pathFileTreeWalker.java
File metadata and controls
248 lines (225 loc) · 7.84 KB
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
/*
* Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.nio.file;
import java.nio.file.attribute.*;
import java.io.IOException;
import java.util.*;
import sun.nio.fs.BasicFileAttributesHolder;
/**
* Simple file tree walker that works in a similar manner to nftw(3C).
*
* @see Files#walkFileTree
*/
class FileTreeWalker {
private final boolean followLinks;
private final LinkOption[] linkOptions;
private final FileVisitor<? super Path> visitor;
private final int maxDepth;
FileTreeWalker(Set<FileVisitOption> options,
FileVisitor<? super Path> visitor,
int maxDepth)
{
boolean fl = false;
for (FileVisitOption option: options) {
// will throw NPE if options contains null
switch (option) {
case FOLLOW_LINKS : fl = true; break;
default:
throw new AssertionError("Should not get here");
}
}
this.followLinks = fl;
this.linkOptions = (fl) ? new LinkOption[0] :
new LinkOption[] { LinkOption.NOFOLLOW_LINKS };
this.visitor = visitor;
this.maxDepth = maxDepth;
}
/**
* Walk file tree starting at the given file
*/
void walk(Path start) throws IOException {
FileVisitResult result = walk(start,
0,
new ArrayList<AncestorDirectory>());
Objects.requireNonNull(result, "FileVisitor returned null");
}
/**
* @param file
* the directory to visit
* @param depth
* depth remaining
* @param ancestors
* use when cycle detection is enabled
*/
private FileVisitResult walk(Path file,
int depth,
List<AncestorDirectory> ancestors)
throws IOException
{
// if attributes are cached then use them if possible
BasicFileAttributes attrs = null;
if ((depth > 0) &&
(file instanceof BasicFileAttributesHolder) &&
(System.getSecurityManager() == null))
{
BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
if (cached != null && (!followLinks || !cached.isSymbolicLink()))
attrs = cached;
}
IOException exc = null;
// attempt to get attributes of file. If fails and we are following
// links then a link target might not exist so get attributes of link
if (attrs == null) {
try {
try {
attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
} catch (IOException x1) {
if (followLinks) {
try {
attrs = Files.readAttributes(file,
BasicFileAttributes.class,
LinkOption.NOFOLLOW_LINKS);
} catch (IOException x2) {
exc = x2;
}
} else {
exc = x1;
}
}
} catch (SecurityException x) {
// If access to starting file is denied then SecurityException
// is thrown, otherwise the file is ignored.
if (depth == 0)
throw x;
return FileVisitResult.CONTINUE;
}
}
// unable to get attributes of file
if (exc != null) {
return visitor.visitFileFailed(file, exc);
}
// at maximum depth or file is not a directory
if (depth >= maxDepth || !attrs.isDirectory()) {
return visitor.visitFile(file, attrs);
}
// check for cycles when following links
if (followLinks) {
Object key = attrs.fileKey();
// if this directory and ancestor has a file key then we compare
// them; otherwise we use less efficient isSameFile test.
for (AncestorDirectory ancestor: ancestors) {
Object ancestorKey = ancestor.fileKey();
if (key != null && ancestorKey != null) {
if (key.equals(ancestorKey)) {
// cycle detected
return visitor.visitFileFailed(file,
new FileSystemLoopException(file.toString()));
}
} else {
boolean isSameFile = false;
try {
isSameFile = Files.isSameFile(file, ancestor.file());
} catch (IOException x) {
// ignore
} catch (SecurityException x) {
// ignore
}
if (isSameFile) {
// cycle detected
return visitor.visitFileFailed(file,
new FileSystemLoopException(file.toString()));
}
}
}
ancestors.add(new AncestorDirectory(file, key));
}
// visit directory
try {
DirectoryStream<Path> stream = null;
FileVisitResult result;
// open the directory
try {
stream = Files.newDirectoryStream(file);
} catch (IOException x) {
return visitor.visitFileFailed(file, x);
} catch (SecurityException x) {
// ignore, as per spec
return FileVisitResult.CONTINUE;
}
// the exception notified to the postVisitDirectory method
IOException ioe = null;
// invoke preVisitDirectory and then visit each entry
try {
result = visitor.preVisitDirectory(file, attrs);
if (result != FileVisitResult.CONTINUE) {
return result;
}
try {
for (Path entry: stream) {
result = walk(entry, depth+1, ancestors);
// returning null will cause NPE to be thrown
if (result == null || result == FileVisitResult.TERMINATE)
return result;
// skip remaining siblings in this directory
if (result == FileVisitResult.SKIP_SIBLINGS)
break;
}
} catch (DirectoryIteratorException e) {
// IOException will be notified to postVisitDirectory
ioe = e.getCause();
}
} finally {
try {
stream.close();
} catch (IOException e) {
// IOException will be notified to postVisitDirectory
if (ioe == null)
ioe = e;
}
}
// invoke postVisitDirectory last
return visitor.postVisitDirectory(file, ioe);
} finally {
// remove key from trail if doing cycle detection
if (followLinks) {
ancestors.remove(ancestors.size()-1);
}
}
}
private static class AncestorDirectory {
private final Path dir;
private final Object key;
AncestorDirectory(Path dir, Object key) {
this.dir = dir;
this.key = key;
}
Path file() {
return dir;
}
Object fileKey() {
return key;
}
}
}