Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/file/TempFileHelper.java
38918 views
/*1* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.file;2627import java.util.Set;28import java.util.EnumSet;29import java.security.SecureRandom;30import static java.security.AccessController.*;31import java.io.IOException;32import java.nio.file.attribute.FileAttribute;33import java.nio.file.attribute.PosixFilePermission;34import java.nio.file.attribute.PosixFilePermissions;35import static java.nio.file.attribute.PosixFilePermission.*;36import sun.security.action.GetPropertyAction;373839/**40* Helper class to support creation of temporary files and directories with41* initial attributes.42*/4344class TempFileHelper {45private TempFileHelper() { }4647// temporary directory location48private static final Path tmpdir =49Paths.get(doPrivileged(new GetPropertyAction("java.io.tmpdir")));5051private static final boolean isPosix =52FileSystems.getDefault().supportedFileAttributeViews().contains("posix");5354// file name generation, same as java.io.File for now55private static final SecureRandom random = new SecureRandom();56private static Path generatePath(String prefix, String suffix, Path dir) {57long n = random.nextLong();58n = (n == Long.MIN_VALUE) ? 0 : Math.abs(n);59Path name = dir.getFileSystem().getPath(prefix + Long.toString(n) + suffix);60// the generated name should be a simple file name61if (name.getParent() != null)62throw new IllegalArgumentException("Invalid prefix or suffix");63return dir.resolve(name);64}6566// default file and directory permissions (lazily initialized)67private static class PosixPermissions {68static final FileAttribute<Set<PosixFilePermission>> filePermissions =69PosixFilePermissions.asFileAttribute(EnumSet.of(OWNER_READ, OWNER_WRITE));70static final FileAttribute<Set<PosixFilePermission>> dirPermissions =71PosixFilePermissions.asFileAttribute(EnumSet72.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE));73}7475/**76* Creates a file or directory in in the given given directory (or in the77* temporary directory if dir is {@code null}).78*/79private static Path create(Path dir,80String prefix,81String suffix,82boolean createDirectory,83FileAttribute<?>[] attrs)84throws IOException85{86if (prefix == null)87prefix = "";88if (suffix == null)89suffix = (createDirectory) ? "" : ".tmp";90if (dir == null)91dir = tmpdir;9293// in POSIX environments use default file and directory permissions94// if initial permissions not given by caller.95if (isPosix && (dir.getFileSystem() == FileSystems.getDefault())) {96if (attrs.length == 0) {97// no attributes so use default permissions98attrs = new FileAttribute<?>[1];99attrs[0] = (createDirectory) ? PosixPermissions.dirPermissions :100PosixPermissions.filePermissions;101} else {102// check if posix permissions given; if not use default103boolean hasPermissions = false;104for (int i=0; i<attrs.length; i++) {105if (attrs[i].name().equals("posix:permissions")) {106hasPermissions = true;107break;108}109}110if (!hasPermissions) {111FileAttribute<?>[] copy = new FileAttribute<?>[attrs.length+1];112System.arraycopy(attrs, 0, copy, 0, attrs.length);113attrs = copy;114attrs[attrs.length-1] = (createDirectory) ?115PosixPermissions.dirPermissions :116PosixPermissions.filePermissions;117}118}119}120121// loop generating random names until file or directory can be created122SecurityManager sm = System.getSecurityManager();123for (;;) {124Path f;125try {126f = generatePath(prefix, suffix, dir);127} catch (InvalidPathException e) {128// don't reveal temporary directory location129if (sm != null)130throw new IllegalArgumentException("Invalid prefix or suffix");131throw e;132}133try {134if (createDirectory) {135return Files.createDirectory(f, attrs);136} else {137return Files.createFile(f, attrs);138}139} catch (SecurityException e) {140// don't reveal temporary directory location141if (dir == tmpdir && sm != null)142throw new SecurityException("Unable to create temporary file or directory");143throw e;144} catch (FileAlreadyExistsException e) {145// ignore146}147}148}149150/**151* Creates a temporary file in the given directory, or in in the152* temporary directory if dir is {@code null}.153*/154static Path createTempFile(Path dir,155String prefix,156String suffix,157FileAttribute<?>[] attrs)158throws IOException159{160return create(dir, prefix, suffix, false, attrs);161}162163/**164* Creates a temporary directory in the given directory, or in in the165* temporary directory if dir is {@code null}.166*/167static Path createTempDirectory(Path dir,168String prefix,169FileAttribute<?>[] attrs)170throws IOException171{172return create(dir, prefix, null, true, attrs);173}174}175176177