jdk/src/java.base/share/classes/java/io/FilePermission.java
author darcy
Thu, 23 Apr 2015 09:32:35 -0700
changeset 29986 97167d851fc4
parent 26219 1a19360ff122
child 31080 00a25f4c4d44
permissions -rw-r--r--
8078467: Update core libraries to use diamond with anonymous classes Reviewed-by: mchung, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2014, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Collections;
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
    34
import java.util.StringJoiner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class represents access to a file or directory.  A FilePermission consists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * of a pathname and a set of actions valid for that pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Pathname is the pathname of the file or directory granted the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * actions. A pathname that ends in "/*" (where "/" is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the file separator character, <code>File.separatorChar</code>) indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * all the files and directories contained in that directory. A pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * that ends with "/-" indicates (recursively) all files
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * and subdirectories contained in that directory. A pathname consisting of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * the special token "&lt;&lt;ALL FILES&gt;&gt;" matches <b>any</b> file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Note: A pathname consisting of a single "*" indicates all the files
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * in the current directory, while a pathname consisting of a single "-"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * indicates all the files in the current directory and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * (recursively) all files and subdirectories contained in the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The actions to be granted are passed to the constructor in a string containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * 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
    57
 * "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
    58
 * defined as follows:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 16100
diff changeset
    59
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <DL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *    <DT> read <DD> read permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *    <DT> write <DD> write permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *    <DT> execute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *    <DD> execute permission. Allows <code>Runtime.exec</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *         be called. Corresponds to <code>SecurityManager.checkExec</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *    <DT> delete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *    <DD> delete permission. Allows <code>File.delete</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *         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
    69
 *    <DT> readlink
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    70
 *    <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
    71
 *         <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
    72
 *         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
    73
 *         readSymbolicLink } method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * </DL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * The actions string is converted to lowercase before processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Be careful when granting FilePermissions. Think about the implications
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * of granting read and especially write access to various files and
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
    80
 * directories. The "&lt;&lt;ALL FILES&gt;&gt;" permission with write action is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * especially dangerous. This grants permission to write to the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * file system. One thing this effectively allows is replacement of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * system binary, including the JVM runtime environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <p>Please note: Code can always read a file from the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * directory it's in (or a subdirectory of that directory); it does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * need explicit permission to do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * @see java.security.PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @author Marianne Mueller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @serial exclude
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
public final class FilePermission extends Permission implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Execute action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private final static int EXECUTE = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Write action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private final static int WRITE   = 0x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Read action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private final static int READ    = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Delete action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private final static int DELETE  = 0x8;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   119
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   120
     * Read link action.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   121
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   122
    private final static int READLINK    = 0x10;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   125
     * All actions (read,write,execute,delete,readlink)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   127
    private final static int ALL     = READ|WRITE|EXECUTE|DELETE|READLINK;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * No actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private final static int NONE    = 0x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // the actions mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private transient int mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // does path indicate a directory? (wildcard or recursive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private transient boolean directory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // is it a recursive directory specification?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private transient boolean recursive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * the actions string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private String actions; // Left null as long as possible, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                            // created and re-used in the getAction function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // canonicalized dir path. In the case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    // directories, it is the name "/blah/*" or "/blah/-" without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    // the last character (the "*" or "-").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private transient String cpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // static Strings used by init(int mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static final char RECURSIVE_CHAR = '-';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static final char WILD_CHAR = '*';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public String toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        sb.append("***\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        sb.append("cpath = "+cpath+"\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        sb.append("mask = "+mask+"\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        sb.append("actions = "+getActions()+"\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        sb.append("directory = "+directory+"\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        sb.append("recursive = "+recursive+"\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        sb.append("***\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private static final long serialVersionUID = 7930732926638008763L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * initialize a FilePermission object. Common to all constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Also called during de-serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param mask the actions mask to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   184
    private void init(int mask) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if ((mask & ALL) != mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                throw new IllegalArgumentException("invalid actions mask");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (mask == NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                throw new IllegalArgumentException("invalid actions mask");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if ((cpath = getName()) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                throw new NullPointerException("name can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        this.mask = mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (cpath.equals("<<ALL FILES>>")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            directory = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            recursive = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            cpath = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // store only the canonical cpath if possible
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 26219
diff changeset
   204
        cpath = AccessController.doPrivileged(new PrivilegedAction<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                try {
4053
c2f8e57ba2f8 6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents: 2057
diff changeset
   207
                    String path = cpath;
c2f8e57ba2f8 6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents: 2057
diff changeset
   208
                    if (cpath.endsWith("*")) {
c2f8e57ba2f8 6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents: 2057
diff changeset
   209
                        // call getCanonicalPath with a path with wildcard character
c2f8e57ba2f8 6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents: 2057
diff changeset
   210
                        // replaced to avoid calling it with paths that are
c2f8e57ba2f8 6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents: 2057
diff changeset
   211
                        // intended to match all entries in a directory
c2f8e57ba2f8 6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents: 2057
diff changeset
   212
                        path = path.substring(0, path.length()-1) + "-";
c2f8e57ba2f8 6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents: 2057
diff changeset
   213
                        path = new File(path).getCanonicalPath();
c2f8e57ba2f8 6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents: 2057
diff changeset
   214
                        return path.substring(0, path.length()-1) + "*";
c2f8e57ba2f8 6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents: 2057
diff changeset
   215
                    } else {
c2f8e57ba2f8 6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents: 2057
diff changeset
   216
                        return new File(path).getCanonicalPath();
c2f8e57ba2f8 6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents: 2057
diff changeset
   217
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    return cpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int len = cpath.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        char last = ((len > 0) ? cpath.charAt(len - 1) : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (last == RECURSIVE_CHAR &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            cpath.charAt(len - 2) == File.separatorChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            directory = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            recursive = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            cpath = cpath.substring(0, --len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        } else if (last == WILD_CHAR &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            cpath.charAt(len - 2) == File.separatorChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            directory = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            //recursive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            cpath = cpath.substring(0, --len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            // overkill since they are initialized to false, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            // commented out here to remind us...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            //directory = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            //recursive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // XXX: at this point the path should be absolute. die if it isn't?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Creates a new FilePermission object with the specified actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <i>path</i> is the pathname of a file or directory, and <i>actions</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * contains a comma-separated list of the desired actions granted on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * 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
   252
     * "read", "write", "execute", "delete", and "readlink".
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <p>A pathname that ends in "/*" (where "/" is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * the file separator character, <code>File.separatorChar</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * indicates all the files and directories contained in that directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * A pathname that ends with "/-" indicates (recursively) all files and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * subdirectories contained in that directory. The special pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * "&lt;&lt;ALL FILES&gt;&gt;" matches any file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <p>A pathname consisting of a single "*" indicates all the files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * in the current directory, while a pathname consisting of a single "-"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * indicates all the files in the current directory and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * (recursively) all files and subdirectories contained in the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <p>A pathname containing an empty string represents an empty path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param path the pathname of the file/directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param actions the action string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *          If actions is <code>null</code>, empty or contains an action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *          other than the specified possible actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   276
    public FilePermission(String path, String actions) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        super(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        init(getMask(actions));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Creates a new FilePermission object using an action mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * More efficient than the FilePermission(String, String) constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Can be used from within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * code that needs to create a FilePermission object to pass into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * <code>implies</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param path the pathname of the file/directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param mask the action mask to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    // package private for use by the FilePermissionCollection add method
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   293
    FilePermission(String path, int mask) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        super(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        init(mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Checks if this FilePermission object "implies" the specified permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <P>
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 16100
diff changeset
   301
     * More specifically, this method returns true if:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <ul>
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 16100
diff changeset
   303
     * <li> <i>p</i> is an instanceof FilePermission,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * <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
   305
     * object's actions, and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <li> <i>p</i>'s pathname is implied by this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *      pathname. For example, "/tmp/*" implies "/tmp/foo", since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *      "/tmp/*" encompasses all files in the "/tmp" directory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *      including the one named "foo".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param p the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @return <code>true</code> if the specified permission is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *                  <code>null</code> and is implied by this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *                  <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public boolean implies(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (!(p instanceof FilePermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        FilePermission that = (FilePermission) p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // we get the effective mask. i.e., the "and" of this and that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        // They must be equal to that.mask for implies to return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return ((this.mask & that.mask) == that.mask) && impliesIgnoreMask(that);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Checks if the Permission's actions are a proper subset of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * this object's actions. Returns the effective mask iff the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * this FilePermission's path also implies that FilePermission's path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param that the FilePermission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @return the effective mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    boolean impliesIgnoreMask(FilePermission that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (this.directory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (this.recursive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                // make sure that.path is longer then path so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                // something like /foo/- does not imply /foo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                if (that.directory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    return (that.cpath.length() >= this.cpath.length()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                            that.cpath.startsWith(this.cpath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                }  else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    return ((that.cpath.length() > this.cpath.length()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        that.cpath.startsWith(this.cpath));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                if (that.directory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    // if the permission passed in is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    // specification, make sure that a non-recursive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    // permission (i.e., this object) can't imply a recursive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    // permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    if (that.recursive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        return (this.cpath.equals(that.cpath));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    int last = that.cpath.lastIndexOf(File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    if (last == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        // this.cpath.equals(that.cpath.substring(0, last+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        // Use regionMatches to avoid creating new string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        return (this.cpath.length() == (last + 1)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                            this.cpath.regionMatches(0, that.cpath, 0, last+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        } else if (that.directory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            // if this is NOT recursive/wildcarded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            // do not let it imply a recursive/wildcarded permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return (this.cpath.equals(that.cpath));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Checks two FilePermission objects for equality. Checks that <i>obj</i> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * 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
   384
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param obj the object we are testing for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @return <code>true</code> if obj is a FilePermission, and has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *          pathname and actions as this FilePermission object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        if (! (obj instanceof FilePermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        FilePermission that = (FilePermission) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return (this.mask == that.mask) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            this.cpath.equals(that.cpath) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            (this.directory == that.directory) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            (this.recursive == that.recursive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public int hashCode() {
14207
7d6689d7e260 6631398: FilePermission improved path checking
weijun
parents: 9035
diff changeset
   411
        return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Converts an actions String to an actions mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 9035
diff changeset
   417
     * @param actions the action string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @return the actions mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    private static int getMask(String actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        int mask = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        // Null action valid?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (actions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 9035
diff changeset
   427
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 9035
diff changeset
   428
        // 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
   429
        // performance benefit (these values are used heavily within the JDK).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (actions == SecurityConstants.FILE_READ_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            return READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        } else if (actions == SecurityConstants.FILE_WRITE_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            return WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        } else if (actions == SecurityConstants.FILE_EXECUTE_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            return EXECUTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        } else if (actions == SecurityConstants.FILE_DELETE_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return DELETE;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   438
        } 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
   439
            return READLINK;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        char[] a = actions.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        int i = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (i < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        while (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            // skip whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            while ((i!=-1) && ((c = a[i]) == ' ' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                               c == '\r' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                               c == '\n' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                               c == '\f' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                               c == '\t'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            // check for the known strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            int matchlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            if (i >= 3 && (a[i-3] == 'r' || a[i-3] == 'R') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                          (a[i-2] == 'e' || a[i-2] == 'E') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                          (a[i-1] == 'a' || a[i-1] == 'A') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                          (a[i] == 'd' || a[i] == 'D'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                matchlen = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                mask |= READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            } else if (i >= 4 && (a[i-4] == 'w' || a[i-4] == 'W') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                                 (a[i-3] == 'r' || a[i-3] == 'R') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                 (a[i-2] == 'i' || a[i-2] == 'I') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                                 (a[i-1] == 't' || a[i-1] == 'T') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                                 (a[i] == 'e' || a[i] == 'E'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                matchlen = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                mask |= WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            } else if (i >= 6 && (a[i-6] == 'e' || a[i-6] == 'E') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                 (a[i-5] == 'x' || a[i-5] == 'X') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                                 (a[i-4] == 'e' || a[i-4] == 'E') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                                 (a[i-3] == 'c' || a[i-3] == 'C') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                 (a[i-2] == 'u' || a[i-2] == 'U') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                                 (a[i-1] == 't' || a[i-1] == 'T') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                                 (a[i] == 'e' || a[i] == 'E'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                matchlen = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                mask |= EXECUTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            } else if (i >= 5 && (a[i-5] == 'd' || a[i-5] == 'D') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                                 (a[i-4] == 'e' || a[i-4] == 'E') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                 (a[i-3] == 'l' || a[i-3] == 'L') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                 (a[i-2] == 'e' || a[i-2] == 'E') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                 (a[i-1] == 't' || a[i-1] == 'T') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                                 (a[i] == 'e' || a[i] == 'E'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                matchlen = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                mask |= DELETE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   500
            } 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
   501
                                 (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
   502
                                 (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
   503
                                 (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
   504
                                 (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
   505
                                 (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
   506
                                 (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
   507
                                 (a[i] == 'k' || a[i] == 'K'))
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   508
            {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   509
                matchlen = 8;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   510
                mask |= READLINK;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   511
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                // parse error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        "invalid permission: " + actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            // make sure we didn't just match the tail of a word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            // like "ackbarfaccept".  Also, skip to the comma.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            boolean seencomma = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            while (i >= matchlen && !seencomma) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                switch(a[i-matchlen]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                case ',':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    seencomma = true;
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 9035
diff changeset
   525
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                case ' ': case '\r': case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                case '\f': case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                            "invalid permission: " + actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            // point i at the location of the comma minus one (or -1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            i -= matchlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * Return the current action mask. Used by the FilePermissionCollection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @return the actions mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    int getMask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * Return the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * 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
   555
     * read, write, execute, delete, readlink.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @return the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   559
    private static String getActions(int mask) {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   560
        StringJoiner sj = new StringJoiner(",");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if ((mask & READ) == READ) {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   563
            sj.add("read");
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   564
        }
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   565
        if ((mask & WRITE) == WRITE) {
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   566
            sj.add("write");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   568
        if ((mask & EXECUTE) == EXECUTE) {
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   569
            sj.add("execute");
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   570
        }
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   571
        if ((mask & DELETE) == DELETE) {
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   572
            sj.add("delete");
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   573
        }
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   574
        if ((mask & READLINK) == READLINK) {
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   575
            sj.add("readlink");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   578
        return sj.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Returns the "canonical string representation" of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * 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
   584
     * 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
   585
     * object allows both write and read actions, a call to <code>getActions</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * will return the string "read,write".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @return the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   590
    public String getActions() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if (actions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            actions = getActions(this.mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Returns a new PermissionCollection object for storing FilePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * FilePermission objects must be stored in a manner that allows them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * to be inserted into the collection in any order, but that also enables the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * PermissionCollection <code>implies</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * method to be implemented in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * <p>For example, if you have two FilePermissions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * <OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * <LI>  <code>"/tmp/-", "read"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * <LI>  <code>"/tmp/scratch/foo", "write"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * </OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * <p>and you are calling the <code>implies</code> method with the FilePermission:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *   "/tmp/scratch/foo", "read,write",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * then the <code>implies</code> function must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * take into account both the "/tmp/-" and "/tmp/scratch/foo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * permissions, so the effective permission is "read,write",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * and <code>implies</code> returns true. The "implies" semantics for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * FilePermissions are handled properly by the PermissionCollection object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * returned by this <code>newPermissionCollection</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @return a new PermissionCollection object suitable for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * FilePermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        return new FilePermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * WriteObject is called to save the state of the FilePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * to a stream. The actions are serialized, and the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * takes care of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        // Write out the actions. The superclass takes care of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        // call getActions to make sure actions field is initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        if (actions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * readObject is called to restore the state of the FilePermission from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        // Read in the actions, then restore everything else by calling init.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        init(getMask(actions));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
 * A FilePermissionCollection stores a set of FilePermission permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
 * FilePermission objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
 * must be stored in a manner that allows them to be inserted in any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
 * order, but enable the implies function to evaluate the implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
 * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
 * For example, if you have two FilePermissions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
 * <OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
 * <LI> "/tmp/-", "read"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
 * <LI> "/tmp/scratch/foo", "write"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
 * </OL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
 * And you are calling the implies function with the FilePermission:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
 * "/tmp/scratch/foo", "read,write", then the implies function must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
 * take into account both the /tmp/- and /tmp/scratch/foo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
 * permissions, so the effective permission is "read,write".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
 * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
 * @see java.security.PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
 * @author Marianne Mueller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
 * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
final class FilePermissionCollection extends PermissionCollection
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   689
    implements Serializable
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   690
{
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    // Not serialized; see serialization section at end of class
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   692
    private transient List<Permission> perms;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    /**
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   695
     * Create an empty FilePermissionCollection object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    public FilePermissionCollection() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   698
        perms = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   702
     * Adds a permission to the FilePermissionCollection. The key for the hash is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * permission.path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @exception IllegalArgumentException - if the permission is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *                                       FilePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @exception SecurityException - if this FilePermissionCollection object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *                                has been marked readonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   713
    public void add(Permission permission) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        if (! (permission instanceof FilePermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            throw new IllegalArgumentException("invalid permission: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                                               permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            throw new SecurityException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                "attempt to add a Permission to a readonly PermissionCollection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            perms.add(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * Check and see if this set of permissions implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * expressed in "permission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   730
     * @param permission the Permission object to compare
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @return true if "permission" is a proper subset of a permission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * the set, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   735
    public boolean implies(Permission permission) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if (! (permission instanceof FilePermission))
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   737
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        FilePermission fp = (FilePermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        int desired = fp.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        int effective = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        int needed = desired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            int len = perms.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                FilePermission x = (FilePermission) perms.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                if (((needed & x.getMask()) != 0) && x.impliesIgnoreMask(fp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    effective |=  x.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                    if ((effective & desired) == desired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    needed = (desired ^ effective);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * Returns an enumeration of all the FilePermission objects in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @return an enumeration of all the FilePermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 9035
diff changeset
   766
    public Enumeration<Permission> elements() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        // Convert Iterator into Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            return Collections.enumeration(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    private static final long serialVersionUID = 2202956749081564585L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    // Need to maintain serialization interoperability with earlier releases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    // which had the serializable field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    //    private Vector permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @serialField permissions java.util.Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *     A list of FilePermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        new ObjectStreamField("permissions", Vector.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @serialData "permissions" field (a Vector containing the FilePermissions).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * Writes the contents of the perms field out as a Vector for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * serialization compatibility with earlier releases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        // Don't call out.defaultWriteObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        // Write out Vector
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   798
        Vector<Permission> permissions = new Vector<>(perms.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            permissions.addAll(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        ObjectOutputStream.PutField pfields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        pfields.put("permissions", permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * Reads in a Vector of FilePermissions and saves them in the perms field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   811
    private void readObject(ObjectInputStream in)
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   812
        throws IOException, ClassNotFoundException
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   813
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        // Don't call defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        // Read in serialized fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        ObjectInputStream.GetField gfields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        // Get the one we want
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 9035
diff changeset
   820
        @SuppressWarnings("unchecked")
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   821
        Vector<Permission> permissions = (Vector<Permission>)gfields.get("permissions", null);
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   822
        perms = new ArrayList<>(permissions.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        perms.addAll(permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
}