author | bpb |
Thu, 22 Mar 2018 12:30:47 -0700 | |
changeset 49285 | 4d2e3f5abb48 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
1 |
/* |
49285
4d2e3f5abb48
8194746: (fs) Add equivalents of Paths.get to Path interface
bpb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved. |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
4 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
10 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
15 |
* accompanied this code). |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
16 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
20 |
* |
5506 | 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. |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
24 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
25 |
|
8158 | 26 |
package java.nio.file; |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
27 |
|
8158 | 28 |
import java.util.Set; |
29 |
import java.util.EnumSet; |
|
30 |
import java.security.SecureRandom; |
|
31 |
import java.io.IOException; |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
32 |
import java.nio.file.attribute.FileAttribute; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
33 |
import java.nio.file.attribute.PosixFilePermission; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
34 |
import java.nio.file.attribute.PosixFilePermissions; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
35 |
import static java.nio.file.attribute.PosixFilePermission.*; |
8158 | 36 |
import sun.security.action.GetPropertyAction; |
37 |
||
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
38 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
39 |
/** |
8158 | 40 |
* Helper class to support creation of temporary files and directories with |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
41 |
* initial attributes. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
42 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
43 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
44 |
class TempFileHelper { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
45 |
private TempFileHelper() { } |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
46 |
|
8158 | 47 |
// temporary directory location |
48 |
private static final Path tmpdir = |
|
49285
4d2e3f5abb48
8194746: (fs) Add equivalents of Paths.get to Path interface
bpb
parents:
47216
diff
changeset
|
49 |
Path.of(GetPropertyAction.privilegedGetProperty("java.io.tmpdir")); |
8158 | 50 |
|
51 |
private static final boolean isPosix = |
|
52 |
FileSystems.getDefault().supportedFileAttributeViews().contains("posix"); |
|
53 |
||
54 |
// file name generation, same as java.io.File for now |
|
55 |
private static final SecureRandom random = new SecureRandom(); |
|
56 |
private static Path generatePath(String prefix, String suffix, Path dir) { |
|
57 |
long n = random.nextLong(); |
|
41881
b26ea3cb64c5
8168923: Use unsigned random long in a temp directory name
igerasim
parents:
37781
diff
changeset
|
58 |
String s = prefix + Long.toUnsignedString(n) + suffix; |
b26ea3cb64c5
8168923: Use unsigned random long in a temp directory name
igerasim
parents:
37781
diff
changeset
|
59 |
Path name = dir.getFileSystem().getPath(s); |
8158 | 60 |
// the generated name should be a simple file name |
61 |
if (name.getParent() != null) |
|
62 |
throw new IllegalArgumentException("Invalid prefix or suffix"); |
|
63 |
return dir.resolve(name); |
|
64 |
} |
|
65 |
||
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
66 |
// default file and directory permissions (lazily initialized) |
8158 | 67 |
private static class PosixPermissions { |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
68 |
static final FileAttribute<Set<PosixFilePermission>> filePermissions = |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
69 |
PosixFilePermissions.asFileAttribute(EnumSet.of(OWNER_READ, OWNER_WRITE)); |
8158 | 70 |
static final FileAttribute<Set<PosixFilePermission>> dirPermissions = |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
71 |
PosixFilePermissions.asFileAttribute(EnumSet |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
72 |
.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE)); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
73 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
74 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
75 |
/** |
26960
d280345c2cfb
8059840: (bb) Typo in javadoc for ByteBuffer.wrap()
igerasim
parents:
25859
diff
changeset
|
76 |
* Creates a file or directory in the given directory (or in the |
8158 | 77 |
* temporary directory if dir is {@code null}). |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
78 |
*/ |
8158 | 79 |
private static Path create(Path dir, |
80 |
String prefix, |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
81 |
String suffix, |
8158 | 82 |
boolean createDirectory, |
21655
55f32ae4f920
8028229: Fix more raw types lint warning in core libraries
darcy
parents:
9035
diff
changeset
|
83 |
FileAttribute<?>[] attrs) |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
84 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
85 |
{ |
8158 | 86 |
if (prefix == null) |
87 |
prefix = ""; |
|
88 |
if (suffix == null) |
|
89 |
suffix = (createDirectory) ? "" : ".tmp"; |
|
90 |
if (dir == null) |
|
91 |
dir = tmpdir; |
|
92 |
||
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
93 |
// in POSIX environments use default file and directory permissions |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
94 |
// if initial permissions not given by caller. |
8158 | 95 |
if (isPosix && (dir.getFileSystem() == FileSystems.getDefault())) { |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
96 |
if (attrs.length == 0) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
97 |
// no attributes so use default permissions |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
98 |
attrs = new FileAttribute<?>[1]; |
8158 | 99 |
attrs[0] = (createDirectory) ? PosixPermissions.dirPermissions : |
100 |
PosixPermissions.filePermissions; |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
101 |
} else { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
102 |
// check if posix permissions given; if not use default |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
103 |
boolean hasPermissions = false; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
104 |
for (int i=0; i<attrs.length; i++) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
105 |
if (attrs[i].name().equals("posix:permissions")) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
106 |
hasPermissions = true; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
107 |
break; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
108 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
109 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
110 |
if (!hasPermissions) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
111 |
FileAttribute<?>[] copy = new FileAttribute<?>[attrs.length+1]; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
112 |
System.arraycopy(attrs, 0, copy, 0, attrs.length); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
113 |
attrs = copy; |
8158 | 114 |
attrs[attrs.length-1] = (createDirectory) ? |
115 |
PosixPermissions.dirPermissions : |
|
116 |
PosixPermissions.filePermissions; |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
117 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
118 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
119 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
120 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
121 |
// loop generating random names until file or directory can be created |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
122 |
SecurityManager sm = System.getSecurityManager(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
123 |
for (;;) { |
8158 | 124 |
Path f; |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
125 |
try { |
8158 | 126 |
f = generatePath(prefix, suffix, dir); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
127 |
} catch (InvalidPathException e) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
128 |
// don't reveal temporary directory location |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
129 |
if (sm != null) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
130 |
throw new IllegalArgumentException("Invalid prefix or suffix"); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
131 |
throw e; |
8158 | 132 |
} |
133 |
try { |
|
134 |
if (createDirectory) { |
|
135 |
return Files.createDirectory(f, attrs); |
|
136 |
} else { |
|
137 |
return Files.createFile(f, attrs); |
|
138 |
} |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
139 |
} catch (SecurityException e) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
140 |
// don't reveal temporary directory location |
8158 | 141 |
if (dir == tmpdir && sm != null) |
142 |
throw new SecurityException("Unable to create temporary file or directory"); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
143 |
throw e; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
144 |
} catch (FileAlreadyExistsException e) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
145 |
// ignore |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
146 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
147 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
148 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
149 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
150 |
/** |
26960
d280345c2cfb
8059840: (bb) Typo in javadoc for ByteBuffer.wrap()
igerasim
parents:
25859
diff
changeset
|
151 |
* Creates a temporary file in the given directory, or in the |
8158 | 152 |
* temporary directory if dir is {@code null}. |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
153 |
*/ |
8158 | 154 |
static Path createTempFile(Path dir, |
155 |
String prefix, |
|
156 |
String suffix, |
|
21655
55f32ae4f920
8028229: Fix more raw types lint warning in core libraries
darcy
parents:
9035
diff
changeset
|
157 |
FileAttribute<?>[] attrs) |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
158 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
159 |
{ |
8158 | 160 |
return create(dir, prefix, suffix, false, attrs); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
161 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
162 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
163 |
/** |
26960
d280345c2cfb
8059840: (bb) Typo in javadoc for ByteBuffer.wrap()
igerasim
parents:
25859
diff
changeset
|
164 |
* Creates a temporary directory in the given directory, or in the |
8158 | 165 |
* temporary directory if dir is {@code null}. |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
166 |
*/ |
8158 | 167 |
static Path createTempDirectory(Path dir, |
168 |
String prefix, |
|
21655
55f32ae4f920
8028229: Fix more raw types lint warning in core libraries
darcy
parents:
9035
diff
changeset
|
169 |
FileAttribute<?>[] attrs) |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
170 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
171 |
{ |
8158 | 172 |
return create(dir, prefix, null, true, attrs); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
173 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
174 |
} |