1 /*
   2  * Copyright (c) 2014, 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.internal.jrtfs;
  27 
  28 import java.nio.file.LinkOption;
  29 import java.nio.file.attribute.*;
  30 import java.io.IOException;
  31 import java.util.LinkedHashMap;
  32 import java.util.Map;
  33 
  34 final class JrtFileAttributeView implements BasicFileAttributeView
  35 {
  36     private static enum AttrID {
  37         size,
  38         creationTime,
  39         lastAccessTime,
  40         lastModifiedTime,
  41         isDirectory,
  42         isRegularFile,
  43         isSymbolicLink,
  44         isOther,
  45         fileKey,
  46         compressedSize,
  47         extension
  48     };
  49 
  50     private final JrtPath path;
  51     private final boolean isJrtView;
  52     private final LinkOption[] options;
  53 
  54     private JrtFileAttributeView(JrtPath path, boolean isJrtView, LinkOption... options) {
  55         this.path = path;
  56         this.isJrtView = isJrtView;
  57         this.options = options;
  58     }
  59 
  60     @SuppressWarnings("unchecked") // Cast to V
  61     static <V extends FileAttributeView> V get(JrtPath path, Class<V> type, LinkOption... options) {
  62         if (type == null)
  63             throw new NullPointerException();
  64         if (type == BasicFileAttributeView.class)
  65             return (V)new JrtFileAttributeView(path, false, options);
  66         if (type == JrtFileAttributeView.class)
  67             return (V)new JrtFileAttributeView(path, true, options);
  68         return null;
  69     }
  70 
  71     static JrtFileAttributeView get(JrtPath path, String type, LinkOption... options) {
  72         if (type == null)
  73             throw new NullPointerException();
  74         if (type.equals("basic"))
  75             return new JrtFileAttributeView(path, false, options);
  76         if (type.equals("jjrt"))
  77             return new JrtFileAttributeView(path, true, options);
  78         return null;
  79     }
  80 
  81     @Override
  82     public String name() {
  83         return isJrtView ? "jjrt" : "basic";
  84     }
  85 
  86     @Override
  87     public JrtFileAttributes readAttributes() throws IOException
  88     {
  89         return path.getAttributes(options);
  90     }
  91 
  92     @Override
  93     public void setTimes(FileTime lastModifiedTime,
  94                          FileTime lastAccessTime,
  95                          FileTime createTime)
  96         throws IOException
  97     {
  98         path.setTimes(lastModifiedTime, lastAccessTime, createTime);
  99     }
 100 
 101     void setAttribute(String attribute, Object value)
 102         throws IOException
 103     {
 104         try {
 105             if (AttrID.valueOf(attribute) == AttrID.lastModifiedTime)
 106                 setTimes ((FileTime)value, null, null);
 107             if (AttrID.valueOf(attribute) == AttrID.lastAccessTime)
 108                 setTimes (null, (FileTime)value, null);
 109             if (AttrID.valueOf(attribute) == AttrID.creationTime)
 110                 setTimes (null, null, (FileTime)value);
 111             return;
 112         } catch (IllegalArgumentException x) {}
 113         throw new UnsupportedOperationException("'" + attribute +
 114             "' is unknown or read-only attribute");
 115     }
 116 
 117     Map<String, Object> readAttributes(String attributes)
 118         throws IOException
 119     {
 120         JrtFileAttributes jrtfas = readAttributes();
 121         LinkedHashMap<String, Object> map = new LinkedHashMap<>();
 122         if ("*".equals(attributes)) {
 123             for (AttrID id : AttrID.values()) {
 124                 try {
 125                     map.put(id.name(), attribute(id, jrtfas));
 126                 } catch (IllegalArgumentException x) {}
 127             }
 128         } else {
 129             String[] as = attributes.split(",");
 130             for (String a : as) {
 131                 try {
 132                     map.put(a, attribute(AttrID.valueOf(a), jrtfas));
 133                 } catch (IllegalArgumentException x) {}
 134             }
 135         }
 136         return map;
 137     }
 138 
 139     Object attribute(AttrID id, JrtFileAttributes jrtfas) {
 140         switch (id) {
 141         case size:
 142             return jrtfas.size();
 143         case creationTime:
 144             return jrtfas.creationTime();
 145         case lastAccessTime:
 146             return jrtfas.lastAccessTime();
 147         case lastModifiedTime:
 148             return jrtfas.lastModifiedTime();
 149         case isDirectory:
 150             return jrtfas.isDirectory();
 151         case isRegularFile:
 152             return jrtfas.isRegularFile();
 153         case isSymbolicLink:
 154             return jrtfas.isSymbolicLink();
 155         case isOther:
 156             return jrtfas.isOther();
 157         case fileKey:
 158             return jrtfas.fileKey();
 159         case compressedSize:
 160             if (isJrtView)
 161                 return jrtfas.compressedSize();
 162             break;
 163         case extension:
 164             if (isJrtView) {
 165                 return jrtfas.extension();
 166             }
 167             break;
 168         }
 169         return null;
 170     }
 171 }