author | weijun |
Thu, 05 Jan 2017 22:58:54 +0800 | |
changeset 42999 | 110c2df82517 |
parent 42684 | 569aca644163 |
child 44638 | 525862468d5a |
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 |
private static final Path here = builtInFS.getPath( |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
210 |
GetPropertyAction.privilegedGetProperty("user.dir")); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
211 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
212 |
/** |
42314
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
213 |
* A private constructor that clones some and updates some, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
214 |
* always with a different name. |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
215 |
* @param input |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
216 |
*/ |
42314
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
217 |
private FilePermission(String name, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
218 |
FilePermission input, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
219 |
Path npath, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
220 |
Path npath2, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
221 |
int mask, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
222 |
String actions) { |
41822
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
223 |
super(name); |
42314
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
224 |
// Customizables |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
225 |
this.npath = npath; |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
226 |
this.npath2 = npath2; |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
227 |
this.actions = actions; |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
228 |
this.mask = mask; |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
229 |
// Cloneds |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
230 |
this.allFiles = input.allFiles; |
41823 | 231 |
this.invalid = input.invalid; |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
232 |
this.recursive = input.recursive; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
233 |
this.directory = input.directory; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
234 |
this.cpath = input.cpath; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
235 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
236 |
|
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 |
* Returns the alternative path as a Path object, i.e. absolute path |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
239 |
* for a relative one, or vice versa. |
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 |
* @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
|
242 |
* @return the alternative path, or null if cannot find one. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
243 |
*/ |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
244 |
private static Path altPath(Path in) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
245 |
try { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
246 |
if (!in.isAbsolute()) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
247 |
return here.resolve(in).normalize(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
248 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
249 |
return here.relativize(in).normalize(); |
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 |
} catch (IllegalArgumentException e) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
252 |
return null; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
253 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
254 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
255 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
256 |
static { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
257 |
SharedSecrets.setJavaIOFilePermissionAccess( |
42684 | 258 |
/** |
259 |
* Creates FilePermission objects with special internals. |
|
260 |
* See {@link FilePermCompat#newPermPlusAltPath(Permission)} and |
|
261 |
* {@link FilePermCompat#newPermUsingAltPath(Permission)}. |
|
262 |
*/ |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
263 |
new JavaIOFilePermissionAccess() { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
264 |
public FilePermission newPermPlusAltPath(FilePermission input) { |
42684 | 265 |
if (!input.invalid && input.npath2 == null && !input.allFiles) { |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
266 |
Path npath2 = altPath(input.npath); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
267 |
if (npath2 != null) { |
41822
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
268 |
// Please note the name of the new permission is |
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
269 |
// different than the original so that when one is |
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
270 |
// added to a FilePermissionCollection it will not |
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
271 |
// be merged with the original one. |
42314
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
272 |
return new FilePermission(input.getName() + "#plus", |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
273 |
input, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
274 |
input.npath, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
275 |
npath2, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
276 |
input.mask, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
277 |
input.actions); |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
278 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
279 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
280 |
return input; |
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 |
public FilePermission newPermUsingAltPath(FilePermission input) { |
42684 | 283 |
if (!input.invalid && !input.allFiles) { |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
284 |
Path npath2 = altPath(input.npath); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
285 |
if (npath2 != null) { |
41822
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
286 |
// New name, see above. |
42314
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
287 |
return new FilePermission(input.getName() + "#using", |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
288 |
input, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
289 |
npath2, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
290 |
null, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
291 |
input.mask, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
292 |
input.actions); |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
293 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
294 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
295 |
return null; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
296 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
297 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
298 |
); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
299 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
300 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
301 |
/** |
2 | 302 |
* initialize a FilePermission object. Common to all constructors. |
303 |
* Also called during de-serialization. |
|
304 |
* |
|
305 |
* @param mask the actions mask to use. |
|
306 |
* |
|
307 |
*/ |
|
14014 | 308 |
private void init(int mask) { |
2 | 309 |
if ((mask & ALL) != mask) |
310 |
throw new IllegalArgumentException("invalid actions mask"); |
|
311 |
||
312 |
if (mask == NONE) |
|
313 |
throw new IllegalArgumentException("invalid actions mask"); |
|
314 |
||
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
315 |
if (FilePermCompat.nb) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
316 |
String name = getName(); |
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 |
if (name == null) |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
319 |
throw new NullPointerException("name can't be null"); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
320 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
321 |
this.mask = mask; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
322 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
323 |
if (name.equals("<<ALL FILES>>")) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
324 |
allFiles = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
325 |
npath = builtInFS.getPath(""); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
326 |
// other fields remain default |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
327 |
return; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
328 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
329 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
330 |
boolean rememberStar = false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
331 |
if (name.endsWith("*")) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
332 |
rememberStar = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
333 |
recursive = false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
334 |
name = name.substring(0, name.length()-1) + "-"; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
335 |
} |
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 |
try { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
338 |
// new File() can "normalize" some name, for example, "/C:/X" on |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
339 |
// Windows. Some JDK codes generate such illegal names. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
340 |
npath = builtInFS.getPath(new File(name).getPath()) |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
341 |
.normalize(); |
42684 | 342 |
// lastName should always be non-null now |
343 |
Path lastName = npath.getFileName(); |
|
344 |
if (lastName != null && lastName.toString().equals("-")) { |
|
345 |
directory = true; |
|
346 |
recursive = !rememberStar; |
|
347 |
npath = npath.getParent(); |
|
348 |
} |
|
349 |
if (npath == null) { |
|
350 |
npath = builtInFS.getPath(""); |
|
351 |
} |
|
41823 | 352 |
invalid = false; |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
353 |
} catch (InvalidPathException ipe) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
354 |
// Still invalid. For compatibility reason, accept it |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
355 |
// but make this permission useless. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
356 |
npath = builtInFS.getPath("-u-s-e-l-e-s-s-"); |
41823 | 357 |
invalid = true; |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
358 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
359 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
360 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
361 |
if ((cpath = getName()) == null) |
2 | 362 |
throw new NullPointerException("name can't be null"); |
363 |
||
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
364 |
this.mask = mask; |
2 | 365 |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
366 |
if (cpath.equals("<<ALL FILES>>")) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
367 |
directory = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
368 |
recursive = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
369 |
cpath = ""; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
370 |
return; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
371 |
} |
2 | 372 |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
373 |
// store only the canonical cpath if possible |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
374 |
cpath = AccessController.doPrivileged(new PrivilegedAction<>() { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
375 |
public String run() { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
376 |
try { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
377 |
String path = cpath; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
378 |
if (cpath.endsWith("*")) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
379 |
// call getCanonicalPath with a path with wildcard character |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
380 |
// replaced to avoid calling it with paths that are |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
381 |
// intended to match all entries in a directory |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
382 |
path = path.substring(0, path.length() - 1) + "-"; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
383 |
path = new File(path).getCanonicalPath(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
384 |
return path.substring(0, path.length() - 1) + "*"; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
385 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
386 |
return new File(path).getCanonicalPath(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
387 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
388 |
} catch (IOException ioe) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
389 |
return cpath; |
4053
c2f8e57ba2f8
6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents:
2057
diff
changeset
|
390 |
} |
2 | 391 |
} |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
392 |
}); |
2 | 393 |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
394 |
int len = cpath.length(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
395 |
char last = ((len > 0) ? cpath.charAt(len - 1) : 0); |
2 | 396 |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
397 |
if (last == RECURSIVE_CHAR && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
398 |
cpath.charAt(len - 2) == File.separatorChar) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
399 |
directory = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
400 |
recursive = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
401 |
cpath = cpath.substring(0, --len); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
402 |
} else if (last == WILD_CHAR && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
403 |
cpath.charAt(len - 2) == File.separatorChar) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
404 |
directory = true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
405 |
//recursive = false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
406 |
cpath = cpath.substring(0, --len); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
407 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
408 |
// overkill since they are initialized to false, but |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
409 |
// commented out here to remind us... |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
410 |
//directory = false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
411 |
//recursive = false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
412 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
413 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
414 |
// XXX: at this point the path should be absolute. die if it isn't? |
2 | 415 |
} |
416 |
} |
|
417 |
||
418 |
/** |
|
419 |
* Creates a new FilePermission object with the specified actions. |
|
420 |
* <i>path</i> is the pathname of a file or directory, and <i>actions</i> |
|
421 |
* contains a comma-separated list of the desired actions granted on the |
|
422 |
* 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
|
423 |
* "read", "write", "execute", "delete", and "readlink". |
2 | 424 |
* |
425 |
* <p>A pathname that ends in "/*" (where "/" is |
|
426 |
* the file separator character, <code>File.separatorChar</code>) |
|
427 |
* indicates all the files and directories contained in that directory. |
|
428 |
* A pathname that ends with "/-" indicates (recursively) all files and |
|
429 |
* subdirectories contained in that directory. The special pathname |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
430 |
* {@literal "<<ALL FILES>>"} matches any file. |
2 | 431 |
* |
432 |
* <p>A pathname consisting of a single "*" indicates all the files |
|
433 |
* in the current directory, while a pathname consisting of a single "-" |
|
434 |
* indicates all the files in the current directory and |
|
435 |
* (recursively) all files and subdirectories contained in the current |
|
436 |
* directory. |
|
437 |
* |
|
438 |
* <p>A pathname containing an empty string represents an empty path. |
|
439 |
* |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
440 |
* @implNote In this implementation, the |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
441 |
* {@code jdk.io.permissionsUseCanonicalPath} system property dictates how |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
442 |
* the {@code path} argument is processed and stored. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
443 |
* <P> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
444 |
* 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
|
445 |
* is canonicalized and stored as a String object named {@code cpath}. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
446 |
* 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
|
447 |
* 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
|
448 |
* resolved to its target, etc. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
449 |
* <P> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
450 |
* 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
|
451 |
* 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
|
452 |
* after {@link Path#normalize() normalization}. No canonicalization is |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
453 |
* performed which means the underlying file system is not accessed. |
42684 | 454 |
* If an {@link InvalidPathException} is thrown during the conversion, |
455 |
* this {@code FilePermission} will be labeled as invalid. |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
456 |
* <P> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
457 |
* In either case, the "*" or "-" character at the end of a wildcard |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
458 |
* {@code path} is removed before canonicalization or normalization. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
459 |
* It is stored in a separate wildcard flag field. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
460 |
* <P> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
461 |
* The default value of the {@code jdk.io.permissionsUseCanonicalPath} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
462 |
* system property is {@code false} in this implementation. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
463 |
* |
2 | 464 |
* @param path the pathname of the file/directory. |
465 |
* @param actions the action string. |
|
466 |
* |
|
467 |
* @throws IllegalArgumentException |
|
468 |
* If actions is <code>null</code>, empty or contains an action |
|
469 |
* other than the specified possible actions. |
|
470 |
*/ |
|
14014 | 471 |
public FilePermission(String path, String actions) { |
2 | 472 |
super(path); |
473 |
init(getMask(actions)); |
|
474 |
} |
|
475 |
||
476 |
/** |
|
477 |
* Creates a new FilePermission object using an action mask. |
|
478 |
* More efficient than the FilePermission(String, String) constructor. |
|
479 |
* Can be used from within |
|
480 |
* code that needs to create a FilePermission object to pass into the |
|
481 |
* <code>implies</code> method. |
|
482 |
* |
|
483 |
* @param path the pathname of the file/directory. |
|
484 |
* @param mask the action mask to use. |
|
485 |
*/ |
|
486 |
// package private for use by the FilePermissionCollection add method |
|
14014 | 487 |
FilePermission(String path, int mask) { |
2 | 488 |
super(path); |
489 |
init(mask); |
|
490 |
} |
|
491 |
||
492 |
/** |
|
493 |
* Checks if this FilePermission object "implies" the specified permission. |
|
494 |
* <P> |
|
21334 | 495 |
* More specifically, this method returns true if: |
2 | 496 |
* <ul> |
21334 | 497 |
* <li> <i>p</i> is an instanceof FilePermission, |
2 | 498 |
* <li> <i>p</i>'s actions are a proper subset of this |
21334 | 499 |
* object's actions, and |
2 | 500 |
* <li> <i>p</i>'s pathname is implied by this object's |
501 |
* pathname. For example, "/tmp/*" implies "/tmp/foo", since |
|
502 |
* "/tmp/*" encompasses all files in the "/tmp" directory, |
|
503 |
* including the one named "foo". |
|
504 |
* </ul> |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
505 |
* <P> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
506 |
* Precisely, a simple pathname implies another simple pathname |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
507 |
* if and only if they are equal. A simple pathname never implies |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
508 |
* a wildcard pathname. A wildcard pathname implies another wildcard |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
509 |
* pathname if and only if all simple pathnames implied by the latter |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
510 |
* are implied by the former. A wildcard pathname implies a simple |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
511 |
* pathname if and only if |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
512 |
* <ul> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
513 |
* <li>if the wildcard flag is "*", the simple pathname's path |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
514 |
* must be right inside the wildcard pathname's path. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
515 |
* <li>if the wildcard flag is "-", the simple pathname's path |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
516 |
* must be recursively inside the wildcard pathname's path. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
517 |
* </ul> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
518 |
* <P> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
519 |
* {@literal "<<ALL FILES>>"} implies every other pathname. No pathname, |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
520 |
* except for {@literal "<<ALL FILES>>"} itself, implies |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
521 |
* {@literal "<<ALL FILES>>"}. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
522 |
* |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
523 |
* @implNote |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
524 |
* If {@code jdk.io.permissionsUseCanonicalPath} is {@code true}, a |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
525 |
* 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
|
526 |
* 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
|
527 |
* sequence) from the former the remaining part equals to the latter, |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
528 |
* a simple {@code cpath} is recursively inside a wildcard {@code cpath} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
529 |
* if and only if the former starts with the latter. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
530 |
* <p> |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
531 |
* If {@code jdk.io.permissionsUseCanonicalPath} is {@code false}, a |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
532 |
* 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
|
533 |
* {@code simple_npath.relativize(wildcard_npath)} is exactly "..", |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
534 |
* a simple {@code npath} is recursively inside a wildcard {@code npath} |
42999
110c2df82517
8170900: Issue with FilePermission::implies for wildcard flag(-)
weijun
parents:
42684
diff
changeset
|
535 |
* if and only if {@code simple_npath.relativize(wildcard_npath)} is a |
110c2df82517
8170900: Issue with FilePermission::implies for wildcard flag(-)
weijun
parents:
42684
diff
changeset
|
536 |
* series of one or more "..". This means "/-" implies "/foo" but not "foo". |
110c2df82517
8170900: Issue with FilePermission::implies for wildcard flag(-)
weijun
parents:
42684
diff
changeset
|
537 |
* <p> |
110c2df82517
8170900: Issue with FilePermission::implies for wildcard flag(-)
weijun
parents:
42684
diff
changeset
|
538 |
* An invalid {@code FilePermission} does not imply any object except for |
110c2df82517
8170900: Issue with FilePermission::implies for wildcard flag(-)
weijun
parents:
42684
diff
changeset
|
539 |
* itself. An invalid {@code FilePermission} is not implied by any object |
110c2df82517
8170900: Issue with FilePermission::implies for wildcard flag(-)
weijun
parents:
42684
diff
changeset
|
540 |
* except for itself or a {@code FilePermission} on |
110c2df82517
8170900: Issue with FilePermission::implies for wildcard flag(-)
weijun
parents:
42684
diff
changeset
|
541 |
* {@literal "<<ALL FILES>>"} whose actions is a superset of this |
42684 | 542 |
* invalid {@code FilePermission}. Even if two {@code FilePermission} |
543 |
* are created with the same invalid path, one does not imply the other. |
|
2 | 544 |
* |
545 |
* @param p the permission to check against. |
|
546 |
* |
|
547 |
* @return <code>true</code> if the specified permission is not |
|
548 |
* <code>null</code> and is implied by this object, |
|
549 |
* <code>false</code> otherwise. |
|
550 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
551 |
@Override |
2 | 552 |
public boolean implies(Permission p) { |
553 |
if (!(p instanceof FilePermission)) |
|
554 |
return false; |
|
555 |
||
556 |
FilePermission that = (FilePermission) p; |
|
557 |
||
558 |
// we get the effective mask. i.e., the "and" of this and that. |
|
559 |
// They must be equal to that.mask for implies to return true. |
|
560 |
||
561 |
return ((this.mask & that.mask) == that.mask) && impliesIgnoreMask(that); |
|
562 |
} |
|
563 |
||
564 |
/** |
|
565 |
* Checks if the Permission's actions are a proper subset of the |
|
566 |
* this object's actions. Returns the effective mask iff the |
|
567 |
* this FilePermission's path also implies that FilePermission's path. |
|
568 |
* |
|
569 |
* @param that the FilePermission to check against. |
|
570 |
* @return the effective mask |
|
571 |
*/ |
|
572 |
boolean impliesIgnoreMask(FilePermission that) { |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
573 |
if (FilePermCompat.nb) { |
41823 | 574 |
if (this == that) { |
575 |
return true; |
|
576 |
} |
|
42684 | 577 |
if (allFiles) { |
578 |
return true; |
|
579 |
} |
|
41823 | 580 |
if (this.invalid || that.invalid) { |
581 |
return false; |
|
582 |
} |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
583 |
if (that.allFiles) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
584 |
return false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
585 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
586 |
// Left at least same level of wildness as right |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
587 |
if ((this.recursive && that.recursive) != that.recursive |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
588 |
|| (this.directory && that.directory) != that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
589 |
return false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
590 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
591 |
// Same npath is good as long as both or neither are directories |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
592 |
if (this.npath.equals(that.npath) |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
593 |
&& this.directory == that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
594 |
return true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
595 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
596 |
int diff = containsPath(this.npath, that.npath); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
597 |
// Right inside left is good if recursive |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
598 |
if (diff >= 1 && recursive) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
599 |
return true; |
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 |
// Right right inside left if it is element in set |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
602 |
if (diff == 1 && directory && !that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
603 |
return true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
604 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
605 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
606 |
// Hack: if a npath2 field exists, apply the same checks |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
607 |
// on it as a fallback. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
608 |
if (this.npath2 != null) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
609 |
if (this.npath2.equals(that.npath) |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
610 |
&& this.directory == that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
611 |
return true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
612 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
613 |
diff = containsPath(this.npath2, that.npath); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
614 |
if (diff >= 1 && recursive) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
615 |
return true; |
2 | 616 |
} |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
617 |
if (diff == 1 && directory && !that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
618 |
return true; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
619 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
620 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
621 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
622 |
return false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
623 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
624 |
if (this.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
625 |
if (this.recursive) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
626 |
// make sure that.path is longer then path so |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
627 |
// something like /foo/- does not imply /foo |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
628 |
if (that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
629 |
return (that.cpath.length() >= this.cpath.length()) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
630 |
that.cpath.startsWith(this.cpath); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
631 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
632 |
return ((that.cpath.length() > this.cpath.length()) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
633 |
that.cpath.startsWith(this.cpath)); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
634 |
} |
2 | 635 |
} else { |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
636 |
if (that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
637 |
// if the permission passed in is a directory |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
638 |
// specification, make sure that a non-recursive |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
639 |
// permission (i.e., this object) can't imply a recursive |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
640 |
// permission. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
641 |
if (that.recursive) |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
642 |
return false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
643 |
else |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
644 |
return (this.cpath.equals(that.cpath)); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
645 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
646 |
int last = that.cpath.lastIndexOf(File.separatorChar); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
647 |
if (last == -1) |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
648 |
return false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
649 |
else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
650 |
// this.cpath.equals(that.cpath.substring(0, last+1)); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
651 |
// Use regionMatches to avoid creating new string |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
652 |
return (this.cpath.length() == (last + 1)) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
653 |
this.cpath.regionMatches(0, that.cpath, 0, last + 1); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
654 |
} |
2 | 655 |
} |
656 |
} |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
657 |
} else if (that.directory) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
658 |
// if this is NOT recursive/wildcarded, |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
659 |
// do not let it imply a recursive/wildcarded permission |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
660 |
return false; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
661 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
662 |
return (this.cpath.equals(that.cpath)); |
2 | 663 |
} |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
664 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
665 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
666 |
|
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
667 |
/** |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
668 |
* 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
|
669 |
* is returned if |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
670 |
* |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
671 |
* - p1 does not contains p2. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
672 |
* - this is not decidable. For example, p1="../x", p2="y". |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
673 |
* - the depth is not decidable. For example, p1="/", p2="x". |
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 |
* This method can return 2 if the depth is greater than 2. |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
676 |
* |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
677 |
* @param p1 the expected outer path, normalized |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
678 |
* @param p2 the expected inner path, normalized |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
679 |
* @return the depth in between |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
680 |
*/ |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
681 |
private static int containsPath(Path p1, Path p2) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
682 |
Path p; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
683 |
try { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
684 |
p = p2.relativize(p1).normalize(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
685 |
if (p.getName(0).toString().isEmpty()) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
686 |
return 0; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
687 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
688 |
for (Path item: p) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
689 |
String s = item.toString(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
690 |
if (!s.equals("..")) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
691 |
return -1; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
692 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
693 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
694 |
return p.getNameCount(); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
695 |
} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
696 |
} catch (IllegalArgumentException iae) { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
697 |
return -1; |
2 | 698 |
} |
699 |
} |
|
700 |
||
701 |
/** |
|
702 |
* Checks two FilePermission objects for equality. Checks that <i>obj</i> is |
|
703 |
* 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
|
704 |
* |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
705 |
* @implNote More specifically, two pathnames are the same if and only if |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
706 |
* they have the same wildcard flag and their {@code cpath} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
707 |
* (if {@code jdk.io.permissionsUseCanonicalPath} is {@code true}) or |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
708 |
* {@code npath} (if {@code jdk.io.permissionsUseCanonicalPath} |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
709 |
* is {@code false}) are equal. Or they are both {@literal "<<ALL FILES>>"}. |
42684 | 710 |
* <p> |
711 |
* When {@code jdk.io.permissionsUseCanonicalPath} is {@code false}, an |
|
712 |
* invalid {@code FilePermission} does not equal to any object except |
|
713 |
* for itself, even if they are created using the same invalid path. |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
714 |
* |
2 | 715 |
* @param obj the object we are testing for equality with this object. |
716 |
* @return <code>true</code> if obj is a FilePermission, and has the same |
|
717 |
* pathname and actions as this FilePermission object, |
|
718 |
* <code>false</code> otherwise. |
|
719 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
720 |
@Override |
2 | 721 |
public boolean equals(Object obj) { |
722 |
if (obj == this) |
|
723 |
return true; |
|
724 |
||
725 |
if (! (obj instanceof FilePermission)) |
|
726 |
return false; |
|
727 |
||
728 |
FilePermission that = (FilePermission) obj; |
|
729 |
||
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
730 |
if (FilePermCompat.nb) { |
41823 | 731 |
if (this.invalid || that.invalid) { |
732 |
return false; |
|
733 |
} |
|
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
734 |
return (this.mask == that.mask) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
735 |
(this.allFiles == that.allFiles) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
736 |
this.npath.equals(that.npath) && |
41822
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
737 |
Objects.equals(npath2, that.npath2) && |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
738 |
(this.directory == that.directory) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
739 |
(this.recursive == that.recursive); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
740 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
741 |
return (this.mask == that.mask) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
742 |
this.cpath.equals(that.cpath) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
743 |
(this.directory == that.directory) && |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
744 |
(this.recursive == that.recursive); |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
745 |
} |
2 | 746 |
} |
747 |
||
748 |
/** |
|
749 |
* Returns the hash code value for this object. |
|
750 |
* |
|
751 |
* @return a hash code value for this object. |
|
752 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
753 |
@Override |
2 | 754 |
public int hashCode() { |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
755 |
if (FilePermCompat.nb) { |
41822
9398d77cc4bb
8168127: FilePermissionCollection merges incorrectly
weijun
parents:
41377
diff
changeset
|
756 |
return Objects.hash( |
41823 | 757 |
mask, allFiles, directory, recursive, npath, npath2, invalid); |
41377
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
758 |
} else { |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
759 |
return 0; |
271ee055cb31
8164705: Remove pathname canonicalization from FilePermission
weijun
parents:
32649
diff
changeset
|
760 |
} |
2 | 761 |
} |
762 |
||
763 |
/** |
|
764 |
* Converts an actions String to an actions mask. |
|
765 |
* |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
9035
diff
changeset
|
766 |
* @param actions the action string. |
2 | 767 |
* @return the actions mask. |
768 |
*/ |
|
769 |
private static int getMask(String actions) { |
|
770 |
int mask = NONE; |
|
771 |
||
772 |
// Null action valid? |
|
773 |
if (actions == null) { |
|
774 |
return mask; |
|
775 |
} |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
9035
diff
changeset
|
776 |
|
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
9035
diff
changeset
|
777 |
// 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
|
778 |
// performance benefit (these values are used heavily within the JDK). |
2 | 779 |
if (actions == SecurityConstants.FILE_READ_ACTION) { |
780 |
return READ; |
|
781 |
} else if (actions == SecurityConstants.FILE_WRITE_ACTION) { |
|
782 |
return WRITE; |
|
783 |
} else if (actions == SecurityConstants.FILE_EXECUTE_ACTION) { |
|
784 |
return EXECUTE; |
|
785 |
} else if (actions == SecurityConstants.FILE_DELETE_ACTION) { |
|
786 |
return DELETE; |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
787 |
} 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
|
788 |
return READLINK; |
2 | 789 |
} |
790 |
||
791 |
char[] a = actions.toCharArray(); |
|
792 |
||
793 |
int i = a.length - 1; |
|
794 |
if (i < 0) |
|
795 |
return mask; |
|
796 |
||
797 |
while (i != -1) { |
|
798 |
char c; |
|
799 |
||
800 |
// skip whitespace |
|
801 |
while ((i!=-1) && ((c = a[i]) == ' ' || |
|
802 |
c == '\r' || |
|
803 |
c == '\n' || |
|
804 |
c == '\f' || |
|
805 |
c == '\t')) |
|
806 |
i--; |
|
807 |
||
808 |
// check for the known strings |
|
809 |
int matchlen; |
|
810 |
||
811 |
if (i >= 3 && (a[i-3] == 'r' || a[i-3] == 'R') && |
|
812 |
(a[i-2] == 'e' || a[i-2] == 'E') && |
|
813 |
(a[i-1] == 'a' || a[i-1] == 'A') && |
|
814 |
(a[i] == 'd' || a[i] == 'D')) |
|
815 |
{ |
|
816 |
matchlen = 4; |
|
817 |
mask |= READ; |
|
818 |
||
819 |
} else if (i >= 4 && (a[i-4] == 'w' || a[i-4] == 'W') && |
|
820 |
(a[i-3] == 'r' || a[i-3] == 'R') && |
|
821 |
(a[i-2] == 'i' || a[i-2] == 'I') && |
|
822 |
(a[i-1] == 't' || a[i-1] == 'T') && |
|
823 |
(a[i] == 'e' || a[i] == 'E')) |
|
824 |
{ |
|
825 |
matchlen = 5; |
|
826 |
mask |= WRITE; |
|
827 |
||
828 |
} else if (i >= 6 && (a[i-6] == 'e' || a[i-6] == 'E') && |
|
829 |
(a[i-5] == 'x' || a[i-5] == 'X') && |
|
830 |
(a[i-4] == 'e' || a[i-4] == 'E') && |
|
831 |
(a[i-3] == 'c' || a[i-3] == 'C') && |
|
832 |
(a[i-2] == 'u' || a[i-2] == 'U') && |
|
833 |
(a[i-1] == 't' || a[i-1] == 'T') && |
|
834 |
(a[i] == 'e' || a[i] == 'E')) |
|
835 |
{ |
|
836 |
matchlen = 7; |
|
837 |
mask |= EXECUTE; |
|
838 |
||
839 |
} else if (i >= 5 && (a[i-5] == 'd' || a[i-5] == 'D') && |
|
840 |
(a[i-4] == 'e' || a[i-4] == 'E') && |
|
841 |
(a[i-3] == 'l' || a[i-3] == 'L') && |
|
842 |
(a[i-2] == 'e' || a[i-2] == 'E') && |
|
843 |
(a[i-1] == 't' || a[i-1] == 'T') && |
|
844 |
(a[i] == 'e' || a[i] == 'E')) |
|
845 |
{ |
|
846 |
matchlen = 6; |
|
847 |
mask |= DELETE; |
|
848 |
||
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
849 |
} 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
|
850 |
(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
|
851 |
(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
|
852 |
(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
|
853 |
(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
|
854 |
(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
|
855 |
(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
|
856 |
(a[i] == 'k' || a[i] == 'K')) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
857 |
{ |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
858 |
matchlen = 8; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
859 |
mask |= READLINK; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
860 |
|
2 | 861 |
} else { |
862 |
// parse error |
|
863 |
throw new IllegalArgumentException( |
|
864 |
"invalid permission: " + actions); |
|
865 |
} |
|
866 |
||
867 |
// make sure we didn't just match the tail of a word |
|
868 |
// like "ackbarfaccept". Also, skip to the comma. |
|
869 |
boolean seencomma = false; |
|
870 |
while (i >= matchlen && !seencomma) { |
|
871 |
switch(a[i-matchlen]) { |
|
872 |
case ',': |
|
873 |
seencomma = true; |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
9035
diff
changeset
|
874 |
break; |
2 | 875 |
case ' ': case '\r': case '\n': |
876 |
case '\f': case '\t': |
|
877 |
break; |
|
878 |
default: |
|
879 |
throw new IllegalArgumentException( |
|
880 |
"invalid permission: " + actions); |
|
881 |
} |
|
882 |
i--; |
|
883 |
} |
|
884 |
||
885 |
// point i at the location of the comma minus one (or -1). |
|
886 |
i -= matchlen; |
|
887 |
} |
|
888 |
||
889 |
return mask; |
|
890 |
} |
|
891 |
||
892 |
/** |
|
893 |
* Return the current action mask. Used by the FilePermissionCollection. |
|
894 |
* |
|
895 |
* @return the actions mask. |
|
896 |
*/ |
|
897 |
int getMask() { |
|
898 |
return mask; |
|
899 |
} |
|
900 |
||
901 |
/** |
|
902 |
* Return the canonical string representation of the actions. |
|
903 |
* 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
|
904 |
* read, write, execute, delete, readlink. |
2 | 905 |
* |
906 |
* @return the canonical string representation of the actions. |
|
907 |
*/ |
|
14014 | 908 |
private static String getActions(int mask) { |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
909 |
StringJoiner sj = new StringJoiner(","); |
2 | 910 |
|
911 |
if ((mask & READ) == READ) { |
|
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
912 |
sj.add("read"); |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
913 |
} |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
914 |
if ((mask & WRITE) == WRITE) { |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
915 |
sj.add("write"); |
2 | 916 |
} |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
917 |
if ((mask & EXECUTE) == EXECUTE) { |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
918 |
sj.add("execute"); |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
919 |
} |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
920 |
if ((mask & DELETE) == DELETE) { |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
921 |
sj.add("delete"); |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
922 |
} |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
923 |
if ((mask & READLINK) == READLINK) { |
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
924 |
sj.add("readlink"); |
2 | 925 |
} |
926 |
||
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
927 |
return sj.toString(); |
2 | 928 |
} |
929 |
||
930 |
/** |
|
931 |
* Returns the "canonical string representation" of the actions. |
|
932 |
* 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
|
933 |
* 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
|
934 |
* object allows both write and read actions, a call to <code>getActions</code> |
2 | 935 |
* will return the string "read,write". |
936 |
* |
|
937 |
* @return the canonical string representation of the actions. |
|
938 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
939 |
@Override |
14014 | 940 |
public String getActions() { |
2 | 941 |
if (actions == null) |
942 |
actions = getActions(this.mask); |
|
943 |
||
944 |
return actions; |
|
945 |
} |
|
946 |
||
947 |
/** |
|
948 |
* Returns a new PermissionCollection object for storing FilePermission |
|
949 |
* objects. |
|
950 |
* <p> |
|
951 |
* FilePermission objects must be stored in a manner that allows them |
|
952 |
* to be inserted into the collection in any order, but that also enables the |
|
953 |
* PermissionCollection <code>implies</code> |
|
954 |
* method to be implemented in an efficient (and consistent) manner. |
|
955 |
* |
|
956 |
* <p>For example, if you have two FilePermissions: |
|
957 |
* <OL> |
|
958 |
* <LI> <code>"/tmp/-", "read"</code> |
|
959 |
* <LI> <code>"/tmp/scratch/foo", "write"</code> |
|
960 |
* </OL> |
|
961 |
* |
|
962 |
* <p>and you are calling the <code>implies</code> method with the FilePermission: |
|
963 |
* |
|
964 |
* <pre> |
|
965 |
* "/tmp/scratch/foo", "read,write", |
|
966 |
* </pre> |
|
967 |
* |
|
968 |
* then the <code>implies</code> function must |
|
969 |
* take into account both the "/tmp/-" and "/tmp/scratch/foo" |
|
970 |
* permissions, so the effective permission is "read,write", |
|
971 |
* and <code>implies</code> returns true. The "implies" semantics for |
|
972 |
* FilePermissions are handled properly by the PermissionCollection object |
|
973 |
* returned by this <code>newPermissionCollection</code> method. |
|
974 |
* |
|
975 |
* @return a new PermissionCollection object suitable for storing |
|
976 |
* FilePermissions. |
|
977 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
978 |
@Override |
2 | 979 |
public PermissionCollection newPermissionCollection() { |
980 |
return new FilePermissionCollection(); |
|
981 |
} |
|
982 |
||
983 |
/** |
|
984 |
* WriteObject is called to save the state of the FilePermission |
|
985 |
* to a stream. The actions are serialized, and the superclass |
|
986 |
* takes care of the name. |
|
987 |
*/ |
|
988 |
private void writeObject(ObjectOutputStream s) |
|
989 |
throws IOException |
|
990 |
{ |
|
991 |
// Write out the actions. The superclass takes care of the name |
|
992 |
// call getActions to make sure actions field is initialized |
|
993 |
if (actions == null) |
|
994 |
getActions(); |
|
995 |
s.defaultWriteObject(); |
|
996 |
} |
|
997 |
||
998 |
/** |
|
999 |
* readObject is called to restore the state of the FilePermission from |
|
1000 |
* a stream. |
|
1001 |
*/ |
|
1002 |
private void readObject(ObjectInputStream s) |
|
1003 |
throws IOException, ClassNotFoundException |
|
1004 |
{ |
|
1005 |
// Read in the actions, then restore everything else by calling init. |
|
1006 |
s.defaultReadObject(); |
|
1007 |
init(getMask(actions)); |
|
1008 |
} |
|
42314
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1009 |
|
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1010 |
/** |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1011 |
* Create a cloned FilePermission with a different actions. |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1012 |
* @param effective the new actions |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1013 |
* @return a new object |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1014 |
*/ |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1015 |
FilePermission withNewActions(int effective) { |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1016 |
return new FilePermission(this.getName(), |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1017 |
this, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1018 |
this.npath, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1019 |
this.npath2, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1020 |
effective, |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1021 |
null); |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1022 |
} |
2 | 1023 |
} |
1024 |
||
1025 |
/** |
|
1026 |
* A FilePermissionCollection stores a set of FilePermission permissions. |
|
1027 |
* FilePermission objects |
|
1028 |
* must be stored in a manner that allows them to be inserted in any |
|
1029 |
* order, but enable the implies function to evaluate the implies |
|
1030 |
* method. |
|
1031 |
* For example, if you have two FilePermissions: |
|
1032 |
* <OL> |
|
1033 |
* <LI> "/tmp/-", "read" |
|
1034 |
* <LI> "/tmp/scratch/foo", "write" |
|
1035 |
* </OL> |
|
1036 |
* And you are calling the implies function with the FilePermission: |
|
1037 |
* "/tmp/scratch/foo", "read,write", then the implies function must |
|
1038 |
* take into account both the /tmp/- and /tmp/scratch/foo |
|
1039 |
* permissions, so the effective permission is "read,write". |
|
1040 |
* |
|
1041 |
* @see java.security.Permission |
|
1042 |
* @see java.security.Permissions |
|
1043 |
* @see java.security.PermissionCollection |
|
1044 |
* |
|
1045 |
* |
|
1046 |
* @author Marianne Mueller |
|
1047 |
* @author Roland Schemers |
|
1048 |
* |
|
1049 |
* @serial include |
|
1050 |
* |
|
1051 |
*/ |
|
1052 |
||
1053 |
final class FilePermissionCollection extends PermissionCollection |
|
14014 | 1054 |
implements Serializable |
1055 |
{ |
|
2 | 1056 |
// 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
|
1057 |
private transient ConcurrentHashMap<String, Permission> perms; |
2 | 1058 |
|
1059 |
/** |
|
14014 | 1060 |
* Create an empty FilePermissionCollection object. |
2 | 1061 |
*/ |
1062 |
public FilePermissionCollection() { |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1063 |
perms = new ConcurrentHashMap<>(); |
2 | 1064 |
} |
1065 |
||
1066 |
/** |
|
14014 | 1067 |
* Adds a permission to the FilePermissionCollection. The key for the hash is |
2 | 1068 |
* permission.path. |
1069 |
* |
|
1070 |
* @param permission the Permission object to add. |
|
1071 |
* |
|
1072 |
* @exception IllegalArgumentException - if the permission is not a |
|
1073 |
* FilePermission |
|
1074 |
* |
|
1075 |
* @exception SecurityException - if this FilePermissionCollection object |
|
1076 |
* has been marked readonly |
|
1077 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1078 |
@Override |
14014 | 1079 |
public void add(Permission permission) { |
2 | 1080 |
if (! (permission instanceof FilePermission)) |
1081 |
throw new IllegalArgumentException("invalid permission: "+ |
|
1082 |
permission); |
|
1083 |
if (isReadOnly()) |
|
1084 |
throw new SecurityException( |
|
1085 |
"attempt to add a Permission to a readonly PermissionCollection"); |
|
1086 |
||
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1087 |
FilePermission fp = (FilePermission)permission; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1088 |
|
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1089 |
// Add permission to map if it is absent, or replace with new |
42327
9acc435acfa2
8170408: LogGeneratedClassesTest.java fails with recent changes
weijun
parents:
42314
diff
changeset
|
1090 |
// permission if applicable. |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1091 |
perms.merge(fp.getName(), fp, |
42343
58076b66ffdb
8170602: Startup regression due to introduction of lambda in java.io.FilePermissionCollection
redestad
parents:
42327
diff
changeset
|
1092 |
new java.util.function.BiFunction<>() { |
58076b66ffdb
8170602: Startup regression due to introduction of lambda in java.io.FilePermissionCollection
redestad
parents:
42327
diff
changeset
|
1093 |
@Override |
58076b66ffdb
8170602: Startup regression due to introduction of lambda in java.io.FilePermissionCollection
redestad
parents:
42327
diff
changeset
|
1094 |
public Permission apply(Permission existingVal, |
58076b66ffdb
8170602: Startup regression due to introduction of lambda in java.io.FilePermissionCollection
redestad
parents:
42327
diff
changeset
|
1095 |
Permission newVal) { |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1096 |
int oldMask = ((FilePermission)existingVal).getMask(); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1097 |
int newMask = ((FilePermission)newVal).getMask(); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1098 |
if (oldMask != newMask) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1099 |
int effective = oldMask | newMask; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1100 |
if (effective == newMask) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1101 |
return newVal; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1102 |
} |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1103 |
if (effective != oldMask) { |
42314
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1104 |
return ((FilePermission)newVal) |
e0c05dfa71db
8170364: FilePermission path modified during merge
weijun
parents:
41823
diff
changeset
|
1105 |
.withNewActions(effective); |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1106 |
} |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1107 |
} |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1108 |
return existingVal; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1109 |
} |
42343
58076b66ffdb
8170602: Startup regression due to introduction of lambda in java.io.FilePermissionCollection
redestad
parents:
42327
diff
changeset
|
1110 |
} |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1111 |
); |
2 | 1112 |
} |
1113 |
||
1114 |
/** |
|
1115 |
* Check and see if this set of permissions implies the permissions |
|
1116 |
* expressed in "permission". |
|
1117 |
* |
|
14014 | 1118 |
* @param permission the Permission object to compare |
2 | 1119 |
* |
1120 |
* @return true if "permission" is a proper subset of a permission in |
|
1121 |
* the set, false if not. |
|
1122 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1123 |
@Override |
14014 | 1124 |
public boolean implies(Permission permission) { |
2 | 1125 |
if (! (permission instanceof FilePermission)) |
14014 | 1126 |
return false; |
2 | 1127 |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1128 |
FilePermission fperm = (FilePermission) permission; |
2 | 1129 |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1130 |
int desired = fperm.getMask(); |
2 | 1131 |
int effective = 0; |
1132 |
int needed = desired; |
|
1133 |
||
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1134 |
for (Permission perm : perms.values()) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1135 |
FilePermission fp = (FilePermission)perm; |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1136 |
if (((needed & fp.getMask()) != 0) && fp.impliesIgnoreMask(fperm)) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1137 |
effective |= fp.getMask(); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1138 |
if ((effective & desired) == desired) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1139 |
return true; |
2 | 1140 |
} |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1141 |
needed = (desired ^ effective); |
2 | 1142 |
} |
1143 |
} |
|
1144 |
return false; |
|
1145 |
} |
|
1146 |
||
1147 |
/** |
|
1148 |
* Returns an enumeration of all the FilePermission objects in the |
|
1149 |
* container. |
|
1150 |
* |
|
1151 |
* @return an enumeration of all the FilePermission objects. |
|
1152 |
*/ |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1153 |
@Override |
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
9035
diff
changeset
|
1154 |
public Enumeration<Permission> elements() { |
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1155 |
return perms.elements(); |
2 | 1156 |
} |
1157 |
||
1158 |
private static final long serialVersionUID = 2202956749081564585L; |
|
1159 |
||
1160 |
// Need to maintain serialization interoperability with earlier releases, |
|
1161 |
// which had the serializable field: |
|
1162 |
// private Vector permissions; |
|
1163 |
||
1164 |
/** |
|
1165 |
* @serialField permissions java.util.Vector |
|
1166 |
* A list of FilePermission objects. |
|
1167 |
*/ |
|
1168 |
private static final ObjectStreamField[] serialPersistentFields = { |
|
1169 |
new ObjectStreamField("permissions", Vector.class), |
|
1170 |
}; |
|
1171 |
||
1172 |
/** |
|
1173 |
* @serialData "permissions" field (a Vector containing the FilePermissions). |
|
1174 |
*/ |
|
1175 |
/* |
|
1176 |
* Writes the contents of the perms field out as a Vector for |
|
1177 |
* serialization compatibility with earlier releases. |
|
1178 |
*/ |
|
1179 |
private void writeObject(ObjectOutputStream out) throws IOException { |
|
1180 |
// Don't call out.defaultWriteObject() |
|
1181 |
||
1182 |
// Write out Vector |
|
31080
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1183 |
Vector<Permission> permissions = new Vector<>(perms.values()); |
2 | 1184 |
|
1185 |
ObjectOutputStream.PutField pfields = out.putFields(); |
|
1186 |
pfields.put("permissions", permissions); |
|
1187 |
out.writeFields(); |
|
1188 |
} |
|
1189 |
||
1190 |
/* |
|
1191 |
* Reads in a Vector of FilePermissions and saves them in the perms field. |
|
1192 |
*/ |
|
14014 | 1193 |
private void readObject(ObjectInputStream in) |
1194 |
throws IOException, ClassNotFoundException |
|
1195 |
{ |
|
2 | 1196 |
// Don't call defaultReadObject() |
1197 |
||
1198 |
// Read in serialized fields |
|
1199 |
ObjectInputStream.GetField gfields = in.readFields(); |
|
1200 |
||
1201 |
// Get the one we want |
|
13795
73850c397272
7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents:
9035
diff
changeset
|
1202 |
@SuppressWarnings("unchecked") |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
1203 |
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
|
1204 |
perms = new ConcurrentHashMap<>(permissions.size()); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1205 |
for (Permission perm : permissions) { |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1206 |
perms.put(perm.getName(), perm); |
00a25f4c4d44
8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents:
29986
diff
changeset
|
1207 |
} |
2 | 1208 |
} |
1209 |
} |