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

Print this page

        

@@ -179,21 +179,29 @@
      * Also called during de-serialization.
      *
      * @param mask the actions mask to use.
      *
      */
-    private void init(int mask) {
+    private void initMask(int mask) {
         if ((mask & ALL) != mask)
                 throw new IllegalArgumentException("invalid actions mask");
 
         if (mask == NONE)
                 throw new IllegalArgumentException("invalid actions mask");
 
-        if ((cpath = getName()) == null)
+        if (getName() == null)
                 throw new NullPointerException("name can't be null");
 
         this.mask = mask;
+    }
+
+    /*
+     * The real process of getting canonical path.
+     */
+    private synchronized void initCanonicalPath() {
+
+        cpath = getName();
 
         if (cpath.equals("<<ALL FILES>>")) {
             directory = true;
             recursive = true;
             cpath = "";

@@ -223,27 +231,25 @@
 
         int len = cpath.length();
         char last = ((len > 0) ? cpath.charAt(len - 1) : 0);
 
         if (last == RECURSIVE_CHAR &&
-            cpath.charAt(len - 2) == File.separatorChar) {
+            (len == 1 || cpath.charAt(len - 2) == File.separatorChar)) {
             directory = true;
             recursive = true;
             cpath = cpath.substring(0, --len);
         } else if (last == WILD_CHAR &&
-            cpath.charAt(len - 2) == File.separatorChar) {
+            (len == 1 || cpath.charAt(len - 2) == File.separatorChar)) {
             directory = true;
             //recursive = false;
             cpath = cpath.substring(0, --len);
         } else {
             // overkill since they are initialized to false, but
             // commented out here to remind us...
             //directory = false;
             //recursive = false;
         }
-
-        // XXX: at this point the path should be absolute. die if it isn't?
     }
 
     /**
      * Creates a new FilePermission object with the specified actions.
      * <i>path</i> is the pathname of a file or directory, and <i>actions</i>

@@ -273,11 +279,11 @@
      *          If actions is <code>null</code>, empty or contains an action
      *          other than the specified possible actions.
      */
     public FilePermission(String path, String actions) {
         super(path);
-        init(getMask(actions));
+        initMask(getMask(actions));
     }
 
     /**
      * Creates a new FilePermission object using an action mask.
      * More efficient than the FilePermission(String, String) constructor.

@@ -290,11 +296,11 @@
      */
 
     // package private for use by the FilePermissionCollection add method
     FilePermission(String path, int mask) {
         super(path);
-        init(mask);
+        initMask(mask);
     }
 
     /**
      * Checks if this FilePermission object "implies" the specified permission.
      * <P>

@@ -334,10 +340,14 @@
      *
      * @param that the FilePermission to check against.
      * @return the effective mask
      */
     boolean impliesIgnoreMask(FilePermission that) {
+        if (this.getName().equals("<<ALL FILES>>"))
+                return true;
+        if (this.cpath == null) this.initCanonicalPath();
+        if (that.cpath == null) that.initCanonicalPath();
         if (this.directory) {
             if (this.recursive) {
                 // make sure that.path is longer then path so
                 // something like /foo/- does not imply /foo
                 if (that.directory) {

@@ -394,10 +404,12 @@
         if (! (obj instanceof FilePermission))
             return false;
 
         FilePermission that = (FilePermission) obj;
 
+        if (this.cpath == null) this.initCanonicalPath();
+        if (that.cpath == null) that.initCanonicalPath();
         return (this.mask == that.mask) &&
             this.cpath.equals(that.cpath) &&
             (this.directory == that.directory) &&
             (this.recursive == that.recursive);
     }

@@ -651,11 +663,11 @@
     private void readObject(ObjectInputStream s)
          throws IOException, ClassNotFoundException
     {
         // Read in the actions, then restore everything else by calling init.
         s.defaultReadObject();
-        init(getMask(actions));
+        initMask(getMask(actions));
     }
 }
 
 /**
  * A FilePermissionCollection stores a set of FilePermission permissions.