Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/unix/classes/sun/nio/fs/UnixFileStore.java
41137 views
1
/*
2
* Copyright (c) 2008, 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 sun.nio.fs;
27
28
import sun.nio.cs.UTF_8;
29
30
import jdk.internal.util.StaticProperty;
31
32
import java.nio.file.*;
33
import java.nio.file.attribute.*;
34
import java.nio.channels.*;
35
import java.util.*;
36
import java.io.IOException;
37
import java.security.AccessController;
38
import java.security.PrivilegedAction;
39
40
/**
41
* Base implementation of FileStore for Unix/like implementations.
42
*/
43
44
abstract class UnixFileStore
45
extends FileStore
46
{
47
// original path of file that identified file system
48
private final UnixPath file;
49
50
// device ID
51
private final long dev;
52
53
// entry in the mount tab
54
private final UnixMountEntry entry;
55
56
// return the device ID where the given file resides
57
private static long devFor(UnixPath file) throws IOException {
58
try {
59
return UnixFileAttributes.get(file, true).dev();
60
} catch (UnixException x) {
61
x.rethrowAsIOException(file);
62
return 0L; // keep compiler happy
63
}
64
}
65
66
UnixFileStore(UnixPath file) throws IOException {
67
this.file = file;
68
this.dev = devFor(file);
69
this.entry = findMountEntry();
70
}
71
72
UnixFileStore(UnixFileSystem fs, UnixMountEntry entry) throws IOException {
73
this.file = new UnixPath(fs, entry.dir());
74
this.dev = (entry.dev() == 0L) ? devFor(this.file) : entry.dev();
75
this.entry = entry;
76
}
77
78
/**
79
* Find the mount entry for the file store
80
*/
81
abstract UnixMountEntry findMountEntry() throws IOException;
82
83
UnixPath file() {
84
return file;
85
}
86
87
long dev() {
88
return dev;
89
}
90
91
UnixMountEntry entry() {
92
return entry;
93
}
94
95
@Override
96
public String name() {
97
return entry.name();
98
}
99
100
@Override
101
public String type() {
102
return entry.fstype();
103
}
104
105
@Override
106
public boolean isReadOnly() {
107
return entry.isReadOnly();
108
}
109
110
// uses statvfs to read the file system information
111
private UnixFileStoreAttributes readAttributes() throws IOException {
112
try {
113
return UnixFileStoreAttributes.get(file);
114
} catch (UnixException x) {
115
x.rethrowAsIOException(file);
116
return null; // keep compile happy
117
}
118
}
119
120
@Override
121
public long getTotalSpace() throws IOException {
122
UnixFileStoreAttributes attrs = readAttributes();
123
try {
124
return Math.multiplyExact(attrs.blockSize(), attrs.totalBlocks());
125
} catch (ArithmeticException ignore) {
126
return Long.MAX_VALUE;
127
}
128
}
129
130
@Override
131
public long getUsableSpace() throws IOException {
132
UnixFileStoreAttributes attrs = readAttributes();
133
try {
134
return Math.multiplyExact(attrs.blockSize(), attrs.availableBlocks());
135
} catch (ArithmeticException ignore) {
136
return Long.MAX_VALUE;
137
}
138
}
139
140
@Override
141
public long getUnallocatedSpace() throws IOException {
142
UnixFileStoreAttributes attrs = readAttributes();
143
try {
144
return Math.multiplyExact(attrs.blockSize(), attrs.freeBlocks());
145
} catch (ArithmeticException ignore) {
146
return Long.MAX_VALUE;
147
}
148
}
149
150
@Override
151
public long getBlockSize() throws IOException {
152
UnixFileStoreAttributes attrs = readAttributes();
153
return attrs.blockSize();
154
}
155
156
@Override
157
public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> view)
158
{
159
if (view == null)
160
throw new NullPointerException();
161
return (V) null;
162
}
163
164
@Override
165
public Object getAttribute(String attribute) throws IOException {
166
if (attribute.equals("totalSpace"))
167
return getTotalSpace();
168
if (attribute.equals("usableSpace"))
169
return getUsableSpace();
170
if (attribute.equals("unallocatedSpace"))
171
return getUnallocatedSpace();
172
throw new UnsupportedOperationException("'" + attribute + "' not recognized");
173
}
174
175
/**
176
* Checks whether extended attributes are enabled on the file system where the given file resides.
177
*
178
* @param path A path pointing to an existing node, such as the file system's root
179
* @return <code>true</code> if enabled, <code>false</code> if disabled or unable to determine
180
*/
181
protected boolean isExtendedAttributesEnabled(UnixPath path) {
182
if (!UnixNativeDispatcher.xattrSupported()) {
183
// avoid I/O if native code doesn't support xattr
184
return false;
185
}
186
187
int fd = -1;
188
try {
189
fd = path.openForAttributeAccess(false);
190
191
// fgetxattr returns size if called with size==0
192
byte[] name = Util.toBytes("user.java");
193
UnixNativeDispatcher.fgetxattr(fd, name, 0L, 0);
194
return true;
195
} catch (UnixException e) {
196
// attribute does not exist
197
if (e.errno() == UnixConstants.XATTR_NOT_FOUND)
198
return true;
199
} finally {
200
UnixNativeDispatcher.close(fd);
201
}
202
return false;
203
}
204
205
@Override
206
public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {
207
if (type == null)
208
throw new NullPointerException();
209
if (type == BasicFileAttributeView.class)
210
return true;
211
if (type == PosixFileAttributeView.class ||
212
type == FileOwnerAttributeView.class)
213
{
214
// lookup fstypes.properties
215
FeatureStatus status = checkIfFeaturePresent("posix");
216
// assume supported if UNKNOWN
217
return (status != FeatureStatus.NOT_PRESENT);
218
}
219
return false;
220
}
221
222
@Override
223
public boolean supportsFileAttributeView(String name) {
224
if (name.equals("basic") || name.equals("unix"))
225
return true;
226
if (name.equals("posix"))
227
return supportsFileAttributeView(PosixFileAttributeView.class);
228
if (name.equals("owner"))
229
return supportsFileAttributeView(FileOwnerAttributeView.class);
230
return false;
231
}
232
233
@Override
234
public boolean equals(Object ob) {
235
if (ob == this)
236
return true;
237
if (!(ob instanceof UnixFileStore))
238
return false;
239
UnixFileStore other = (UnixFileStore)ob;
240
return (this.dev == other.dev) &&
241
Arrays.equals(this.entry.dir(), other.entry.dir()) &&
242
this.entry.name().equals(other.entry.name());
243
}
244
245
@Override
246
public int hashCode() {
247
return (int)(dev ^ (dev >>> 32)) ^ Arrays.hashCode(entry.dir());
248
}
249
250
@Override
251
public String toString() {
252
StringBuilder sb = new StringBuilder(Util.toString(entry.dir()));
253
sb.append(" (");
254
sb.append(entry.name());
255
sb.append(")");
256
return sb.toString();
257
}
258
259
// -- fstypes.properties --
260
261
private static final Object loadLock = new Object();
262
private static volatile Properties props;
263
264
enum FeatureStatus {
265
PRESENT,
266
NOT_PRESENT,
267
UNKNOWN;
268
}
269
270
/**
271
* Returns status to indicate if file system supports a given feature
272
*/
273
@SuppressWarnings("removal")
274
FeatureStatus checkIfFeaturePresent(String feature) {
275
if (props == null) {
276
synchronized (loadLock) {
277
if (props == null) {
278
props = AccessController.doPrivileged(
279
new PrivilegedAction<>() {
280
@Override
281
public Properties run() {
282
return loadProperties();
283
}});
284
}
285
}
286
}
287
288
String value = props.getProperty(type());
289
if (value != null) {
290
String[] values = value.split("\\s");
291
for (String s: values) {
292
s = s.trim().toLowerCase();
293
if (s.equals(feature)) {
294
return FeatureStatus.PRESENT;
295
}
296
if (s.startsWith("no")) {
297
s = s.substring(2);
298
if (s.equals(feature)) {
299
return FeatureStatus.NOT_PRESENT;
300
}
301
}
302
}
303
}
304
return FeatureStatus.UNKNOWN;
305
}
306
307
private static Properties loadProperties() {
308
Properties result = new Properties();
309
String fstypes = StaticProperty.javaHome() + "/lib/fstypes.properties";
310
Path file = Path.of(fstypes);
311
try {
312
try (ReadableByteChannel rbc = Files.newByteChannel(file)) {
313
result.load(Channels.newReader(rbc, UTF_8.INSTANCE));
314
}
315
} catch (IOException x) {
316
}
317
return result;
318
}
319
}
320
321