author | alanb |
Mon, 04 Apr 2011 18:09:53 +0100 | |
changeset 9025 | a72fc1fc4b71 |
parent 8158 | 77d9c0f1c19f |
child 9050 | 26c2c1de1631 |
permissions | -rw-r--r-- |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
1 |
/* |
5506 | 2 |
* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
4 |
* |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
10 |
* |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
15 |
* accompanied this code). |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
16 |
* |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
20 |
* |
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
24 |
*/ |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
25 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
26 |
package sun.nio.fs; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
27 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
28 |
import java.nio.file.*; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
29 |
import java.nio.file.attribute.*; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
30 |
import java.nio.channels.*; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
31 |
import java.net.URI; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
32 |
import java.util.concurrent.ExecutorService; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
33 |
import java.io.IOException; |
8158 | 34 |
import java.io.FilePermission; |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
35 |
import java.util.*; |
8158 | 36 |
import java.security.AccessController; |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
37 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
38 |
import sun.nio.ch.ThreadPool; |
8158 | 39 |
import sun.security.util.SecurityConstants; |
40 |
import static sun.nio.fs.UnixNativeDispatcher.*; |
|
41 |
import static sun.nio.fs.UnixConstants.*; |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
42 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
43 |
/** |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
44 |
* Base implementation of FileSystemProvider |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
45 |
*/ |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
46 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
47 |
public abstract class UnixFileSystemProvider |
8158 | 48 |
extends AbstractFileSystemProvider |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
49 |
{ |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
50 |
private static final String USER_DIR = "user.dir"; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
51 |
private final UnixFileSystem theFileSystem; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
52 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
53 |
public UnixFileSystemProvider() { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
54 |
String userDir = System.getProperty(USER_DIR); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
55 |
theFileSystem = newFileSystem(userDir); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
56 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
57 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
58 |
/** |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
59 |
* Constructs a new file system using the given default directory. |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
60 |
*/ |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
61 |
abstract UnixFileSystem newFileSystem(String dir); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
62 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
63 |
@Override |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
64 |
public final String getScheme() { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
65 |
return "file"; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
66 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
67 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
68 |
private void checkUri(URI uri) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
69 |
if (!uri.getScheme().equalsIgnoreCase(getScheme())) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
70 |
throw new IllegalArgumentException("URI does not match this provider"); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
71 |
if (uri.getAuthority() != null) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
72 |
throw new IllegalArgumentException("Authority component present"); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
73 |
if (uri.getPath() == null) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
74 |
throw new IllegalArgumentException("Path component is undefined"); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
75 |
if (!uri.getPath().equals("/")) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
76 |
throw new IllegalArgumentException("Path component should be '/'"); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
77 |
if (uri.getQuery() != null) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
78 |
throw new IllegalArgumentException("Query component present"); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
79 |
if (uri.getFragment() != null) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
80 |
throw new IllegalArgumentException("Fragment component present"); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
81 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
82 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
83 |
@Override |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
84 |
public final FileSystem newFileSystem(URI uri, Map<String,?> env) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
85 |
checkUri(uri); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
86 |
throw new FileSystemAlreadyExistsException(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
87 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
88 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
89 |
@Override |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
90 |
public final FileSystem getFileSystem(URI uri) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
91 |
checkUri(uri); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
92 |
return theFileSystem; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
93 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
94 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
95 |
@Override |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
96 |
public Path getPath(URI uri) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
97 |
return UnixUriUtils.fromUri(theFileSystem, uri); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
98 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
99 |
|
8158 | 100 |
UnixPath checkPath(Path obj) { |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
101 |
if (obj == null) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
102 |
throw new NullPointerException(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
103 |
if (!(obj instanceof UnixPath)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
104 |
throw new ProviderMismatchException(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
105 |
return (UnixPath)obj; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
106 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
107 |
|
8158 | 108 |
@Override |
109 |
@SuppressWarnings("unchecked") |
|
110 |
public <V extends FileAttributeView> V getFileAttributeView(Path obj, |
|
111 |
Class<V> type, |
|
112 |
LinkOption... options) |
|
113 |
{ |
|
114 |
UnixPath file = UnixPath.toUnixPath(obj); |
|
9025
a72fc1fc4b71
7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents:
8158
diff
changeset
|
115 |
boolean followLinks = Util.followLinks(options); |
8158 | 116 |
if (type == BasicFileAttributeView.class) |
117 |
return (V) UnixFileAttributeViews.createBasicView(file, followLinks); |
|
118 |
if (type == PosixFileAttributeView.class) |
|
119 |
return (V) UnixFileAttributeViews.createPosixView(file, followLinks); |
|
120 |
if (type == FileOwnerAttributeView.class) |
|
121 |
return (V) UnixFileAttributeViews.createOwnerView(file, followLinks); |
|
122 |
if (type == null) |
|
123 |
throw new NullPointerException(); |
|
124 |
return (V) null; |
|
125 |
} |
|
126 |
||
127 |
@Override |
|
128 |
@SuppressWarnings("unchecked") |
|
129 |
public <A extends BasicFileAttributes> A readAttributes(Path file, |
|
130 |
Class<A> type, |
|
131 |
LinkOption... options) |
|
132 |
throws IOException |
|
133 |
{ |
|
134 |
Class<? extends BasicFileAttributeView> view; |
|
135 |
if (type == BasicFileAttributes.class) |
|
136 |
view = BasicFileAttributeView.class; |
|
137 |
else if (type == PosixFileAttributes.class) |
|
138 |
view = PosixFileAttributeView.class; |
|
139 |
else if (type == null) |
|
140 |
throw new NullPointerException(); |
|
141 |
else |
|
142 |
throw new UnsupportedOperationException(); |
|
143 |
return (A) getFileAttributeView(file, view, options).readAttributes(); |
|
144 |
} |
|
145 |
||
146 |
@Override |
|
147 |
protected DynamicFileAttributeView getFileAttributeView(Path obj, |
|
148 |
String name, |
|
149 |
LinkOption... options) |
|
150 |
{ |
|
151 |
UnixPath file = UnixPath.toUnixPath(obj); |
|
9025
a72fc1fc4b71
7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents:
8158
diff
changeset
|
152 |
boolean followLinks = Util.followLinks(options); |
8158 | 153 |
if (name.equals("basic")) |
154 |
return UnixFileAttributeViews.createBasicView(file, followLinks); |
|
155 |
if (name.equals("posix")) |
|
156 |
return UnixFileAttributeViews.createPosixView(file, followLinks); |
|
157 |
if (name.equals("unix")) |
|
158 |
return UnixFileAttributeViews.createUnixView(file, followLinks); |
|
159 |
if (name.equals("owner")) |
|
160 |
return UnixFileAttributeViews.createOwnerView(file, followLinks); |
|
161 |
return null; |
|
162 |
} |
|
163 |
||
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
164 |
@Override |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
165 |
public FileChannel newFileChannel(Path obj, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
166 |
Set<? extends OpenOption> options, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
167 |
FileAttribute<?>... attrs) |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
168 |
throws IOException |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
169 |
{ |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
170 |
UnixPath file = checkPath(obj); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
171 |
int mode = UnixFileModeAttribute |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
172 |
.toUnixMode(UnixFileModeAttribute.ALL_READWRITE, attrs); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
173 |
try { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
174 |
return UnixChannelFactory.newFileChannel(file, options, mode); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
175 |
} catch (UnixException x) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
176 |
x.rethrowAsIOException(file); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
177 |
return null; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
178 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
179 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
180 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
181 |
@Override |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
182 |
public AsynchronousFileChannel newAsynchronousFileChannel(Path obj, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
183 |
Set<? extends OpenOption> options, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
184 |
ExecutorService executor, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
185 |
FileAttribute<?>... attrs) throws IOException |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
186 |
{ |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
187 |
UnixPath file = checkPath(obj); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
188 |
int mode = UnixFileModeAttribute |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
189 |
.toUnixMode(UnixFileModeAttribute.ALL_READWRITE, attrs); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
190 |
ThreadPool pool = (executor == null) ? null : ThreadPool.wrap(executor, 0); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
191 |
try { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
192 |
return UnixChannelFactory |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
193 |
.newAsynchronousFileChannel(file, options, mode, pool); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
194 |
} catch (UnixException x) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
195 |
x.rethrowAsIOException(file); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
196 |
return null; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
197 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
198 |
} |
8158 | 199 |
|
200 |
||
201 |
@Override |
|
202 |
public SeekableByteChannel newByteChannel(Path obj, |
|
203 |
Set<? extends OpenOption> options, |
|
204 |
FileAttribute<?>... attrs) |
|
205 |
throws IOException |
|
206 |
{ |
|
207 |
UnixPath file = UnixPath.toUnixPath(obj); |
|
208 |
int mode = UnixFileModeAttribute |
|
209 |
.toUnixMode(UnixFileModeAttribute.ALL_READWRITE, attrs); |
|
210 |
try { |
|
211 |
return UnixChannelFactory.newFileChannel(file, options, mode); |
|
212 |
} catch (UnixException x) { |
|
213 |
x.rethrowAsIOException(file); |
|
214 |
return null; // keep compiler happy |
|
215 |
} |
|
216 |
} |
|
217 |
||
218 |
@Override |
|
219 |
boolean implDelete(Path obj, boolean failIfNotExists) throws IOException { |
|
220 |
UnixPath file = UnixPath.toUnixPath(obj); |
|
221 |
file.checkDelete(); |
|
222 |
||
223 |
// need file attributes to know if file is directory |
|
224 |
UnixFileAttributes attrs = null; |
|
225 |
try { |
|
226 |
attrs = UnixFileAttributes.get(file, false); |
|
227 |
if (attrs.isDirectory()) { |
|
228 |
rmdir(file); |
|
229 |
} else { |
|
230 |
unlink(file); |
|
231 |
} |
|
232 |
return true; |
|
233 |
} catch (UnixException x) { |
|
234 |
// no-op if file does not exist |
|
235 |
if (!failIfNotExists && x.errno() == ENOENT) |
|
236 |
return false; |
|
237 |
||
238 |
// DirectoryNotEmptyException if not empty |
|
239 |
if (attrs != null && attrs.isDirectory() && |
|
240 |
(x.errno() == EEXIST || x.errno() == ENOTEMPTY)) |
|
241 |
throw new DirectoryNotEmptyException(file.getPathForExecptionMessage()); |
|
242 |
||
243 |
x.rethrowAsIOException(file); |
|
244 |
return false; |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
@Override |
|
249 |
public void copy(Path source, Path target, CopyOption... options) |
|
250 |
throws IOException |
|
251 |
{ |
|
252 |
UnixCopyFile.copy(UnixPath.toUnixPath(source), |
|
253 |
UnixPath.toUnixPath(target), |
|
254 |
options); |
|
255 |
} |
|
256 |
||
257 |
@Override |
|
258 |
public void move(Path source, Path target, CopyOption... options) |
|
259 |
throws IOException |
|
260 |
{ |
|
261 |
UnixCopyFile.move(UnixPath.toUnixPath(source), |
|
262 |
UnixPath.toUnixPath(target), |
|
263 |
options); |
|
264 |
} |
|
265 |
||
266 |
@Override |
|
267 |
public void checkAccess(Path obj, AccessMode... modes) throws IOException { |
|
268 |
UnixPath file = UnixPath.toUnixPath(obj); |
|
269 |
boolean e = false; |
|
270 |
boolean r = false; |
|
271 |
boolean w = false; |
|
272 |
boolean x = false; |
|
273 |
||
274 |
if (modes.length == 0) { |
|
275 |
e = true; |
|
276 |
} else { |
|
277 |
for (AccessMode mode: modes) { |
|
278 |
switch (mode) { |
|
279 |
case READ : r = true; break; |
|
280 |
case WRITE : w = true; break; |
|
281 |
case EXECUTE : x = true; break; |
|
282 |
default: throw new AssertionError("Should not get here"); |
|
283 |
} |
|
284 |
} |
|
285 |
} |
|
286 |
||
287 |
int mode = 0; |
|
288 |
if (e || r) { |
|
289 |
file.checkRead(); |
|
290 |
mode |= (r) ? R_OK : F_OK; |
|
291 |
} |
|
292 |
if (w) { |
|
293 |
file.checkWrite(); |
|
294 |
mode |= W_OK; |
|
295 |
} |
|
296 |
if (x) { |
|
297 |
SecurityManager sm = System.getSecurityManager(); |
|
298 |
if (sm != null) { |
|
299 |
// not cached |
|
300 |
sm.checkExec(file.getPathForPermissionCheck()); |
|
301 |
} |
|
302 |
mode |= X_OK; |
|
303 |
} |
|
304 |
try { |
|
305 |
access(file, mode); |
|
306 |
} catch (UnixException exc) { |
|
307 |
exc.rethrowAsIOException(file); |
|
308 |
} |
|
309 |
} |
|
310 |
||
311 |
@Override |
|
312 |
public boolean isSameFile(Path obj1, Path obj2) throws IOException { |
|
313 |
UnixPath file1 = UnixPath.toUnixPath(obj1); |
|
314 |
if (file1.equals(obj2)) |
|
315 |
return true; |
|
316 |
if (obj2 == null) |
|
317 |
throw new NullPointerException(); |
|
318 |
if (!(obj2 instanceof UnixPath)) |
|
319 |
return false; |
|
320 |
UnixPath file2 = (UnixPath)obj2; |
|
321 |
||
322 |
// check security manager access to both files |
|
323 |
file1.checkRead(); |
|
324 |
file2.checkRead(); |
|
325 |
||
326 |
UnixFileAttributes attrs1; |
|
327 |
UnixFileAttributes attrs2; |
|
328 |
try { |
|
329 |
attrs1 = UnixFileAttributes.get(file1, true); |
|
330 |
} catch (UnixException x) { |
|
331 |
x.rethrowAsIOException(file1); |
|
332 |
return false; // keep compiler happy |
|
333 |
} |
|
334 |
try { |
|
335 |
attrs2 = UnixFileAttributes.get(file2, true); |
|
336 |
} catch (UnixException x) { |
|
337 |
x.rethrowAsIOException(file2); |
|
338 |
return false; // keep compiler happy |
|
339 |
} |
|
340 |
return attrs1.isSameFile(attrs2); |
|
341 |
} |
|
342 |
||
343 |
@Override |
|
344 |
public boolean isHidden(Path obj) { |
|
345 |
UnixPath file = UnixPath.toUnixPath(obj); |
|
346 |
file.checkRead(); |
|
347 |
UnixPath name = file.getFileName(); |
|
348 |
if (name == null) |
|
349 |
return false; |
|
350 |
return (name.asByteArray()[0] == '.'); |
|
351 |
} |
|
352 |
||
353 |
/** |
|
354 |
* Returns a FileStore to represent the file system where the given file |
|
355 |
* reside. |
|
356 |
*/ |
|
357 |
abstract FileStore getFileStore(UnixPath path) throws IOException; |
|
358 |
||
359 |
@Override |
|
360 |
public FileStore getFileStore(Path obj) throws IOException { |
|
361 |
UnixPath file = UnixPath.toUnixPath(obj); |
|
362 |
SecurityManager sm = System.getSecurityManager(); |
|
363 |
if (sm != null) { |
|
364 |
sm.checkPermission(new RuntimePermission("getFileStoreAttributes")); |
|
365 |
file.checkRead(); |
|
366 |
} |
|
367 |
return getFileStore(file); |
|
368 |
} |
|
369 |
||
370 |
@Override |
|
371 |
public void createDirectory(Path obj, FileAttribute<?>... attrs) |
|
372 |
throws IOException |
|
373 |
{ |
|
374 |
UnixPath dir = UnixPath.toUnixPath(obj); |
|
375 |
dir.checkWrite(); |
|
376 |
||
377 |
int mode = UnixFileModeAttribute |
|
378 |
.toUnixMode(UnixFileModeAttribute.ALL_PERMISSIONS, attrs); |
|
379 |
try { |
|
380 |
mkdir(dir, mode); |
|
381 |
} catch (UnixException x) { |
|
382 |
x.rethrowAsIOException(dir); |
|
383 |
} |
|
384 |
} |
|
385 |
||
386 |
||
387 |
@Override |
|
388 |
public DirectoryStream<Path> newDirectoryStream(Path obj, DirectoryStream.Filter<? super Path> filter) |
|
389 |
throws IOException |
|
390 |
{ |
|
391 |
UnixPath dir = UnixPath.toUnixPath(obj); |
|
392 |
dir.checkRead(); |
|
393 |
if (filter == null) |
|
394 |
throw new NullPointerException(); |
|
395 |
||
396 |
// can't return SecureDirectoryStream on kernels that don't support |
|
397 |
// openat, etc. |
|
398 |
if (!supportsAtSysCalls()) { |
|
399 |
try { |
|
400 |
long ptr = opendir(dir); |
|
401 |
return new UnixDirectoryStream(dir, ptr, filter); |
|
402 |
} catch (UnixException x) { |
|
403 |
if (x.errno() == ENOTDIR) |
|
404 |
throw new NotDirectoryException(dir.getPathForExecptionMessage()); |
|
405 |
x.rethrowAsIOException(dir); |
|
406 |
} |
|
407 |
} |
|
408 |
||
409 |
// open directory and dup file descriptor for use by |
|
410 |
// opendir/readdir/closedir |
|
411 |
int dfd1 = -1; |
|
412 |
int dfd2 = -1; |
|
413 |
long dp = 0L; |
|
414 |
try { |
|
415 |
dfd1 = open(dir, O_RDONLY, 0); |
|
416 |
dfd2 = dup(dfd1); |
|
417 |
dp = fdopendir(dfd1); |
|
418 |
} catch (UnixException x) { |
|
419 |
if (dfd1 != -1) |
|
420 |
UnixNativeDispatcher.close(dfd1); |
|
421 |
if (dfd2 != -1) |
|
422 |
UnixNativeDispatcher.close(dfd2); |
|
423 |
if (x.errno() == UnixConstants.ENOTDIR) |
|
424 |
throw new NotDirectoryException(dir.getPathForExecptionMessage()); |
|
425 |
x.rethrowAsIOException(dir); |
|
426 |
} |
|
427 |
return new UnixSecureDirectoryStream(dir, dp, dfd2, filter); |
|
428 |
} |
|
429 |
||
430 |
@Override |
|
431 |
public void createSymbolicLink(Path obj1, Path obj2, FileAttribute<?>... attrs) |
|
432 |
throws IOException |
|
433 |
{ |
|
434 |
UnixPath link = UnixPath.toUnixPath(obj1); |
|
435 |
UnixPath target = UnixPath.toUnixPath(obj2); |
|
436 |
||
437 |
// no attributes supported when creating links |
|
438 |
if (attrs.length > 0) { |
|
439 |
UnixFileModeAttribute.toUnixMode(0, attrs); // may throw NPE or UOE |
|
440 |
throw new UnsupportedOperationException("Initial file attributes" + |
|
441 |
"not supported when creating symbolic link"); |
|
442 |
} |
|
443 |
||
444 |
// permission check |
|
445 |
SecurityManager sm = System.getSecurityManager(); |
|
446 |
if (sm != null) { |
|
447 |
sm.checkPermission(new LinkPermission("symbolic")); |
|
448 |
link.checkWrite(); |
|
449 |
} |
|
450 |
||
451 |
// create link |
|
452 |
try { |
|
453 |
symlink(target.asByteArray(), link); |
|
454 |
} catch (UnixException x) { |
|
455 |
x.rethrowAsIOException(link); |
|
456 |
} |
|
457 |
} |
|
458 |
||
459 |
@Override |
|
460 |
public void createLink(Path obj1, Path obj2) throws IOException { |
|
461 |
UnixPath link = UnixPath.toUnixPath(obj1); |
|
462 |
UnixPath existing = UnixPath.toUnixPath(obj2); |
|
463 |
||
464 |
// permission check |
|
465 |
SecurityManager sm = System.getSecurityManager(); |
|
466 |
if (sm != null) { |
|
467 |
sm.checkPermission(new LinkPermission("hard")); |
|
468 |
link.checkWrite(); |
|
469 |
existing.checkWrite(); |
|
470 |
} |
|
471 |
try { |
|
472 |
link(existing, link); |
|
473 |
} catch (UnixException x) { |
|
474 |
x.rethrowAsIOException(link, existing); |
|
475 |
} |
|
476 |
} |
|
477 |
||
478 |
@Override |
|
479 |
public Path readSymbolicLink(Path obj1) throws IOException { |
|
480 |
UnixPath link = UnixPath.toUnixPath(obj1); |
|
481 |
// permission check |
|
482 |
SecurityManager sm = System.getSecurityManager(); |
|
483 |
if (sm != null) { |
|
484 |
FilePermission perm = new FilePermission(link.getPathForPermissionCheck(), |
|
485 |
SecurityConstants.FILE_READLINK_ACTION); |
|
486 |
AccessController.checkPermission(perm); |
|
487 |
} |
|
488 |
try { |
|
489 |
byte[] target = readlink(link); |
|
490 |
return new UnixPath(link.getFileSystem(), target); |
|
491 |
} catch (UnixException x) { |
|
492 |
if (x.errno() == UnixConstants.EINVAL) |
|
493 |
throw new NotLinkException(link.getPathForExecptionMessage()); |
|
494 |
x.rethrowAsIOException(link); |
|
495 |
return null; // keep compiler happy |
|
496 |
} |
|
497 |
} |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
498 |
} |