author | weijun |
Mon, 31 Oct 2016 16:41:41 +0800 | |
changeset 41823 | 10b6e8a41af5 |
parent 41822 | 9398d77cc4bb |
child 42314 | e0c05dfa71db |
permissions | -rw-r--r-- |
2 | 1 |
/* |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
2 |
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
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 |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.io; |
|
27 |
||
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
28 |
import java.net.URI; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
29 |
import java.nio.file.*; |
2 | 30 |
import java.security.*; |
31 |
import java.util.Enumeration; |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
32 |
import java.util.Objects; |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
33 |
import java.util.StringJoiner; |
2 | 34 |
import java.util.Vector; |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
35 |
import java.util.concurrent.ConcurrentHashMap; |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
36 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
37 |
import jdk.internal.misc.JavaIOFilePermissionAccess; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
38 |
import jdk.internal.misc.SharedSecrets; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
39 |
import sun.nio.fs.DefaultFileSystemProvider; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
40 |
import sun.security.action.GetPropertyAction; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
41 |
import sun.security.util.FilePermCompat; |
2 | 42 |
import sun.security.util.SecurityConstants; |
43 |
||
44 |
/** |
|
45 |
* This class represents access to a file or directory. A FilePermission consists |
|
46 |
* of a pathname and a set of actions valid for that pathname. |
|
47 |
* <P> |
|
48 |
* Pathname is the pathname of the file or directory granted the specified |
|
49 |
* actions. A pathname that ends in "/*" (where "/" is |
|
50 |
* the file separator character, <code>File.separatorChar</code>) indicates |
|
51 |
* all the files and directories contained in that directory. A pathname |
|
52 |
* that ends with "/-" indicates (recursively) all files |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
53 |
* and subdirectories contained in that directory. Such a pathname is called |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
54 |
* a wildcard pathname. Otherwise, it's a simple pathname. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
55 |
* <P> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
56 |
* A pathname consisting of the special token {@literal "<<ALL FILES>>"} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
57 |
* matches <b>any</b> file. |
2 | 58 |
* <P> |
59 |
* Note: A pathname consisting of a single "*" indicates all the files |
|
60 |
* in the current directory, while a pathname consisting of a single "-" |
|
61 |
* indicates all the files in the current directory and |
|
62 |
* (recursively) all files and subdirectories contained in the current |
|
63 |
* directory. |
|
64 |
* <P> |
|
65 |
* The actions to be granted are passed to the constructor in a string containing |
|
66 |
* a list of one or more comma-separated keywords. The possible keywords are |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
67 |
* "read", "write", "execute", "delete", and "readlink". Their meaning is |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
68 |
* defined as follows: |
21334 | 69 |
* |
2 | 70 |
* <DL> |
71 |
* <DT> read <DD> read permission |
|
72 |
* <DT> write <DD> write permission |
|
73 |
* <DT> execute |
|
74 |
* <DD> execute permission. Allows <code>Runtime.exec</code> to |
|
75 |
* be called. Corresponds to <code>SecurityManager.checkExec</code>. |
|
76 |
* <DT> delete |
|
77 |
* <DD> delete permission. Allows <code>File.delete</code> to |
|
78 |
* be called. Corresponds to <code>SecurityManager.checkDelete</code>. |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
79 |
* <DT> readlink |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
80 |
* <DD> read link permission. Allows the target of a |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
81 |
* <a href="../nio/file/package-summary.html#links">symbolic link</a> |
8158 | 82 |
* to be read by invoking the {@link java.nio.file.Files#readSymbolicLink |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
83 |
* readSymbolicLink } method. |
2 | 84 |
* </DL> |
85 |
* <P> |
|
86 |
* The actions string is converted to lowercase before processing. |
|
87 |
* <P> |
|
88 |
* Be careful when granting FilePermissions. Think about the implications |
|
89 |
* of granting read and especially write access to various files and |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
90 |
* directories. The {@literal "<<ALL FILES>>"} permission with write action is |
2 | 91 |
* especially dangerous. This grants permission to write to the entire |
92 |
* file system. One thing this effectively allows is replacement of the |
|
93 |
* system binary, including the JVM runtime environment. |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
94 |
* <P> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
95 |
* Please note: Code can always read a file from the same |
2 | 96 |
* directory it's in (or a subdirectory of that directory); it does not |
97 |
* need explicit permission to do so. |
|
98 |
* |
|
99 |
* @see java.security.Permission |
|
100 |
* @see java.security.Permissions |
|
101 |
* @see java.security.PermissionCollection |
|
102 |
* |
|
103 |
* |
|
104 |
* @author Marianne Mueller |
|
105 |
* @author Roland Schemers |
|
106 |
* @since 1.2 |
|
107 |
* |
|
108 |
* @serial exclude |
|
109 |
*/ |
|
110 |
||
111 |
public final class FilePermission extends Permission implements Serializable { |
|
112 |
||
113 |
/** |
|
114 |
* Execute action. |
|
115 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31080
diff
changeset
|
116 |
private static final int EXECUTE = 0x1; |
2 | 117 |
/** |
118 |
* Write action. |
|
119 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31080
diff
changeset
|
120 |
private static final int WRITE = 0x2; |
2 | 121 |
/** |
122 |
* Read action. |
|
123 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31080
diff
changeset
|
124 |
private static final int READ = 0x4; |
2 | 125 |
/** |
126 |
* Delete action. |
|
127 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31080
diff
changeset
|
128 |
private static final int DELETE = 0x8; |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
129 |
/** |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
130 |
* Read link action. |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
131 |
*/ |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31080
diff
changeset
|
132 |
private static final int READLINK = 0x10; |
2 | 133 |
|
134 |
/** |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
135 |
* All actions (read,write,execute,delete,readlink) |
2 | 136 |
*/ |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31080
diff
changeset
|
137 |
private static final int ALL = READ|WRITE|EXECUTE|DELETE|READLINK; |
2 | 138 |
/** |
139 |
* No actions. |
|
140 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31080
diff
changeset
|
141 |
private static final int NONE = 0x0; |
2 | 142 |
|
143 |
// the actions mask |
|
144 |
private transient int mask; |
|
145 |
||
146 |
// does path indicate a directory? (wildcard or recursive) |
|
147 |
private transient boolean directory; |
|
148 |
||
149 |
// is it a recursive directory specification? |
|
150 |
private transient boolean recursive; |
|
151 |
||
152 |
/** |
|
153 |
* the actions string. |
|
154 |
* |
|
155 |
* @serial |
|
156 |
*/ |
|
157 |
private String actions; // Left null as long as possible, then |
|
158 |
// created and re-used in the getAction function. |
|
159 |
||
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
160 |
// canonicalized dir path. used by the "old" behavior (nb == false). |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
161 |
// In the case of directories, it is the name "/blah/*" or "/blah/-" |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
162 |
// without the last character (the "*" or "-"). |
2 | 163 |
|
164 |
private transient String cpath; |
|
165 |
||
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
166 |
// Following fields used by the "new" behavior (nb == true), in which |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
167 |
// input path is not canonicalized. For compatibility (so that granting |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
168 |
// FilePermission on "x" allows reading "`pwd`/x", an alternative path |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
169 |
// can be added so that both can be used in an implies() check. Please note |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
170 |
// the alternative path only deals with absolute/relative path, and does |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
171 |
// not deal with symlink/target. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
172 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
173 |
private transient Path npath; // normalized dir path. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
174 |
private transient Path npath2; // alternative normalized dir path. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
175 |
private transient boolean allFiles; // whether this is <<ALL FILES>> |
41823 | 176 |
private transient boolean invalid; // whether input path is invalid |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
177 |
|
2 | 178 |
// static Strings used by init(int mask) |
179 |
private static final char RECURSIVE_CHAR = '-'; |
|
180 |
private static final char WILD_CHAR = '*'; |
|
181 |
||
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
182 |
// public String toString() { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
183 |
// StringBuffer sb = new StringBuffer(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
184 |
// sb.append("*** FilePermission on " + getName() + " ***"); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
185 |
// for (Field f : FilePermission.class.getDeclaredFields()) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
186 |
// if (!Modifier.isStatic(f.getModifiers())) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
187 |
// try { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
188 |
// sb.append(f.getName() + " = " + f.get(this)); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
189 |
// } catch (Exception e) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
190 |
// sb.append(f.getName() + " = " + e.toString()); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
191 |
// } |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
192 |
// sb.append('\n'); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
193 |
// } |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
194 |
// } |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
195 |
// sb.append("***\n"); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
196 |
// return sb.toString(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
197 |
// } |
2 | 198 |
|
199 |
private static final long serialVersionUID = 7930732926638008763L; |
|
200 |
||
201 |
/** |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
202 |
* Always use the internal default file system, in case it was modified |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
203 |
* with java.nio.file.spi.DefaultFileSystemProvider. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
204 |
*/ |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
205 |
private static final java.nio.file.FileSystem builtInFS = |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
206 |
DefaultFileSystemProvider.create() |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
207 |
.getFileSystem(URI.create("file:///")); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
208 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
209 |
/** |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
210 |
* Creates FilePermission objects with special internals. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
211 |
* See {@link FilePermCompat#newPermPlusAltPath(Permission)} and |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
212 |
* {@link FilePermCompat#newPermUsingAltPath(Permission)}. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
213 |
*/ |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
214 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
215 |
private static final Path here = builtInFS.getPath( |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
216 |
GetPropertyAction.privilegedGetProperty("user.dir")); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
217 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
218 |
/** |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
219 |
* A private constructor like a clone, only npath2 is not touched. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
220 |
* @param input |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
221 |
*/ |
41822
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
222 |
private FilePermission(String name, FilePermission input) { |
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
223 |
super(name); |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
224 |
this.npath = input.npath; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
225 |
this.actions = input.actions; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
226 |
this.allFiles = input.allFiles; |
41823 | 227 |
this.invalid = input.invalid; |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
228 |
this.recursive = input.recursive; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
229 |
this.directory = input.directory; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
230 |
this.cpath = input.cpath; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
231 |
this.mask = input.mask; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
232 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
233 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
234 |
/** |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
235 |
* Returns the alternative path as a Path object, i.e. absolute path |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
236 |
* for a relative one, or vice versa. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
237 |
* |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
238 |
* @param in a real path w/o "-" or "*" at the end, and not <<ALL FILES>>. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
239 |
* @return the alternative path, or null if cannot find one. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
240 |
*/ |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
241 |
private static Path altPath(Path in) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
242 |
try { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
243 |
if (!in.isAbsolute()) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
244 |
return here.resolve(in).normalize(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
245 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
246 |
return here.relativize(in).normalize(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
247 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
248 |
} catch (IllegalArgumentException e) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
249 |
return null; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
250 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
251 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
252 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
253 |
static { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
254 |
SharedSecrets.setJavaIOFilePermissionAccess( |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
255 |
new JavaIOFilePermissionAccess() { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
256 |
public FilePermission newPermPlusAltPath(FilePermission input) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
257 |
if (input.npath2 == null && !input.allFiles) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
258 |
Path npath2 = altPath(input.npath); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
259 |
if (npath2 != null) { |
41822
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
260 |
// Please note the name of the new permission is |
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
261 |
// different than the original so that when one is |
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
262 |
// added to a FilePermissionCollection it will not |
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
263 |
// be merged with the original one. |
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
264 |
FilePermission np = new FilePermission( |
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
265 |
input.getName()+"#plus", input); |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
266 |
np.npath2 = npath2; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
267 |
return np; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
268 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
269 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
270 |
return input; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
271 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
272 |
public FilePermission newPermUsingAltPath(FilePermission input) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
273 |
if (!input.allFiles) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
274 |
Path npath2 = altPath(input.npath); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
275 |
if (npath2 != null) { |
41822
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
276 |
// New name, see above. |
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
277 |
FilePermission np = new FilePermission( |
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
278 |
input.getName()+"#using", input); |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
279 |
np.npath = npath2; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
280 |
return np; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
281 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
282 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
283 |
return null; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
284 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
285 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
286 |
); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
287 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
288 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
289 |
/** |
2 | 290 |
* initialize a FilePermission object. Common to all constructors. |
291 |
* Also called during de-serialization. |
|
292 |
* |
|
293 |
* @param mask the actions mask to use. |
|
294 |
* |
|
295 |
*/ |
|
14014 | 296 |
private void init(int mask) { |
2 | 297 |
if ((mask & ALL) != mask) |
298 |
throw new IllegalArgumentException("invalid actions mask"); |
|
299 |
||
300 |
if (mask == NONE) |
|
301 |
throw new IllegalArgumentException("invalid actions mask"); |
|
302 |
||
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
303 |
if (FilePermCompat.nb) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
304 |
String name = getName(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
305 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
306 |
if (name == null) |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
307 |
throw new NullPointerException("name can't be null"); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
308 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
309 |
this.mask = mask; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
310 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
311 |
if (name.equals("<<ALL FILES>>")) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
312 |
allFiles = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
313 |
npath = builtInFS.getPath(""); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
314 |
// other fields remain default |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
315 |
return; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
316 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
317 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
318 |
boolean rememberStar = false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
319 |
if (name.endsWith("*")) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
320 |
rememberStar = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
321 |
recursive = false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
322 |
name = name.substring(0, name.length()-1) + "-"; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
323 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
324 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
325 |
try { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
326 |
// new File() can "normalize" some name, for example, "/C:/X" on |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
327 |
// Windows. Some JDK codes generate such illegal names. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
328 |
npath = builtInFS.getPath(new File(name).getPath()) |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
329 |
.normalize(); |
41823 | 330 |
invalid = false; |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
331 |
} catch (InvalidPathException ipe) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
332 |
// Still invalid. For compatibility reason, accept it |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
333 |
// but make this permission useless. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
334 |
npath = builtInFS.getPath("-u-s-e-l-e-s-s-"); |
41823 | 335 |
invalid = true; |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
336 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
337 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
338 |
// lastName should always be non-null now |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
339 |
Path lastName = npath.getFileName(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
340 |
if (lastName != null && lastName.toString().equals("-")) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
341 |
directory = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
342 |
recursive = !rememberStar; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
343 |
npath = npath.getParent(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
344 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
345 |
if (npath == null) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
346 |
npath = builtInFS.getPath(""); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
347 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
348 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
349 |
if ((cpath = getName()) == null) |
2 | 350 |
throw new NullPointerException("name can't be null"); |
351 |
||
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
352 |
this.mask = mask; |
2 | 353 |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
354 |
if (cpath.equals("<<ALL FILES>>")) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
355 |
directory = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
356 |
recursive = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
357 |
cpath = ""; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
358 |
return; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
359 |
} |
2 | 360 |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
361 |
// store only the canonical cpath if possible |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
362 |
cpath = AccessController.doPrivileged(new PrivilegedAction<>() { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
363 |
public String run() { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
364 |
try { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
365 |
String path = cpath; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
366 |
if (cpath.endsWith("*")) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
367 |
// call getCanonicalPath with a path with wildcard character |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
368 |
// replaced to avoid calling it with paths that are |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
369 |
// intended to match all entries in a directory |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
370 |
path = path.substring(0, path.length() - 1) + "-"; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
371 |
path = new File(path).getCanonicalPath(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
372 |
return path.substring(0, path.length() - 1) + "*"; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
373 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
374 |
return new File(path).getCanonicalPath(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
375 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
376 |
} catch (IOException ioe) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
377 |
return cpath; |
4053
c2f8e57ba2f8
6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents:
2057
diff
changeset
|
378 |
} |
2 | 379 |
} |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
380 |
}); |
2 | 381 |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
382 |
int len = cpath.length(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
383 |
char last = ((len > 0) ? cpath.charAt(len - 1) : 0); |
2 | 384 |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
385 |
if (last == RECURSIVE_CHAR && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
386 |
cpath.charAt(len - 2) == File.separatorChar) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
387 |
directory = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
388 |
recursive = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
389 |
cpath = cpath.substring(0, --len); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
390 |
} else if (last == WILD_CHAR && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
391 |
cpath.charAt(len - 2) == File.separatorChar) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
392 |
directory = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
393 |
//recursive = false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
394 |
cpath = cpath.substring(0, --len); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
395 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
396 |
// overkill since they are initialized to false, but |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
397 |
// commented out here to remind us... |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
398 |
//directory = false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
399 |
//recursive = false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
400 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
401 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
402 |
// XXX: at this point the path should be absolute. die if it isn't? |
2 | 403 |
} |
404 |
} |
|
405 |
||
406 |
/** |
|
407 |
* Creates a new FilePermission object with the specified actions. |
|
408 |
* <i>path</i> is the pathname of a file or directory, and <i>actions</i> |
|
409 |
* contains a comma-separated list of the desired actions granted on the |
|
410 |
* file or directory. Possible actions are |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
411 |
* "read", "write", "execute", "delete", and "readlink". |
2 | 412 |
* |
413 |
* <p>A pathname that ends in "/*" (where "/" is |
|
414 |
* the file separator character, <code>File.separatorChar</code>) |
|
415 |
* indicates all the files and directories contained in that directory. |
|
416 |
* A pathname that ends with "/-" indicates (recursively) all files and |
|
417 |
* subdirectories contained in that directory. The special pathname |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
418 |
* {@literal "<<ALL FILES>>"} matches any file. |
2 | 419 |
* |
420 |
* <p>A pathname consisting of a single "*" indicates all the files |
|
421 |
* in the current directory, while a pathname consisting of a single "-" |
|
422 |
* indicates all the files in the current directory and |
|
423 |
* (recursively) all files and subdirectories contained in the current |
|
424 |
* directory. |
|
425 |
* |
|
426 |
* <p>A pathname containing an empty string represents an empty path. |
|
427 |
* |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
428 |
* @implNote In this implementation, the |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
429 |
* {@code jdk.io.permissionsUseCanonicalPath} system property dictates how |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
430 |
* the {@code path} argument is processed and stored. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
431 |
* <P> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
432 |
* If the value of the system property is set to {@code true}, {@code path} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
433 |
* is canonicalized and stored as a String object named {@code cpath}. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
434 |
* This means a relative path is converted to an absolute path, a Windows |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
435 |
* DOS-style 8.3 path is expanded to a long path, and a symbolic link is |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
436 |
* resolved to its target, etc. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
437 |
* <P> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
438 |
* If the value of the system property is set to {@code false}, {@code path} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
439 |
* is converted to a {@link java.nio.file.Path} object named {@code npath} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
440 |
* after {@link Path#normalize() normalization}. No canonicalization is |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
441 |
* performed which means the underlying file system is not accessed. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
442 |
* <P> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
443 |
* In either case, the "*" or "-" character at the end of a wildcard |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
444 |
* {@code path} is removed before canonicalization or normalization. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
445 |
* It is stored in a separate wildcard flag field. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
446 |
* <P> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
447 |
* The default value of the {@code jdk.io.permissionsUseCanonicalPath} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
448 |
* system property is {@code false} in this implementation. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
449 |
* |
2 | 450 |
* @param path the pathname of the file/directory. |
451 |
* @param actions the action string. |
|
452 |
* |
|
453 |
* @throws IllegalArgumentException |
|
454 |
* If actions is <code>null</code>, empty or contains an action |
|
455 |
* other than the specified possible actions. |
|
456 |
*/ |
|
14014 | 457 |
public FilePermission(String path, String actions) { |
2 | 458 |
super(path); |
459 |
init(getMask(actions)); |
|
460 |
} |
|
461 |
||
462 |
/** |
|
463 |
* Creates a new FilePermission object using an action mask. |
|
464 |
* More efficient than the FilePermission(String, String) constructor. |
|
465 |
* Can be used from within |
|
466 |
* code that needs to create a FilePermission object to pass into the |
|
467 |
* <code>implies</code> method. |
|
468 |
* |
|
469 |
* @param path the pathname of the file/directory. |
|
470 |
* @param mask the action mask to use. |
|
471 |
*/ |
|
472 |
// package private for use by the FilePermissionCollection add method |
|
14014 | 473 |
FilePermission(String path, int mask) { |
2 | 474 |
super(path); |
475 |
init(mask); |
|
476 |
} |
|
477 |
||
478 |
/** |
|
479 |
* Checks if this FilePermission object "implies" the specified permission. |
|
480 |
* <P> |
|
21334 | 481 |
* More specifically, this method returns true if: |
2 | 482 |
* <ul> |
21334 | 483 |
* <li> <i>p</i> is an instanceof FilePermission, |
2 | 484 |
* <li> <i>p</i>'s actions are a proper subset of this |
21334 | 485 |
* object's actions, and |
2 | 486 |
* <li> <i>p</i>'s pathname is implied by this object's |
487 |
* pathname. For example, "/tmp/*" implies "/tmp/foo", since |
|
488 |
* "/tmp/*" encompasses all files in the "/tmp" directory, |
|
489 |
* including the one named "foo". |
|
490 |
* </ul> |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
491 |
* <P> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
492 |
* Precisely, a simple pathname implies another simple pathname |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
493 |
* if and only if they are equal. A simple pathname never implies |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
494 |
* a wildcard pathname. A wildcard pathname implies another wildcard |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
495 |
* pathname if and only if all simple pathnames implied by the latter |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
496 |
* are implied by the former. A wildcard pathname implies a simple |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
497 |
* pathname if and only if |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
498 |
* <ul> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
499 |
* <li>if the wildcard flag is "*", the simple pathname's path |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
500 |
* must be right inside the wildcard pathname's path. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
501 |
* <li>if the wildcard flag is "-", the simple pathname's path |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
502 |
* must be recursively inside the wildcard pathname's path. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
503 |
* </ul> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
504 |
* <P> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
505 |
* {@literal "<<ALL FILES>>"} implies every other pathname. No pathname, |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
506 |
* except for {@literal "<<ALL FILES>>"} itself, implies |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
507 |
* {@literal "<<ALL FILES>>"}. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
508 |
* |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
509 |
* @implNote |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
510 |
* If {@code jdk.io.permissionsUseCanonicalPath} is {@code true}, a |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
511 |
* simple {@code cpath} is inside a wildcard {@code cpath} if and only if |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
512 |
* after removing the base name (the last name in the pathname's name |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
513 |
* sequence) from the former the remaining part equals to the latter, |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
514 |
* a simple {@code cpath} is recursively inside a wildcard {@code cpath} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
515 |
* if and only if the former starts with the latter. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
516 |
* <p> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
517 |
* If {@code jdk.io.permissionsUseCanonicalPath} is {@code false}, a |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
518 |
* simple {@code npath} is inside a wildcard {@code npath} if and only if |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
519 |
* {@code simple_npath.relativize(wildcard_npath)} is exactly "..", |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
520 |
* a simple {@code npath} is recursively inside a wildcard {@code npath} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
521 |
* if and only if {@code simple_npath.relativize(wildcard_npath)} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
522 |
* is a series of one or more "..". |
2 | 523 |
* |
524 |
* @param p the permission to check against. |
|
525 |
* |
|
526 |
* @return <code>true</code> if the specified permission is not |
|
527 |
* <code>null</code> and is implied by this object, |
|
528 |
* <code>false</code> otherwise. |
|
529 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
530 |
@Override |
2 | 531 |
public boolean implies(Permission p) { |
532 |
if (!(p instanceof FilePermission)) |
|
533 |
return false; |
|
534 |
||
535 |
FilePermission that = (FilePermission) p; |
|
536 |
||
537 |
// we get the effective mask. i.e., the "and" of this and that. |
|
538 |
// They must be equal to that.mask for implies to return true. |
|
539 |
||
540 |
return ((this.mask & that.mask) == that.mask) && impliesIgnoreMask(that); |
|
541 |
} |
|
542 |
||
543 |
/** |
|
544 |
* Checks if the Permission's actions are a proper subset of the |
|
545 |
* this object's actions. Returns the effective mask iff the |
|
546 |
* this FilePermission's path also implies that FilePermission's path. |
|
547 |
* |
|
548 |
* @param that the FilePermission to check against. |
|
549 |
* @return the effective mask |
|
550 |
*/ |
|
551 |
boolean impliesIgnoreMask(FilePermission that) { |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
552 |
if (FilePermCompat.nb) { |
41823 | 553 |
if (this == that) { |
554 |
return true; |
|
555 |
} |
|
556 |
if (this.invalid || that.invalid) { |
|
557 |
return false; |
|
558 |
} |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
559 |
if (allFiles) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
560 |
return true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
561 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
562 |
if (that.allFiles) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
563 |
return false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
564 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
565 |
// Left at least same level of wildness as right |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
566 |
if ((this.recursive && that.recursive) != that.recursive |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
567 |
|| (this.directory && that.directory) != that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
568 |
return false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
569 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
570 |
// Same npath is good as long as both or neither are directories |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
571 |
if (this.npath.equals(that.npath) |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
572 |
&& this.directory == that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
573 |
return true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
574 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
575 |
int diff = containsPath(this.npath, that.npath); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
576 |
// Right inside left is good if recursive |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
577 |
if (diff >= 1 && recursive) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
578 |
return true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
579 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
580 |
// Right right inside left if it is element in set |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
581 |
if (diff == 1 && directory && !that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
582 |
return true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
583 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
584 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
585 |
// Hack: if a npath2 field exists, apply the same checks |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
586 |
// on it as a fallback. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
587 |
if (this.npath2 != null) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
588 |
if (this.npath2.equals(that.npath) |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
589 |
&& this.directory == that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
590 |
return true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
591 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
592 |
diff = containsPath(this.npath2, that.npath); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
593 |
if (diff >= 1 && recursive) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
594 |
return true; |
2 | 595 |
} |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
596 |
if (diff == 1 && directory && !that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
597 |
return true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
598 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
599 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
600 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
601 |
return false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
602 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
603 |
if (this.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
604 |
if (this.recursive) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
605 |
// make sure that.path is longer then path so |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
606 |
// something like /foo/- does not imply /foo |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
607 |
if (that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
608 |
return (that.cpath.length() >= this.cpath.length()) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
609 |
that.cpath.startsWith(this.cpath); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
610 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
611 |
return ((that.cpath.length() > this.cpath.length()) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
612 |
that.cpath.startsWith(this.cpath)); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
613 |
} |
2 | 614 |
} else { |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
615 |
if (that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
616 |
// if the permission passed in is a directory |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
617 |
// specification, make sure that a non-recursive |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
618 |
// permission (i.e., this object) can't imply a recursive |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
619 |
// permission. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
620 |
if (that.recursive) |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
621 |
return false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
622 |
else |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
623 |
return (this.cpath.equals(that.cpath)); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
624 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
625 |
int last = that.cpath.lastIndexOf(File.separatorChar); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
626 |
if (last == -1) |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
627 |
return false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
628 |
else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
629 |
// this.cpath.equals(that.cpath.substring(0, last+1)); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
630 |
// Use regionMatches to avoid creating new string |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
631 |
return (this.cpath.length() == (last + 1)) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
632 |
this.cpath.regionMatches(0, that.cpath, 0, last + 1); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
633 |
} |
2 | 634 |
} |
635 |
} |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
636 |
} else if (that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
637 |
// if this is NOT recursive/wildcarded, |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
638 |
// do not let it imply a recursive/wildcarded permission |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
639 |
return false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
640 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
641 |
return (this.cpath.equals(that.cpath)); |
2 | 642 |
} |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
643 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
644 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
645 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
646 |
/** |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
647 |
* Returns the depth between an outer path p1 and an inner path p2. -1 |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
648 |
* is returned if |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
649 |
* |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
650 |
* - p1 does not contains p2. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
651 |
* - this is not decidable. For example, p1="../x", p2="y". |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
652 |
* - the depth is not decidable. For example, p1="/", p2="x". |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
653 |
* |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
654 |
* This method can return 2 if the depth is greater than 2. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
655 |
* |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
656 |
* @param p1 the expected outer path, normalized |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
657 |
* @param p2 the expected inner path, normalized |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
658 |
* @return the depth in between |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
659 |
*/ |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
660 |
private static int containsPath(Path p1, Path p2) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
661 |
Path p; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
662 |
try { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
663 |
p = p2.relativize(p1).normalize(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
664 |
if (p.getName(0).toString().isEmpty()) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
665 |
return 0; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
666 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
667 |
for (Path item: p) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
668 |
String s = item.toString(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
669 |
if (!s.equals("..")) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
670 |
return -1; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
671 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
672 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
673 |
return p.getNameCount(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
674 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
675 |
} catch (IllegalArgumentException iae) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
676 |
return -1; |
2 | 677 |
} |
678 |
} |
|
679 |
||
680 |
/** |
|
681 |
* Checks two FilePermission objects for equality. Checks that <i>obj</i> is |
|
682 |
* a FilePermission, and has the same pathname and actions as this object. |
|
21801
b8a5ff5f0c2a
8028049: Tidy warnings cleanup for packages java.nio/java.io
yan
parents:
21334
diff
changeset
|
683 |
* |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
684 |
* @implNote More specifically, two pathnames are the same if and only if |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
685 |
* they have the same wildcard flag and their {@code cpath} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
686 |
* (if {@code jdk.io.permissionsUseCanonicalPath} is {@code true}) or |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
687 |
* {@code npath} (if {@code jdk.io.permissionsUseCanonicalPath} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
688 |
* is {@code false}) are equal. Or they are both {@literal "<<ALL FILES>>"}. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
689 |
* |
2 | 690 |
* @param obj the object we are testing for equality with this object. |
691 |
* @return <code>true</code> if obj is a FilePermission, and has the same |
|
692 |
* pathname and actions as this FilePermission object, |
|
693 |
* <code>false</code> otherwise. |
|
694 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
695 |
@Override |
2 | 696 |
public boolean equals(Object obj) { |
697 |
if (obj == this) |
|
698 |
return true; |
|
699 |
||
700 |
if (! (obj instanceof FilePermission)) |
|
701 |
return false; |
|
702 |
||
703 |
FilePermission that = (FilePermission) obj; |
|
704 |
||
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
705 |
if (FilePermCompat.nb) { |
41823 | 706 |
if (this.invalid || that.invalid) { |
707 |
return false; |
|
708 |
} |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
709 |
return (this.mask == that.mask) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
710 |
(this.allFiles == that.allFiles) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
711 |
this.npath.equals(that.npath) && |
41822
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
712 |
Objects.equals(npath2, that.npath2) && |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
713 |
(this.directory == that.directory) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
714 |
(this.recursive == that.recursive); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
715 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
716 |
return (this.mask == that.mask) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
717 |
this.cpath.equals(that.cpath) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
718 |
(this.directory == that.directory) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
719 |
(this.recursive == that.recursive); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
720 |
} |
2 | 721 |
} |
722 |
||
723 |
/** |
|
724 |
* Returns the hash code value for this object. |
|
725 |
* |
|
726 |
* @return a hash code value for this object. |
|
727 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
728 |
@Override |
2 | 729 |
public int hashCode() { |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
730 |
if (FilePermCompat.nb) { |
41822
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
731 |
return Objects.hash( |
41823 | 732 |
mask, allFiles, directory, recursive, npath, npath2, invalid); |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
733 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
734 |
return 0; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
735 |
} |
2 | 736 |
} |
737 |
||
738 |
/** |
|
739 |
* Converts an actions String to an actions mask. |
|
740 |
* |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
9035
diff
changeset
|
741 |
* @param actions the action string. |
2 | 742 |
* @return the actions mask. |
743 |
*/ |
|
744 |
private static int getMask(String actions) { |
|
745 |
int mask = NONE; |
|
746 |
||
747 |
// Null action valid? |
|
748 |
if (actions == null) { |
|
749 |
return mask; |
|
750 |
} |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
9035
diff
changeset
|
751 |
|
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
9035
diff
changeset
|
752 |
// Use object identity comparison against known-interned strings for |
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
9035
diff
changeset
|
753 |
// performance benefit (these values are used heavily within the JDK). |
2 | 754 |
if (actions == SecurityConstants.FILE_READ_ACTION) { |
755 |
return READ; |
|
756 |
} else if (actions == SecurityConstants.FILE_WRITE_ACTION) { |
|
757 |
return WRITE; |
|
758 |
} else if (actions == SecurityConstants.FILE_EXECUTE_ACTION) { |
|
759 |
return EXECUTE; |
|
760 |
} else if (actions == SecurityConstants.FILE_DELETE_ACTION) { |
|
761 |
return DELETE; |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
762 |
} else if (actions == SecurityConstants.FILE_READLINK_ACTION) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
763 |
return READLINK; |
2 | 764 |
} |
765 |
||
766 |
char[] a = actions.toCharArray(); |
|
767 |
||
768 |
int i = a.length - 1; |
|
769 |
if (i < 0) |
|
770 |
return mask; |
|
771 |
||
772 |
while (i != -1) { |
|
773 |
char c; |
|
774 |
||
775 |
// skip whitespace |
|
776 |
while ((i!=-1) && ((c = a[i]) == ' ' || |
|
777 |
c == '\r' || |
|
778 |
c == '\n' || |
|
779 |
c == '\f' || |
|
780 |
c == '\t')) |
|
781 |
i--; |
|
782 |
||
783 |
// check for the known strings |
|
784 |
int matchlen; |
|
785 |
||
786 |
if (i >= 3 && (a[i-3] == 'r' || a[i-3] == 'R') && |
|
787 |
(a[i-2] == 'e' || a[i-2] == 'E') && |
|
788 |
(a[i-1] == 'a' || a[i-1] == 'A') && |
|
789 |
(a[i] == 'd' || a[i] == 'D')) |
|
790 |
{ |
|
791 |
matchlen = 4; |
|
792 |
mask |= READ; |
|
793 |
||
794 |
} else if (i >= 4 && (a[i-4] == 'w' || a[i-4] == 'W') && |
|
795 |
(a[i-3] == 'r' || a[i-3] == 'R') && |
|
796 |
(a[i-2] == 'i' || a[i-2] == 'I') && |
|
797 |
(a[i-1] == 't' || a[i-1] == 'T') && |
|
798 |
(a[i] == 'e' || a[i] == 'E')) |
|
799 |
{ |
|
800 |
matchlen = 5; |
|
801 |
mask |= WRITE; |
|
802 |
||
803 |
} else if (i >= 6 && (a[i-6] == 'e' || a[i-6] == 'E') && |
|
804 |
(a[i-5] == 'x' || a[i-5] == 'X') && |
|
805 |
(a[i-4] == 'e' || a[i-4] == 'E') && |
|
806 |
(a[i-3] == 'c' || a[i-3] == 'C') && |
|
807 |
(a[i-2] == 'u' || a[i-2] == 'U') && |
|
808 |
(a[i-1] == 't' || a[i-1] == 'T') && |
|
809 |
(a[i] == 'e' || a[i] == 'E')) |
|
810 |
{ |
|
811 |
matchlen = 7; |
|
812 |
mask |= EXECUTE; |
|
813 |
||
814 |
} else if (i >= 5 && (a[i-5] == 'd' || a[i-5] == 'D') && |
|
815 |
(a[i-4] == 'e' || a[i-4] == 'E') && |
|
816 |
(a[i-3] == 'l' || a[i-3] == 'L') && |
|
817 |
(a[i-2] == 'e' || a[i-2] == 'E') && |
|
818 |
(a[i-1] == 't' || a[i-1] == 'T') && |
|
819 |
(a[i] == 'e' || a[i] == 'E')) |
|
820 |
{ |
|
821 |
matchlen = 6; |
|
822 |
mask |= DELETE; |
|
823 |
||
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
824 |
} else if (i >= 7 && (a[i-7] == 'r' || a[i-7] == 'R') && |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
825 |
(a[i-6] == 'e' || a[i-6] == 'E') && |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
826 |
(a[i-5] == 'a' || a[i-5] == 'A') && |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
827 |
(a[i-4] == 'd' || a[i-4] == 'D') && |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
828 |
(a[i-3] == 'l' || a[i-3] == 'L') && |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
829 |
(a[i-2] == 'i' || a[i-2] == 'I') && |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
830 |
(a[i-1] == 'n' || a[i-1] == 'N') && |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
831 |
(a[i] == 'k' || a[i] == 'K')) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
832 |
{ |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
833 |
matchlen = 8; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
834 |
mask |= READLINK; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
835 |
|
2 | 836 |
} else { |
837 |
// parse error |
|
838 |
throw new IllegalArgumentException( |
|
839 |
"invalid permission: " + actions); |
|
840 |
} |
|
841 |
||
842 |
// make sure we didn't just match the tail of a word |
|
843 |
// like "ackbarfaccept". Also, skip to the comma. |
|
844 |
boolean seencomma = false; |
|
845 |
while (i >= matchlen && !seencomma) { |
|
846 |
switch(a[i-matchlen]) { |
|
847 |
case ',': |
|
848 |
seencomma = true; |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
9035
diff
changeset
|
849 |
break; |
2 | 850 |
case ' ': case '\r': case '\n': |
851 |
case '\f': case '\t': |
|
852 |
break; |
|
853 |
default: |
|
854 |
throw new IllegalArgumentException( |
|
855 |
"invalid permission: " + actions); |
|
856 |
} |
|
857 |
i--; |
|
858 |
} |
|
859 |
||
860 |
// point i at the location of the comma minus one (or -1). |
|
861 |
i -= matchlen; |
|
862 |
} |
|
863 |
||
864 |
return mask; |
|
865 |
} |
|
866 |
||
867 |
/** |
|
868 |
* Return the current action mask. Used by the FilePermissionCollection. |
|
869 |
* |
|
870 |
* @return the actions mask. |
|
871 |
*/ |
|
872 |
int getMask() { |
|
873 |
return mask; |
|
874 |
} |
|
875 |
||
876 |
/** |
|
877 |
* Return the canonical string representation of the actions. |
|
878 |
* Always returns present actions in the following order: |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
879 |
* read, write, execute, delete, readlink. |
2 | 880 |
* |
881 |
* @return the canonical string representation of the actions. |
|
882 |
*/ |
|
14014 | 883 |
private static String getActions(int mask) { |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
884 |
StringJoiner sj = new StringJoiner(","); |
2 | 885 |
|
886 |
if ((mask & READ) == READ) { |
|
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
887 |
sj.add("read"); |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
888 |
} |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
889 |
if ((mask & WRITE) == WRITE) { |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
890 |
sj.add("write"); |
2 | 891 |
} |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
892 |
if ((mask & EXECUTE) == EXECUTE) { |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
893 |
sj.add("execute"); |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
894 |
} |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
895 |
if ((mask & DELETE) == DELETE) { |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
896 |
sj.add("delete"); |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
897 |
} |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
898 |
if ((mask & READLINK) == READLINK) { |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
899 |
sj.add("readlink"); |
2 | 900 |
} |
901 |
||
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
902 |
return sj.toString(); |
2 | 903 |
} |
904 |
||
905 |
/** |
|
906 |
* Returns the "canonical string representation" of the actions. |
|
907 |
* That is, this method always returns present actions in the following order: |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
908 |
* read, write, execute, delete, readlink. For example, if this FilePermission |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
909 |
* object allows both write and read actions, a call to <code>getActions</code> |
2 | 910 |
* will return the string "read,write". |
911 |
* |
|
912 |
* @return the canonical string representation of the actions. |
|
913 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
914 |
@Override |
14014 | 915 |
public String getActions() { |
2 | 916 |
if (actions == null) |
917 |
actions = getActions(this.mask); |
|
918 |
||
919 |
return actions; |
|
920 |
} |
|
921 |
||
922 |
/** |
|
923 |
* Returns a new PermissionCollection object for storing FilePermission |
|
924 |
* objects. |
|
925 |
* <p> |
|
926 |
* FilePermission objects must be stored in a manner that allows them |
|
927 |
* to be inserted into the collection in any order, but that also enables the |
|
928 |
* PermissionCollection <code>implies</code> |
|
929 |
* method to be implemented in an efficient (and consistent) manner. |
|
930 |
* |
|
931 |
* <p>For example, if you have two FilePermissions: |
|
932 |
* <OL> |
|
933 |
* <LI> <code>"/tmp/-", "read"</code> |
|
934 |
* <LI> <code>"/tmp/scratch/foo", "write"</code> |
|
935 |
* </OL> |
|
936 |
* |
|
937 |
* <p>and you are calling the <code>implies</code> method with the FilePermission: |
|
938 |
* |
|
939 |
* <pre> |
|
940 |
* "/tmp/scratch/foo", "read,write", |
|
941 |
* </pre> |
|
942 |
* |
|
943 |
* then the <code>implies</code> function must |
|
944 |
* take into account both the "/tmp/-" and "/tmp/scratch/foo" |
|
945 |
* permissions, so the effective permission is "read,write", |
|
946 |
* and <code>implies</code> returns true. The "implies" semantics for |
|
947 |
* FilePermissions are handled properly by the PermissionCollection object |
|
948 |
* returned by this <code>newPermissionCollection</code> method. |
|
949 |
* |
|
950 |
* @return a new PermissionCollection object suitable for storing |
|
951 |
* FilePermissions. |
|
952 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
953 |
@Override |
2 | 954 |
public PermissionCollection newPermissionCollection() { |
955 |
return new FilePermissionCollection(); |
|
956 |
} |
|
957 |
||
958 |
/** |
|
959 |
* WriteObject is called to save the state of the FilePermission |
|
960 |
* to a stream. The actions are serialized, and the superclass |
|
961 |
* takes care of the name. |
|
962 |
*/ |
|
963 |
private void writeObject(ObjectOutputStream s) |
|
964 |
throws IOException |
|
965 |
{ |
|
966 |
// Write out the actions. The superclass takes care of the name |
|
967 |
// call getActions to make sure actions field is initialized |
|
968 |
if (actions == null) |
|
969 |
getActions(); |
|
970 |
s.defaultWriteObject(); |
|
971 |
} |
|
972 |
||
973 |
/** |
|
974 |
* readObject is called to restore the state of the FilePermission from |
|
975 |
* a stream. |
|
976 |
*/ |
|
977 |
private void readObject(ObjectInputStream s) |
|
978 |
throws IOException, ClassNotFoundException |
|
979 |
{ |
|
980 |
// Read in the actions, then restore everything else by calling init. |
|
981 |
s.defaultReadObject(); |
|
982 |
init(getMask(actions)); |
|
983 |
} |
|
984 |
} |
|
985 |
||
986 |
/** |
|
987 |
* A FilePermissionCollection stores a set of FilePermission permissions. |
|
988 |
* FilePermission objects |
|
989 |
* must be stored in a manner that allows them to be inserted in any |
|
990 |
* order, but enable the implies function to evaluate the implies |
|
991 |
* method. |
|
992 |
* For example, if you have two FilePermissions: |
|
993 |
* <OL> |
|
994 |
* <LI> "/tmp/-", "read" |
|
995 |
* <LI> "/tmp/scratch/foo", "write" |
|
996 |
* </OL> |
|
997 |
* And you are calling the implies function with the FilePermission: |
|
998 |
* "/tmp/scratch/foo", "read,write", then the implies function must |
|
999 |
* take into account both the /tmp/- and /tmp/scratch/foo |
|
1000 |
* permissions, so the effective permission is "read,write". |
|
1001 |
* |
|
1002 |
* @see java.security.Permission |
|
1003 |
* @see java.security.Permissions |
|
1004 |
* @see java.security.PermissionCollection |
|
1005 |
* |
|
1006 |
* |
|
1007 |
* @author Marianne Mueller |
|
1008 |
* @author Roland Schemers |
|
1009 |
* |
|
1010 |
* @serial include |
|
1011 |
* |
|
1012 |
*/ |
|
1013 |
||
1014 |
final class FilePermissionCollection extends PermissionCollection |
|
14014 | 1015 |
implements Serializable |
1016 |
{ |
|
2 | 1017 |
// Not serialized; see serialization section at end of class |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1018 |
private transient ConcurrentHashMap<String, Permission> perms; |
2 | 1019 |
|
1020 |
/** |
|
14014 | 1021 |
* Create an empty FilePermissionCollection object. |
2 | 1022 |
*/ |
1023 |
public FilePermissionCollection() { |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1024 |
perms = new ConcurrentHashMap<>(); |
2 | 1025 |
} |
1026 |
||
1027 |
/** |
|
14014 | 1028 |
* Adds a permission to the FilePermissionCollection. The key for the hash is |
2 | 1029 |
* permission.path. |
1030 |
* |
|
1031 |
* @param permission the Permission object to add. |
|
1032 |
* |
|
1033 |
* @exception IllegalArgumentException - if the permission is not a |
|
1034 |
* FilePermission |
|
1035 |
* |
|
1036 |
* @exception SecurityException - if this FilePermissionCollection object |
|
1037 |
* has been marked readonly |
|
1038 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1039 |
@Override |
14014 | 1040 |
public void add(Permission permission) { |
2 | 1041 |
if (! (permission instanceof FilePermission)) |
1042 |
throw new IllegalArgumentException("invalid permission: "+ |
|
1043 |
permission); |
|
1044 |
if (isReadOnly()) |
|
1045 |
throw new SecurityException( |
|
1046 |
"attempt to add a Permission to a readonly PermissionCollection"); |
|
1047 |
||
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1048 |
FilePermission fp = (FilePermission)permission; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1049 |
|
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1050 |
// Add permission to map if it is absent, or replace with new |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1051 |
// permission if applicable. NOTE: cannot use lambda for |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1052 |
// remappingFunction parameter until JDK-8076596 is fixed. |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1053 |
perms.merge(fp.getName(), fp, |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1054 |
new java.util.function.BiFunction<>() { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1055 |
@Override |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1056 |
public Permission apply(Permission existingVal, |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1057 |
Permission newVal) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1058 |
int oldMask = ((FilePermission)existingVal).getMask(); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1059 |
int newMask = ((FilePermission)newVal).getMask(); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1060 |
if (oldMask != newMask) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1061 |
int effective = oldMask | newMask; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1062 |
if (effective == newMask) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1063 |
return newVal; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1064 |
} |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1065 |
if (effective != oldMask) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1066 |
return new FilePermission(fp.getName(), effective); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1067 |
} |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1068 |
} |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1069 |
return existingVal; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1070 |
} |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1071 |
} |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1072 |
); |
2 | 1073 |
} |
1074 |
||
1075 |
/** |
|
1076 |
* Check and see if this set of permissions implies the permissions |
|
1077 |
* expressed in "permission". |
|
1078 |
* |
|
14014 | 1079 |
* @param permission the Permission object to compare |
2 | 1080 |
* |
1081 |
* @return true if "permission" is a proper subset of a permission in |
|
1082 |
* the set, false if not. |
|
1083 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1084 |
@Override |
14014 | 1085 |
public boolean implies(Permission permission) { |
2 | 1086 |
if (! (permission instanceof FilePermission)) |
14014 | 1087 |
return false; |
2 | 1088 |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1089 |
FilePermission fperm = (FilePermission) permission; |
2 | 1090 |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1091 |
int desired = fperm.getMask(); |
2 | 1092 |
int effective = 0; |
1093 |
int needed = desired; |
|
1094 |
||
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1095 |
for (Permission perm : perms.values()) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1096 |
FilePermission fp = (FilePermission)perm; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1097 |
if (((needed & fp.getMask()) != 0) && fp.impliesIgnoreMask(fperm)) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1098 |
effective |= fp.getMask(); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1099 |
if ((effective & desired) == desired) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1100 |
return true; |
2 | 1101 |
} |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1102 |
needed = (desired ^ effective); |
2 | 1103 |
} |
1104 |
} |
|
1105 |
return false; |
|
1106 |
} |
|
1107 |
||
1108 |
/** |
|
1109 |
* Returns an enumeration of all the FilePermission objects in the |
|
1110 |
* container. |
|
1111 |
* |
|
1112 |
* @return an enumeration of all the FilePermission objects. |
|
1113 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1114 |
@Override |
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
9035
diff
changeset
|
1115 |
public Enumeration<Permission> elements() { |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1116 |
return perms.elements(); |
2 | 1117 |
} |
1118 |
||
1119 |
private static final long serialVersionUID = 2202956749081564585L; |
|
1120 |
||
1121 |
// Need to maintain serialization interoperability with earlier releases, |
|
1122 |
// which had the serializable field: |
|
1123 |
// private Vector permissions; |
|
1124 |
||
1125 |
/** |
|
1126 |
* @serialField permissions java.util.Vector |
|
1127 |
* A list of FilePermission objects. |
|
1128 |
*/ |
|
1129 |
private static final ObjectStreamField[] serialPersistentFields = { |
|
1130 |
new ObjectStreamField("permissions", Vector.class), |
|
1131 |
}; |
|
1132 |
||
1133 |
/** |
|
1134 |
* @serialData "permissions" field (a Vector containing the FilePermissions). |
|
1135 |
*/ |
|
1136 |
/* |
|
1137 |
* Writes the contents of the perms field out as a Vector for |
|
1138 |
* serialization compatibility with earlier releases. |
|
1139 |
*/ |
|
1140 |
private void writeObject(ObjectOutputStream out) throws IOException { |
|
1141 |
// Don't call out.defaultWriteObject() |
|
1142 |
||
1143 |
// Write out Vector |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1144 |
Vector<Permission> permissions = new Vector<>(perms.values()); |
2 | 1145 |
|
1146 |
ObjectOutputStream.PutField pfields = out.putFields(); |
|
1147 |
pfields.put("permissions", permissions); |
|
1148 |
out.writeFields(); |
|
1149 |
} |
|
1150 |
||
1151 |
/* |
|
1152 |
* Reads in a Vector of FilePermissions and saves them in the perms field. |
|
1153 |
*/ |
|
14014 | 1154 |
private void readObject(ObjectInputStream in) |
1155 |
throws IOException, ClassNotFoundException |
|
1156 |
{ |
|
2 | 1157 |
// Don't call defaultReadObject() |
1158 |
||
1159 |
// Read in serialized fields |
|
1160 |
ObjectInputStream.GetField gfields = in.readFields(); |
|
1161 |
||
1162 |
// Get the one we want |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
9035
diff
changeset
|
1163 |
@SuppressWarnings("unchecked") |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
1164 |
Vector<Permission> permissions = (Vector<Permission>)gfields.get("permissions", null); |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1165 |
perms = new ConcurrentHashMap<>(permissions.size()); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1166 |
for (Permission perm : permissions) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1167 |
perms.put(perm.getName(), perm); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1168 |
} |
2 | 1169 |
} |
1170 |
} |