19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
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.
24 */
25
26 package java.io;
27
28 import java.net.URI;
29 import java.nio.file.*;
30 import java.security.*;
31 import java.util.Enumeration;
32 import java.util.Objects;
33 import java.util.StringJoiner;
34 import java.util.Vector;
35 import java.util.concurrent.ConcurrentHashMap;
36
37 import jdk.internal.access.JavaIOFilePermissionAccess;
38 import jdk.internal.access.SharedSecrets;
39 import sun.nio.fs.DefaultFileSystemProvider;
40 import sun.security.action.GetPropertyAction;
41 import sun.security.util.FilePermCompat;
42 import sun.security.util.SecurityConstants;
43
44 /**
45 * This class represents access to a file or directory. A FilePermission consists
46 * of a pathname and a set of actions valid for that pathname.
47 * <P>
48 * Pathname is the pathname of the file or directory granted the specified
49 * actions. A pathname that ends in "/*" (where "/" is
50 * the file separator character, <code>File.separatorChar</code>) indicates
51 * all the files and directories contained in that directory. A pathname
52 * that ends with "/-" indicates (recursively) all files
53 * and subdirectories contained in that directory. Such a pathname is called
54 * a wildcard pathname. Otherwise, it's a simple pathname.
55 * <P>
56 * A pathname consisting of the special token {@literal "<<ALL FILES>>"}
57 * matches <b>any</b> file.
58 * <P>
59 * Note: A pathname consisting of a single "*" indicates all the files
60 * in the current directory, while a pathname consisting of a single "-"
61 * indicates all the files in the current directory and
62 * (recursively) all files and subdirectories contained in the current
181
182 // public String toString() {
183 // StringBuffer sb = new StringBuffer();
184 // sb.append("*** FilePermission on " + getName() + " ***");
185 // for (Field f : FilePermission.class.getDeclaredFields()) {
186 // if (!Modifier.isStatic(f.getModifiers())) {
187 // try {
188 // sb.append(f.getName() + " = " + f.get(this));
189 // } catch (Exception e) {
190 // sb.append(f.getName() + " = " + e.toString());
191 // }
192 // sb.append('\n');
193 // }
194 // }
195 // sb.append("***\n");
196 // return sb.toString();
197 // }
198
199 private static final long serialVersionUID = 7930732926638008763L;
200
201 /**
202 * Always use the internal default file system, in case it was modified
203 * with java.nio.file.spi.DefaultFileSystemProvider.
204 */
205 private static final java.nio.file.FileSystem builtInFS =
206 DefaultFileSystemProvider.create()
207 .getFileSystem(URI.create("file:///"));
208
209 private static final Path here = builtInFS.getPath(
210 GetPropertyAction.privilegedGetProperty("user.dir"));
211
212 private static final Path EMPTY_PATH = builtInFS.getPath("");
213 private static final Path DASH_PATH = builtInFS.getPath("-");
214 private static final Path DOTDOT_PATH = builtInFS.getPath("..");
215
216 /**
217 * A private constructor that clones some and updates some,
218 * always with a different name.
219 * @param input
220 */
221 private FilePermission(String name,
222 FilePermission input,
223 Path npath,
224 Path npath2,
225 int mask,
226 String actions) {
227 super(name);
228 // Customizables
229 this.npath = npath;
230 this.npath2 = npath2;
231 this.actions = actions;
232 this.mask = mask;
233 // Cloneds
234 this.allFiles = input.allFiles;
309 * @param mask the actions mask to use.
310 *
311 */
312 private void init(int mask) {
313 if ((mask & ALL) != mask)
314 throw new IllegalArgumentException("invalid actions mask");
315
316 if (mask == NONE)
317 throw new IllegalArgumentException("invalid actions mask");
318
319 if (FilePermCompat.nb) {
320 String name = getName();
321
322 if (name == null)
323 throw new NullPointerException("name can't be null");
324
325 this.mask = mask;
326
327 if (name.equals("<<ALL FILES>>")) {
328 allFiles = true;
329 npath = builtInFS.getPath("");
330 // other fields remain default
331 return;
332 }
333
334 boolean rememberStar = false;
335 if (name.endsWith("*")) {
336 rememberStar = true;
337 recursive = false;
338 name = name.substring(0, name.length()-1) + "-";
339 }
340
341 try {
342 // new File() can "normalize" some name, for example, "/C:/X" on
343 // Windows. Some JDK codes generate such illegal names.
344 npath = builtInFS.getPath(new File(name).getPath())
345 .normalize();
346 // lastName should always be non-null now
347 Path lastName = npath.getFileName();
348 if (lastName != null && lastName.equals(DASH_PATH)) {
349 directory = true;
350 recursive = !rememberStar;
351 npath = npath.getParent();
352 }
353 if (npath == null) {
354 npath = builtInFS.getPath("");
355 }
356 invalid = false;
357 } catch (InvalidPathException ipe) {
358 // Still invalid. For compatibility reason, accept it
359 // but make this permission useless.
360 npath = builtInFS.getPath("-u-s-e-l-e-s-s-");
361 invalid = true;
362 }
363
364 } else {
365 if ((cpath = getName()) == null)
366 throw new NullPointerException("name can't be null");
367
368 this.mask = mask;
369
370 if (cpath.equals("<<ALL FILES>>")) {
371 directory = true;
372 recursive = true;
373 cpath = "";
374 return;
375 }
376
377 // store only the canonical cpath if possible
378 cpath = AccessController.doPrivileged(new PrivilegedAction<>() {
379 public String run() {
380 try {
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
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.
24 */
25
26 package java.io;
27
28 import java.net.URI;
29 import java.nio.file.*;
30 import java.security.*;
31 import java.util.Enumeration;
32 import java.util.Objects;
33 import java.util.StringJoiner;
34 import java.util.Vector;
35 import java.util.concurrent.ConcurrentHashMap;
36
37 import jdk.internal.access.JavaIOFilePermissionAccess;
38 import jdk.internal.access.SharedSecrets;
39 import sun.security.action.GetPropertyAction;
40 import sun.security.util.FilePermCompat;
41 import sun.security.util.SecurityConstants;
42 import static sun.nio.fs.DefaultFileSystemProvider.FILE_SYSTEM;
43
44 /**
45 * This class represents access to a file or directory. A FilePermission consists
46 * of a pathname and a set of actions valid for that pathname.
47 * <P>
48 * Pathname is the pathname of the file or directory granted the specified
49 * actions. A pathname that ends in "/*" (where "/" is
50 * the file separator character, <code>File.separatorChar</code>) indicates
51 * all the files and directories contained in that directory. A pathname
52 * that ends with "/-" indicates (recursively) all files
53 * and subdirectories contained in that directory. Such a pathname is called
54 * a wildcard pathname. Otherwise, it's a simple pathname.
55 * <P>
56 * A pathname consisting of the special token {@literal "<<ALL FILES>>"}
57 * matches <b>any</b> file.
58 * <P>
59 * Note: A pathname consisting of a single "*" indicates all the files
60 * in the current directory, while a pathname consisting of a single "-"
61 * indicates all the files in the current directory and
62 * (recursively) all files and subdirectories contained in the current
181
182 // public String toString() {
183 // StringBuffer sb = new StringBuffer();
184 // sb.append("*** FilePermission on " + getName() + " ***");
185 // for (Field f : FilePermission.class.getDeclaredFields()) {
186 // if (!Modifier.isStatic(f.getModifiers())) {
187 // try {
188 // sb.append(f.getName() + " = " + f.get(this));
189 // } catch (Exception e) {
190 // sb.append(f.getName() + " = " + e.toString());
191 // }
192 // sb.append('\n');
193 // }
194 // }
195 // sb.append("***\n");
196 // return sb.toString();
197 // }
198
199 private static final long serialVersionUID = 7930732926638008763L;
200
201 // Always use the internal default file system, in case it was modified
202 // with the java.nio.file.spi.DefaultFileSystemProvider system property.
203
204 private static final Path here = FILE_SYSTEM.getPath(
205 GetPropertyAction.privilegedGetProperty("user.dir"));
206
207 private static final Path EMPTY_PATH = FILE_SYSTEM.getPath("");
208 private static final Path DASH_PATH = FILE_SYSTEM.getPath("-");
209 private static final Path DOTDOT_PATH = FILE_SYSTEM.getPath("..");
210
211 /**
212 * A private constructor that clones some and updates some,
213 * always with a different name.
214 * @param input
215 */
216 private FilePermission(String name,
217 FilePermission input,
218 Path npath,
219 Path npath2,
220 int mask,
221 String actions) {
222 super(name);
223 // Customizables
224 this.npath = npath;
225 this.npath2 = npath2;
226 this.actions = actions;
227 this.mask = mask;
228 // Cloneds
229 this.allFiles = input.allFiles;
304 * @param mask the actions mask to use.
305 *
306 */
307 private void init(int mask) {
308 if ((mask & ALL) != mask)
309 throw new IllegalArgumentException("invalid actions mask");
310
311 if (mask == NONE)
312 throw new IllegalArgumentException("invalid actions mask");
313
314 if (FilePermCompat.nb) {
315 String name = getName();
316
317 if (name == null)
318 throw new NullPointerException("name can't be null");
319
320 this.mask = mask;
321
322 if (name.equals("<<ALL FILES>>")) {
323 allFiles = true;
324 npath = EMPTY_PATH;
325 // other fields remain default
326 return;
327 }
328
329 boolean rememberStar = false;
330 if (name.endsWith("*")) {
331 rememberStar = true;
332 recursive = false;
333 name = name.substring(0, name.length()-1) + "-";
334 }
335
336 try {
337 // new File() can "normalize" some name, for example, "/C:/X" on
338 // Windows. Some JDK codes generate such illegal names.
339 npath = FILE_SYSTEM.getPath(new File(name).getPath())
340 .normalize();
341 // lastName should always be non-null now
342 Path lastName = npath.getFileName();
343 if (lastName != null && lastName.equals(DASH_PATH)) {
344 directory = true;
345 recursive = !rememberStar;
346 npath = npath.getParent();
347 }
348 if (npath == null) {
349 npath = EMPTY_PATH;
350 }
351 invalid = false;
352 } catch (InvalidPathException ipe) {
353 // Still invalid. For compatibility reason, accept it
354 // but make this permission useless.
355 npath = FILE_SYSTEM.getPath("-u-s-e-l-e-s-s-");
356 invalid = true;
357 }
358
359 } else {
360 if ((cpath = getName()) == null)
361 throw new NullPointerException("name can't be null");
362
363 this.mask = mask;
364
365 if (cpath.equals("<<ALL FILES>>")) {
366 directory = true;
367 recursive = true;
368 cpath = "";
369 return;
370 }
371
372 // store only the canonical cpath if possible
373 cpath = AccessController.doPrivileged(new PrivilegedAction<>() {
374 public String run() {
375 try {
|