author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 8158 | 77d9c0f1c19f |
child 21655 | 55f32ae4f920 |
permissions | -rw-r--r-- |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8158
diff
changeset
|
2 |
* Copyright (c) 2009, 2011, 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 static java.security.AccessController.*; |
|
32 |
import java.io.IOException; |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
33 |
import java.nio.file.attribute.FileAttribute; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
34 |
import java.nio.file.attribute.PosixFilePermission; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
35 |
import java.nio.file.attribute.PosixFilePermissions; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
36 |
import static java.nio.file.attribute.PosixFilePermission.*; |
8158 | 37 |
import sun.security.action.GetPropertyAction; |
38 |
||
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
39 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
40 |
/** |
8158 | 41 |
* 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
|
42 |
* initial attributes. |
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 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
45 |
class TempFileHelper { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
46 |
private TempFileHelper() { } |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
47 |
|
8158 | 48 |
// temporary directory location |
49 |
private static final Path tmpdir = |
|
50 |
Paths.get(doPrivileged(new GetPropertyAction("java.io.tmpdir"))); |
|
51 |
||
52 |
private static final boolean isPosix = |
|
53 |
FileSystems.getDefault().supportedFileAttributeViews().contains("posix"); |
|
54 |
||
55 |
// file name generation, same as java.io.File for now |
|
56 |
private static final SecureRandom random = new SecureRandom(); |
|
57 |
private static Path generatePath(String prefix, String suffix, Path dir) { |
|
58 |
long n = random.nextLong(); |
|
59 |
n = (n == Long.MIN_VALUE) ? 0 : Math.abs(n); |
|
60 |
Path name = dir.getFileSystem().getPath(prefix + Long.toString(n) + suffix); |
|
61 |
// the generated name should be a simple file name |
|
62 |
if (name.getParent() != null) |
|
63 |
throw new IllegalArgumentException("Invalid prefix or suffix"); |
|
64 |
return dir.resolve(name); |
|
65 |
} |
|
66 |
||
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
67 |
// default file and directory permissions (lazily initialized) |
8158 | 68 |
private static class PosixPermissions { |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
69 |
static final FileAttribute<Set<PosixFilePermission>> filePermissions = |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
70 |
PosixFilePermissions.asFileAttribute(EnumSet.of(OWNER_READ, OWNER_WRITE)); |
8158 | 71 |
static final FileAttribute<Set<PosixFilePermission>> dirPermissions = |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
72 |
PosixFilePermissions.asFileAttribute(EnumSet |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
73 |
.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE)); |
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 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
76 |
/** |
8158 | 77 |
* Creates a file or directory in in the given given directory (or in the |
78 |
* 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
|
79 |
*/ |
8158 | 80 |
private static Path create(Path dir, |
81 |
String prefix, |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
82 |
String suffix, |
8158 | 83 |
boolean createDirectory, |
84 |
FileAttribute[] attrs) |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
85 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
86 |
{ |
8158 | 87 |
if (prefix == null) |
88 |
prefix = ""; |
|
89 |
if (suffix == null) |
|
90 |
suffix = (createDirectory) ? "" : ".tmp"; |
|
91 |
if (dir == null) |
|
92 |
dir = tmpdir; |
|
93 |
||
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
94 |
// 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
|
95 |
// if initial permissions not given by caller. |
8158 | 96 |
if (isPosix && (dir.getFileSystem() == FileSystems.getDefault())) { |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
97 |
if (attrs.length == 0) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
98 |
// no attributes so use default permissions |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
99 |
attrs = new FileAttribute<?>[1]; |
8158 | 100 |
attrs[0] = (createDirectory) ? PosixPermissions.dirPermissions : |
101 |
PosixPermissions.filePermissions; |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
102 |
} else { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
103 |
// 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
|
104 |
boolean hasPermissions = false; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
105 |
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
|
106 |
if (attrs[i].name().equals("posix:permissions")) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
107 |
hasPermissions = true; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
108 |
break; |
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 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
111 |
if (!hasPermissions) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
112 |
FileAttribute<?>[] copy = new FileAttribute<?>[attrs.length+1]; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
113 |
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
|
114 |
attrs = copy; |
8158 | 115 |
attrs[attrs.length-1] = (createDirectory) ? |
116 |
PosixPermissions.dirPermissions : |
|
117 |
PosixPermissions.filePermissions; |
|
3065
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 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
122 |
// 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
|
123 |
SecurityManager sm = System.getSecurityManager(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
124 |
for (;;) { |
8158 | 125 |
Path f; |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
126 |
try { |
8158 | 127 |
f = generatePath(prefix, suffix, dir); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
128 |
} catch (InvalidPathException e) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
129 |
// don't reveal temporary directory location |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
130 |
if (sm != null) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
131 |
throw new IllegalArgumentException("Invalid prefix or suffix"); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
132 |
throw e; |
8158 | 133 |
} |
134 |
try { |
|
135 |
if (createDirectory) { |
|
136 |
return Files.createDirectory(f, attrs); |
|
137 |
} else { |
|
138 |
return Files.createFile(f, attrs); |
|
139 |
} |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
140 |
} catch (SecurityException e) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
141 |
// don't reveal temporary directory location |
8158 | 142 |
if (dir == tmpdir && sm != null) |
143 |
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
|
144 |
throw e; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
145 |
} catch (FileAlreadyExistsException e) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
146 |
// ignore |
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 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
151 |
/** |
8158 | 152 |
* Creates a temporary file in the given directory, or in in the |
153 |
* 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
|
154 |
*/ |
8158 | 155 |
static Path createTempFile(Path dir, |
156 |
String prefix, |
|
157 |
String suffix, |
|
158 |
FileAttribute[] attrs) |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
159 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
160 |
{ |
8158 | 161 |
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
|
162 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
163 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
164 |
/** |
8158 | 165 |
* Creates a temporary directory in the given directory, or in in the |
166 |
* 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
|
167 |
*/ |
8158 | 168 |
static Path createTempDirectory(Path dir, |
169 |
String prefix, |
|
170 |
FileAttribute[] attrs) |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
171 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
172 |
{ |
8158 | 173 |
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
|
174 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
175 |
} |