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