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