893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage sun.nio.fs;
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport java.nio.file.attribute.*;
893N/Aimport java.io.IOException;
893N/A
893N/Aimport static sun.nio.fs.WindowsConstants.*;
893N/Aimport static sun.nio.fs.WindowsNativeDispatcher.*;
893N/A
893N/A/**
893N/A * Windows implementation of FileStore.
893N/A */
893N/A
893N/Aclass WindowsFileStore
893N/A extends FileStore
893N/A{
893N/A private final String root;
893N/A private final VolumeInformation volInfo;
893N/A private final int volType;
893N/A private final String displayName; // returned by toString
893N/A
893N/A private WindowsFileStore(String root) throws WindowsException {
893N/A assert root.charAt(root.length()-1) == '\\';
893N/A this.root = root;
893N/A this.volInfo = GetVolumeInformation(root);
893N/A this.volType = GetDriveType(root);
893N/A
893N/A // file store "display name" is the volume name if available
893N/A String vol = volInfo.volumeName();
893N/A if (vol.length() > 0) {
893N/A this.displayName = vol;
893N/A } else {
893N/A // TBD - should we map all types? Does this need to be localized?
893N/A this.displayName = (volType == DRIVE_REMOVABLE) ? "Removable Disk" : "";
893N/A }
893N/A }
893N/A
893N/A static WindowsFileStore create(String root, boolean ignoreNotReady)
893N/A throws IOException
893N/A {
893N/A try {
893N/A return new WindowsFileStore(root);
893N/A } catch (WindowsException x) {
893N/A if (ignoreNotReady && x.lastError() == ERROR_NOT_READY)
893N/A return null;
893N/A x.rethrowAsIOException(root);
893N/A return null; // keep compiler happy
893N/A }
893N/A }
893N/A
893N/A static WindowsFileStore create(WindowsPath file) throws IOException {
893N/A try {
893N/A // if the file is a link then GetVolumePathName returns the
893N/A // volume that the link is on so we need to call it with the
893N/A // final target
893N/A String target;
893N/A if (file.getFileSystem().supportsLinks()) {
893N/A target = WindowsLinkSupport.getFinalPath(file, true);
893N/A } else {
893N/A // file must exist
893N/A WindowsFileAttributes.get(file, true);
893N/A target = file.getPathForWin32Calls();
893N/A }
893N/A String root = GetVolumePathName(target);
893N/A return new WindowsFileStore(root);
893N/A } catch (WindowsException x) {
893N/A x.rethrowAsIOException(file);
893N/A return null; // keep compiler happy
893N/A }
893N/A }
893N/A
893N/A VolumeInformation volumeInformation() {
893N/A return volInfo;
893N/A }
893N/A
893N/A int volumeType() {
893N/A return volType;
893N/A }
893N/A
893N/A @Override
893N/A public String name() {
893N/A return volInfo.volumeName(); // "SYSTEM", "DVD-RW", ...
893N/A }
893N/A
893N/A @Override
893N/A public String type() {
893N/A return volInfo.fileSystemName(); // "FAT", "NTFS", ...
893N/A }
893N/A
893N/A @Override
893N/A public boolean isReadOnly() {
893N/A return ((volInfo.flags() & FILE_READ_ONLY_VOLUME) != 0);
893N/A }
893N/A
3471N/A // read the free space info
3471N/A private DiskFreeSpace readDiskFreeSpace() throws IOException {
3471N/A try {
3471N/A return GetDiskFreeSpaceEx(root);
3471N/A } catch (WindowsException x) {
3471N/A x.rethrowAsIOException(root);
3471N/A return null;
3471N/A }
3471N/A }
3471N/A
893N/A @Override
3471N/A public long getTotalSpace() throws IOException {
3471N/A return readDiskFreeSpace().totalNumberOfBytes();
3471N/A }
3471N/A
3471N/A @Override
3471N/A public long getUsableSpace() throws IOException {
3471N/A return readDiskFreeSpace().freeBytesAvailable();
3471N/A }
3471N/A
3471N/A @Override
3471N/A public long getUnallocatedSpace() throws IOException {
3471N/A return readDiskFreeSpace().freeBytesAvailable();
3471N/A }
3471N/A
3471N/A @Override
1319N/A public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> type) {
1319N/A if (type == null)
1319N/A throw new NullPointerException();
893N/A return (V) null;
893N/A }
893N/A
893N/A @Override
1319N/A public Object getAttribute(String attribute) throws IOException {
1319N/A // standard
3471N/A if (attribute.equals("totalSpace"))
3471N/A return getTotalSpace();
3471N/A if (attribute.equals("usableSpace"))
3471N/A return getUsableSpace();
3471N/A if (attribute.equals("unallocatedSpace"))
3471N/A return getUnallocatedSpace();
1319N/A // windows specific for testing purposes
1319N/A if (attribute.equals("volume:vsn"))
1319N/A return volInfo.volumeSerialNumber();
1319N/A if (attribute.equals("volume:isRemovable"))
1319N/A return volType == DRIVE_REMOVABLE;
1319N/A if (attribute.equals("volume:isCdrom"))
1319N/A return volType == DRIVE_CDROM;
1319N/A throw new UnsupportedOperationException("'" + attribute + "' not recognized");
893N/A }
893N/A
893N/A @Override
893N/A public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {
1319N/A if (type == null)
1319N/A throw new NullPointerException();
1609N/A if (type == BasicFileAttributeView.class || type == DosFileAttributeView.class)
893N/A return true;
893N/A if (type == AclFileAttributeView.class || type == FileOwnerAttributeView.class)
893N/A return ((volInfo.flags() & FILE_PERSISTENT_ACLS) != 0);
893N/A if (type == UserDefinedFileAttributeView.class)
893N/A return ((volInfo.flags() & FILE_NAMED_STREAMS) != 0);
893N/A return false;
893N/A }
893N/A
893N/A @Override
893N/A public boolean supportsFileAttributeView(String name) {
893N/A if (name.equals("basic") || name.equals("dos"))
893N/A return true;
893N/A if (name.equals("acl"))
893N/A return supportsFileAttributeView(AclFileAttributeView.class);
893N/A if (name.equals("owner"))
893N/A return supportsFileAttributeView(FileOwnerAttributeView.class);
1319N/A if (name.equals("user"))
893N/A return supportsFileAttributeView(UserDefinedFileAttributeView.class);
893N/A return false;
893N/A }
893N/A
893N/A @Override
893N/A public boolean equals(Object ob) {
893N/A if (ob == this)
893N/A return true;
893N/A if (!(ob instanceof WindowsFileStore))
893N/A return false;
893N/A WindowsFileStore other = (WindowsFileStore)ob;
3640N/A return root.equals(other.root);
893N/A }
893N/A
893N/A @Override
893N/A public int hashCode() {
3640N/A return root.hashCode();
893N/A }
893N/A
893N/A @Override
893N/A public String toString() {
893N/A StringBuilder sb = new StringBuilder(displayName);
893N/A if (sb.length() > 0)
893N/A sb.append(" ");
893N/A sb.append("(");
893N/A // drop trailing slash
893N/A sb.append(root.subSequence(0, root.length()-1));
893N/A sb.append(")");
893N/A return sb.toString();
893N/A }
1319N/A }