author | chegar |
Wed, 11 Nov 2015 09:19:12 +0000 | |
changeset 33674 | 566777f73c32 |
parent 25859 | 3317bb8137f4 |
child 34782 | fe102f179318 |
permissions | -rw-r--r-- |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
22351
diff
changeset
|
2 |
* Copyright (c) 2008, 2013, 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; |
8158 | 33 |
import java.io.*; |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
34 |
import java.util.*; |
8158 | 35 |
import java.security.AccessController; |
33674
566777f73c32
8140606: Update library code to use internal Unsafe
chegar
parents:
25859
diff
changeset
|
36 |
import jdk.internal.misc.Unsafe; |
8158 | 37 |
import sun.nio.ch.ThreadPool; |
38 |
import sun.security.util.SecurityConstants; |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
39 |
|
8158 | 40 |
import static sun.nio.fs.WindowsNativeDispatcher.*; |
16473
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
41 |
import static sun.nio.fs.WindowsSecurity.*; |
8158 | 42 |
import static sun.nio.fs.WindowsConstants.*; |
2057
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 |
public class WindowsFileSystemProvider |
8158 | 45 |
extends AbstractFileSystemProvider |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
46 |
{ |
8158 | 47 |
private static final Unsafe unsafe = Unsafe.getUnsafe(); |
48 |
||
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
49 |
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
|
50 |
private final WindowsFileSystem theFileSystem; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
51 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
52 |
public WindowsFileSystemProvider() { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
53 |
theFileSystem = new WindowsFileSystem(this, System.getProperty(USER_DIR)); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
54 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
55 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
56 |
@Override |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
57 |
public String getScheme() { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
58 |
return "file"; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
59 |
} |
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 |
private void checkUri(URI uri) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
62 |
if (!uri.getScheme().equalsIgnoreCase(getScheme())) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
63 |
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
|
64 |
if (uri.getAuthority() != null) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
65 |
throw new IllegalArgumentException("Authority component present"); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
66 |
if (uri.getPath() == null) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
67 |
throw new IllegalArgumentException("Path component is undefined"); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
68 |
if (!uri.getPath().equals("/")) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
69 |
throw new IllegalArgumentException("Path component should be '/'"); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
70 |
if (uri.getQuery() != null) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
71 |
throw new IllegalArgumentException("Query component present"); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
72 |
if (uri.getFragment() != null) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
73 |
throw new IllegalArgumentException("Fragment component present"); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
74 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
75 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
76 |
@Override |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
77 |
public FileSystem newFileSystem(URI uri, Map<String,?> env) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
78 |
throws IOException |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
79 |
{ |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
80 |
checkUri(uri); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
81 |
throw new FileSystemAlreadyExistsException(); |
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 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
84 |
@Override |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
85 |
public final FileSystem getFileSystem(URI uri) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
86 |
checkUri(uri); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
87 |
return theFileSystem; |
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 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
90 |
@Override |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
91 |
public Path getPath(URI uri) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
92 |
return WindowsUriSupport.fromUri(theFileSystem, uri); |
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 FileChannel newFileChannel(Path path, |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
97 |
Set<? extends OpenOption> options, |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
98 |
FileAttribute<?>... attrs) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
99 |
throws IOException |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
100 |
{ |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
101 |
if (path == 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 (!(path instanceof WindowsPath)) |
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 |
WindowsPath file = (WindowsPath)path; |
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 |
WindowsSecurityDescriptor sd = WindowsSecurityDescriptor.fromAttribute(attrs); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
108 |
try { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
109 |
return WindowsChannelFactory |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
110 |
.newFileChannel(file.getPathForWin32Calls(), |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
111 |
file.getPathForPermissionCheck(), |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
112 |
options, |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
113 |
sd.address()); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
114 |
} catch (WindowsException x) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
115 |
x.rethrowAsIOException(file); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
116 |
return null; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
117 |
} finally { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
118 |
if (sd != null) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
119 |
sd.release(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
120 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
121 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
122 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
123 |
@Override |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
124 |
public AsynchronousFileChannel newAsynchronousFileChannel(Path path, |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
125 |
Set<? extends OpenOption> options, |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
126 |
ExecutorService executor, |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
127 |
FileAttribute<?>... attrs) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
128 |
throws IOException |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
129 |
{ |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
130 |
if (path == null) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
131 |
throw new NullPointerException(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
132 |
if (!(path instanceof WindowsPath)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
133 |
throw new ProviderMismatchException(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
134 |
WindowsPath file = (WindowsPath)path; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
135 |
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
|
136 |
WindowsSecurityDescriptor sd = |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
137 |
WindowsSecurityDescriptor.fromAttribute(attrs); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
138 |
try { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
139 |
return WindowsChannelFactory |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
140 |
.newAsynchronousFileChannel(file.getPathForWin32Calls(), |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
141 |
file.getPathForPermissionCheck(), |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
142 |
options, |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
143 |
sd.address(), |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
144 |
pool); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
145 |
} catch (WindowsException x) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
146 |
x.rethrowAsIOException(file); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
147 |
return null; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
148 |
} finally { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
149 |
if (sd != null) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
150 |
sd.release(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
151 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
152 |
} |
8158 | 153 |
|
154 |
@Override |
|
155 |
@SuppressWarnings("unchecked") |
|
156 |
public <V extends FileAttributeView> V |
|
157 |
getFileAttributeView(Path obj, Class<V> view, LinkOption... options) |
|
158 |
{ |
|
159 |
WindowsPath file = WindowsPath.toWindowsPath(obj); |
|
160 |
if (view == null) |
|
161 |
throw new NullPointerException(); |
|
9025
a72fc1fc4b71
7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents:
8158
diff
changeset
|
162 |
boolean followLinks = Util.followLinks(options); |
8158 | 163 |
if (view == BasicFileAttributeView.class) |
164 |
return (V) WindowsFileAttributeViews.createBasicView(file, followLinks); |
|
165 |
if (view == DosFileAttributeView.class) |
|
166 |
return (V) WindowsFileAttributeViews.createDosView(file, followLinks); |
|
167 |
if (view == AclFileAttributeView.class) |
|
168 |
return (V) new WindowsAclFileAttributeView(file, followLinks); |
|
169 |
if (view == FileOwnerAttributeView.class) |
|
170 |
return (V) new FileOwnerAttributeViewImpl( |
|
171 |
new WindowsAclFileAttributeView(file, followLinks)); |
|
172 |
if (view == UserDefinedFileAttributeView.class) |
|
173 |
return (V) new WindowsUserDefinedFileAttributeView(file, followLinks); |
|
174 |
return (V) null; |
|
175 |
} |
|
176 |
||
177 |
@Override |
|
178 |
@SuppressWarnings("unchecked") |
|
179 |
public <A extends BasicFileAttributes> A readAttributes(Path file, |
|
180 |
Class<A> type, |
|
181 |
LinkOption... options) |
|
182 |
throws IOException |
|
183 |
{ |
|
184 |
Class<? extends BasicFileAttributeView> view; |
|
185 |
if (type == BasicFileAttributes.class) |
|
186 |
view = BasicFileAttributeView.class; |
|
187 |
else if (type == DosFileAttributes.class) |
|
188 |
view = DosFileAttributeView.class; |
|
189 |
else if (type == null) |
|
190 |
throw new NullPointerException(); |
|
191 |
else |
|
192 |
throw new UnsupportedOperationException(); |
|
193 |
return (A) getFileAttributeView(file, view, options).readAttributes(); |
|
194 |
} |
|
195 |
||
196 |
@Override |
|
197 |
public DynamicFileAttributeView getFileAttributeView(Path obj, String name, LinkOption... options) { |
|
198 |
WindowsPath file = WindowsPath.toWindowsPath(obj); |
|
9025
a72fc1fc4b71
7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents:
8158
diff
changeset
|
199 |
boolean followLinks = Util.followLinks(options); |
8158 | 200 |
if (name.equals("basic")) |
201 |
return WindowsFileAttributeViews.createBasicView(file, followLinks); |
|
202 |
if (name.equals("dos")) |
|
203 |
return WindowsFileAttributeViews.createDosView(file, followLinks); |
|
204 |
if (name.equals("acl")) |
|
205 |
return new WindowsAclFileAttributeView(file, followLinks); |
|
206 |
if (name.equals("owner")) |
|
207 |
return new FileOwnerAttributeViewImpl( |
|
208 |
new WindowsAclFileAttributeView(file, followLinks)); |
|
209 |
if (name.equals("user")) |
|
210 |
return new WindowsUserDefinedFileAttributeView(file, followLinks); |
|
211 |
return null; |
|
212 |
} |
|
213 |
||
214 |
@Override |
|
215 |
public SeekableByteChannel newByteChannel(Path obj, |
|
216 |
Set<? extends OpenOption> options, |
|
217 |
FileAttribute<?>... attrs) |
|
218 |
throws IOException |
|
219 |
{ |
|
220 |
WindowsPath file = WindowsPath.toWindowsPath(obj); |
|
221 |
WindowsSecurityDescriptor sd = |
|
222 |
WindowsSecurityDescriptor.fromAttribute(attrs); |
|
223 |
try { |
|
224 |
return WindowsChannelFactory |
|
225 |
.newFileChannel(file.getPathForWin32Calls(), |
|
226 |
file.getPathForPermissionCheck(), |
|
227 |
options, |
|
228 |
sd.address()); |
|
229 |
} catch (WindowsException x) { |
|
230 |
x.rethrowAsIOException(file); |
|
231 |
return null; // keep compiler happy |
|
232 |
} finally { |
|
233 |
sd.release(); |
|
234 |
} |
|
235 |
} |
|
236 |
||
237 |
@Override |
|
238 |
boolean implDelete(Path obj, boolean failIfNotExists) throws IOException { |
|
239 |
WindowsPath file = WindowsPath.toWindowsPath(obj); |
|
240 |
file.checkDelete(); |
|
241 |
||
242 |
WindowsFileAttributes attrs = null; |
|
243 |
try { |
|
244 |
// need to know if file is a directory or junction |
|
245 |
attrs = WindowsFileAttributes.get(file, false); |
|
246 |
if (attrs.isDirectory() || attrs.isDirectoryLink()) { |
|
247 |
RemoveDirectory(file.getPathForWin32Calls()); |
|
248 |
} else { |
|
249 |
DeleteFile(file.getPathForWin32Calls()); |
|
250 |
} |
|
251 |
return true; |
|
252 |
} catch (WindowsException x) { |
|
253 |
||
254 |
// no-op if file does not exist |
|
255 |
if (!failIfNotExists && |
|
256 |
(x.lastError() == ERROR_FILE_NOT_FOUND || |
|
257 |
x.lastError() == ERROR_PATH_NOT_FOUND)) return false; |
|
258 |
||
259 |
if (attrs != null && attrs.isDirectory()) { |
|
260 |
// ERROR_ALREADY_EXISTS is returned when attempting to delete |
|
261 |
// non-empty directory on SAMBA servers. |
|
262 |
if (x.lastError() == ERROR_DIR_NOT_EMPTY || |
|
263 |
x.lastError() == ERROR_ALREADY_EXISTS) |
|
264 |
{ |
|
265 |
throw new DirectoryNotEmptyException( |
|
266 |
file.getPathForExceptionMessage()); |
|
267 |
} |
|
268 |
} |
|
269 |
x.rethrowAsIOException(file); |
|
270 |
return false; |
|
271 |
} |
|
272 |
} |
|
273 |
||
274 |
@Override |
|
275 |
public void copy(Path source, Path target, CopyOption... options) |
|
276 |
throws IOException |
|
277 |
{ |
|
278 |
WindowsFileCopy.copy(WindowsPath.toWindowsPath(source), |
|
279 |
WindowsPath.toWindowsPath(target), |
|
280 |
options); |
|
281 |
} |
|
282 |
||
283 |
@Override |
|
284 |
public void move(Path source, Path target, CopyOption... options) |
|
285 |
throws IOException |
|
286 |
{ |
|
287 |
WindowsFileCopy.move(WindowsPath.toWindowsPath(source), |
|
288 |
WindowsPath.toWindowsPath(target), |
|
289 |
options); |
|
290 |
} |
|
291 |
||
292 |
/** |
|
16473
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
293 |
* Checks the file security against desired access. |
8158 | 294 |
*/ |
16473
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
295 |
private static boolean hasDesiredAccess(WindowsPath file, int rights) throws IOException { |
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
296 |
// read security descriptor containing ACL (symlinks are followed) |
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
297 |
boolean hasRights = false; |
8158 | 298 |
String target = WindowsLinkSupport.getFinalPath(file, true); |
299 |
NativeBuffer aclBuffer = WindowsAclFileAttributeView |
|
16473
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
300 |
.getFileSecurity(target, |
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
301 |
DACL_SECURITY_INFORMATION |
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
302 |
| OWNER_SECURITY_INFORMATION |
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
303 |
| GROUP_SECURITY_INFORMATION); |
8158 | 304 |
try { |
16473
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
305 |
hasRights = checkAccessMask(aclBuffer.address(), rights, |
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
306 |
FILE_GENERIC_READ, |
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
307 |
FILE_GENERIC_WRITE, |
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
308 |
FILE_GENERIC_EXECUTE, |
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
309 |
FILE_ALL_ACCESS); |
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
310 |
} catch (WindowsException exc) { |
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
311 |
exc.rethrowAsIOException(file); |
8158 | 312 |
} finally { |
313 |
aclBuffer.release(); |
|
314 |
} |
|
16473
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
315 |
return hasRights; |
8158 | 316 |
} |
317 |
||
13588 | 318 |
/** |
319 |
* Checks if the given file(or directory) exists and is readable. |
|
320 |
*/ |
|
321 |
private void checkReadAccess(WindowsPath file) throws IOException { |
|
322 |
try { |
|
323 |
Set<OpenOption> opts = Collections.emptySet(); |
|
324 |
FileChannel fc = WindowsChannelFactory |
|
325 |
.newFileChannel(file.getPathForWin32Calls(), |
|
326 |
file.getPathForPermissionCheck(), |
|
327 |
opts, |
|
328 |
0L); |
|
329 |
fc.close(); |
|
330 |
} catch (WindowsException exc) { |
|
331 |
// Windows errors are very inconsistent when the file is a directory |
|
332 |
// (ERROR_PATH_NOT_FOUND returned for root directories for example) |
|
333 |
// so we retry by attempting to open it as a directory. |
|
334 |
try { |
|
335 |
new WindowsDirectoryStream(file, null).close(); |
|
336 |
} catch (IOException ioe) { |
|
337 |
// translate and throw original exception |
|
338 |
exc.rethrowAsIOException(file); |
|
339 |
} |
|
340 |
} |
|
341 |
} |
|
342 |
||
8158 | 343 |
@Override |
344 |
public void checkAccess(Path obj, AccessMode... modes) throws IOException { |
|
345 |
WindowsPath file = WindowsPath.toWindowsPath(obj); |
|
346 |
||
347 |
boolean r = false; |
|
348 |
boolean w = false; |
|
349 |
boolean x = false; |
|
350 |
for (AccessMode mode: modes) { |
|
351 |
switch (mode) { |
|
352 |
case READ : r = true; break; |
|
353 |
case WRITE : w = true; break; |
|
354 |
case EXECUTE : x = true; break; |
|
355 |
default: throw new AssertionError("Should not get here"); |
|
356 |
} |
|
357 |
} |
|
358 |
||
13588 | 359 |
// special-case read access to avoid needing to determine effective |
360 |
// access to file; default if modes not specified |
|
361 |
if (!w && !x) { |
|
362 |
checkReadAccess(file); |
|
363 |
return; |
|
364 |
} |
|
365 |
||
8158 | 366 |
int mask = 0; |
367 |
if (r) { |
|
368 |
file.checkRead(); |
|
369 |
mask |= FILE_READ_DATA; |
|
370 |
} |
|
371 |
if (w) { |
|
372 |
file.checkWrite(); |
|
373 |
mask |= FILE_WRITE_DATA; |
|
374 |
} |
|
375 |
if (x) { |
|
376 |
SecurityManager sm = System.getSecurityManager(); |
|
377 |
if (sm != null) |
|
378 |
sm.checkExec(file.getPathForPermissionCheck()); |
|
379 |
mask |= FILE_EXECUTE; |
|
380 |
} |
|
381 |
||
16473
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
382 |
if (!hasDesiredAccess(file, mask)) |
8158 | 383 |
throw new AccessDeniedException( |
384 |
file.getPathForExceptionMessage(), null, |
|
16473
81d282e3cdaf
7190897: (fs) Files.isWritable method returns false when the path is writable (win)
uta
parents:
14342
diff
changeset
|
385 |
"Permissions does not allow requested access"); |
8158 | 386 |
|
23887
7b2fb8d5f6be
8040262: (fs) Misc. typos in comments and implementation
alanb
parents:
23010
diff
changeset
|
387 |
// for write access we need to check if the DOS readonly attribute |
8158 | 388 |
// and if the volume is read-only |
389 |
if (w) { |
|
390 |
try { |
|
391 |
WindowsFileAttributes attrs = WindowsFileAttributes.get(file, true); |
|
392 |
if (!attrs.isDirectory() && attrs.isReadOnly()) |
|
393 |
throw new AccessDeniedException( |
|
394 |
file.getPathForExceptionMessage(), null, |
|
395 |
"DOS readonly attribute is set"); |
|
396 |
} catch (WindowsException exc) { |
|
397 |
exc.rethrowAsIOException(file); |
|
398 |
} |
|
399 |
||
400 |
if (WindowsFileStore.create(file).isReadOnly()) { |
|
401 |
throw new AccessDeniedException( |
|
402 |
file.getPathForExceptionMessage(), null, "Read-only file system"); |
|
403 |
} |
|
404 |
} |
|
405 |
} |
|
406 |
||
407 |
@Override |
|
408 |
public boolean isSameFile(Path obj1, Path obj2) throws IOException { |
|
409 |
WindowsPath file1 = WindowsPath.toWindowsPath(obj1); |
|
410 |
if (file1.equals(obj2)) |
|
411 |
return true; |
|
412 |
if (obj2 == null) |
|
413 |
throw new NullPointerException(); |
|
414 |
if (!(obj2 instanceof WindowsPath)) |
|
415 |
return false; |
|
416 |
WindowsPath file2 = (WindowsPath)obj2; |
|
417 |
||
418 |
// check security manager access to both files |
|
419 |
file1.checkRead(); |
|
420 |
file2.checkRead(); |
|
421 |
||
422 |
// open both files and see if they are the same |
|
423 |
long h1 = 0L; |
|
424 |
try { |
|
425 |
h1 = file1.openForReadAttributeAccess(true); |
|
426 |
} catch (WindowsException x) { |
|
427 |
x.rethrowAsIOException(file1); |
|
428 |
} |
|
429 |
try { |
|
430 |
WindowsFileAttributes attrs1 = null; |
|
431 |
try { |
|
432 |
attrs1 = WindowsFileAttributes.readAttributes(h1); |
|
433 |
} catch (WindowsException x) { |
|
434 |
x.rethrowAsIOException(file1); |
|
435 |
} |
|
436 |
long h2 = 0L; |
|
437 |
try { |
|
438 |
h2 = file2.openForReadAttributeAccess(true); |
|
439 |
} catch (WindowsException x) { |
|
440 |
x.rethrowAsIOException(file2); |
|
441 |
} |
|
442 |
try { |
|
443 |
WindowsFileAttributes attrs2 = null; |
|
444 |
try { |
|
445 |
attrs2 = WindowsFileAttributes.readAttributes(h2); |
|
446 |
} catch (WindowsException x) { |
|
447 |
x.rethrowAsIOException(file2); |
|
448 |
} |
|
449 |
return WindowsFileAttributes.isSameFile(attrs1, attrs2); |
|
450 |
} finally { |
|
451 |
CloseHandle(h2); |
|
452 |
} |
|
453 |
} finally { |
|
454 |
CloseHandle(h1); |
|
455 |
} |
|
456 |
} |
|
457 |
||
458 |
@Override |
|
459 |
public boolean isHidden(Path obj) throws IOException { |
|
460 |
WindowsPath file = WindowsPath.toWindowsPath(obj); |
|
461 |
file.checkRead(); |
|
462 |
WindowsFileAttributes attrs = null; |
|
463 |
try { |
|
464 |
attrs = WindowsFileAttributes.get(file, true); |
|
465 |
} catch (WindowsException x) { |
|
466 |
x.rethrowAsIOException(file); |
|
467 |
} |
|
468 |
// DOS hidden attribute not meaningful when set on directories |
|
469 |
if (attrs.isDirectory()) |
|
470 |
return false; |
|
471 |
return attrs.isHidden(); |
|
472 |
} |
|
473 |
||
474 |
@Override |
|
475 |
public FileStore getFileStore(Path obj) throws IOException { |
|
476 |
WindowsPath file = WindowsPath.toWindowsPath(obj); |
|
477 |
SecurityManager sm = System.getSecurityManager(); |
|
478 |
if (sm != null) { |
|
479 |
sm.checkPermission(new RuntimePermission("getFileStoreAttributes")); |
|
480 |
file.checkRead(); |
|
481 |
} |
|
482 |
return WindowsFileStore.create(file); |
|
483 |
} |
|
484 |
||
485 |
||
486 |
@Override |
|
487 |
public void createDirectory(Path obj, FileAttribute<?>... attrs) |
|
488 |
throws IOException |
|
489 |
{ |
|
490 |
WindowsPath dir = WindowsPath.toWindowsPath(obj); |
|
491 |
dir.checkWrite(); |
|
492 |
WindowsSecurityDescriptor sd = WindowsSecurityDescriptor.fromAttribute(attrs); |
|
493 |
try { |
|
494 |
CreateDirectory(dir.getPathForWin32Calls(), sd.address()); |
|
495 |
} catch (WindowsException x) { |
|
22351
d8ac878ba804
8032220: Files.createDirectories throws exception with confusing message for root directories that exist
alanb
parents:
21665
diff
changeset
|
496 |
// convert ERROR_ACCESS_DENIED to FileAlreadyExistsException if we can |
d8ac878ba804
8032220: Files.createDirectories throws exception with confusing message for root directories that exist
alanb
parents:
21665
diff
changeset
|
497 |
// verify that the directory exists |
d8ac878ba804
8032220: Files.createDirectories throws exception with confusing message for root directories that exist
alanb
parents:
21665
diff
changeset
|
498 |
if (x.lastError() == ERROR_ACCESS_DENIED) { |
d8ac878ba804
8032220: Files.createDirectories throws exception with confusing message for root directories that exist
alanb
parents:
21665
diff
changeset
|
499 |
try { |
d8ac878ba804
8032220: Files.createDirectories throws exception with confusing message for root directories that exist
alanb
parents:
21665
diff
changeset
|
500 |
if (WindowsFileAttributes.get(dir, false).isDirectory()) |
d8ac878ba804
8032220: Files.createDirectories throws exception with confusing message for root directories that exist
alanb
parents:
21665
diff
changeset
|
501 |
throw new FileAlreadyExistsException(dir.toString()); |
d8ac878ba804
8032220: Files.createDirectories throws exception with confusing message for root directories that exist
alanb
parents:
21665
diff
changeset
|
502 |
} catch (WindowsException ignore) { } |
d8ac878ba804
8032220: Files.createDirectories throws exception with confusing message for root directories that exist
alanb
parents:
21665
diff
changeset
|
503 |
} |
8158 | 504 |
x.rethrowAsIOException(dir); |
505 |
} finally { |
|
506 |
sd.release(); |
|
507 |
} |
|
508 |
} |
|
509 |
||
510 |
@Override |
|
511 |
public DirectoryStream<Path> newDirectoryStream(Path obj, DirectoryStream.Filter<? super Path> filter) |
|
512 |
throws IOException |
|
513 |
{ |
|
514 |
WindowsPath dir = WindowsPath.toWindowsPath(obj); |
|
515 |
dir.checkRead(); |
|
516 |
if (filter == null) |
|
517 |
throw new NullPointerException(); |
|
518 |
return new WindowsDirectoryStream(dir, filter); |
|
519 |
} |
|
520 |
||
521 |
@Override |
|
522 |
public void createSymbolicLink(Path obj1, Path obj2, FileAttribute<?>... attrs) |
|
523 |
throws IOException |
|
524 |
{ |
|
525 |
WindowsPath link = WindowsPath.toWindowsPath(obj1); |
|
526 |
WindowsPath target = WindowsPath.toWindowsPath(obj2); |
|
527 |
||
528 |
if (!link.getFileSystem().supportsLinks()) { |
|
529 |
throw new UnsupportedOperationException("Symbolic links not supported " |
|
530 |
+ "on this operating system"); |
|
531 |
} |
|
532 |
||
533 |
// no attributes allowed |
|
534 |
if (attrs.length > 0) { |
|
535 |
WindowsSecurityDescriptor.fromAttribute(attrs); // may throw NPE or UOE |
|
536 |
throw new UnsupportedOperationException("Initial file attributes" + |
|
537 |
"not supported when creating symbolic link"); |
|
538 |
} |
|
539 |
||
540 |
// permission check |
|
541 |
SecurityManager sm = System.getSecurityManager(); |
|
542 |
if (sm != null) { |
|
543 |
sm.checkPermission(new LinkPermission("symbolic")); |
|
544 |
link.checkWrite(); |
|
545 |
} |
|
546 |
||
547 |
/** |
|
548 |
* Throw I/O exception for the drive-relative case because Windows |
|
549 |
* creates a link with the resolved target for this case. |
|
550 |
*/ |
|
551 |
if (target.type() == WindowsPathType.DRIVE_RELATIVE) { |
|
552 |
throw new IOException("Cannot create symbolic link to working directory relative target"); |
|
553 |
} |
|
554 |
||
555 |
/* |
|
23887
7b2fb8d5f6be
8040262: (fs) Misc. typos in comments and implementation
alanb
parents:
23010
diff
changeset
|
556 |
* Windows treats symbolic links to directories differently than it |
8158 | 557 |
* does to other file types. For that reason we need to check if the |
558 |
* target is a directory (or a directory junction). |
|
559 |
*/ |
|
560 |
WindowsPath resolvedTarget; |
|
561 |
if (target.type() == WindowsPathType.RELATIVE) { |
|
562 |
WindowsPath parent = link.getParent(); |
|
563 |
resolvedTarget = (parent == null) ? target : parent.resolve(target); |
|
564 |
} else { |
|
565 |
resolvedTarget = link.resolve(target); |
|
566 |
} |
|
567 |
int flags = 0; |
|
568 |
try { |
|
569 |
WindowsFileAttributes wattrs = WindowsFileAttributes.get(resolvedTarget, false); |
|
570 |
if (wattrs.isDirectory() || wattrs.isDirectoryLink()) |
|
571 |
flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; |
|
572 |
} catch (WindowsException x) { |
|
573 |
// unable to access target so assume target is not a directory |
|
574 |
} |
|
575 |
||
576 |
// create the link |
|
577 |
try { |
|
578 |
CreateSymbolicLink(link.getPathForWin32Calls(), |
|
579 |
WindowsPath.addPrefixIfNeeded(target.toString()), |
|
580 |
flags); |
|
581 |
} catch (WindowsException x) { |
|
582 |
if (x.lastError() == ERROR_INVALID_REPARSE_DATA) { |
|
583 |
x.rethrowAsIOException(link, target); |
|
584 |
} else { |
|
585 |
x.rethrowAsIOException(link); |
|
586 |
} |
|
587 |
} |
|
588 |
} |
|
589 |
||
590 |
@Override |
|
591 |
public void createLink(Path obj1, Path obj2) throws IOException { |
|
592 |
WindowsPath link = WindowsPath.toWindowsPath(obj1); |
|
593 |
WindowsPath existing = WindowsPath.toWindowsPath(obj2); |
|
594 |
||
595 |
// permission check |
|
596 |
SecurityManager sm = System.getSecurityManager(); |
|
597 |
if (sm != null) { |
|
598 |
sm.checkPermission(new LinkPermission("hard")); |
|
599 |
link.checkWrite(); |
|
600 |
existing.checkWrite(); |
|
601 |
} |
|
602 |
||
603 |
// create hard link |
|
604 |
try { |
|
605 |
CreateHardLink(link.getPathForWin32Calls(), |
|
606 |
existing.getPathForWin32Calls()); |
|
607 |
} catch (WindowsException x) { |
|
608 |
x.rethrowAsIOException(link, existing); |
|
609 |
} |
|
610 |
} |
|
611 |
||
612 |
@Override |
|
613 |
public Path readSymbolicLink(Path obj1) throws IOException { |
|
614 |
WindowsPath link = WindowsPath.toWindowsPath(obj1); |
|
615 |
WindowsFileSystem fs = link.getFileSystem(); |
|
616 |
if (!fs.supportsLinks()) { |
|
617 |
throw new UnsupportedOperationException("symbolic links not supported"); |
|
618 |
} |
|
619 |
||
620 |
// permission check |
|
621 |
SecurityManager sm = System.getSecurityManager(); |
|
622 |
if (sm != null) { |
|
623 |
FilePermission perm = new FilePermission(link.getPathForPermissionCheck(), |
|
624 |
SecurityConstants.FILE_READLINK_ACTION); |
|
21665
18a9cae3b4d5
8028270: Files.readSymbolicLink calls AccessController directly so security manager can't grant the permission
alanb
parents:
16473
diff
changeset
|
625 |
sm.checkPermission(perm); |
8158 | 626 |
} |
627 |
||
628 |
String target = WindowsLinkSupport.readLink(link); |
|
629 |
return WindowsPath.createFromNormalizedPath(fs, target); |
|
630 |
} |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
631 |
} |