Path: blob/master/src/java.base/windows/classes/sun/nio/fs/WindowsFileStore.java
41139 views
/*1* Copyright (c) 2008, 2021, 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.FileStore;28import java.nio.file.FileSystemException;29import java.nio.file.attribute.AclFileAttributeView;30import java.nio.file.attribute.BasicFileAttributeView;31import java.nio.file.attribute.DosFileAttributeView;32import java.nio.file.attribute.FileAttributeView;33import java.nio.file.attribute.FileOwnerAttributeView;34import java.nio.file.attribute.FileStoreAttributeView;35import java.nio.file.attribute.UserDefinedFileAttributeView;36import java.io.IOException;37import java.util.Locale;3839import static sun.nio.fs.WindowsConstants.*;40import static sun.nio.fs.WindowsNativeDispatcher.*;4142/**43* Windows implementation of FileStore.44*/4546class WindowsFileStore47extends FileStore48{49private final String root;50private final VolumeInformation volInfo;51private final int volType;52private final String displayName; // returned by toString5354private int hashCode;5556private WindowsFileStore(String root) throws WindowsException {57assert root.charAt(root.length()-1) == '\\';58this.root = root;59this.volInfo = GetVolumeInformation(root);60this.volType = GetDriveType(root);6162// file store "display name" is the volume name if available63String vol = volInfo.volumeName();64if (!vol.isEmpty()) {65this.displayName = vol;66} else {67// TBD - should we map all types? Does this need to be localized?68this.displayName = (volType == DRIVE_REMOVABLE) ? "Removable Disk" : "";69}70}7172static WindowsFileStore create(String root, boolean ignoreNotReady)73throws IOException74{75try {76return new WindowsFileStore(root);77} catch (WindowsException x) {78if (ignoreNotReady && x.lastError() == ERROR_NOT_READY)79return null;80x.rethrowAsIOException(root);81return null; // keep compiler happy82}83}8485static WindowsFileStore create(WindowsPath file) throws IOException {86try {87// if the file is a link then GetVolumePathName returns the88// volume that the link is on so we need to call it with the89// final target90String target = WindowsLinkSupport.getFinalPath(file, true);91try {92return createFromPath(target);93} catch (WindowsException e) {94// GetVolumePathName might return the following error codes95// when the drives were created using `subst`.96// Try expanding the path again in such cases.97if (e.lastError() != ERROR_DIR_NOT_ROOT &&98e.lastError() != ERROR_INVALID_PARAMETER &&99e.lastError() != ERROR_DIRECTORY)100throw e;101target = WindowsLinkSupport.getFinalPath(file);102if (target == null)103throw new FileSystemException(file.getPathForExceptionMessage(),104null, "Couldn't resolve path");105return createFromPath(target);106}107} catch (WindowsException x) {108x.rethrowAsIOException(file);109return null; // keep compiler happy110}111}112113private static WindowsFileStore createFromPath(String target) throws WindowsException {114String root = GetVolumePathName(target);115return new WindowsFileStore(root);116}117118VolumeInformation volumeInformation() {119return volInfo;120}121122int volumeType() {123return volType;124}125126@Override127public String name() {128return volInfo.volumeName(); // "SYSTEM", "DVD-RW", ...129}130131@Override132public String type() {133return volInfo.fileSystemName(); // "FAT", "NTFS", ...134}135136@Override137public boolean isReadOnly() {138return ((volInfo.flags() & FILE_READ_ONLY_VOLUME) != 0);139}140141// read the free space info142private DiskFreeSpace readDiskFreeSpaceEx() throws IOException {143try {144return GetDiskFreeSpaceEx(root);145} catch (WindowsException x) {146x.rethrowAsIOException(root);147return null;148}149}150151private DiskFreeSpace readDiskFreeSpace() throws IOException {152try {153return GetDiskFreeSpace(root);154} catch (WindowsException x) {155x.rethrowAsIOException(root);156return null;157}158}159160@Override161public long getTotalSpace() throws IOException {162long space = readDiskFreeSpaceEx().totalNumberOfBytes();163return space >= 0 ? space : Long.MAX_VALUE;164}165166@Override167public long getUsableSpace() throws IOException {168long space = readDiskFreeSpaceEx().freeBytesAvailable();169return space >= 0 ? space : Long.MAX_VALUE;170}171172@Override173public long getUnallocatedSpace() throws IOException {174long space = readDiskFreeSpaceEx().freeBytesAvailable();175return space >= 0 ? space : Long.MAX_VALUE;176}177178@Override179public long getBlockSize() throws IOException {180return readDiskFreeSpace().bytesPerSector();181}182183@Override184public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> type) {185if (type == null)186throw new NullPointerException();187return (V) null;188}189190@Override191public Object getAttribute(String attribute) throws IOException {192// standard193if (attribute.equals("totalSpace"))194return getTotalSpace();195if (attribute.equals("usableSpace"))196return getUsableSpace();197if (attribute.equals("unallocatedSpace"))198return getUnallocatedSpace();199if (attribute.equals("bytesPerSector"))200return getBlockSize();201// windows specific for testing purposes202if (attribute.equals("volume:vsn"))203return volInfo.volumeSerialNumber();204if (attribute.equals("volume:isRemovable"))205return volType == DRIVE_REMOVABLE;206if (attribute.equals("volume:isCdrom"))207return volType == DRIVE_CDROM;208throw new UnsupportedOperationException("'" + attribute + "' not recognized");209}210211@Override212public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {213if (type == null)214throw new NullPointerException();215if (type == BasicFileAttributeView.class || type == DosFileAttributeView.class)216return true;217if (type == AclFileAttributeView.class || type == FileOwnerAttributeView.class)218return ((volInfo.flags() & FILE_PERSISTENT_ACLS) != 0);219if (type == UserDefinedFileAttributeView.class)220return ((volInfo.flags() & FILE_NAMED_STREAMS) != 0);221return false;222}223224@Override225public boolean supportsFileAttributeView(String name) {226if (name.equals("basic") || name.equals("dos"))227return true;228if (name.equals("acl"))229return supportsFileAttributeView(AclFileAttributeView.class);230if (name.equals("owner"))231return supportsFileAttributeView(FileOwnerAttributeView.class);232if (name.equals("user"))233return supportsFileAttributeView(UserDefinedFileAttributeView.class);234return false;235}236237@Override238public boolean equals(Object ob) {239if (ob == this)240return true;241if (ob instanceof WindowsFileStore other) {242if (root.equals(other.root))243return true;244if (volType == DRIVE_FIXED && other.volumeType() == DRIVE_FIXED)245return root.equalsIgnoreCase(other.root);246}247return false;248}249250@Override251public int hashCode() {252int hc = hashCode;253if (hc == 0) {254hc = (volType == DRIVE_FIXED) ?255root.toLowerCase(Locale.ROOT).hashCode() : root.hashCode();256hashCode = hc;257}258return hc;259}260261@Override262public String toString() {263StringBuilder sb = new StringBuilder(displayName);264if (sb.length() > 0)265sb.append(" ");266sb.append("(");267// drop trailing slash268sb.append(root.subSequence(0, root.length()-1));269sb.append(")");270return sb.toString();271}272}273274275