1 /* 2 * Copyright (c) 2009, 2019, 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 jdk.nio.zipfs; 27 28 import java.io.IOException; 29 import java.nio.file.FileStore; 30 import java.nio.file.FileSystems; 31 import java.nio.file.Files; 32 import java.nio.file.Path; 33 import java.nio.file.attribute.BasicFileAttributeView; 34 import java.nio.file.attribute.FileAttributeView; 35 import java.nio.file.attribute.FileOwnerAttributeView; 36 import java.nio.file.attribute.FileStoreAttributeView; 37 import java.nio.file.attribute.PosixFileAttributeView; 38 39 /** 40 * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal 41 */ 42 class ZipFileStore extends FileStore { 43 44 private final ZipFileSystem zfs; 45 46 ZipFileStore(ZipPath zpath) { 47 this.zfs = zpath.getFileSystem(); 48 } 49 50 @Override 51 public String name() { 52 return zfs.toString() + "/"; 53 } 54 55 @Override 56 public String type() { 57 return "zipfs"; 58 } 59 60 @Override 61 public boolean isReadOnly() { 62 return zfs.isReadOnly(); 63 } 64 65 @Override 66 public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) { 67 return (type == BasicFileAttributeView.class || 68 type == ZipFileAttributeView.class || 69 ((type == FileOwnerAttributeView.class || 70 type == PosixFileAttributeView.class) && zfs.supportPosix)); 71 } 72 73 @Override 74 public boolean supportsFileAttributeView(String name) { 75 return "basic".equals(name) || "zip".equals(name) || 76 (("owner".equals(name) || "posix".equals(name)) && zfs.supportPosix); 77 } 78 79 @Override 80 public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> type) { 81 if (type == null) 82 throw new NullPointerException(); 83 return null; 84 } 85 86 @Override 87 public long getTotalSpace() throws IOException { 88 return new ZipFileStoreAttributes(this).totalSpace(); 89 } 90 91 @Override 92 public long getUsableSpace() throws IOException { 93 return new ZipFileStoreAttributes(this).usableSpace(); 94 } 95 96 @Override 97 public long getUnallocatedSpace() throws IOException { 98 return new ZipFileStoreAttributes(this).unallocatedSpace(); 99 } 100 101 @Override 102 public Object getAttribute(String attribute) throws IOException { 103 if (attribute.equals("totalSpace")) 104 return getTotalSpace(); 105 if (attribute.equals("usableSpace")) 106 return getUsableSpace(); 107 if (attribute.equals("unallocatedSpace")) 108 return getUnallocatedSpace(); 109 throw new UnsupportedOperationException("does not support the given attribute"); 110 } 111 112 private static class ZipFileStoreAttributes { 113 final FileStore fstore; 114 final long size; 115 116 ZipFileStoreAttributes(ZipFileStore fileStore) 117 throws IOException 118 { 119 Path path = FileSystems.getDefault().getPath(fileStore.name()); 120 this.size = Files.size(path); 121 this.fstore = Files.getFileStore(path); 122 } 123 124 long totalSpace() { 125 return size; 126 } 127 128 long usableSpace() throws IOException { 129 if (!fstore.isReadOnly()) 130 return fstore.getUsableSpace(); 131 return 0; 132 } 133 134 long unallocatedSpace() throws IOException { 135 if (!fstore.isReadOnly()) 136 return fstore.getUnallocatedSpace(); 137 return 0; 138 } 139 } 140 }