Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/windows/classes/sun/nio/fs/WindowsFileStore.java
41139 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 java.nio.file.FileStore;
29
import java.nio.file.FileSystemException;
30
import java.nio.file.attribute.AclFileAttributeView;
31
import java.nio.file.attribute.BasicFileAttributeView;
32
import java.nio.file.attribute.DosFileAttributeView;
33
import java.nio.file.attribute.FileAttributeView;
34
import java.nio.file.attribute.FileOwnerAttributeView;
35
import java.nio.file.attribute.FileStoreAttributeView;
36
import java.nio.file.attribute.UserDefinedFileAttributeView;
37
import java.io.IOException;
38
import java.util.Locale;
39
40
import static sun.nio.fs.WindowsConstants.*;
41
import static sun.nio.fs.WindowsNativeDispatcher.*;
42
43
/**
44
* Windows implementation of FileStore.
45
*/
46
47
class WindowsFileStore
48
extends FileStore
49
{
50
private final String root;
51
private final VolumeInformation volInfo;
52
private final int volType;
53
private final String displayName; // returned by toString
54
55
private int hashCode;
56
57
private WindowsFileStore(String root) throws WindowsException {
58
assert root.charAt(root.length()-1) == '\\';
59
this.root = root;
60
this.volInfo = GetVolumeInformation(root);
61
this.volType = GetDriveType(root);
62
63
// file store "display name" is the volume name if available
64
String vol = volInfo.volumeName();
65
if (!vol.isEmpty()) {
66
this.displayName = vol;
67
} else {
68
// TBD - should we map all types? Does this need to be localized?
69
this.displayName = (volType == DRIVE_REMOVABLE) ? "Removable Disk" : "";
70
}
71
}
72
73
static WindowsFileStore create(String root, boolean ignoreNotReady)
74
throws IOException
75
{
76
try {
77
return new WindowsFileStore(root);
78
} catch (WindowsException x) {
79
if (ignoreNotReady && x.lastError() == ERROR_NOT_READY)
80
return null;
81
x.rethrowAsIOException(root);
82
return null; // keep compiler happy
83
}
84
}
85
86
static WindowsFileStore create(WindowsPath file) throws IOException {
87
try {
88
// if the file is a link then GetVolumePathName returns the
89
// volume that the link is on so we need to call it with the
90
// final target
91
String target = WindowsLinkSupport.getFinalPath(file, true);
92
try {
93
return createFromPath(target);
94
} catch (WindowsException e) {
95
// GetVolumePathName might return the following error codes
96
// when the drives were created using `subst`.
97
// Try expanding the path again in such cases.
98
if (e.lastError() != ERROR_DIR_NOT_ROOT &&
99
e.lastError() != ERROR_INVALID_PARAMETER &&
100
e.lastError() != ERROR_DIRECTORY)
101
throw e;
102
target = WindowsLinkSupport.getFinalPath(file);
103
if (target == null)
104
throw new FileSystemException(file.getPathForExceptionMessage(),
105
null, "Couldn't resolve path");
106
return createFromPath(target);
107
}
108
} catch (WindowsException x) {
109
x.rethrowAsIOException(file);
110
return null; // keep compiler happy
111
}
112
}
113
114
private static WindowsFileStore createFromPath(String target) throws WindowsException {
115
String root = GetVolumePathName(target);
116
return new WindowsFileStore(root);
117
}
118
119
VolumeInformation volumeInformation() {
120
return volInfo;
121
}
122
123
int volumeType() {
124
return volType;
125
}
126
127
@Override
128
public String name() {
129
return volInfo.volumeName(); // "SYSTEM", "DVD-RW", ...
130
}
131
132
@Override
133
public String type() {
134
return volInfo.fileSystemName(); // "FAT", "NTFS", ...
135
}
136
137
@Override
138
public boolean isReadOnly() {
139
return ((volInfo.flags() & FILE_READ_ONLY_VOLUME) != 0);
140
}
141
142
// read the free space info
143
private DiskFreeSpace readDiskFreeSpaceEx() throws IOException {
144
try {
145
return GetDiskFreeSpaceEx(root);
146
} catch (WindowsException x) {
147
x.rethrowAsIOException(root);
148
return null;
149
}
150
}
151
152
private DiskFreeSpace readDiskFreeSpace() throws IOException {
153
try {
154
return GetDiskFreeSpace(root);
155
} catch (WindowsException x) {
156
x.rethrowAsIOException(root);
157
return null;
158
}
159
}
160
161
@Override
162
public long getTotalSpace() throws IOException {
163
long space = readDiskFreeSpaceEx().totalNumberOfBytes();
164
return space >= 0 ? space : Long.MAX_VALUE;
165
}
166
167
@Override
168
public long getUsableSpace() throws IOException {
169
long space = readDiskFreeSpaceEx().freeBytesAvailable();
170
return space >= 0 ? space : Long.MAX_VALUE;
171
}
172
173
@Override
174
public long getUnallocatedSpace() throws IOException {
175
long space = readDiskFreeSpaceEx().freeBytesAvailable();
176
return space >= 0 ? space : Long.MAX_VALUE;
177
}
178
179
@Override
180
public long getBlockSize() throws IOException {
181
return readDiskFreeSpace().bytesPerSector();
182
}
183
184
@Override
185
public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> type) {
186
if (type == null)
187
throw new NullPointerException();
188
return (V) null;
189
}
190
191
@Override
192
public Object getAttribute(String attribute) throws IOException {
193
// standard
194
if (attribute.equals("totalSpace"))
195
return getTotalSpace();
196
if (attribute.equals("usableSpace"))
197
return getUsableSpace();
198
if (attribute.equals("unallocatedSpace"))
199
return getUnallocatedSpace();
200
if (attribute.equals("bytesPerSector"))
201
return getBlockSize();
202
// windows specific for testing purposes
203
if (attribute.equals("volume:vsn"))
204
return volInfo.volumeSerialNumber();
205
if (attribute.equals("volume:isRemovable"))
206
return volType == DRIVE_REMOVABLE;
207
if (attribute.equals("volume:isCdrom"))
208
return volType == DRIVE_CDROM;
209
throw new UnsupportedOperationException("'" + attribute + "' not recognized");
210
}
211
212
@Override
213
public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {
214
if (type == null)
215
throw new NullPointerException();
216
if (type == BasicFileAttributeView.class || type == DosFileAttributeView.class)
217
return true;
218
if (type == AclFileAttributeView.class || type == FileOwnerAttributeView.class)
219
return ((volInfo.flags() & FILE_PERSISTENT_ACLS) != 0);
220
if (type == UserDefinedFileAttributeView.class)
221
return ((volInfo.flags() & FILE_NAMED_STREAMS) != 0);
222
return false;
223
}
224
225
@Override
226
public boolean supportsFileAttributeView(String name) {
227
if (name.equals("basic") || name.equals("dos"))
228
return true;
229
if (name.equals("acl"))
230
return supportsFileAttributeView(AclFileAttributeView.class);
231
if (name.equals("owner"))
232
return supportsFileAttributeView(FileOwnerAttributeView.class);
233
if (name.equals("user"))
234
return supportsFileAttributeView(UserDefinedFileAttributeView.class);
235
return false;
236
}
237
238
@Override
239
public boolean equals(Object ob) {
240
if (ob == this)
241
return true;
242
if (ob instanceof WindowsFileStore other) {
243
if (root.equals(other.root))
244
return true;
245
if (volType == DRIVE_FIXED && other.volumeType() == DRIVE_FIXED)
246
return root.equalsIgnoreCase(other.root);
247
}
248
return false;
249
}
250
251
@Override
252
public int hashCode() {
253
int hc = hashCode;
254
if (hc == 0) {
255
hc = (volType == DRIVE_FIXED) ?
256
root.toLowerCase(Locale.ROOT).hashCode() : root.hashCode();
257
hashCode = hc;
258
}
259
return hc;
260
}
261
262
@Override
263
public String toString() {
264
StringBuilder sb = new StringBuilder(displayName);
265
if (sb.length() > 0)
266
sb.append(" ");
267
sb.append("(");
268
// drop trailing slash
269
sb.append(root.subSequence(0, root.length()-1));
270
sb.append(")");
271
return sb.toString();
272
}
273
}
274
275