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