< prev index next >

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

Print this page
8213406: (fs) More than one instance of built-in FileSystem observed in heap
Reviewed-by: alanb, cushon, weijun


   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.net.URI;
  29 import java.nio.file.*;
  30 import java.security.*;
  31 import java.util.Enumeration;
  32 import java.util.Objects;
  33 import java.util.StringJoiner;
  34 import java.util.Vector;
  35 import java.util.concurrent.ConcurrentHashMap;
  36 
  37 import jdk.internal.access.JavaIOFilePermissionAccess;
  38 import jdk.internal.access.SharedSecrets;
  39 import sun.nio.fs.DefaultFileSystemProvider;
  40 import sun.security.action.GetPropertyAction;
  41 import sun.security.util.FilePermCompat;
  42 import sun.security.util.SecurityConstants;
  43 
  44 /**
  45  * This class represents access to a file or directory.  A FilePermission consists
  46  * of a pathname and a set of actions valid for that pathname.
  47  * <P>
  48  * Pathname is the pathname of the file or directory granted the specified


 182 //    public String toString() {
 183 //        StringBuffer sb = new StringBuffer();
 184 //        sb.append("*** FilePermission on " + getName() + " ***");
 185 //        for (Field f : FilePermission.class.getDeclaredFields()) {
 186 //            if (!Modifier.isStatic(f.getModifiers())) {
 187 //                try {
 188 //                    sb.append(f.getName() + " = " + f.get(this));
 189 //                } catch (Exception e) {
 190 //                    sb.append(f.getName() + " = " + e.toString());
 191 //                }
 192 //                sb.append('\n');
 193 //            }
 194 //        }
 195 //        sb.append("***\n");
 196 //        return sb.toString();
 197 //    }
 198 
 199     private static final long serialVersionUID = 7930732926638008763L;
 200 
 201     /**
 202      * Always use the internal default file system, in case it was modified
 203      * with java.nio.file.spi.DefaultFileSystemProvider.
 204      */
 205     private static final java.nio.file.FileSystem builtInFS =
 206             DefaultFileSystemProvider.create()
 207                     .getFileSystem(URI.create("file:///"));
 208 
 209     private static final Path here = builtInFS.getPath(
 210             GetPropertyAction.privilegedGetProperty("user.dir"));
 211 
 212     private static final Path EMPTY_PATH = builtInFS.getPath("");
 213     private static final Path DASH_PATH = builtInFS.getPath("-");
 214     private static final Path DOTDOT_PATH = builtInFS.getPath("..");
 215 
 216     /**
 217      * A private constructor that clones some and updates some,
 218      * always with a different name.
 219      * @param input
 220      */
 221     private FilePermission(String name,
 222                            FilePermission input,
 223                            Path npath,
 224                            Path npath2,
 225                            int mask,
 226                            String actions) {
 227         super(name);


 309      * @param mask the actions mask to use.
 310      *
 311      */
 312     private void init(int mask) {
 313         if ((mask & ALL) != mask)
 314                 throw new IllegalArgumentException("invalid actions mask");
 315 
 316         if (mask == NONE)
 317                 throw new IllegalArgumentException("invalid actions mask");
 318 
 319         if (FilePermCompat.nb) {
 320             String name = getName();
 321 
 322             if (name == null)
 323                 throw new NullPointerException("name can't be null");
 324 
 325             this.mask = mask;
 326 
 327             if (name.equals("<<ALL FILES>>")) {
 328                 allFiles = true;
 329                 npath = builtInFS.getPath("");
 330                 // other fields remain default
 331                 return;
 332             }
 333 
 334             boolean rememberStar = false;
 335             if (name.endsWith("*")) {
 336                 rememberStar = true;
 337                 recursive = false;
 338                 name = name.substring(0, name.length()-1) + "-";
 339             }
 340 
 341             try {
 342                 // new File() can "normalize" some name, for example, "/C:/X" on
 343                 // Windows. Some JDK codes generate such illegal names.
 344                 npath = builtInFS.getPath(new File(name).getPath())
 345                         .normalize();
 346                 // lastName should always be non-null now
 347                 Path lastName = npath.getFileName();
 348                 if (lastName != null && lastName.equals(DASH_PATH)) {
 349                     directory = true;
 350                     recursive = !rememberStar;
 351                     npath = npath.getParent();
 352                 }
 353                 if (npath == null) {
 354                     npath = builtInFS.getPath("");
 355                 }
 356                 invalid = false;
 357             } catch (InvalidPathException ipe) {
 358                 // Still invalid. For compatibility reason, accept it
 359                 // but make this permission useless.
 360                 npath = builtInFS.getPath("-u-s-e-l-e-s-s-");
 361                 invalid = true;
 362             }
 363 
 364         } else {
 365             if ((cpath = getName()) == null)
 366                 throw new NullPointerException("name can't be null");
 367 
 368             this.mask = mask;
 369 
 370             if (cpath.equals("<<ALL FILES>>")) {
 371                 directory = true;
 372                 recursive = true;
 373                 cpath = "";
 374                 return;




   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.nio.file.*;
  29 import java.security.*;
  30 import java.util.Enumeration;
  31 import java.util.Objects;
  32 import java.util.StringJoiner;
  33 import java.util.Vector;
  34 import java.util.concurrent.ConcurrentHashMap;
  35 
  36 import jdk.internal.access.JavaIOFilePermissionAccess;
  37 import jdk.internal.access.SharedSecrets;
  38 import sun.nio.fs.DefaultFileSystemProvider;
  39 import sun.security.action.GetPropertyAction;
  40 import sun.security.util.FilePermCompat;
  41 import sun.security.util.SecurityConstants;
  42 
  43 /**
  44  * This class represents access to a file or directory.  A FilePermission consists
  45  * of a pathname and a set of actions valid for that pathname.
  46  * <P>
  47  * Pathname is the pathname of the file or directory granted the specified


 181 //    public String toString() {
 182 //        StringBuffer sb = new StringBuffer();
 183 //        sb.append("*** FilePermission on " + getName() + " ***");
 184 //        for (Field f : FilePermission.class.getDeclaredFields()) {
 185 //            if (!Modifier.isStatic(f.getModifiers())) {
 186 //                try {
 187 //                    sb.append(f.getName() + " = " + f.get(this));
 188 //                } catch (Exception e) {
 189 //                    sb.append(f.getName() + " = " + e.toString());
 190 //                }
 191 //                sb.append('\n');
 192 //            }
 193 //        }
 194 //        sb.append("***\n");
 195 //        return sb.toString();
 196 //    }
 197 
 198     private static final long serialVersionUID = 7930732926638008763L;
 199 
 200      /**
 201      * Always use the builtin system-default file system, in case the
 202      * java.nio.file.spi.DefaultFileSystemProvider system property is set.
 203      */
 204     private static final java.nio.file.FileSystem builtInFS =
 205         DefaultFileSystemProvider.theFileSystem();

 206 
 207     private static final Path here = builtInFS.getPath(
 208             GetPropertyAction.privilegedGetProperty("user.dir"));
 209 
 210     private static final Path EMPTY_PATH = builtInFS.getPath("");
 211     private static final Path DASH_PATH = builtInFS.getPath("-");
 212     private static final Path DOTDOT_PATH = builtInFS.getPath("..");
 213 
 214     /**
 215      * A private constructor that clones some and updates some,
 216      * always with a different name.
 217      * @param input
 218      */
 219     private FilePermission(String name,
 220                            FilePermission input,
 221                            Path npath,
 222                            Path npath2,
 223                            int mask,
 224                            String actions) {
 225         super(name);


 307      * @param mask the actions mask to use.
 308      *
 309      */
 310     private void init(int mask) {
 311         if ((mask & ALL) != mask)
 312                 throw new IllegalArgumentException("invalid actions mask");
 313 
 314         if (mask == NONE)
 315                 throw new IllegalArgumentException("invalid actions mask");
 316 
 317         if (FilePermCompat.nb) {
 318             String name = getName();
 319 
 320             if (name == null)
 321                 throw new NullPointerException("name can't be null");
 322 
 323             this.mask = mask;
 324 
 325             if (name.equals("<<ALL FILES>>")) {
 326                 allFiles = true;
 327                 npath = EMPTY_PATH;
 328                 // other fields remain default
 329                 return;
 330             }
 331 
 332             boolean rememberStar = false;
 333             if (name.endsWith("*")) {
 334                 rememberStar = true;
 335                 recursive = false;
 336                 name = name.substring(0, name.length()-1) + "-";
 337             }
 338 
 339             try {
 340                 // new File() can "normalize" some name, for example, "/C:/X" on
 341                 // Windows. Some JDK codes generate such illegal names.
 342                 npath = builtInFS.getPath(new File(name).getPath())
 343                         .normalize();
 344                 // lastName should always be non-null now
 345                 Path lastName = npath.getFileName();
 346                 if (lastName != null && lastName.equals(DASH_PATH)) {
 347                     directory = true;
 348                     recursive = !rememberStar;
 349                     npath = npath.getParent();
 350                 }
 351                 if (npath == null) {
 352                     npath = EMPTY_PATH;
 353                 }
 354                 invalid = false;
 355             } catch (InvalidPathException ipe) {
 356                 // Still invalid. For compatibility reason, accept it
 357                 // but make this permission useless.
 358                 npath = builtInFS.getPath("-u-s-e-l-e-s-s-");
 359                 invalid = true;
 360             }
 361 
 362         } else {
 363             if ((cpath = getName()) == null)
 364                 throw new NullPointerException("name can't be null");
 365 
 366             this.mask = mask;
 367 
 368             if (cpath.equals("<<ALL FILES>>")) {
 369                 directory = true;
 370                 recursive = true;
 371                 cpath = "";
 372                 return;


< prev index next >