src/share/classes/java/io/FilePermission.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.security.*;
  29 import java.util.Enumeration;
  30 import java.util.List;
  31 import java.util.ArrayList;
  32 import java.util.Vector;
  33 import java.util.Collections;
  34 import java.io.ObjectStreamField;
  35 import java.io.ObjectOutputStream;
  36 import java.io.ObjectInputStream;
  37 import java.io.IOException;
  38 import sun.security.util.SecurityConstants;
  39 
  40 /**
  41  * This class represents access to a file or directory.  A FilePermission consists
  42  * of a pathname and a set of actions valid for that pathname.
  43  * <P>
  44  * Pathname is the pathname of the file or directory granted the specified
  45  * actions. A pathname that ends in "/*" (where "/" is
  46  * the file separator character, <code>File.separatorChar</code>) indicates
  47  * all the files and directories contained in that directory. A pathname
  48  * that ends with "/-" indicates (recursively) all files
  49  * and subdirectories contained in that directory. A pathname consisting of
  50  * the special token "&lt;&lt;ALL FILES&gt;&gt;" matches <b>any</b> file.
  51  * <P>
  52  * Note: A pathname consisting of a single "*" indicates all the files
  53  * in the current directory, while a pathname consisting of a single "-"
  54  * indicates all the files in the current directory and
  55  * (recursively) all files and subdirectories contained in the current
  56  * directory.
  57  * <P>


 407 
 408         return (this.mask == that.mask) &&
 409             this.cpath.equals(that.cpath) &&
 410             (this.directory == that.directory) &&
 411             (this.recursive == that.recursive);
 412     }
 413 
 414     /**
 415      * Returns the hash code value for this object.
 416      *
 417      * @return a hash code value for this object.
 418      */
 419 
 420     public int hashCode() {
 421         return this.cpath.hashCode();
 422     }
 423 
 424     /**
 425      * Converts an actions String to an actions mask.
 426      *
 427      * @param action the action string.
 428      * @return the actions mask.
 429      */

 430     private static int getMask(String actions) {
 431 
 432         int mask = NONE;
 433 
 434         // Null action valid?
 435         if (actions == null) {
 436             return mask;
 437         }
 438         // Check against use of constants (used heavily within the JDK)
 439         if (actions == SecurityConstants.FILE_READ_ACTION) {
 440             return READ;
 441         } else if (actions == SecurityConstants.FILE_WRITE_ACTION) {
 442             return WRITE;
 443         } else if (actions == SecurityConstants.FILE_EXECUTE_ACTION) {
 444             return EXECUTE;
 445         } else if (actions == SecurityConstants.FILE_DELETE_ACTION) {
 446             return DELETE;
 447         } else if (actions == SecurityConstants.FILE_READLINK_ACTION) {
 448             return READLINK;
 449         }


 781             for (int i = 0; i < len; i++) {
 782                 FilePermission x = (FilePermission) perms.get(i);
 783                 if (((needed & x.getMask()) != 0) && x.impliesIgnoreMask(fp)) {
 784                     effective |=  x.getMask();
 785                     if ((effective & desired) == desired)
 786                         return true;
 787                     needed = (desired ^ effective);
 788                 }
 789             }
 790         }
 791         return false;
 792     }
 793 
 794     /**
 795      * Returns an enumeration of all the FilePermission objects in the
 796      * container.
 797      *
 798      * @return an enumeration of all the FilePermission objects.
 799      */
 800 
 801     public Enumeration elements() {
 802         // Convert Iterator into Enumeration
 803         synchronized (this) {
 804             return Collections.enumeration(perms);
 805         }
 806     }
 807 
 808     private static final long serialVersionUID = 2202956749081564585L;
 809 
 810     // Need to maintain serialization interoperability with earlier releases,
 811     // which had the serializable field:
 812     //    private Vector permissions;
 813 
 814     /**
 815      * @serialField permissions java.util.Vector
 816      *     A list of FilePermission objects.
 817      */
 818     private static final ObjectStreamField[] serialPersistentFields = {
 819         new ObjectStreamField("permissions", Vector.class),
 820     };
 821 


 826      * Writes the contents of the perms field out as a Vector for
 827      * serialization compatibility with earlier releases.
 828      */
 829     private void writeObject(ObjectOutputStream out) throws IOException {
 830         // Don't call out.defaultWriteObject()
 831 
 832         // Write out Vector
 833         Vector<Permission> permissions = new Vector<>(perms.size());
 834         synchronized (this) {
 835             permissions.addAll(perms);
 836         }
 837 
 838         ObjectOutputStream.PutField pfields = out.putFields();
 839         pfields.put("permissions", permissions);
 840         out.writeFields();
 841     }
 842 
 843     /*
 844      * Reads in a Vector of FilePermissions and saves them in the perms field.
 845      */
 846     @SuppressWarnings("unchecked")
 847     private void readObject(ObjectInputStream in) throws IOException,
 848     ClassNotFoundException {
 849         // Don't call defaultReadObject()
 850 
 851         // Read in serialized fields
 852         ObjectInputStream.GetField gfields = in.readFields();
 853 
 854         // Get the one we want

 855         Vector<Permission> permissions = (Vector<Permission>)gfields.get("permissions", null);
 856         perms = new ArrayList<>(permissions.size());
 857         perms.addAll(permissions);
 858     }
 859 }
   1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.security.*;
  29 import java.util.Enumeration;
  30 import java.util.List;
  31 import java.util.ArrayList;
  32 import java.util.Vector;
  33 import java.util.Collections;




  34 import sun.security.util.SecurityConstants;
  35 
  36 /**
  37  * This class represents access to a file or directory.  A FilePermission consists
  38  * of a pathname and a set of actions valid for that pathname.
  39  * <P>
  40  * Pathname is the pathname of the file or directory granted the specified
  41  * actions. A pathname that ends in "/*" (where "/" is
  42  * the file separator character, <code>File.separatorChar</code>) indicates
  43  * all the files and directories contained in that directory. A pathname
  44  * that ends with "/-" indicates (recursively) all files
  45  * and subdirectories contained in that directory. A pathname consisting of
  46  * the special token "&lt;&lt;ALL FILES&gt;&gt;" matches <b>any</b> file.
  47  * <P>
  48  * Note: A pathname consisting of a single "*" indicates all the files
  49  * in the current directory, while a pathname consisting of a single "-"
  50  * indicates all the files in the current directory and
  51  * (recursively) all files and subdirectories contained in the current
  52  * directory.
  53  * <P>


 403 
 404         return (this.mask == that.mask) &&
 405             this.cpath.equals(that.cpath) &&
 406             (this.directory == that.directory) &&
 407             (this.recursive == that.recursive);
 408     }
 409 
 410     /**
 411      * Returns the hash code value for this object.
 412      *
 413      * @return a hash code value for this object.
 414      */
 415 
 416     public int hashCode() {
 417         return this.cpath.hashCode();
 418     }
 419 
 420     /**
 421      * Converts an actions String to an actions mask.
 422      *
 423      * @param actions the action string.
 424      * @return the actions mask.
 425      */
 426     @SuppressWarnings("fallthrough")
 427     private static int getMask(String actions) {
 428 
 429         int mask = NONE;
 430 
 431         // Null action valid?
 432         if (actions == null) {
 433             return mask;
 434         }
 435         // Check against use of constants (used heavily within the JDK)
 436         if (actions == SecurityConstants.FILE_READ_ACTION) {
 437             return READ;
 438         } else if (actions == SecurityConstants.FILE_WRITE_ACTION) {
 439             return WRITE;
 440         } else if (actions == SecurityConstants.FILE_EXECUTE_ACTION) {
 441             return EXECUTE;
 442         } else if (actions == SecurityConstants.FILE_DELETE_ACTION) {
 443             return DELETE;
 444         } else if (actions == SecurityConstants.FILE_READLINK_ACTION) {
 445             return READLINK;
 446         }


 778             for (int i = 0; i < len; i++) {
 779                 FilePermission x = (FilePermission) perms.get(i);
 780                 if (((needed & x.getMask()) != 0) && x.impliesIgnoreMask(fp)) {
 781                     effective |=  x.getMask();
 782                     if ((effective & desired) == desired)
 783                         return true;
 784                     needed = (desired ^ effective);
 785                 }
 786             }
 787         }
 788         return false;
 789     }
 790 
 791     /**
 792      * Returns an enumeration of all the FilePermission objects in the
 793      * container.
 794      *
 795      * @return an enumeration of all the FilePermission objects.
 796      */
 797 
 798     public Enumeration<Permission> elements() {
 799         // Convert Iterator into Enumeration
 800         synchronized (this) {
 801             return Collections.enumeration(perms);
 802         }
 803     }
 804 
 805     private static final long serialVersionUID = 2202956749081564585L;
 806 
 807     // Need to maintain serialization interoperability with earlier releases,
 808     // which had the serializable field:
 809     //    private Vector permissions;
 810 
 811     /**
 812      * @serialField permissions java.util.Vector
 813      *     A list of FilePermission objects.
 814      */
 815     private static final ObjectStreamField[] serialPersistentFields = {
 816         new ObjectStreamField("permissions", Vector.class),
 817     };
 818 


 823      * Writes the contents of the perms field out as a Vector for
 824      * serialization compatibility with earlier releases.
 825      */
 826     private void writeObject(ObjectOutputStream out) throws IOException {
 827         // Don't call out.defaultWriteObject()
 828 
 829         // Write out Vector
 830         Vector<Permission> permissions = new Vector<>(perms.size());
 831         synchronized (this) {
 832             permissions.addAll(perms);
 833         }
 834 
 835         ObjectOutputStream.PutField pfields = out.putFields();
 836         pfields.put("permissions", permissions);
 837         out.writeFields();
 838     }
 839 
 840     /*
 841      * Reads in a Vector of FilePermissions and saves them in the perms field.
 842      */

 843     private void readObject(ObjectInputStream in) throws IOException,
 844     ClassNotFoundException {
 845         // Don't call defaultReadObject()
 846 
 847         // Read in serialized fields
 848         ObjectInputStream.GetField gfields = in.readFields();
 849 
 850         // Get the one we want
 851         @SuppressWarnings("unchecked")
 852         Vector<Permission> permissions = (Vector<Permission>)gfields.get("permissions", null);
 853         perms = new ArrayList<>(permissions.size());
 854         perms.addAll(permissions);
 855     }
 856 }