Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/linux/classes/sun/nio/fs/LinuxFileStore.java
40948 views
1
/*
2
* Copyright (c) 2008, 2018, 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 java.io.IOException;
29
import java.nio.file.attribute.DosFileAttributeView;
30
import java.nio.file.attribute.FileAttributeView;
31
import java.nio.file.attribute.PosixFileAttributeView;
32
import java.nio.file.attribute.UserDefinedFileAttributeView;
33
import java.util.Arrays;
34
import java.util.List;
35
import java.util.regex.Pattern;
36
37
/**
38
* Linux implementation of FileStore
39
*/
40
41
class LinuxFileStore
42
extends UnixFileStore
43
{
44
// used when checking if extended attributes are enabled or not
45
private volatile boolean xattrChecked;
46
private volatile boolean xattrEnabled;
47
48
LinuxFileStore(UnixPath file) throws IOException {
49
super(file);
50
}
51
52
LinuxFileStore(UnixFileSystem fs, UnixMountEntry entry) throws IOException {
53
super(fs, entry);
54
}
55
56
/**
57
* Finds, and returns, the mount entry for the file system where the file
58
* resides.
59
*/
60
@Override
61
UnixMountEntry findMountEntry() throws IOException {
62
LinuxFileSystem fs = (LinuxFileSystem)file().getFileSystem();
63
64
// step 1: get realpath
65
UnixPath path = null;
66
try {
67
byte[] rp = UnixNativeDispatcher.realpath(file());
68
path = new UnixPath(fs, rp);
69
} catch (UnixException x) {
70
x.rethrowAsIOException(file());
71
}
72
73
// step 2: find mount point
74
List<UnixMountEntry> procMountsEntries =
75
fs.getMountEntries("/proc/mounts");
76
UnixPath parent = path.getParent();
77
while (parent != null) {
78
UnixFileAttributes attrs = null;
79
try {
80
attrs = UnixFileAttributes.get(parent, true);
81
} catch (UnixException x) {
82
x.rethrowAsIOException(parent);
83
}
84
if (attrs.dev() != dev()) {
85
// step 3: lookup mounted file systems (use /proc/mounts to
86
// ensure we find the file system even when not in /etc/mtab)
87
byte[] dir = path.asByteArray();
88
for (UnixMountEntry entry : procMountsEntries) {
89
if (Arrays.equals(dir, entry.dir()))
90
return entry;
91
}
92
}
93
path = parent;
94
parent = parent.getParent();
95
}
96
97
// step 3: lookup mounted file systems (use /proc/mounts to
98
// ensure we find the file system even when not in /etc/mtab)
99
byte[] dir = path.asByteArray();
100
for (UnixMountEntry entry : procMountsEntries) {
101
if (Arrays.equals(dir, entry.dir()))
102
return entry;
103
}
104
105
throw new IOException("Mount point not found");
106
}
107
108
// get kernel version as a three element array {major, minor, micro}
109
private static int[] getKernelVersion() {
110
Pattern pattern = Pattern.compile("\\D+");
111
String[] matches = pattern.split(System.getProperty("os.version"));
112
int[] majorMinorMicro = new int[3];
113
int length = Math.min(matches.length, majorMinorMicro.length);
114
for (int i = 0; i < length; i++) {
115
majorMinorMicro[i] = Integer.valueOf(matches[i]);
116
}
117
return majorMinorMicro;
118
}
119
120
@Override
121
public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {
122
// support DosFileAttributeView and UserDefinedAttributeView if extended
123
// attributes enabled
124
if (type == DosFileAttributeView.class ||
125
type == UserDefinedFileAttributeView.class)
126
{
127
// lookup fstypes.properties
128
FeatureStatus status = checkIfFeaturePresent("user_xattr");
129
if (status == FeatureStatus.PRESENT)
130
return true;
131
if (status == FeatureStatus.NOT_PRESENT)
132
return false;
133
134
// if file system is mounted with user_xattr option then assume
135
// extended attributes are enabled
136
if ((entry().hasOption("user_xattr")))
137
return true;
138
139
// check for explicit disabling of extended attributes
140
if (entry().hasOption("nouser_xattr")) {
141
return false;
142
}
143
144
// user_xattr option not present but we special-case ext4 as we
145
// know that extended attributes are enabled by default for
146
// kernel version >= 2.6.39
147
if (entry().fstype().equals("ext4")) {
148
if (!xattrChecked) {
149
// check kernel version
150
int[] kernelVersion = getKernelVersion();
151
xattrEnabled = kernelVersion[0] > 2 ||
152
(kernelVersion[0] == 2 && kernelVersion[1] > 6) ||
153
(kernelVersion[0] == 2 && kernelVersion[1] == 6 &&
154
kernelVersion[2] >= 39);
155
xattrChecked = true;
156
}
157
return xattrEnabled;
158
}
159
160
// not ext4 so probe mount point
161
if (!xattrChecked) {
162
UnixPath dir = new UnixPath(file().getFileSystem(), entry().dir());
163
xattrEnabled = isExtendedAttributesEnabled(dir);
164
xattrChecked = true;
165
}
166
return xattrEnabled;
167
}
168
// POSIX attributes not supported on FAT
169
if (type == PosixFileAttributeView.class && entry().fstype().equals("vfat"))
170
return false;
171
return super.supportsFileAttributeView(type);
172
}
173
174
@Override
175
public boolean supportsFileAttributeView(String name) {
176
if (name.equals("dos"))
177
return supportsFileAttributeView(DosFileAttributeView.class);
178
if (name.equals("user"))
179
return supportsFileAttributeView(UserDefinedFileAttributeView.class);
180
return super.supportsFileAttributeView(name);
181
}
182
}
183
184