41 extends FileSystem
42 {
43 private final UnixFileSystemProvider provider;
44 private final byte[] defaultDirectory;
45 private final boolean needToResolveAgainstDefaultDirectory;
46 private final UnixPath rootDirectory;
47
48 // package-private
49 UnixFileSystem(UnixFileSystemProvider provider, String dir) {
50 this.provider = provider;
51 this.defaultDirectory = Util.toBytes(UnixPath.normalizeAndCheck(dir));
52 if (this.defaultDirectory[0] != '/') {
53 throw new RuntimeException("default directory must be absolute");
54 }
55
56 // if process-wide chdir is allowed or default directory is not the
57 // process working directory then paths must be resolved against the
58 // default directory.
59 String propValue = GetPropertyAction
60 .privilegedGetProperty("sun.nio.fs.chdirAllowed", "false");
61 boolean chdirAllowed = (propValue.length() == 0) ?
62 true : Boolean.valueOf(propValue);
63 if (chdirAllowed) {
64 this.needToResolveAgainstDefaultDirectory = true;
65 } else {
66 byte[] cwd = UnixNativeDispatcher.getcwd();
67 boolean defaultIsCwd = (cwd.length == defaultDirectory.length);
68 if (defaultIsCwd) {
69 for (int i=0; i<cwd.length; i++) {
70 if (cwd[i] != defaultDirectory[i]) {
71 defaultIsCwd = false;
72 break;
73 }
74 }
75 }
76 this.needToResolveAgainstDefaultDirectory = !defaultIsCwd;
77 }
78
79 // the root directory
80 this.rootDirectory = new UnixPath(this, "/");
81 }
82
252 } catch (SecurityException se) {
253 return Collections.emptyList();
254 }
255 }
256 return new Iterable<>() {
257 public Iterator<FileStore> iterator() {
258 return new FileStoreIterator();
259 }
260 };
261 }
262
263 @Override
264 public final Path getPath(String first, String... more) {
265 String path;
266 if (more.length == 0) {
267 path = first;
268 } else {
269 StringBuilder sb = new StringBuilder();
270 sb.append(first);
271 for (String segment: more) {
272 if (segment.length() > 0) {
273 if (sb.length() > 0)
274 sb.append('/');
275 sb.append(segment);
276 }
277 }
278 path = sb.toString();
279 }
280 return new UnixPath(this, path);
281 }
282
283 @Override
284 public PathMatcher getPathMatcher(String syntaxAndInput) {
285 int pos = syntaxAndInput.indexOf(':');
286 if (pos <= 0 || pos == syntaxAndInput.length())
287 throw new IllegalArgumentException();
288 String syntax = syntaxAndInput.substring(0, pos);
289 String input = syntaxAndInput.substring(pos+1);
290
291 String expr;
292 if (syntax.equalsIgnoreCase(GLOB_SYNTAX)) {
|
41 extends FileSystem
42 {
43 private final UnixFileSystemProvider provider;
44 private final byte[] defaultDirectory;
45 private final boolean needToResolveAgainstDefaultDirectory;
46 private final UnixPath rootDirectory;
47
48 // package-private
49 UnixFileSystem(UnixFileSystemProvider provider, String dir) {
50 this.provider = provider;
51 this.defaultDirectory = Util.toBytes(UnixPath.normalizeAndCheck(dir));
52 if (this.defaultDirectory[0] != '/') {
53 throw new RuntimeException("default directory must be absolute");
54 }
55
56 // if process-wide chdir is allowed or default directory is not the
57 // process working directory then paths must be resolved against the
58 // default directory.
59 String propValue = GetPropertyAction
60 .privilegedGetProperty("sun.nio.fs.chdirAllowed", "false");
61 boolean chdirAllowed = propValue.isEmpty() ? true : Boolean.parseBoolean(propValue);
62 if (chdirAllowed) {
63 this.needToResolveAgainstDefaultDirectory = true;
64 } else {
65 byte[] cwd = UnixNativeDispatcher.getcwd();
66 boolean defaultIsCwd = (cwd.length == defaultDirectory.length);
67 if (defaultIsCwd) {
68 for (int i=0; i<cwd.length; i++) {
69 if (cwd[i] != defaultDirectory[i]) {
70 defaultIsCwd = false;
71 break;
72 }
73 }
74 }
75 this.needToResolveAgainstDefaultDirectory = !defaultIsCwd;
76 }
77
78 // the root directory
79 this.rootDirectory = new UnixPath(this, "/");
80 }
81
251 } catch (SecurityException se) {
252 return Collections.emptyList();
253 }
254 }
255 return new Iterable<>() {
256 public Iterator<FileStore> iterator() {
257 return new FileStoreIterator();
258 }
259 };
260 }
261
262 @Override
263 public final Path getPath(String first, String... more) {
264 String path;
265 if (more.length == 0) {
266 path = first;
267 } else {
268 StringBuilder sb = new StringBuilder();
269 sb.append(first);
270 for (String segment: more) {
271 if (!segment.isEmpty()) {
272 if (sb.length() > 0)
273 sb.append('/');
274 sb.append(segment);
275 }
276 }
277 path = sb.toString();
278 }
279 return new UnixPath(this, path);
280 }
281
282 @Override
283 public PathMatcher getPathMatcher(String syntaxAndInput) {
284 int pos = syntaxAndInput.indexOf(':');
285 if (pos <= 0 || pos == syntaxAndInput.length())
286 throw new IllegalArgumentException();
287 String syntax = syntaxAndInput.substring(0, pos);
288 String input = syntaxAndInput.substring(pos+1);
289
290 String expr;
291 if (syntax.equalsIgnoreCase(GLOB_SYNTAX)) {
|