Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/fs/WindowsFileStore.java
32288 views
/*1* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.fs;2627import java.nio.file.*;28import java.nio.file.attribute.*;29import java.io.IOException;3031import static sun.nio.fs.WindowsConstants.*;32import static sun.nio.fs.WindowsNativeDispatcher.*;3334/**35* Windows implementation of FileStore.36*/3738class WindowsFileStore39extends FileStore40{41private final String root;42private final VolumeInformation volInfo;43private final int volType;44private final String displayName; // returned by toString4546private WindowsFileStore(String root) throws WindowsException {47assert root.charAt(root.length()-1) == '\\';48this.root = root;49this.volInfo = GetVolumeInformation(root);50this.volType = GetDriveType(root);5152// file store "display name" is the volume name if available53String vol = volInfo.volumeName();54if (vol.length() > 0) {55this.displayName = vol;56} else {57// TBD - should we map all types? Does this need to be localized?58this.displayName = (volType == DRIVE_REMOVABLE) ? "Removable Disk" : "";59}60}6162static WindowsFileStore create(String root, boolean ignoreNotReady)63throws IOException64{65try {66return new WindowsFileStore(root);67} catch (WindowsException x) {68if (ignoreNotReady && x.lastError() == ERROR_NOT_READY)69return null;70x.rethrowAsIOException(root);71return null; // keep compiler happy72}73}7475static WindowsFileStore create(WindowsPath file) throws IOException {76try {77// if the file is a link then GetVolumePathName returns the78// volume that the link is on so we need to call it with the79// final target80String target;81if (file.getFileSystem().supportsLinks()) {82target = WindowsLinkSupport.getFinalPath(file, true);83} else {84// file must exist85WindowsFileAttributes.get(file, true);86target = file.getPathForWin32Calls();87}88try {89return createFromPath(target);90} catch (WindowsException e) {91if (e.lastError() != ERROR_DIR_NOT_ROOT)92throw e;93target = WindowsLinkSupport.getFinalPath(file);94if (target == null)95throw new FileSystemException(file.getPathForExceptionMessage(),96null, "Couldn't resolve path");97return createFromPath(target);98}99} catch (WindowsException x) {100x.rethrowAsIOException(file);101return null; // keep compiler happy102}103}104105private static WindowsFileStore createFromPath(String target) throws WindowsException {106String root = GetVolumePathName(target);107return new WindowsFileStore(root);108}109110VolumeInformation volumeInformation() {111return volInfo;112}113114int volumeType() {115return volType;116}117118@Override119public String name() {120return volInfo.volumeName(); // "SYSTEM", "DVD-RW", ...121}122123@Override124public String type() {125return volInfo.fileSystemName(); // "FAT", "NTFS", ...126}127128@Override129public boolean isReadOnly() {130return ((volInfo.flags() & FILE_READ_ONLY_VOLUME) != 0);131}132133// read the free space info134private DiskFreeSpace readDiskFreeSpace() throws IOException {135try {136return GetDiskFreeSpaceEx(root);137} catch (WindowsException x) {138x.rethrowAsIOException(root);139return null;140}141}142143@Override144public long getTotalSpace() throws IOException {145return readDiskFreeSpace().totalNumberOfBytes();146}147148@Override149public long getUsableSpace() throws IOException {150return readDiskFreeSpace().freeBytesAvailable();151}152153@Override154public long getUnallocatedSpace() throws IOException {155return readDiskFreeSpace().freeBytesAvailable();156}157158@Override159public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> type) {160if (type == null)161throw new NullPointerException();162return (V) null;163}164165@Override166public Object getAttribute(String attribute) throws IOException {167// standard168if (attribute.equals("totalSpace"))169return getTotalSpace();170if (attribute.equals("usableSpace"))171return getUsableSpace();172if (attribute.equals("unallocatedSpace"))173return getUnallocatedSpace();174// windows specific for testing purposes175if (attribute.equals("volume:vsn"))176return volInfo.volumeSerialNumber();177if (attribute.equals("volume:isRemovable"))178return volType == DRIVE_REMOVABLE;179if (attribute.equals("volume:isCdrom"))180return volType == DRIVE_CDROM;181throw new UnsupportedOperationException("'" + attribute + "' not recognized");182}183184@Override185public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {186if (type == null)187throw new NullPointerException();188if (type == BasicFileAttributeView.class || type == DosFileAttributeView.class)189return true;190if (type == AclFileAttributeView.class || type == FileOwnerAttributeView.class)191return ((volInfo.flags() & FILE_PERSISTENT_ACLS) != 0);192if (type == UserDefinedFileAttributeView.class)193return ((volInfo.flags() & FILE_NAMED_STREAMS) != 0);194return false;195}196197@Override198public boolean supportsFileAttributeView(String name) {199if (name.equals("basic") || name.equals("dos"))200return true;201if (name.equals("acl"))202return supportsFileAttributeView(AclFileAttributeView.class);203if (name.equals("owner"))204return supportsFileAttributeView(FileOwnerAttributeView.class);205if (name.equals("user"))206return supportsFileAttributeView(UserDefinedFileAttributeView.class);207return false;208}209210@Override211public boolean equals(Object ob) {212if (ob == this)213return true;214if (!(ob instanceof WindowsFileStore))215return false;216WindowsFileStore other = (WindowsFileStore)ob;217return root.equals(other.root);218}219220@Override221public int hashCode() {222return root.hashCode();223}224225@Override226public String toString() {227StringBuilder sb = new StringBuilder(displayName);228if (sb.length() > 0)229sb.append(" ");230sb.append("(");231// drop trailing slash232sb.append(root.subSequence(0, root.length()-1));233sb.append(")");234return sb.toString();235}236}237238239