1 /*
   2  * Copyright (c) 2008, 2009, 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             String root = GetVolumePathName(target);
  90             return new WindowsFileStore(root);
  91         } catch (WindowsException x) {
  92             x.rethrowAsIOException(file);
  93             return null; // keep compiler happy
  94         }
  95     }
  96 
  97     VolumeInformation volumeInformation() {
  98         return volInfo;
  99     }
 100 
 101     int volumeType() {
 102         return volType;
 103     }
 104 
 105     @Override
 106     public String name() {
 107         return volInfo.volumeName();   // "SYSTEM", "DVD-RW", ...
 108     }
 109 
 110     @Override
 111     public String type() {
 112         return volInfo.fileSystemName();  // "FAT", "NTFS", ...
 113     }
 114 
 115     @Override
 116     public boolean isReadOnly() {
 117         return ((volInfo.flags() & FILE_READ_ONLY_VOLUME) != 0);
 118     }
 119 
 120     // read the free space info
 121     private DiskFreeSpace readDiskFreeSpace() throws IOException {
 122         try {
 123             return GetDiskFreeSpaceEx(root);
 124         } catch (WindowsException x) {
 125             x.rethrowAsIOException(root);
 126             return null;
 127         }
 128     }
 129 
 130     @Override
 131     public long getTotalSpace() throws IOException {
 132         return readDiskFreeSpace().totalNumberOfBytes();
 133     }
 134 
 135     @Override
 136     public long getUsableSpace() throws IOException {
 137         return readDiskFreeSpace().freeBytesAvailable();
 138     }
 139 
 140     @Override
 141     public long getUnallocatedSpace() throws IOException {
 142         return readDiskFreeSpace().freeBytesAvailable();
 143     }
 144 
 145     @Override
 146     public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> type) {
 147         if (type == null)
 148             throw new NullPointerException();
 149         return (V) null;
 150     }
 151 
 152     @Override
 153     public Object getAttribute(String attribute) throws IOException {
 154         // standard
 155         if (attribute.equals("totalSpace"))
 156             return getTotalSpace();
 157         if (attribute.equals("usableSpace"))
 158             return getUsableSpace();
 159         if (attribute.equals("unallocatedSpace"))
 160             return getUnallocatedSpace();
 161         // windows specific for testing purposes
 162         if (attribute.equals("volume:vsn"))
 163             return volInfo.volumeSerialNumber();
 164         if (attribute.equals("volume:isRemovable"))
 165             return volType == DRIVE_REMOVABLE;
 166         if (attribute.equals("volume:isCdrom"))
 167             return volType == DRIVE_CDROM;
 168         throw new UnsupportedOperationException("'" + attribute + "' not recognized");
 169     }
 170 
 171     @Override
 172     public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {
 173         if (type == null)
 174             throw new NullPointerException();
 175         if (type == BasicFileAttributeView.class || type == DosFileAttributeView.class)
 176             return true;
 177         if (type == AclFileAttributeView.class || type == FileOwnerAttributeView.class)
 178             return ((volInfo.flags() & FILE_PERSISTENT_ACLS) != 0);
 179         if (type == UserDefinedFileAttributeView.class)
 180             return ((volInfo.flags() & FILE_NAMED_STREAMS) != 0);
 181         return false;
 182     }
 183 
 184     @Override
 185     public boolean supportsFileAttributeView(String name) {
 186         if (name.equals("basic") || name.equals("dos"))
 187             return true;
 188         if (name.equals("acl"))
 189             return supportsFileAttributeView(AclFileAttributeView.class);
 190         if (name.equals("owner"))
 191             return supportsFileAttributeView(FileOwnerAttributeView.class);
 192         if (name.equals("user"))
 193             return supportsFileAttributeView(UserDefinedFileAttributeView.class);
 194         return false;
 195     }
 196 
 197     @Override
 198     public boolean equals(Object ob) {
 199         if (ob == this)
 200             return true;
 201         if (!(ob instanceof WindowsFileStore))
 202             return false;
 203         WindowsFileStore other = (WindowsFileStore)ob;
 204         return root.equals(other.root);
 205     }
 206 
 207     @Override
 208     public int hashCode() {
 209         return root.hashCode();
 210     }
 211 
 212     @Override
 213     public String toString() {
 214         StringBuilder sb = new StringBuilder(displayName);
 215         if (sb.length() > 0)
 216             sb.append(" ");
 217         sb.append("(");
 218         // drop trailing slash
 219         sb.append(root.subSequence(0, root.length()-1));
 220         sb.append(")");
 221         return sb.toString();
 222     }
 223  }