Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/nio/file/TempFileHelper.java
41159 views
1
/*
2
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
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.nio.file;
27
28
import java.util.Set;
29
import java.util.EnumSet;
30
import java.security.SecureRandom;
31
import java.io.IOException;
32
import java.nio.file.attribute.FileAttribute;
33
import java.nio.file.attribute.PosixFilePermission;
34
import java.nio.file.attribute.PosixFilePermissions;
35
import static java.nio.file.attribute.PosixFilePermission.*;
36
import jdk.internal.util.StaticProperty;
37
38
/**
39
* Helper class to support creation of temporary files and directories with
40
* initial attributes.
41
*/
42
43
class TempFileHelper {
44
private TempFileHelper() { }
45
46
// temporary directory location
47
private static final Path tmpdir = Path.of(StaticProperty.javaIoTmpDir());
48
49
private static final boolean isPosix =
50
FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
51
52
// file name generation, same as java.io.File for now
53
private static final SecureRandom random = new SecureRandom();
54
private static Path generatePath(String prefix, String suffix, Path dir) {
55
long n = random.nextLong();
56
String s = prefix + Long.toUnsignedString(n) + suffix;
57
Path name = dir.getFileSystem().getPath(s);
58
// the generated name should be a simple file name
59
if (name.getParent() != null)
60
throw new IllegalArgumentException("Invalid prefix or suffix");
61
return dir.resolve(name);
62
}
63
64
// default file and directory permissions (lazily initialized)
65
private static class PosixPermissions {
66
static final FileAttribute<Set<PosixFilePermission>> filePermissions =
67
PosixFilePermissions.asFileAttribute(EnumSet.of(OWNER_READ, OWNER_WRITE));
68
static final FileAttribute<Set<PosixFilePermission>> dirPermissions =
69
PosixFilePermissions.asFileAttribute(EnumSet
70
.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE));
71
}
72
73
/**
74
* Creates a file or directory in the given directory (or in the
75
* temporary directory if dir is {@code null}).
76
*/
77
private static Path create(Path dir,
78
String prefix,
79
String suffix,
80
boolean createDirectory,
81
FileAttribute<?>[] attrs)
82
throws IOException
83
{
84
if (prefix == null)
85
prefix = "";
86
if (suffix == null)
87
suffix = (createDirectory) ? "" : ".tmp";
88
if (dir == null)
89
dir = tmpdir;
90
91
// in POSIX environments use default file and directory permissions
92
// if initial permissions not given by caller.
93
if (isPosix && (dir.getFileSystem() == FileSystems.getDefault())) {
94
if (attrs.length == 0) {
95
// no attributes so use default permissions
96
attrs = new FileAttribute<?>[1];
97
attrs[0] = (createDirectory) ? PosixPermissions.dirPermissions :
98
PosixPermissions.filePermissions;
99
} else {
100
// check if posix permissions given; if not use default
101
boolean hasPermissions = false;
102
for (int i=0; i<attrs.length; i++) {
103
if (attrs[i].name().equals("posix:permissions")) {
104
hasPermissions = true;
105
break;
106
}
107
}
108
if (!hasPermissions) {
109
FileAttribute<?>[] copy = new FileAttribute<?>[attrs.length+1];
110
System.arraycopy(attrs, 0, copy, 0, attrs.length);
111
attrs = copy;
112
attrs[attrs.length-1] = (createDirectory) ?
113
PosixPermissions.dirPermissions :
114
PosixPermissions.filePermissions;
115
}
116
}
117
}
118
119
// loop generating random names until file or directory can be created
120
@SuppressWarnings("removal")
121
SecurityManager sm = System.getSecurityManager();
122
for (;;) {
123
Path f;
124
try {
125
f = generatePath(prefix, suffix, dir);
126
} catch (InvalidPathException e) {
127
// don't reveal temporary directory location
128
if (sm != null)
129
throw new IllegalArgumentException("Invalid prefix or suffix");
130
throw e;
131
}
132
try {
133
if (createDirectory) {
134
return Files.createDirectory(f, attrs);
135
} else {
136
return Files.createFile(f, attrs);
137
}
138
} catch (SecurityException e) {
139
// don't reveal temporary directory location
140
if (dir == tmpdir && sm != null)
141
throw new SecurityException("Unable to create temporary file or directory");
142
throw e;
143
} catch (FileAlreadyExistsException e) {
144
// ignore
145
}
146
}
147
}
148
149
/**
150
* Creates a temporary file in the given directory, or in the
151
* temporary directory if dir is {@code null}.
152
*/
153
static Path createTempFile(Path dir,
154
String prefix,
155
String suffix,
156
FileAttribute<?>[] attrs)
157
throws IOException
158
{
159
return create(dir, prefix, suffix, false, attrs);
160
}
161
162
/**
163
* Creates a temporary directory in the given directory, or in the
164
* temporary directory if dir is {@code null}.
165
*/
166
static Path createTempDirectory(Path dir,
167
String prefix,
168
FileAttribute<?>[] attrs)
169
throws IOException
170
{
171
return create(dir, prefix, null, true, attrs);
172
}
173
}
174
175