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