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